CodeGenModule.cpp revision 89ed31d3f9eeb8ec77c284a5cf404a74bf5e7acf
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" 20bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/Frontend/CompileOptions.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" 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 37bd3606426d389370616126af969904ec493cb105Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts, 38fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 39bd3606426d389370616126af969904ec493cb105Chris Lattner Diagnostic &diags) 40bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 41bd3606426d389370616126af969904ec493cb105Chris Lattner Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M), 422a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), 43a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0), 44a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson VMContext(M.getContext()) { 45208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 513c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 523c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 533c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 54e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 55e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 566dba432c7b862c2219e5d6e52b0cd188fbf84b01Devang Patel DebugInfo = CompileOpts.DebugInfo ? new CGDebugInfo(this) : 0; 572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 582b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 592b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 63815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 64815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 6589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson // We need to call this first because it can add deferred declarations. 6689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson EmitCXXGlobalInitFunc(); 6789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson 6882227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 69208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 70208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 71208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 726bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 74532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 78488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 792c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 8090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 8190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 8290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 8390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 84488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 8556b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 862c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 870a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 880a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 892c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 9058c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 91488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 92c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 9390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 9490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 9590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 9690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 97488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 9856b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 99c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1000a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 101c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 102c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 10304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarLangOptions::VisibilityMode 10404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const { 10504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 10604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (VD->getStorageClass() == VarDecl::PrivateExtern) 10704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 10804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 10940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 11004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (attr->getVisibility()) { 11104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar default: assert(0 && "Unknown visibility!"); 11204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::DefaultVisibility: 11304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Default; 11404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::HiddenVisibility: 11504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 11604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::ProtectedVisibility: 11704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Protected; 11804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar } 1197e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1206ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 12104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return getLangOptions().getVisibilityMode(); 1224f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1234f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 12404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbarvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 12504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar const Decl *D) const { 12604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Internal definitions always have default visibility. 127df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner if (GV->hasLocalLinkage()) { 1287e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1296ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar return; 1307e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1316ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 13204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (getDeclVisibilityMode(D)) { 1337cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian default: assert(0 && "Unknown visibility!"); 13404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Default: 13504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 13604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Hidden: 13704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 13804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Protected: 13904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1407cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian } 1417cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian} 1427cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian 1432a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlssonconst char *CodeGenModule::getMangledName(const GlobalDecl &GD) { 1442a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const NamedDecl *ND = GD.getDecl(); 1452a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 1462a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 1472a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXCtorName(D, GD.getCtorType()); 1482a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 1492a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXDtorName(D, GD.getDtorType()); 1502a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 1512a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledName(ND); 1522a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson} 1532a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 1545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 157c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 158c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1606ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 161c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner // In C, functions with no attributes never need to be mangled. Fastpath them. 162c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { 163c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1643c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 165c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 166c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner 1676ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1686ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 169fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar if (!mangleName(ND, Context, Out)) { 170fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1713c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 172fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar } 1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 17595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return UniqueMangledName(Name.begin(), Name.end()); 17695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson} 17795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson 17895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlssonconst char *CodeGenModule::UniqueMangledName(const char *NameStart, 17995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson const char *NameEnd) { 18095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!"); 18195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson 18295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData(); 1835f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1845f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1856d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1866d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 18849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1906d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1916d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 19549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 20296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::FunctionType::get(llvm::Type::VoidTy, 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 20596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 208572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 20947a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson llvm::StructType::get(VMContext, llvm::Type::Int32Ty, 21096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(CtorFTy), NULL); 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 216a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson S.push_back( 2174a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2183c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 21908e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 22396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2241c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(TheModule, AT, false, 225572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2267db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(AT, Ctors), 2271c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson GlobalName); 2286d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2296d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2306d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 23796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 2411c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(TheModule, Array->getType(), false, 242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 2431c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.global.annotations"); 244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 24786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattnerstatic CodeGenModule::GVALinkage 24868584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas GregorGetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, 24968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor const LangOptions &Features) { 2501fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor // The kind of external linkage this function will have, if it is not 2511fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor // inline or static. 2521fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; 2531fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor if (Context.getLangOptions().CPlusPlus && 2541fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor (FD->getPrimaryTemplate() || FD->getInstantiatedFromMemberFunction()) && 2551fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor !FD->isExplicitSpecialization()) 2561fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor External = CodeGenModule::GVA_TemplateInstantiation; 2571fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 258167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 259167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson // C++ member functions defined inside the class are always inline. 260f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis if (MD->isInline() || !MD->isOutOfLine()) 261167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson return CodeGenModule::GVA_CXXInline; 262167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson 2631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 264167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson } 265167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson 26643907e87c776391d7d96da04b48e7bf06d8baafcEli Friedman // "static" functions get internal linkage. 2675e222139610a9f8b2b5f4ddd112f10dec9ec1e97Eli Friedman if (FD->getStorageClass() == FunctionDecl::Static) 2687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar return CodeGenModule::GVA_Internal; 2697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 270005eedce54da6580de37bb115bf581008637e4b1Chris Lattner if (!FD->isInline()) 2711fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 27286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner 273d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner // If the inline function explicitly has the GNU inline attribute on it, or if 274d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner // this is C89 mode, we use to GNU semantics. 2759f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor if (!Features.C99 && !Features.CPlusPlus) { 276d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner // extern inline in GNU mode is like C99 inline. 2779f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor if (FD->getStorageClass() == FunctionDecl::Extern) 2789f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return CodeGenModule::GVA_C99Inline; 2799f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor // Normal inline is a strong symbol. 2809f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return CodeGenModule::GVA_StrongExternal; 28168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor } else if (FD->hasActiveGNUInlineAttribute(Context)) { 2829f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor // GCC in C99 mode seems to use a different decision-making 2839f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor // process for extern inline, which factors in previous 2849f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor // declarations. 28568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor if (FD->isExternGNUInline(Context)) 286d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner return CodeGenModule::GVA_C99Inline; 287d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner // Normal inline is a strong symbol. 2881fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 289d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner } 290cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner 29186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // The definition of inline changes based on the language. Note that we 29286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // have already handled "static inline" above, with the GVA_Internal case. 293cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner if (Features.CPlusPlus) // inline and extern inline. 29486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner return CodeGenModule::GVA_CXXInline; 29586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner 296d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner assert(Features.C99 && "Must be in C99 mode if not in C89 or C++ mode"); 297b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor if (FD->isC99InlineDefinition()) 298b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor return CodeGenModule::GVA_C99Inline; 299b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor 300b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor return CodeGenModule::GVA_StrongExternal; 3017c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 3027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar/// SetFunctionDefinitionAttributes - Set attributes for a global. 304b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar/// 305f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump/// FIXME: This is currently only done for aliases and functions, but not for 306f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump/// variables (these details are set in EmitGlobalVarDefinition for variables). 3077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 3087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 30968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); 3107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 31155d6f50b23a1fd0a04b568787a25beb7537e6c9bDaniel Dunbar if (Linkage == GVA_Internal) { 3129f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 31340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<DLLExportAttr>()) { 3147c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setLinkage(llvm::Function::DLLExportLinkage); 31540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<WeakAttr>()) { 31644b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner GV->setLinkage(llvm::Function::WeakAnyLinkage); 317cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner } else if (Linkage == GVA_C99Inline) { 318cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // In C99 mode, 'inline' functions are guaranteed to have a strong 319cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // definition somewhere else, so we can use available_externally linkage. 320d9d049a93c55624908e81cf3927b7905efeba05fChris Lattner GV->setLinkage(llvm::Function::AvailableExternallyLinkage); 3211fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) { 32286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // In C++, the compiler has to emit a definition in every translation unit 32386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // that references the function. We should use linkonce_odr because 32486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // a) if all references in this translation unit are optimized away, we 32586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // don't need to codegen it. b) if the function persists, it needs to be 32686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // merged with other definitions. c) C++ has the ODR, so we know the 32786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // definition is dependable. 32886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner GV->setLinkage(llvm::Function::LinkOnceODRLinkage); 3297c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 330cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner assert(Linkage == GVA_StrongExternal); 331cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // Otherwise, we have strong external linkage. 3327c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setLinkage(llvm::Function::ExternalLinkage); 3330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 334d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 3357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 336d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 337d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 3387dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 3397dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar const CGFunctionInfo &Info, 3407dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar llvm::Function *F) { 341761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 34288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 343c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 344761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 345761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 346ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 347ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 34840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<FastCallAttr>()) 349f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 350f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 35140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<StdCallAttr>()) 352f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 353f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 354f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 3567c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::Function *F) { 35774ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 358f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 359af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 36040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<AlwaysInlineAttr>()) 361af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 36281ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 36340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<NoinlineAttr>()) 36481ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 365f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 366f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetCommonAttributes(const Decl *D, 3687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 3697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar setGlobalVisibility(GV, D); 3707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 37140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<UsedAttr>()) 3727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar AddUsedGlobal(GV); 3737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 37440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 3757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setSection(SA->getName()); 3767c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 3777c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3780e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 3790e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar llvm::Function *F, 3800e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar const CGFunctionInfo &FI) { 3810e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributes(D, FI, F); 3820e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, F); 3837c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3847c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::InternalLinkage); 3857c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 3860e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetCommonAttributes(D, F); 387f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 388f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 389f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 390c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman llvm::Function *F, 391c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman bool IsIncompleteFunction) { 392c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman if (!IsIncompleteFunction) 393c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 39445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 3957c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Only a few attributes are set on declarations; these may later be 3967c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // overridden by a definition. 3977c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 39840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (FD->hasAttr<DLLImportAttr>()) { 3997c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::DLLImportLinkage); 40040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (FD->hasAttr<WeakAttr>() || 40140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<WeakImportAttr>()) { 4027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 4037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // separate linkage types for this. 4047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::ExternalWeakLinkage); 4057c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 4067c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::ExternalLinkage); 4077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 4087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 40940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 4107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setSection(SA->getName()); 411219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 412219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 41635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 421ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner if (LLVMUsed.empty()) 4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 42496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 42535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 42635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 42735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 42835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 42935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 43035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray[i] = 4313c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 432a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson i8PTy); 43335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 43435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 435c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian if (UsedArray.empty()) 436c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian return; 43796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 438c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian 4390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 4401c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(getModule(), ATy, false, 4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4427db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(ATy, UsedArray), 4431c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.used"); 4440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 44967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 45067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 45167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 45267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner while (!DeferredDeclsToEmit.empty()) { 4532a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson GlobalDecl D = DeferredDeclsToEmit.back(); 45467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 45567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 45667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // The mangled name for the decl must have been emitted in GlobalDeclMap. 45767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Look it up to see if it was defined with a stronger definition (e.g. an 45867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // extern inline function with a strong function redefinition). If so, 45967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // just ignore the deferred decl. 46067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; 46167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 462b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 46367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 46467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 46567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 46667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 46767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 46867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4743c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 48996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4907db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4917db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 49795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, anno->getType(), false, 49895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, anno, 49995b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GV->getName()); 5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 50295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, unit->getType(), false, 50395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, unit, 50495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ".str"); 5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 50657d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar // Create the ConstantStruct for the global annotation. 5078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 5083c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, SBP), 5093c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(annoGV, SBP), 5103c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(unitGV, SBP), 5114a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 5128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 51347a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 5148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 51673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5175c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5185c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 51940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 52073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 521bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 522bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 52440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (FD->hasAttr<ConstructorAttr>() || 52540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<DestructorAttr>()) 52673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52868584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); 529dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner 530dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner // static, static inline, always_inline, and extern inline functions can 53186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // always be deferred. Normal inline functions can be deferred in C99/C++. 532cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || 533cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner Linkage == GVA_CXXInline) 534dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return true; 535dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return false; 53673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 537dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner 538dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner const VarDecl *VD = cast<VarDecl>(Global); 539dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 540b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 541dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return VD->getStorageClass() == VarDecl::Static; 54273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 54373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 544b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobal(GlobalDecl GD) { 5452a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const ValueDecl *Global = GD.getDecl(); 5462a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 547bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 548bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 54940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Global->hasAttr<AliasAttr>()) 550bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return EmitAliasDefinition(Global); 551219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 55267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 5535e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 55473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 55573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 55673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5570269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5580269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 559bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 560bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 561b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor // In C++, if this is marked "extern", defer code generation. 5622928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson if (getLangOptions().CPlusPlus && !VD->getInit() && 5632928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson (VD->getStorageClass() == VarDecl::Extern || 5642928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson VD->isExternC(getContext()))) 565b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor return; 566b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 567b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor // In C, if this isn't a definition, defer code generation. 568b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor if (!getLangOptions().CPlusPlus && !VD->getInit()) 56973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5704c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5714c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 57267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 57367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 57473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 57567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // If the value has already been used, add it directly to the 57667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit list. 5772a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const char *MangledName = getMangledName(GD); 57867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (GlobalDeclMap.count(MangledName)) 5792a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDeclsToEmit.push_back(GD); 58067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner else { 58167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 58267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // first use of the mangled name will cause it to move into 58367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit. 5842a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDecls[MangledName] = GD; 58567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 586bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 587bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 588bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 589bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 5902a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitGlobalDefinition(GD); 5914c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5924c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 593b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 5942a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const ValueDecl *D = GD.getDecl(); 5952a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 5962a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 5972a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitCXXConstructor(CD, GD.getCtorType()); 5987267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 5997267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson EmitCXXDestructor(DD, GD.getDtorType()); 600b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner else if (isa<FunctionDecl>(D)) 601b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner EmitGlobalFunctionDefinition(GD); 6022a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 603bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 6042a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else { 605bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 606bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 607bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 608bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 60974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 61074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 61174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 61274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 61374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 61474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 61574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 61674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, 61774391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty, 618b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner GlobalDecl D) { 6190558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 6200558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 6210558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 6220558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 6230558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 6240558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 6250558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 62696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 6273c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, PTy); 6280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 6290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 63067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 63167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 63267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 6332a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 6349fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner DeferredDecls.find(MangledName); 63567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 63667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 63767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 63867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 63967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 640b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { 6410c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // If this the first reference to a C++ inline function in a class, queue up 6420c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // the deferred function body for emission. These are not seen as 6430c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // top-level declarations. 644b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) 645b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner DeferredDeclsToEmit.push_back(D); 646c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // A called constructor which has no definition or declaration need be 647c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // synthesized. 648c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 649c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian const CXXRecordDecl *ClassDecl = 650c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian cast<CXXRecordDecl>(CD->getDeclContext()); 65197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (CD->isCopyConstructor(getContext())) 65297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian DeferredCopyConstructorToEmit(D); 6539889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian else if (!ClassDecl->hasUserDeclaredConstructor()) 654c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian DeferredDeclsToEmit.push_back(D); 655c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian } 65667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 65767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 6580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // This function doesn't have a complete type (for example, the return 6590558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // type is an incomplete struct). Use a fake type instead, and make 6600558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // sure not to try to set attributes. 661c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman bool IsIncompleteFunction = false; 6620558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (!isa<llvm::FunctionType>(Ty)) { 66396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::FunctionType::get(llvm::Type::VoidTy, 6640558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner std::vector<const llvm::Type*>(), false); 665c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman IsIncompleteFunction = true; 6660558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 6670558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 6680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function::ExternalLinkage, 669d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner "", &getModule()); 670d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner F->setName(MangledName); 671c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman if (D.getDecl()) 672c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman SetFunctionAttributes(cast<FunctionDecl>(D.getDecl()), F, 673c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman IsIncompleteFunction); 6740558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = F; 6750558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return F; 6760558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 6770558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 67897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian/// Defer definition of copy constructor(s) which need be implicitly defined. 67997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanianvoid CodeGenModule::DeferredCopyConstructorToEmit(GlobalDecl CopyCtorDecl) { 68097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian const CXXConstructorDecl *CD = 68197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian cast<CXXConstructorDecl>(CopyCtorDecl.getDecl()); 68297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 68397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (ClassDecl->hasTrivialCopyConstructor() || 68497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian ClassDecl->hasUserDeclaredCopyConstructor()) 68597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian return; 68697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian 68797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // First make sure all direct base classes and virtual bases and non-static 68897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // data mebers which need to have their copy constructors implicitly defined 68997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian // are defined. 12.8.p7 69097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 69197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian Base != ClassDecl->bases_end(); ++Base) { 69297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian CXXRecordDecl *BaseClassDecl 69397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 69497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (CXXConstructorDecl *BaseCopyCtor = 69580e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian BaseClassDecl->getCopyConstructor(Context, 0)) 69697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian GetAddrOfCXXConstructor(BaseCopyCtor, Ctor_Complete); 69797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 69897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian 69997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 70097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian FieldEnd = ClassDecl->field_end(); 70197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian Field != FieldEnd; ++Field) { 70297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian QualType FieldType = Context.getCanonicalType((*Field)->getType()); 70397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (const ArrayType *Array = Context.getAsArrayType(FieldType)) 70497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian FieldType = Array->getElementType(); 70597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 70697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian CXXRecordDecl *FieldClassDecl 70797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian = cast<CXXRecordDecl>(FieldClassType->getDecl()); 70897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian if (CXXConstructorDecl *FieldCopyCtor = 70980e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian FieldClassDecl->getCopyConstructor(Context, 0)) 71097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian GetAddrOfCXXConstructor(FieldCopyCtor, Ctor_Complete); 71197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 71297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian } 71397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian DeferredDeclsToEmit.push_back(CopyCtorDecl); 71497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian 71597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian} 71697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian 71774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 71874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 71974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 720b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 72174391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 72274391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 72374391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 724b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner Ty = getTypes().ConvertType(GD.getDecl()->getType()); 725b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(getMangledName(GD.getDecl()), Ty, GD); 72674391b48b4791cded373683a3baf67314f358d50Chris Lattner} 72777ba708819285931932ecd33691a672bb59d221aEli Friedman 72874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 72974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 73074391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 73174391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 73274391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 73374391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 73474391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 735b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 73674391b48b4791cded373683a3baf67314f358d50Chris Lattner} 737bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 73874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 73974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 74074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 74174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 74274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 74374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 74474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 74574391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, 74674391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType*Ty, 74774391b48b4791cded373683a3baf67314f358d50Chris Lattner const VarDecl *D) { 7483c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 7495d4f5c724533b994de05df49ae259120482ec366Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 75099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 75174391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 752570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 753570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 75499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 7553c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, Ty); 75699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 75767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 75867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 75967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 76067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 7612a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 76267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.find(MangledName); 76367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 76467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 76567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 76667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 76767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 76867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 76967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 77099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalVariable *GV = 7711c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 77299b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 7731c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson 0, "", 0, 77456ebe5082da7411fb37479e230b52735f77cff35Eli Friedman false, Ty->getAddressSpace()); 775d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner GV->setName(MangledName); 77649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 77799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 77874391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 779f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This code is overly simple and should be merged with other global 780f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // handling. 78174391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setConstant(D->getType().isConstant(Context)); 78249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 78374391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 78474391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 78504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 78649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 78740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<WeakAttr>() || 78840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) 78974391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 79056ebe5082da7411fb37479e230b52735f77cff35Eli Friedman 79156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman GV->setThreadLocal(D->isThreadSpecified()); 79274391b48b4791cded373683a3baf67314f358d50Chris Lattner } 79374391b48b4791cded373683a3baf67314f358d50Chris Lattner 79474391b48b4791cded373683a3baf67314f358d50Chris Lattner return Entry = GV; 79574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 796eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 797eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 79874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 79974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 80074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 80174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 80274391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 80374391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 80474391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 80574391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 80674391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 80774391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 80874391b48b4791cded373683a3baf67314f358d50Chris Lattner 80974391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType *PTy = 81096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 81174391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); 81274391b48b4791cded373683a3baf67314f358d50Chris Lattner} 8133f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 81474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 81574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 81674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 81774391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 81874391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 81974391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 82074391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 82196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 822bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 823bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 82403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 82503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!D->getInit() && "Cannot emit definite definitions here!"); 82603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 8277520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (MayDeferGeneration(D)) { 8287520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // If we have not seen a reference to this variable yet, place it 8297520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // into the deferred declarations table to be emitted if needed 8307520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // later. 8317520bd1de12af10ea08c662440565adbdf589317Douglas Gregor const char *MangledName = getMangledName(D); 8327520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (GlobalDeclMap.count(MangledName) == 0) { 8332a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDecls[MangledName] = GlobalDecl(D); 83403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar return; 8357520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 8367520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 8377520bd1de12af10ea08c662440565adbdf589317Douglas Gregor 8387520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // The tentative definition is the only definition. 83903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar EmitGlobalVarDefinition(D); 84003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar} 84103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 842bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 8438f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 84477ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 845b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner 8468f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 847cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 84803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // implicitly initialized with { 0 }. 84903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // 85003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // Note that tentative definitions are only emitted at the end of 85103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // a translation unit, so they should never have incomplete 85203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // type. In addition, EmitTentativeDefinition makes sure that we 85303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // never attempt to emit a tentative definition if a real one 85403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // exists. A use may still exists, however, so we still may need 85503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // to do a RAUW. 85603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 857b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson Init = EmitNullConstant(D->getType()); 85877ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 859e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson Init = EmitConstantExpr(D->getInit(), D->getType()); 86089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson 8616e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 8626e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 86389ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson if (getLangOptions().CPlusPlus) { 86489ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson CXXGlobalInits.push_back(D); 86589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = EmitNullConstant(T); 86689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } else { 86789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson ErrorUnsupported(D, "static initializer"); 86889ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 86989ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } 8706e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 8718f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 8728e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 8732d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 874570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 875570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 876570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 877570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 8789d4a15fd3b85434c43ea27562793de63a793321aChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast || 8799d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // all zero index gep. 8809d4a15fd3b85434c43ea27562793de63a793321aChris Lattner CE->getOpcode() == llvm::Instruction::GetElementPtr); 881570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 882570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 8833c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 884570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 885570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 886570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 887570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 888570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 889570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 890570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 891570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 892570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 893570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 894570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 895570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 896570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 897570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 898570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 899570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 900570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Remove the old entry from GlobalDeclMap so that we'll create a new one. 901570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GlobalDeclMap.erase(getMangledName(D)); 902232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 903570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 904570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 9050558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner GV->takeName(cast<llvm::GlobalValue>(Entry)); 9060558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 90777ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 90877ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 9093c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 910570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 91177ba708819285931932ecd33691a672bb59d221aEli Friedman 91277ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 913570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 91477ba708819285931932ecd33691a672bb59d221aEli Friedman } 91577ba708819285931932ecd33691a672bb59d221aEli Friedman 91640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 9178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 9188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 919f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 9208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 9218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 92288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 923e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner 924e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // If it is safe to mark the global 'constant', do so now. 925e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 926e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (D->getType().isConstant(Context)) { 927e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // FIXME: In C++, if the variable has a non-trivial ctor/dtor or any mutable 928e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // members, it cannot be declared "LLVM const". 929e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(true); 930e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } 931e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner 9320de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 93308d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 93488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 9358fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 9368fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 93740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLImportAttr>()) 938ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 93940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLExportAttr>()) 940ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 941e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else if (D->hasAttr<WeakAttr>()) { 942e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (GV->isConstant()) 943e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); 944e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else 945e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 946e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } else if (!CompileOpts.NoCommon && 947309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner !D->hasExternalStorage() && !D->getInit() && 948e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner !D->getAttr<SectionAttr>()) { 94904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 950e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // common vars aren't constant even if declared const. 951e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 952e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } else 95304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 9547e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar 9557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 95604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 957686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 9582d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 95966031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 960686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 961686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 96288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 964bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 965bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// implement a function with no prototype, e.g. "int foo() {}". If there are 966bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// existing call uses of the old function in the module, this adjusts them to 967bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// call the new function directly. 968bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// 969bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// This is not just a cleanup: the always_inline pass requires direct calls to 970bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// functions to be able to inline them. If there is a bitcast in the way, it 971bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// won't inline them. Instcombine normally deletes these calls, but it isn't 972bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// run at -O0. 973bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 974bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *NewFn) { 975bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If we're redefining a global as a function, don't transform it. 976bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 977bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (OldFn == 0) return; 978bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 979bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner const llvm::Type *NewRetTy = NewFn->getReturnType(); 980bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::SmallVector<llvm::Value*, 4> ArgList; 981bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 982bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 983bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner UI != E; ) { 984bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // TODO: Do invokes ever occur in C code? If so, we should handle them too. 98508c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner unsigned OpNo = UI.getOperandNo(); 986bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*UI++); 98708c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner if (!CI || OpNo != 0) continue; 988bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 989bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the return types don't match exactly, and if the call isn't dead, then 990bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // we can't transform this call. 991bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getType() != NewRetTy && !CI->use_empty()) 992bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 993bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 994bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the function was passed too few arguments, don't transform. If extra 995bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // arguments were passed, we silently drop them. If any of the types 996bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // mismatch, we don't transform. 997bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner unsigned ArgNo = 0; 998bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner bool DontTransform = false; 999bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1000bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1001bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getNumOperands()-1 == ArgNo || 1002bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->getOperand(ArgNo+1)->getType() != AI->getType()) { 1003bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner DontTransform = true; 1004bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner break; 1005bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1006bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1007bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (DontTransform) 1008bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 1009bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1010bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Okay, we can transform this. Create the new call instruction and copy 1011bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // over the required information. 1012bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.append(CI->op_begin()+1, CI->op_begin()+1+ArgNo); 1013bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1014bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.end(), "", CI); 1015bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.clear(); 1016bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (NewCall->getType() != llvm::Type::VoidTy) 1017bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->takeName(CI); 1018bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->setCallingConv(CI->getCallingConv()); 1019bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->setAttributes(CI->getAttributes()); 1020bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1021bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Finally, remove the old call, replacing any uses with the new one. 1022bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (!CI->use_empty()) 1023bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->replaceAllUsesWith(NewCall); 1024bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->eraseFromParent(); 1025bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1026bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner} 1027bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1028bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1029b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 10302b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson const llvm::FunctionType *Ty; 1031b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 1032b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner 10332b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 10342b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson bool isVariadic = D->getType()->getAsFunctionProtoType()->isVariadic(); 10352b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson 10362b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic); 10372b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } else { 10382b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 10392b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson 10402b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // As a special case, make sure that definitions of K&R function 10412b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // "type foo()" aren't declared as varargs (which forces the backend 10422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // to do unnecessary work). 10432b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (D->getType()->isFunctionNoProtoType()) { 10442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson assert(Ty->isVarArg() && "Didn't lower type as expected"); 10452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Due to stret, the lowered function could have arguments. 10462b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Just create the same type as was lowered by ConvertType 10472b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // but strip off the varargs bit. 10482b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); 104996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); 10502b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 1051ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner } 1052d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 10539fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner // Get or create the prototype for the function. 1054b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 105534809507232bc4c3c4840c7d092c7440219fddafChris Lattner 10560558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 10570558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 10580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 10590558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 10600558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 10610558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 10620558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 10630558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1064bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 1065bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 106642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 1067bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner assert(OldFn->isDeclaration() && 10680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 106934809507232bc4c3c4840c7d092c7440219fddafChris Lattner 107062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 107162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 107262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 107362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 107462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 107562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 107662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f(int x)"). Start by making a new function of the 107762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // correct type, RAUW, then steal the name. 107834809507232bc4c3c4840c7d092c7440219fddafChris Lattner GlobalDeclMap.erase(getMangledName(D)); 1079b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1080bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewFn->takeName(OldFn); 1081bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1082bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If this is an implementation of a function without a prototype, try to 1083bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // replace any existing uses of the function (which may be calls) with uses 1084bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // of the new function 10859fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner if (D->getType()->isFunctionNoProtoType()) { 1086bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 10879fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner OldFn->removeDeadConstantUsers(); 10889fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner } 108962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 109062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 1091bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (!Entry->use_empty()) { 1092bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Constant *NewPtrForOldDecl = 10933c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1094bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 1095bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 109662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 109762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 1098bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner OldFn->eraseFromParent(); 109962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 11000558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 1101bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 11020558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 11030558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 1104bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1105219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 11066379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 11077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetFunctionDefinitionAttributes(D, Fn); 11087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, Fn); 1109219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 111040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1111219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 111240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1113219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 1114bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 1115bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1116bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { 111740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis const AliasAttr *AA = D->getAttr<AliasAttr>(); 1118bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 1119bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1120bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 1121bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1122bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Unique the name through the identifier table. 1123bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *AliaseeName = AA->getAliasee().c_str(); 1124bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner AliaseeName = getContext().Idents.get(AliaseeName).getName(); 1125bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1126bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 1127bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 1128bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 1129bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 1130b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl()); 1131bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 1132bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMGlobal(AliaseeName, 113396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(DeclTy), 0); 1134bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1135bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 1136bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *GA = 1137bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 1138bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 1139bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 1140bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1141bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // See if there is already something with the alias' name in the module. 1142bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *MangledName = getMangledName(D); 1143bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 1144bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1145bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry && !Entry->isDeclaration()) { 1146bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a definition in the module, then it wins over the alias. 1147bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // This is dubious, but allow it to be safe. Just ignore the alias. 1148bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->eraseFromParent(); 1149bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return; 1150bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 1151bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1152bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 1153bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 1154bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 1155bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 1156bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 1157bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 1158bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 1159bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 1160bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 11613c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1162bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 1163bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 1164bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 1165bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1166bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Now we know that there is no conflict, set the name. 1167bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry = GA; 1168bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->setName(MangledName); 1169bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 11707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Set attributes which are particular to an alias; this is a 11717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // specialization of the attributes which may be set on a global 11727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // variable/function. 117340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<DLLExportAttr>()) { 11747c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 11757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // The dllexport attribute is ignored for undefined symbols. 11766fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (FD->getBody()) 11777c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 11787c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 11797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 11807c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 118140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<WeakAttr>() || 118240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) { 11837c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::WeakAnyLinkage); 11847c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 11857c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 11867c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GA); 1187bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 1188bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1189b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 1190b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 1191c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 11923e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 11933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 11943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 1195bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 11963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 11973e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 11983e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 11993e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 1200bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 1201bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 120286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ASTContext::GetBuiltinTypeError Error; 120386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Type = Context.GetBuiltinType(BuiltinID, Error); 120486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(Error == ASTContext::GE_None && "Can't get builtin type"); 1205370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 1206bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 1207bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 1208bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 1209b808c952bbff821dce727dd801a1098d64394f98Chris Lattner // Unique the name through the identifier table. 1210b808c952bbff821dce727dd801a1098d64394f98Chris Lattner Name = getContext().Idents.get(Name).getName(); 1211bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 1212b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); 1213bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 1214bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 12157acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 12167acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 12177acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 12187acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 12197acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 1220bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 12234e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 12244e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1226c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 12270c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 12280c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 12294e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 12304e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 12310c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 12320c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 123341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 123441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 12354e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 12364e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 123741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 12387acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 1239e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 1240e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 124144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 124244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 12433c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner RecordDecl* RD, const llvm::StructType *STy) { 1244e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 1245e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 1246e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 124744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 1248e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1249e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 125044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 1251e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 1252e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 125344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 1254e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1255e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1256e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 1257e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 1258e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 1259c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 1260e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1261e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 1262e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1263e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 1264e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 12651d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarstatic llvm::StringMapEntry<llvm::Constant*> & 12661d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarGetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 12671d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const StringLiteral *Literal, 126870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar bool TargetIsLSB, 12691d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool &IsUTF16, 12701d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned &StringLength) { 12711d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned NumBytes = Literal->getByteLength(); 12721d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 12731d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Check for simple case. 12741d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (!Literal->containsNonAsciiOrNull()) { 12751d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = NumBytes; 12761d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 12771d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength)); 12781d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar } 12791d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 12801d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Otherwise, convert the UTF8 literals into a byte string. 12811d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 12821d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 12831d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar UTF16 *ToPtr = &ToBuf[0]; 1284e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff 12851d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 12861d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar &ToPtr, ToPtr + NumBytes, 12871d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar strictConversion); 12881d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 12891d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Check for conversion failure. 12901d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (Result != conversionOK) { 12911d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove 12921d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // this duplicate code. 12931d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); 12941d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = NumBytes; 12951d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 12961d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength)); 1297e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff } 12981d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 129970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // ConvertUTF8toUTF16 returns the length in ToPtr. 13001d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = ToPtr - &ToBuf[0]; 130170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 130270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // Render the UTF-16 string into a byte array and convert to the target byte 130370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // order. 130470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // 130570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // FIXME: This isn't something we should need to do here. 130670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar llvm::SmallString<128> AsBytes; 130770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.reserve(StringLength * 2); 130870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar for (unsigned i = 0; i != StringLength; ++i) { 130970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar unsigned short Val = ToBuf[i]; 131070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar if (TargetIsLSB) { 131170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 131270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 131370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } else { 131470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 131570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 131670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 131770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 1318434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // Append one extra null character, the second is automatically added by our 1319434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // caller. 1320434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar AsBytes.push_back(0); 132170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 13221d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar IsUTF16 = true; 132370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 13241d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar} 13251d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 13261d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarllvm::Constant * 13271d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarCodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 13281d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned StringLength = 0; 13291d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool isUTF16 = false; 13301d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::StringMapEntry<llvm::Constant*> &Entry = 133170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar GetConstantCFStringEntry(CFConstantStringMap, Literal, 133270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar getTargetData().isLittleEndian(), 133370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar isUTF16, StringLength); 1334c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 13351d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (llvm::Constant *C = Entry.getValue()) 13361d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return C; 1337c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1338c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 13393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 1340c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 13419d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // If we don't already have it, get __CFConstantStringClassReference. 1342c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1343c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 134496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::ArrayType::get(Ty, 0); 13459d4a15fd3b85434c43ea27562793de63a793321aChris Lattner llvm::Constant *GV = CreateRuntimeVariable(Ty, 13469d4a15fd3b85434c43ea27562793de63a793321aChris Lattner "__CFConstantStringClassReference"); 13473e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 13483e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 13493c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1350c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1351c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1352e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 13536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek RecordDecl *CFRD = CFTy->getAs<RecordType>()->getDecl(); 13543e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1355e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1356e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1357e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1358e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 135917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis RecordDecl::field_iterator Field = CFRD->field_begin(); 136044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1361c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 136244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 136344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 136444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 136544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1366c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1367c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 136844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 136944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 13703e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 137144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 13724a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) 13734a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson : llvm::ConstantInt::get(Ty, 0x07C8), 1374e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff CFRD, STy); 1375c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1376c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 137744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 137844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 13797db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::Constant *C = llvm::ConstantArray::get(Entry.getKey().str()); 1380a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar 1381a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar const char *Sect, *Prefix; 1382a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar bool isConstant; 138395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::LinkageTypes Linkage; 1384a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1385a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Prefix = getContext().Target.getUnicodeStringSymbolPrefix(); 1386a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Sect = getContext().Target.getUnicodeStringSection(); 138795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner // FIXME: why do utf strings get "l" labels instead of "L" labels? 138895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::InternalLinkage; 1389a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // FIXME: Why does GCC not set constant here? 1390a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar isConstant = false; 1391a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } else { 139295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Prefix = ".str"; 1393a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Sect = getContext().Target.getCFStringDataSection(); 139495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::PrivateLinkage; 1395a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // FIXME: -fwritable-strings should probably affect this, but we 1396a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // are following gcc here. 1397a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar isConstant = true; 1398a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 13998e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::GlobalVariable *GV = 14001c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(getModule(), C->getType(), isConstant, 140195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage, C, Prefix); 1402a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (Sect) 14038e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 1404a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1405a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; 1406a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar GV->setAlignment(Align); 1407a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 140844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 14093c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2), 1410e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1411c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1412c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 141344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 141444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1415c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 141644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 14174a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::ConstantInt::get(Ty, StringLength), CFRD, STy); 1418c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1419c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 142008e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson C = llvm::ConstantStruct::get(STy, Fields); 14211c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 142295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalVariable::PrivateLinkage, C, 142395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner "_unnamed_cfstring_"); 14248e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (const char *Sect = getContext().Target.getCFStringSection()) 14258e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 14261d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar Entry.setValue(GV); 14273e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 14280c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1429c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 143045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 14316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 14321e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 14336143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 14341e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 14351e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 14361e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 14371e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 14381e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 14391e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 14401e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1441dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 14421e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 14431e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1444dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1445dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1446dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1447dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 14481e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 14491e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 14501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 14511e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 14521e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 14536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 14546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 14556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 14566143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 14576143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 14586143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 14596143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 14606143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1461eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1462eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1463eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1464eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1465eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1466eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1467a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1468a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1469eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1470eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1471eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1472a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 147345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 147445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 14755fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 14765fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 14776143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 14787db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::Constant *C = llvm::ConstantArray::get(str, false); 147945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 148045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 14811c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 148295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, 14831c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson C, GlobalName); 148445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 148545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 14866143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 14876143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 14886143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 14896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 14906143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 14916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 14926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 14936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 14945fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 14955fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 14968e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar bool IsConstant = !Features.WritableStrings; 14978e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 14988e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Get the default prefix if a name wasn't specified. 14998e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!GlobalName) 150095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GlobalName = ".str"; 15018e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 15028e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Don't share any string literals if strings aren't constant. 15038e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!IsConstant) 15045fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 150545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 150645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 150795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 150845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 150945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1510eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 151145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 151245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 15135fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 151445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 151545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 151645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 15176143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 15186143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 15196143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 15206143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 15215fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 15225fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1523c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 15246143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 152541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1526af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1527af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1528af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1529af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1530653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor for (ObjCImplementationDecl::propimpl_iterator 153117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1532af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1533af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1534af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1535af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1536af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1537af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1538af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1539af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1540af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1541af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1542af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 154317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis if (!D->getInstanceMethod(PD->getGetterName())) 1544fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1545fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1546af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 154717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis !D->getInstanceMethod(PD->getSetterName())) 1548fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1549fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1550af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1551af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1552af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1553af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 155491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace. 1555984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 155617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1557984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson I != E; ++I) 1558984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitTopLevelDecl(*I); 1559984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson} 1560984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson 156191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec. 156291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1563f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1564f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 156591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson ErrorUnsupported(LSD, "linkage spec"); 156691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson return; 156791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson } 156891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 156917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 157091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson I != E; ++I) 157191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitTopLevelDecl(*I); 157291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson} 157391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 157441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 157541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 157641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 157741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 157841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 157941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 158041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 158141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 158216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Ignore dependent declarations. 158316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 158416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 158516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 158641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 15872b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson case Decl::CXXMethod: 158841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 158916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Skip function templates 159016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 159116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 159216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 159316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Fall through 159416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 159541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 15962a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitGlobal(GlobalDecl(cast<ValueDecl>(D))); 159741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 159841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 159995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // C++ Decls 160041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1601984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitNamespace(cast<NamespaceDecl>(D)); 160241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 16039cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // No code generation needed. 16049cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor case Decl::Using: 1605127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::ClassTemplate: 1606127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::FunctionTemplate: 16079cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor break; 160895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson case Decl::CXXConstructor: 160995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 161095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson break; 161127ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson case Decl::CXXDestructor: 161227ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 161327ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson break; 161436674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 161536674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson case Decl::StaticAssert: 161636674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson // Nothing to do. 161736674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson break; 161836674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 161995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // Objective-C Decls 162041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 162138e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 162241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 162341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1624b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1625b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 162641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1627285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner 162841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1629b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 163041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 163141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 163241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1633af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1634af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 163541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 163641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 163741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1638af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1639af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1640af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1641af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 164241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1643af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 164441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 164541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 164641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 16476fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (OMD->getBody()) 164841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 164941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 165041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 165141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1652305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 165341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 165441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 165591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson case Decl::LinkageSpec: 165691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 165741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 165841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 165941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 166041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 166141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 166241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 166341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 166441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 166541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 166641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 166741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 166841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 166941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 167041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 167141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 167241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 1673f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Make sure we handled everything we should, every other kind is a 1674f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 1675f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // function. Need to recode Decl::Kind to do that easily. 167641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 167741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 167841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1679