CodeGenModule.cpp revision f981bf84ad7745861bc9c971f46417527e1e5db5
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 202811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth#include "clang/CodeGen/CodeGenOptions.h" 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2321ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 252c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 28e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h" 29ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 30bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 3220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 3378f7ece00e2ddfb64d4ed72a7be770b5b9b805e3Chris Lattner#include "llvm/Support/ErrorHandling.h" 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 382811ccf48d6d898c42cc4cfad37abedb36236d20Chandler CarruthCodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 39fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 40bd3606426d389370616126af969904ec493cb105Chris Lattner Diagnostic &diags) 41bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 422811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 43dbd920c7758e6dfb678a8f503fb14ba97c26f23aAnders Carlsson TheTargetData(TD), Diags(diags), Types(C, M, TD), MangleCtx(C), 44dbd920c7758e6dfb678a8f503fb14ba97c26f23aAnders Carlsson VtableInfo(*this), Runtime(0), 45a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0), 46a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson VMContext(M.getContext()) { 47208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 513c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 523c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 533c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 543c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 553c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 56e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 57e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 582811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(this) : 0; 592b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 602b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 612b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 63815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 64815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 65815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 66815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 6789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson // We need to call this first because it can add deferred declarations. 6889ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson EmitCXXGlobalInitFunc(); 6989ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson 7082227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 71208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 72208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 73208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 746bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 76532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 770269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 78f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 79f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 80488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 812c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 8290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 8390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 8490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 8590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 8756b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 882c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 890a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 900a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 912c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 9258c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 93488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 94c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 9590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 9690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 9790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 9890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 10056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 101c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1020a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 103c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 104c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 1051eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLangOptions::VisibilityMode 10604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const { 10704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 10804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (VD->getStorageClass() == VarDecl::PrivateExtern) 10904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 11004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 11140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 11204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (attr->getVisibility()) { 11304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar default: assert(0 && "Unknown visibility!"); 1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case VisibilityAttr::DefaultVisibility: 11504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Default; 11604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::HiddenVisibility: 11704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 11804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::ProtectedVisibility: 11904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Protected; 12004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar } 1217e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1226ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 12304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return getLangOptions().getVisibilityMode(); 1244f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1254f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 12704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar const Decl *D) const { 12804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Internal definitions always have default visibility. 129df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner if (GV->hasLocalLinkage()) { 1307e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1316ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar return; 1327e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1336ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 13404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (getDeclVisibilityMode(D)) { 1357cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian default: assert(0 && "Unknown visibility!"); 13604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Default: 13704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 13804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Hidden: 13904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 14004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Protected: 14104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1427cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian } 1437cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian} 1447cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian 1452a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlssonconst char *CodeGenModule::getMangledName(const GlobalDecl &GD) { 1464a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1482a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 1492a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXCtorName(D, GD.getCtorType()); 1502a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 1512a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXDtorName(D, GD.getDtorType()); 1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1532a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledName(ND); 1542a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson} 1552a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 159c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 160c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1626ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 163f981bf84ad7745861bc9c971f46417527e1e5db5Daniel Dunbar if (!getMangleContext().shouldMangleDeclName(ND)) { 164c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1653c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 166c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1686ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 169f981bf84ad7745861bc9c971f46417527e1e5db5Daniel Dunbar getMangleContext().mangleName(ND, Name); 1706ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 17195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return UniqueMangledName(Name.begin(), Name.end()); 17295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson} 17395d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson 17495d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlssonconst char *CodeGenModule::UniqueMangledName(const char *NameStart, 17595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson const char *NameEnd) { 17695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!"); 1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData(); 1795f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1805f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1816d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1826d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 18449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1856bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1866d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1876d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 19149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 20196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType* CtorStructTy = 2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), 20696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(CtorFTy), NULL); 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2130032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson I->second, false)); 2143c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 21508e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 21996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2201c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(TheModule, AT, false, 221572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2227db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(AT, Ctors), 2231c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson GlobalName); 2246d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2256d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2266d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 227532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 228532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 229532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 23396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *gv = 2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(TheModule, Array->getType(), false, 2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::AppendingLinkage, Array, 2391c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.global.annotations"); 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 241532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 24386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattnerstatic CodeGenModule::GVALinkage 2441eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpGetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, 24568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor const LangOptions &Features) { 2469aeed32282fe8a775c24c01c923717ca86695685John McCall // Everything located semantically within an anonymous namespace is 2479aeed32282fe8a775c24c01c923717ca86695685John McCall // always internal. 2489aeed32282fe8a775c24c01c923717ca86695685John McCall if (FD->isInAnonymousNamespace()) 2499aeed32282fe8a775c24c01c923717ca86695685John McCall return CodeGenModule::GVA_Internal; 2509aeed32282fe8a775c24c01c923717ca86695685John McCall 2517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // "static" functions get internal linkage. 2527d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor if (FD->getStorageClass() == FunctionDecl::Static && !isa<CXXMethodDecl>(FD)) 2537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return CodeGenModule::GVA_Internal; 2547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 2551fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor // The kind of external linkage this function will have, if it is not 2561fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor // inline or static. 2571fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; 2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Context.getLangOptions().CPlusPlus && 259d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) 2601fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor External = CodeGenModule::GVA_TemplateInstantiation; 2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2627ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor if (!FD->isInlined()) 2631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { 2661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // GNU or C99 inline semantics. Determine whether this symbol should be 2671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // externally visible. 2681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (FD->isInlineDefinitionExternallyVisible()) 2691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return External; 270cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner 2711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // C99 inline semantics, where the symbol is not externally visible. 272b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor return CodeGenModule::GVA_C99Inline; 2731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor } 274b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor 2757d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // C++0x [temp.explicit]p9: 2767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // [ Note: The intent is that an inline function that is the subject of 2777d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // an explicit instantiation declaration will still be implicitly 2787d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // instantiated when used so that the body can be considered for 2797d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // inlining, but that no out-of-line copy of the inline function would be 2807d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // generated in the translation unit. -- end note ] 2817d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor if (FD->getTemplateSpecializationKind() 2827d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor == TSK_ExplicitInstantiationDeclaration) 2837d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return CodeGenModule::GVA_C99Inline; 2847d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 2851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return CodeGenModule::GVA_CXXInline; 2867c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 2877c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 2887c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar/// SetFunctionDefinitionAttributes - Set attributes for a global. 289b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar/// 290f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump/// FIXME: This is currently only done for aliases and functions, but not for 291f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump/// variables (these details are set in EmitGlobalVarDefinition for variables). 2927c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 2937c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 29468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); 2957c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 29655d6f50b23a1fd0a04b568787a25beb7537e6c9bDaniel Dunbar if (Linkage == GVA_Internal) { 2979f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 29840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<DLLExportAttr>()) { 2997c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setLinkage(llvm::Function::DLLExportLinkage); 30040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<WeakAttr>()) { 30144b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner GV->setLinkage(llvm::Function::WeakAnyLinkage); 302cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner } else if (Linkage == GVA_C99Inline) { 303cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // In C99 mode, 'inline' functions are guaranteed to have a strong 304cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // definition somewhere else, so we can use available_externally linkage. 305d9d049a93c55624908e81cf3927b7905efeba05fChris Lattner GV->setLinkage(llvm::Function::AvailableExternallyLinkage); 3061fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) { 30786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // In C++, the compiler has to emit a definition in every translation unit 30886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // that references the function. We should use linkonce_odr because 30986daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // a) if all references in this translation unit are optimized away, we 31086daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // don't need to codegen it. b) if the function persists, it needs to be 31186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // merged with other definitions. c) C++ has the ODR, so we know the 31286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // definition is dependable. 31386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner GV->setLinkage(llvm::Function::LinkOnceODRLinkage); 3147c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 315cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner assert(Linkage == GVA_StrongExternal); 316cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // Otherwise, we have strong external linkage. 3177c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setLinkage(llvm::Function::ExternalLinkage); 3180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 319d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 3207c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 321d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 322d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 3237dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const CGFunctionInfo &Info, 3257dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar llvm::Function *F) { 326ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned CallingConv; 327761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 328ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar ConstructAttributeList(Info, D, AttributeList, CallingConv); 329761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 330ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar AttributeList.size())); 331ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 332f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 333f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3347c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 3357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::Function *F) { 33674ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->addFnAttr(llvm::Attribute::NoUnwind); 338af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 33940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<AlwaysInlineAttr>()) 340af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3421feade8e520be483293dbf55eb57a51720899589Mike Stump if (D->hasAttr<NoInlineAttr>()) 34381ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 344f55314dce992fd60816ba337ad151a2fb7c42239Mike Stump 345fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson if (Features.getStackProtectorMode() == LangOptions::SSPOn) 346fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtect); 347fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 348fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtectReq); 349fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson 350bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) { 351bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt unsigned width = Context.Target.getCharWidth(); 352bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(AA->getAlignment() / width); 353bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt while ((AA = AA->getNext<AlignedAttr>())) 354bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width)); 355bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 356fb51ddfafcd5f8536d0312b3daa3c0b74b90ab5bMike Stump // C++ ABI requires 2-byte alignment for member functions. 357bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 358bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump F->setAlignment(2); 359f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 360f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::SetCommonAttributes(const Decl *D, 3627c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 3637c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar setGlobalVisibility(GV, D); 3647c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 36540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<UsedAttr>()) 3667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar AddUsedGlobal(GV); 3677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 36840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 3697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setSection(SA->getName()); 3707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 3717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3720e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 3730e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar llvm::Function *F, 3740e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar const CGFunctionInfo &FI) { 3750e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributes(D, FI, F); 3760e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, F); 3777c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3787c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::InternalLinkage); 3797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3800e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetCommonAttributes(D, F); 381f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 382f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 383f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 384c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman llvm::Function *F, 385c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman bool IsIncompleteFunction) { 386c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman if (!IsIncompleteFunction) 387c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3897c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Only a few attributes are set on declarations; these may later be 3907c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // overridden by a definition. 3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 39240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (FD->hasAttr<DLLImportAttr>()) { 3937c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::DLLImportLinkage); 3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (FD->hasAttr<WeakAttr>() || 39540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<WeakImportAttr>()) { 3967c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // separate linkage types for this. 3987c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::ExternalWeakLinkage); 3997c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->setLinkage(llvm::Function::ExternalLinkage); 4017c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 4027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 40340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 4047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setSection(SA->getName()); 405219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 406219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 4070269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!GV->isDeclaration() && 4090269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 41035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 4110269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4120269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 415ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner if (LLVMUsed.empty()) 4160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4183c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 42035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 42135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 42235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 42335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump UsedArray[i] = 4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 426a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson i8PTy); 42735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 429c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian if (UsedArray.empty()) 430c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian return; 43196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), ATy, false, 4350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4367db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(ATy, UsedArray), 4371c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.used"); 4380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 44367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 44467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 44567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 44667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner while (!DeferredDeclsToEmit.empty()) { 4472a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson GlobalDecl D = DeferredDeclsToEmit.back(); 44867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 44967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 45067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // The mangled name for the decl must have been emitted in GlobalDeclMap. 45167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Look it up to see if it was defined with a stronger definition (e.g. an 45267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // extern inline function with a strong function redefinition). If so, 45367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // just ignore the deferred decl. 45467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; 45567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 45867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 46167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 46267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4668bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// GlobalValue being annotated. The second field is the constant string 4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// created from the AnnotateAttr's annotation. The third field is a constant 4708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4833c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 4850032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson AA->getAnnotation(), true); 4860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 4870032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson M->getModuleIdentifier(), 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *annoGV = 49395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, anno->getType(), false, 49495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, anno, 49595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GV->getName()); 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 49895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, unit->getType(), false, 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::PrivateLinkage, unit, 50095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ".str"); 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 50257d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar // Create the ConstantStruct for the global annotation. 5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 5043c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, SBP), 5053c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(annoGV, SBP), 5063c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(unitGV, SBP), 5070032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 5088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 50947a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 5108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 51273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5135c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5145c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 51540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 51673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 517bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 518bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 51973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FD->hasAttr<ConstructorAttr>() || 52140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<DestructorAttr>()) 52273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); 5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 526dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner // static, static inline, always_inline, and extern inline functions can 52786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // always be deferred. Normal inline functions can be deferred in C99/C++. 528cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || 529cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner Linkage == GVA_CXXInline) 530dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return true; 531dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return false; 53273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 534dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner const VarDecl *VD = cast<VarDecl>(Global); 535dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 536b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 53774d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // We never want to defer structs that have non-trivial constructors or 53874d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // destructors. 53974d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 54074d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // FIXME: Handle references. 54174d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { 54274d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 54374d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) 54474d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson return false; 54574d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 54674d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 54774d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 5481028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Static data may be deferred, but out-of-line static data members 5491028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // cannot be. 550393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (VD->isInAnonymousNamespace()) 551393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return true; 552393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (VD->getStorageClass() == VarDecl::Static) { 553393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian // Initializer has side effects? 554393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (VD->getInit() && VD->getInit()->HasSideEffects(Context)) 555393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return false; 556393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return !(VD->isStaticDataMember() && VD->isOutOfLine()); 557393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian } 558393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return false; 55973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 56073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 561b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobal(GlobalDecl GD) { 5624a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 564bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 565bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 56640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Global->hasAttr<AliasAttr>()) 567bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return EmitAliasDefinition(Global); 568219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 56967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 5705e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 57173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 57273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 57373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5740269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 576bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 577bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 578b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor // In C++, if this is marked "extern", defer code generation. 5792928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson if (getLangOptions().CPlusPlus && !VD->getInit() && 5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (VD->getStorageClass() == VarDecl::Extern || 58148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor VD->isExternC())) 582b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor return; 583b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 584b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor // In C, if this isn't a definition, defer code generation. 585b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor if (!getLangOptions().CPlusPlus && !VD->getInit()) 58673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5874c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5884c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 58967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 59067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 59173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 59267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // If the value has already been used, add it directly to the 59367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit list. 5942a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const char *MangledName = getMangledName(GD); 59567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (GlobalDeclMap.count(MangledName)) 5962a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDeclsToEmit.push_back(GD); 59767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner else { 59867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 59967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // first use of the mangled name will cause it to move into 60067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit. 6012a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDecls[MangledName] = GD; 60267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 603bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 604bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 605bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 606bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 6072a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitGlobalDefinition(GD); 6084c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 6094c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 610b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 6114a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6138e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson PrettyStackTraceDecl CrashInfo((ValueDecl *)D, D->getLocation(), 6148e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson Context.getSourceManager(), 6158e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson "Generating code for declaration"); 6168e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson 61758588943a8283baf17815d56275bdb381560970cMike Stump if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 61858588943a8283baf17815d56275bdb381560970cMike Stump const CXXRecordDecl *RD = MD->getParent(); 61958588943a8283baf17815d56275bdb381560970cMike Stump // We have to convert it to have a record layout. 62058588943a8283baf17815d56275bdb381560970cMike Stump Types.ConvertTagDeclType(RD); 62158588943a8283baf17815d56275bdb381560970cMike Stump const CGRecordLayout &CGLayout = Types.getCGRecordLayout(RD); 62258588943a8283baf17815d56275bdb381560970cMike Stump // A definition of a KeyFunction, generates all the class data, such 62358588943a8283baf17815d56275bdb381560970cMike Stump // as vtable, rtti and the VTT. 624a84b4044b79f2cc424c6ae7ee5edb97e948ba806Mike Stump if (CGLayout.getKeyFunction() 625a84b4044b79f2cc424c6ae7ee5edb97e948ba806Mike Stump && (CGLayout.getKeyFunction()->getCanonicalDecl() 626a84b4044b79f2cc424c6ae7ee5edb97e948ba806Mike Stump == MD->getCanonicalDecl())) 62758588943a8283baf17815d56275bdb381560970cMike Stump getVtableInfo().GenerateClassData(RD); 62858588943a8283baf17815d56275bdb381560970cMike Stump } 6292a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 6302a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitCXXConstructor(CD, GD.getCtorType()); 6317267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 6327267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson EmitCXXDestructor(DD, GD.getDtorType()); 633b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner else if (isa<FunctionDecl>(D)) 634b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner EmitGlobalFunctionDefinition(GD); 6352a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 636bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 6372a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else { 638bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 639bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 640bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 641bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 64274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 64374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 64474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 64574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 64674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 64774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 64874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 64974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, 65074391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty, 651b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner GlobalDecl D) { 6520558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 6530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 6540558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 6550558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 6560558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 65996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 6603c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, PTy); 6610558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 663654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // This function doesn't have a complete type (for example, the return 664654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // type is an incomplete struct). Use a fake type instead, and make 665654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // sure not to try to set attributes. 666654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman bool IsIncompleteFunction = false; 667654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman if (!isa<llvm::FunctionType>(Ty)) { 668654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman Ty = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 669654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman std::vector<const llvm::Type*>(), false); 670654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman IsIncompleteFunction = true; 671654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman } 672654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 673654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman llvm::Function::ExternalLinkage, 674654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman "", &getModule()); 675654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman F->setName(MangledName); 676654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman if (D.getDecl()) 677654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman SetFunctionAttributes(cast<FunctionDecl>(D.getDecl()), F, 678654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman IsIncompleteFunction); 679654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman Entry = F; 680654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman 68167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 68267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 68367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 6859fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner DeferredDecls.find(MangledName); 68667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 68767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 68867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 68967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 69067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 691b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { 6920c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // If this the first reference to a C++ inline function in a class, queue up 6930c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // the deferred function body for emission. These are not seen as 6940c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // top-level declarations. 695b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) 696b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner DeferredDeclsToEmit.push_back(D); 697c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // A called constructor which has no definition or declaration need be 698c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // synthesized. 699c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const CXXRecordDecl *ClassDecl = 701c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian cast<CXXRecordDecl>(CD->getDeclContext()); 70297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (CD->isCopyConstructor(getContext())) 70397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian DeferredCopyConstructorToEmit(D); 7049889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian else if (!ClassDecl->hasUserDeclaredConstructor()) 705c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian DeferredDeclsToEmit.push_back(D); 706c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian } 7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump else if (isa<CXXDestructorDecl>(FD)) 708b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian DeferredDestructorToEmit(D); 709ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 710ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian if (MD->isCopyAssignment()) 711ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian DeferredCopyAssignmentToEmit(D); 71267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7140558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return F; 7150558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 7160558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 71797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian/// Defer definition of copy constructor(s) which need be implicitly defined. 71897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanianvoid CodeGenModule::DeferredCopyConstructorToEmit(GlobalDecl CopyCtorDecl) { 7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const CXXConstructorDecl *CD = 72097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian cast<CXXConstructorDecl>(CopyCtorDecl.getDecl()); 72197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 72297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (ClassDecl->hasTrivialCopyConstructor() || 72397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian ClassDecl->hasUserDeclaredCopyConstructor()) 72497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian return; 7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 72697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // First make sure all direct base classes and virtual bases and non-static 72797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // data mebers which need to have their copy constructors implicitly defined 72897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // are defined. 12.8.p7 72997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 73097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian Base != ClassDecl->bases_end(); ++Base) { 73197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian CXXRecordDecl *BaseClassDecl 73297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 7331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CXXConstructorDecl *BaseCopyCtor = 73480e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian BaseClassDecl->getCopyConstructor(Context, 0)) 73597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian GetAddrOfCXXConstructor(BaseCopyCtor, Ctor_Complete); 73697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 7371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 73897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 73997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian FieldEnd = ClassDecl->field_end(); 74097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian Field != FieldEnd; ++Field) { 74197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian QualType FieldType = Context.getCanonicalType((*Field)->getType()); 74297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (const ArrayType *Array = Context.getAsArrayType(FieldType)) 74397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian FieldType = Array->getElementType(); 74497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 745e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian if ((*Field)->isAnonymousStructOrUnion()) 746e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian continue; 74797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian CXXRecordDecl *FieldClassDecl 748e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian = cast<CXXRecordDecl>(FieldClassType->getDecl()); 7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CXXConstructorDecl *FieldCopyCtor = 75080e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian FieldClassDecl->getCopyConstructor(Context, 0)) 75197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian GetAddrOfCXXConstructor(FieldCopyCtor, Ctor_Complete); 75297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 75397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 75497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian DeferredDeclsToEmit.push_back(CopyCtorDecl); 755ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian} 756ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian 757ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian/// Defer definition of copy assignments which need be implicitly defined. 758ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanianvoid CodeGenModule::DeferredCopyAssignmentToEmit(GlobalDecl CopyAssignDecl) { 759ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian const CXXMethodDecl *CD = cast<CXXMethodDecl>(CopyAssignDecl.getDecl()); 760ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 762ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian if (ClassDecl->hasTrivialCopyAssignment() || 763ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian ClassDecl->hasUserDeclaredCopyAssignment()) 764ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian return; 7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 766ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian // First make sure all direct base classes and virtual bases and non-static 767ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian // data mebers which need to have their copy assignments implicitly defined 768ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian // are defined. 12.8.p12 769ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 770ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian Base != ClassDecl->bases_end(); ++Base) { 771ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian CXXRecordDecl *BaseClassDecl 772ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 773ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian const CXXMethodDecl *MD = 0; 774183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian if (!BaseClassDecl->hasTrivialCopyAssignment() && 775183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian !BaseClassDecl->hasUserDeclaredCopyAssignment() && 776183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian BaseClassDecl->hasConstCopyAssignment(getContext(), MD)) 777555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson GetAddrOfFunction(MD, 0); 778ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian } 7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 780ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 781ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian FieldEnd = ClassDecl->field_end(); 782ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian Field != FieldEnd; ++Field) { 783ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian QualType FieldType = Context.getCanonicalType((*Field)->getType()); 784ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian if (const ArrayType *Array = Context.getAsArrayType(FieldType)) 785ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian FieldType = Array->getElementType(); 786ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 787ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian if ((*Field)->isAnonymousStructOrUnion()) 788ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian continue; 789ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian CXXRecordDecl *FieldClassDecl 790ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian = cast<CXXRecordDecl>(FieldClassType->getDecl()); 791ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian const CXXMethodDecl *MD = 0; 792183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian if (!FieldClassDecl->hasTrivialCopyAssignment() && 793183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian !FieldClassDecl->hasUserDeclaredCopyAssignment() && 794183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian FieldClassDecl->hasConstCopyAssignment(getContext(), MD)) 795555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson GetAddrOfFunction(MD, 0); 796ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian } 797ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian } 7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump DeferredDeclsToEmit.push_back(CopyAssignDecl); 79997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian} 80097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian 801b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanianvoid CodeGenModule::DeferredDestructorToEmit(GlobalDecl DtorDecl) { 802b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian const CXXDestructorDecl *DD = cast<CXXDestructorDecl>(DtorDecl.getDecl()); 803b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); 804b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian if (ClassDecl->hasTrivialDestructor() || 805b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian ClassDecl->hasUserDeclaredDestructor()) 806b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian return; 807b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian 808b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 809b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian Base != ClassDecl->bases_end(); ++Base) { 810b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian CXXRecordDecl *BaseClassDecl 811b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const CXXDestructorDecl *BaseDtor = 813b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian BaseClassDecl->getDestructor(Context)) 814b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian GetAddrOfCXXDestructor(BaseDtor, Dtor_Complete); 815b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian } 8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 817b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 818b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian FieldEnd = ClassDecl->field_end(); 819b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian Field != FieldEnd; ++Field) { 820b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian QualType FieldType = Context.getCanonicalType((*Field)->getType()); 821b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian if (const ArrayType *Array = Context.getAsArrayType(FieldType)) 822b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian FieldType = Array->getElementType(); 823b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 824b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian if ((*Field)->isAnonymousStructOrUnion()) 825b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian continue; 826b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian CXXRecordDecl *FieldClassDecl 827b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian = cast<CXXRecordDecl>(FieldClassType->getDecl()); 8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const CXXDestructorDecl *FieldDtor = 829b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian FieldClassDecl->getDestructor(Context)) 830b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian GetAddrOfCXXDestructor(FieldDtor, Dtor_Complete); 831b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian } 832b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian } 833b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian DeferredDeclsToEmit.push_back(DtorDecl); 834b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian} 835b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian 836b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian 83774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 83874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 83974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 840b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 84174391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 84274391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 84374391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 8444a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 8454fe95f99a2693f1145785ea5835ba6937e49c730Douglas Gregor return GetOrCreateLLVMFunction(getMangledName(GD), Ty, GD); 84674391b48b4791cded373683a3baf67314f358d50Chris Lattner} 84777ba708819285931932ecd33691a672bb59d221aEli Friedman 84874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 84974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 85074391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 85174391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 85274391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 85374391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 854e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 855b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 85674391b48b4791cded373683a3baf67314f358d50Chris Lattner} 857bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 85874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 85974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 86074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 86174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 86274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 86374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 86474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 86574391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, 86674391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType*Ty, 86774391b48b4791cded373683a3baf67314f358d50Chris Lattner const VarDecl *D) { 8683c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 8695d4f5c724533b994de05df49ae259120482ec366Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 87099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 87174391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 872570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 87499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 8753c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, Ty); 87699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 87867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 87967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 88067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 88267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.find(MangledName); 88367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 88467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 88567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 88667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 88767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 88867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 8891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 89299b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 0, "", 0, 89456ebe5082da7411fb37479e230b52735f77cff35Eli Friedman false, Ty->getAddressSpace()); 895d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner GV->setName(MangledName); 89649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 89799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 89874391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 899f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This code is overly simple and should be merged with other global 900f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // handling. 90174391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setConstant(D->getType().isConstant(Context)); 90249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 90374391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 90474391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 90504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 90649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (D->hasAttr<WeakAttr>() || 90840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) 90974391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 91056ebe5082da7411fb37479e230b52735f77cff35Eli Friedman 91156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman GV->setThreadLocal(D->isThreadSpecified()); 91274391b48b4791cded373683a3baf67314f358d50Chris Lattner } 9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 91474391b48b4791cded373683a3baf67314f358d50Chris Lattner return Entry = GV; 91574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 916eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 917eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 91874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 91974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 92074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 92174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 92274391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 92374391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 92474391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 92574391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 92674391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 92774391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::PointerType *PTy = 93096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 93174391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); 93274391b48b4791cded373683a3baf67314f358d50Chris Lattner} 9333f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 93474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 93574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 93674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 93774391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 93874391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 93974391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 940e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 94196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 942bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 943bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 94403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 94503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!D->getInit() && "Cannot emit definite definitions here!"); 94603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 9477520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (MayDeferGeneration(D)) { 9487520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // If we have not seen a reference to this variable yet, place it 9497520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // into the deferred declarations table to be emitted if needed 9507520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // later. 9517520bd1de12af10ea08c662440565adbdf589317Douglas Gregor const char *MangledName = getMangledName(D); 9527520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (GlobalDeclMap.count(MangledName) == 0) { 953555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson DeferredDecls[MangledName] = D; 95403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar return; 9557520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 9567520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 9577520bd1de12af10ea08c662440565adbdf589317Douglas Gregor 9587520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // The tentative definition is the only definition. 95903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar EmitGlobalVarDefinition(D); 96003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar} 96103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 9621028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorstatic CodeGenModule::GVALinkage 9631028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorGetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { 9641028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Everything located semantically within an anonymous namespace is 9651028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // always internal. 9661028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor if (VD->isInAnonymousNamespace()) 9671028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_Internal; 9681028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9691028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Handle linkage for static data members. 9701028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor if (VD->isStaticDataMember()) { 9711028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor switch (VD->getTemplateSpecializationKind()) { 9721028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_Undeclared: 9731028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitSpecialization: 9741028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitInstantiationDefinition: 9751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_StrongExternal; 9761028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 9789f0306d969700c9d79a31d559f95a1eb4c0bfcb4Douglas Gregor llvm::llvm_unreachable("Variable should not be instantiated"); 9791028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Fall through to treat this like any other instantiation. 9801028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9811028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ImplicitInstantiation: 9821028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_TemplateInstantiation; 9831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } 9841028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } 9851028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9861028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Static variables get internal linkage. 9871028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor if (VD->getStorageClass() == VarDecl::Static) 9881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_Internal; 9891028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_StrongExternal; 9911028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor} 9921028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 993bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 9948f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 99577ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 9961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9978f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 998cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 99903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // implicitly initialized with { 0 }. 100003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // 100103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // Note that tentative definitions are only emitted at the end of 100203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // a translation unit, so they should never have incomplete 100303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // type. In addition, EmitTentativeDefinition makes sure that we 100403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // never attempt to emit a tentative definition if a real one 100503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // exists. A use may still exists, however, so we still may need 100603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // to do a RAUW. 100703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1008b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson Init = EmitNullConstant(D->getType()); 100977ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 1010e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson Init = EmitConstantExpr(D->getInit(), D->getType()); 10111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10126e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 10136e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 101489ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson if (getLangOptions().CPlusPlus) { 101589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson CXXGlobalInits.push_back(D); 101689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = EmitNullConstant(T); 101789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } else { 101889ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson ErrorUnsupported(D, "static initializer"); 101989ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 102089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } 10216e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 10228f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 10238e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 10242d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 1025570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1027570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 1028570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 10299d4a15fd3b85434c43ea27562793de63a793321aChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast || 10309d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // all zero index gep. 10319d4a15fd3b85434c43ea27562793de63a793321aChris Lattner CE->getOpcode() == llvm::Instruction::GetElementPtr); 1032570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 1033570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1035570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 1036570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1038570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 1039570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 1040570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 1041570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 1042570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 1043570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 1044570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 1045570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 1046570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 1047570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 1048570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 1049570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1051570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Remove the old entry from GlobalDeclMap so that we'll create a new one. 1052570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GlobalDeclMap.erase(getMangledName(D)); 1053232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 1054570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 1055570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 10560558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner GV->takeName(cast<llvm::GlobalValue>(Entry)); 10570558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 105877ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 10603c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1061570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 106277ba708819285931932ecd33691a672bb59d221aEli Friedman 106377ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 1064570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 106577ba708819285931932ecd33691a672bb59d221aEli Friedman } 106677ba708819285931932ecd33691a672bb59d221aEli Friedman 106740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 10688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 10698bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 1070f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 10718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 10728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 107388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 1074e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner 1075e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // If it is safe to mark the global 'constant', do so now. 1076e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 1077e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (D->getType().isConstant(Context)) { 1078e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // FIXME: In C++, if the variable has a non-trivial ctor/dtor or any mutable 1079e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // members, it cannot be declared "LLVM const". 1080e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(true); 1081e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } 10821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10830de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 108408d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 108588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 10861028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GVALinkage Linkage = GetLinkageForVariable(getContext(), D); 1087a0f00a71fcb0f98298709d5e5318339acf7958acDouglas Gregor if (Linkage == GVA_Internal) 10888fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 108940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLImportAttr>()) 1090ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 109140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLExportAttr>()) 1092ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 1093e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else if (D->hasAttr<WeakAttr>()) { 1094e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (GV->isConstant()) 1095e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); 1096e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else 1097e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 10981028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } else if (Linkage == GVA_TemplateInstantiation) 10991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 11002811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth else if (!CodeGenOpts.NoCommon && 1101309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner !D->hasExternalStorage() && !D->getInit() && 1102e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner !D->getAttr<SectionAttr>()) { 110304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 1104e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // common vars aren't constant even if declared const. 1105e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 1106e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } else 110704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 11087e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar 11097c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 111004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 1111686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 11122d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 111366031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 1114686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 1115686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 111688a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1118bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1119bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// implement a function with no prototype, e.g. "int foo() {}". If there are 1120bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// existing call uses of the old function in the module, this adjusts them to 1121bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// call the new function directly. 1122bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// 1123bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// This is not just a cleanup: the always_inline pass requires direct calls to 1124bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// functions to be able to inline them. If there is a bitcast in the way, it 1125bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// won't inline them. Instcombine normally deletes these calls, but it isn't 1126bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// run at -O0. 1127bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1128bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *NewFn) { 1129bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If we're redefining a global as a function, don't transform it. 1130bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1131bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (OldFn == 0) return; 11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1133bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner const llvm::Type *NewRetTy = NewFn->getReturnType(); 1134bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::SmallVector<llvm::Value*, 4> ArgList; 1135bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1136bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1137bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner UI != E; ) { 1138bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // TODO: Do invokes ever occur in C code? If so, we should handle them too. 113908c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner unsigned OpNo = UI.getOperandNo(); 1140bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*UI++); 114108c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner if (!CI || OpNo != 0) continue; 11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1143bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the return types don't match exactly, and if the call isn't dead, then 1144bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // we can't transform this call. 1145bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getType() != NewRetTy && !CI->use_empty()) 1146bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 1147bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1148bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the function was passed too few arguments, don't transform. If extra 1149bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // arguments were passed, we silently drop them. If any of the types 1150bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // mismatch, we don't transform. 1151bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner unsigned ArgNo = 0; 1152bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner bool DontTransform = false; 1153bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1154bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1155bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getNumOperands()-1 == ArgNo || 1156bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->getOperand(ArgNo+1)->getType() != AI->getType()) { 1157bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner DontTransform = true; 1158bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner break; 1159bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1160bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1161bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (DontTransform) 1162bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1164bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Okay, we can transform this. Create the new call instruction and copy 1165bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // over the required information. 1166bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.append(CI->op_begin()+1, CI->op_begin()+1+ArgNo); 1167bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1168bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.end(), "", CI); 1169bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.clear(); 1170ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer if (!NewCall->getType()->isVoidTy()) 1171bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->takeName(CI); 1172bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->setAttributes(CI->getAttributes()); 1173ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar NewCall->setCallingConv(CI->getCallingConv()); 1174bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1175bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Finally, remove the old call, replacing any uses with the new one. 117600549fcec0490b2daf27543e532f94adbb186063Chris Lattner if (!CI->use_empty()) 117700549fcec0490b2daf27543e532f94adbb186063Chris Lattner CI->replaceAllUsesWith(NewCall); 11783b122bc5f1203615e2128e0c1a63da438865b1ccDevang Patel 1179aa40454a14837c573f323e29269cc0b53f495a13Devang Patel // Copy any custom metadata attached with CI. 1180aa40454a14837c573f323e29269cc0b53f495a13Devang Patel llvm::MetadataContext &TheMetadata = CI->getContext().getMetadata(); 1181aa40454a14837c573f323e29269cc0b53f495a13Devang Patel TheMetadata.copyMD(CI, NewCall); 1182aa40454a14837c573f323e29269cc0b53f495a13Devang Patel 1183bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->eraseFromParent(); 1184bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1185bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner} 1186bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1187bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1188b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 11892b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson const llvm::FunctionType *Ty; 1190b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11922b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 1193183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall bool isVariadic = D->getType()->getAs<FunctionProtoType>()->isVariadic(); 11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11952b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic); 11962b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } else { 11972b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11992b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // As a special case, make sure that definitions of K&R function 12002b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // "type foo()" aren't declared as varargs (which forces the backend 12012b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // to do unnecessary work). 12022b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (D->getType()->isFunctionNoProtoType()) { 12032b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson assert(Ty->isVarArg() && "Didn't lower type as expected"); 12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Due to stret, the lowered function could have arguments. 12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Just create the same type as was lowered by ConvertType 12062b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // but strip off the varargs bit. 12072b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); 120896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); 12092b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 1210ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner } 1211d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 12129fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner // Get or create the prototype for the function. 1213b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 12141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12150558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 12160558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 12170558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 12180558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 12190558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12220558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1223bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 1226bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner assert(OldFn->isDeclaration() && 12270558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 122834809507232bc4c3c4840c7d092c7440219fddafChris Lattner 122962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 123062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 123162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 123262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 123362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 123462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 123562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f(int x)"). Start by making a new function of the 123662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // correct type, RAUW, then steal the name. 123734809507232bc4c3c4840c7d092c7440219fddafChris Lattner GlobalDeclMap.erase(getMangledName(D)); 1238b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1239bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewFn->takeName(OldFn); 12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1241bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If this is an implementation of a function without a prototype, try to 1242bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // replace any existing uses of the function (which may be calls) with uses 1243bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // of the new function 12449fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner if (D->getType()->isFunctionNoProtoType()) { 1245bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 12469fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner OldFn->removeDeadConstantUsers(); 12479fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner } 12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 1250bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (!Entry->use_empty()) { 12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 12523c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1253bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 1254bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 12551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 1257bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner OldFn->eraseFromParent(); 12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12590558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 1260bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 12611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12620558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 1263bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1264219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 12656379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 12667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetFunctionDefinitionAttributes(D, Fn); 12677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, Fn); 12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 126940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1270219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 127140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1272219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 1273bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 1274bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1275bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { 127640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis const AliasAttr *AA = D->getAttr<AliasAttr>(); 1277bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 1278bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1279bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 12801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1281bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Unique the name through the identifier table. 1282bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *AliaseeName = AA->getAliasee().c_str(); 1283e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar AliaseeName = getContext().Idents.get(AliaseeName).getNameStart(); 1284bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1285bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 1286bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 1287bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 1288bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 1289b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl()); 1290bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 1291bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMGlobal(AliaseeName, 129296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(DeclTy), 0); 1293bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1294bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 12951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *GA = 1296bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 1297bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 1298bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1300bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // See if there is already something with the alias' name in the module. 1301bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *MangledName = getMangledName(D); 1302bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1304bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry && !Entry->isDeclaration()) { 1305bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a definition in the module, then it wins over the alias. 1306bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // This is dubious, but allow it to be safe. Just ignore the alias. 1307bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->eraseFromParent(); 1308bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return; 1309bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1311bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 1312bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 1313bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 1314bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 1315bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 1316bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 1317bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 1318bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 13191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13203c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1321bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 1322bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 1323bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1325bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Now we know that there is no conflict, set the name. 1326bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry = GA; 1327bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->setName(MangledName); 1328bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 13297c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Set attributes which are particular to an alias; this is a 13307c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // specialization of the attributes which may be set on a global 13317c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // variable/function. 133240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<DLLExportAttr>()) { 13337c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 13347c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // The dllexport attribute is ignored for undefined symbols. 13356fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (FD->getBody()) 13367c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 13377c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 13387c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 13397c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (D->hasAttr<WeakAttr>() || 134140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) { 13427c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::WeakAnyLinkage); 13437c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 13447c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 13457c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GA); 1346bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 1347bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1348b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 1349b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 135034771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbarllvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 135134771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar unsigned BuiltinID) { 13523e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 13531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 13543e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13563e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 13573e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 13583e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 13593e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 13601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1361bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 136286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ASTContext::GetBuiltinTypeError Error; 136386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Type = Context.GetBuiltinType(BuiltinID, Error); 136486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(Error == ASTContext::GE_None && "Can't get builtin type"); 1365370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 13661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::FunctionType *Ty = 1367bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 1368bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 1369b808c952bbff821dce727dd801a1098d64394f98Chris Lattner // Unique the name through the identifier table. 1370e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 137134771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); 1372bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 1373bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 13747acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 13757acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 13767acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 13777acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 13787acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 1379bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 13805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 13820032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13834e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1385c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 13860c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 13870c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 13880032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13894e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 13900c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 13910c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 139241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 139341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 13940032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13954e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 139641ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 13977acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 13981d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarstatic llvm::StringMapEntry<llvm::Constant*> & 13991d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarGetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 14001d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const StringLiteral *Literal, 140170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar bool TargetIsLSB, 14021d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool &IsUTF16, 14031d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned &StringLength) { 1404df4eee9b035cc4b95b1ea72635e7429d06b0ecc8Daniel Dunbar unsigned NumBytes = Literal->getByteLength(); 14051d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 1406f015b034159d40e7033309e50036804eb1971787Daniel Dunbar // Check for simple case. 1407f015b034159d40e7033309e50036804eb1971787Daniel Dunbar if (!Literal->containsNonAsciiOrNull()) { 1408f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1409f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1410f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1411f015b034159d40e7033309e50036804eb1971787Daniel Dunbar } 1412f015b034159d40e7033309e50036804eb1971787Daniel Dunbar 14131d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Otherwise, convert the UTF8 literals into a byte string. 14141d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 14151d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 14161d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar UTF16 *ToPtr = &ToBuf[0]; 14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 14191d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar &ToPtr, ToPtr + NumBytes, 14201d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar strictConversion); 14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14221d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Check for conversion failure. 14231d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (Result != conversionOK) { 14241d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove 14251d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // this duplicate code. 14261d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); 1427f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1428f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1429f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1430e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff } 14311d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 143270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // ConvertUTF8toUTF16 returns the length in ToPtr. 14331d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = ToPtr - &ToBuf[0]; 143470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 143570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // Render the UTF-16 string into a byte array and convert to the target byte 143670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // order. 143770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // 143870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // FIXME: This isn't something we should need to do here. 143970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar llvm::SmallString<128> AsBytes; 144070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.reserve(StringLength * 2); 144170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar for (unsigned i = 0; i != StringLength; ++i) { 144270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar unsigned short Val = ToBuf[i]; 144370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar if (TargetIsLSB) { 144470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 144570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 144670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } else { 144770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 144870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 144970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 145070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 1451434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // Append one extra null character, the second is automatically added by our 1452434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // caller. 1453434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar AsBytes.push_back(0); 145470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 14551d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar IsUTF16 = true; 145670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 14571d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar} 14581d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 14591d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarllvm::Constant * 14601d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarCodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 14611d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned StringLength = 0; 14621d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool isUTF16 = false; 14631d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::StringMapEntry<llvm::Constant*> &Entry = 14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GetConstantCFStringEntry(CFConstantStringMap, Literal, 146570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar getTargetData().isLittleEndian(), 146670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar isUTF16, StringLength); 14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14681d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (llvm::Constant *C = Entry.getValue()) 14691d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return C; 14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14710032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *Zero = 14720032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 14733e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 14741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14759d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // If we don't already have it, get __CFConstantStringClassReference. 1476c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1477c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 147896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::ArrayType::get(Ty, 0); 14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *GV = CreateRuntimeVariable(Ty, 14809d4a15fd3b85434c43ea27562793de63a793321aChris Lattner "__CFConstantStringClassReference"); 14813e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 14823e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 14833c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1484c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1486e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 14873e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::StructType *STy = 1489e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1490e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 14915add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson std::vector<llvm::Constant*> Fields(4); 149244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1493c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 14945add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[0] = CFConstantStringClassRef; 14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1496c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 14973e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 14995add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson llvm::ConstantInt::get(Ty, 0x07C8); 15005add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1501c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 15020032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1503a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar 1504278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner const char *Sect = 0; 150595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::LinkageTypes Linkage; 1506278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner bool isConstant; 1507a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1508a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Sect = getContext().Target.getUnicodeStringSection(); 1509278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // FIXME: why do utf strings get "_" labels instead of "L" labels? 151095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::InternalLinkage; 1511278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1512278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // does make plain ascii ones writable. 1513278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = true; 1514a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } else { 151595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::PrivateLinkage; 1516278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = !Features.WritableStrings; 1517a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 1518278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner 15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 1520278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1521278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner ".str"); 1522a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (Sect) 15238e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 1524a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1525a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; 15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV->setAlignment(Align); 1527a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 15285add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 15295add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1530c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 1531c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 15325add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 15331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1534c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 153508e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson C = llvm::ConstantStruct::get(STy, Fields); 15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable::PrivateLinkage, C, 153895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner "_unnamed_cfstring_"); 15398e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (const char *Sect = getContext().Target.getCFStringSection()) 15408e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 15411d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar Entry.setValue(GV); 15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15430c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1544c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 154545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 15466143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 15471e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 15486143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 15491e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 15501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 15511e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 15521e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 15531e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 15541e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1556dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 15571e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 15581e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1560dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1561dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15631e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15651e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 15661e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 15671e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 15686143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 15696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 15706143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 15716143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 15726143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 15737eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman // FIXME: We shouldn't need to bitcast the constant in the wide string case. 15747eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 15757eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman if (S->isWide()) { 15767eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Type *DestTy = 15777eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 15787eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman C = llvm::ConstantExpr::getBitCast(C, DestTy); 15797eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman } 15807eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman return C; 15816143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 15826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1583eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1584eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1585eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1586eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1587eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1588eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1589a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1590a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1591eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1592eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1593eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1594a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 15951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 159645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 15975fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 15985fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 15996143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 16000032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = 16010032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 160345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 160595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, 16061c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson C, GlobalName); 160745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 160845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 16096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 16106143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 16116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 16126143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 16136143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 16146143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 16156143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 16166143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 16175fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 16185fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 16198e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar bool IsConstant = !Features.WritableStrings; 16208e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 16218e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Get the default prefix if a name wasn't specified. 16228e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!GlobalName) 162395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GlobalName = ".str"; 16248e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 16258e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Don't share any string literals if strings aren't constant. 16268e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!IsConstant) 16275fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StringMapEntry<llvm::Constant *> &Entry = 163095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 163145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 163245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1633eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 163445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 163545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 16365fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 163745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 163845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 163945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 16406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 16416143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 16426143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 16436143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 16445fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 16455fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1646c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 16476143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 164841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1649af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1650af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::EmitObjCPropertyImplementations(const 1652af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (ObjCImplementationDecl::propimpl_iterator 165417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1655af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1657af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1658af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1659af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1660af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1661af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1662af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1663af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1664af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1665af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 166617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis if (!D->getInstanceMethod(PD->getGetterName())) 1667fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1668fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1669af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 167017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis !D->getInstanceMethod(PD->getSetterName())) 1671fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1672fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1673af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1674af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1675af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1676af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 167791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace. 1678984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 167917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1680984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson I != E; ++I) 1681984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitTopLevelDecl(*I); 1682984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson} 1683984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson 168491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec. 168591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1686f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1687f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 168891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson ErrorUnsupported(LSD, "linkage spec"); 168991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson return; 169091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson } 169191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 169217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 169391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson I != E; ++I) 169491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitTopLevelDecl(*I); 169591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson} 169691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 169741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 169841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 169941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 170041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 170141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 170241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 170341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 170441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 170516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Ignore dependent declarations. 170616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 170716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 17081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 170941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 1710293361afd4199c92aabff9267fddea890943c586Anders Carlsson case Decl::CXXConversion: 17112b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson case Decl::CXXMethod: 171241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 171316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Skip function templates 171416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 171516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 17161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1717555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<FunctionDecl>(D)); 1718555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson break; 1719555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson 172041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 1721555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<VarDecl>(D)); 172241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 172341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 172495d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // C++ Decls 172541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1726984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitNamespace(cast<NamespaceDecl>(D)); 172741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 17289cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // No code generation needed. 17299d0c6613ec040ad8d952556be909232a7f25dedeJohn McCall case Decl::UsingShadow: 17309cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor case Decl::Using: 1731dd9967a6374c9a44be4af02aaeee340ffb82848fDouglas Gregor case Decl::UsingDirective: 1732127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::ClassTemplate: 1733127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::FunctionTemplate: 1734018837b081bd1a725bae1c020eb1649975b04d67Anders Carlsson case Decl::NamespaceAlias: 17359cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor break; 173695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson case Decl::CXXConstructor: 173795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 173895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson break; 173927ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson case Decl::CXXDestructor: 174027ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 174127ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson break; 174236674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 174336674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson case Decl::StaticAssert: 174436674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson // Nothing to do. 174536674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson break; 174636674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 174795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // Objective-C Decls 17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 174938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 175041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 175141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1752b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1753b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 175441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1755285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner 175641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1757b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 175841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 175941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 176041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1761af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1762af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 176341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 176441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 176541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1766af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1767af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1768af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1769af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 177041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 17711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 177241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 177341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 177441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 17756fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (OMD->getBody()) 177641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 177741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 177841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 17791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Decl::ObjCCompatibleAlias: 1780305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 178141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 178241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 178391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson case Decl::LinkageSpec: 178491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 178541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 178641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 178741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 178841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 178941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 179041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 17911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 179241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 179341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 179441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 179541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 179641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 179741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 179841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 17991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump default: 1801f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Make sure we handled everything we should, every other kind is a 1802f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 1803f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // function. Need to recode Decl::Kind to do that easily. 180441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 180541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 180641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1807