CodeGenModule.cpp revision c50689bd1e8788a7fc8f19070b7505ff95034979
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 14e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h" 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2221ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 232c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 26ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 27bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 2920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, 35fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 36f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar Diagnostic &diags, bool GenerateDebugInfo) 3790a904309c79977fcba2ff0542e2e4cd8e3c3fafMike Stump : BlockModule(C, M, TD, Types, *this), Context(C), Features(LO), TheModule(M), 382a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), 392a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) { 40208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 41208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Features.ObjC1) { 42f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar if (Features.NeXTRuntime) { 4330bc57187be7535c57ef1ca8ff3e765653e94332Fariborz Jahanian Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) 44ee0af74d1e0990c7b66d32657f3e4e54b8691552Fariborz Jahanian : CreateMacObjCRuntime(*this); 45208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } else { 46208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Runtime = CreateGNUObjCRuntime(*this); 47208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } 48c17a4d3b16a2624a76de5d7508805534545bd3bfDaniel Dunbar } 49e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 50e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 5126efc3388adb010984da2f70e1f24e8286e6476dMike Stump DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; 522b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 532b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 542b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 55815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 56815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 57815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 600269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitDeferred(); 61219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar EmitAliases(); 62208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 63208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 67532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 680269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 69b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar BindRuntimeGlobals(); 702b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 72b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbarvoid CodeGenModule::BindRuntimeGlobals() { 73f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Deal with protecting runtime function names. 74b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar for (unsigned i = 0, e = RuntimeGlobals.size(); i < e; ++i) { 75b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalValue *GV = RuntimeGlobals[i].first; 76b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar const std::string &Name = RuntimeGlobals[i].second; 77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 78b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar // Discard unused runtime declarations. 79b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar if (GV->isDeclaration() && GV->use_empty()) { 80b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar GV->eraseFromParent(); 810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar continue; 820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar } 830293d540baafbe070c1035611787a81001a4118eDaniel Dunbar 84f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // See if there is a conflict against a function. 85b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalValue *Conflict = TheModule.getNamedValue(Name); 86f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (Conflict) { 87f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Decide which version to take. If the conflict is a definition 88f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // we are forced to take that, otherwise assume the runtime 89f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // knows best. 90b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar 91b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar // FIXME: This will fail phenomenally when the conflict is the 92b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar // wrong type of value. Just bail on it for now. This should 93b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar // really reuse something inside the LLVM Linker code. 94b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar assert(GV->getValueID() == Conflict->getValueID() && 95b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar "Unable to resolve conflict between globals of different types."); 96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (!Conflict->isDeclaration()) { 97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 98b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::ConstantExpr::getBitCast(Conflict, GV->getType()); 99b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar GV->replaceAllUsesWith(Casted); 100b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar GV->eraseFromParent(); 101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 102b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar GV->takeName(Conflict); 103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 104b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::ConstantExpr::getBitCast(GV, Conflict->getType()); 105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->replaceAllUsesWith(Casted); 106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->eraseFromParent(); 107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 108b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar } else 109b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar GV->setName(Name); 110f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 111f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 112f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 113488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1142c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 11790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 119488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 12056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1212c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1220a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1230a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 126488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 127c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 12890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 12990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 13090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 13190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 132488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 13356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 134c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1350a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 136c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 137c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 13841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 13941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 14041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 14141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1524f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1534f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1544f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1554f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 159c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 160c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We 1635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a 1645f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require 165b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// changes to the GlobalDeclMap, too. (I disagree, I think what we 166b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// actually need is for Sema to provide some notion of which Decls 167b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// refer to the same semantic decl. We shouldn't need to mangle the 168b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// names and see what comes out the same to figure this out. - DWD) 1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start 1715f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above 1725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first. 1736ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 174c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner // In C, functions with no attributes never need to be mangled. Fastpath them. 175c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { 176c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 177c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner return ND->getIdentifier()->getName(); 178c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 179c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner 1806ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1816ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 182fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar if (!mangleName(ND, Context, Out)) { 183fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1846ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return ND->getIdentifier()->getName(); 185fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar } 1865f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1876ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 1886ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return MangledNames.GetOrCreateValue(Name.begin(), Name.end()) 1896ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor .getKeyData(); 1905f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1915f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1926d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1936d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 19549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1976d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1986d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 20249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 215572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2236bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2246bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 2256bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2266bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2276bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2286bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 2296bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2306bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 231572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2326bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2336bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 234572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2356d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2366d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2376d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 241532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 243532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 247532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 248532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 249532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 250532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 251532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 252532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 253532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2545c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 2555c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2565c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2575c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2585c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 25949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 260d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 261219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 262219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2632f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2642f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2652f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2663ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2672f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2682f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2692f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2702f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2712f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2722f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2732f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2743ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2755e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || 2765e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar D->getAttr<WeakImportAttr>()) { 2775e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 2785e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // separate linkage types for this. 279e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands GV->setLinkage(llvm::Function::ExternalWeakLinkage); 2805e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } 281219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 282219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 283219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 284219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2852f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2862f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2872f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2882f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2892f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2902f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2912f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2925e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() || 2935e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar IsInline) 294286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump GV->setLinkage(llvm::Function::WeakAnyLinkage); 295219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2960dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 297d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 29849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 29949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 3000dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 30141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 302d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 303a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 304eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 305eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // should not be munged. 306eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) 307a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 30817f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 30917f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 31017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 3115c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 3125c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 3135c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // might add multiple times or risk the value being replaced by a 3145c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // subsequent RAUW. 3155c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (ForDefinition) { 3165c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 3175c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 3185c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 319d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 320d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 321761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 32245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 323b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 324761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 32588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 326c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 327761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 328761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 329ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 330ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 33145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 332f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 333f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 334f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 335f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 336f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 337f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 338f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 339f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 340219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 341219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 342219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 343219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 344219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 345219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 346219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 347219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 348219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 349219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 35074ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 351f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 352af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 353af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 354af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 35581ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 35681ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson if (D->getAttr<NoinlineAttr>()) 35781ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 358f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 359f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 360f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 361f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 362541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 363f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 364219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 365f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 366f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 367f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 368f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 369541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 37045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 37545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 3785e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar const ValueDecl *D = Aliases[i]; 379219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 382219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 387219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 388219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 3895e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar llvm::GlobalValue *aliasee = getModule().getNamedValue(aliaseeName); 390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 391219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 393219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 394219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 395219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 397219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 399219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 4006ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor getMangledName(D), aliasee, 4015f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 402219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 4035f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 404219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 405219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 406219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 407219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 408219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 409219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 410219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 411219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 412ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 413219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 414219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 415219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 416219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 417219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 418219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 419ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 420ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 4210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 4240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); 4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 4300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 4310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), 4340269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.size()); 4350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 4360269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 4370269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ConstantArray::get(ATy, LLVMUsed), 4390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4430269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 4450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Emit code for any deferred decl which was used. Since a 4460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // previously unused static decl may become used during the 4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // generation of code for a static function, iterate until no 4480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // changes are made. 4494c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 4504c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 4514c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 452b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(), 4540269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar e = DeferredDecls.end(); i != e; ) { 455b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 456b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4576f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 4586f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 4596f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 460219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 46173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // need to check for uses in an alias. 4625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!GlobalDeclMap.count(getMangledName(D))) { 463232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ++i; 464a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 465b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 466b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 467bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 468bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 469bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4704c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 4710269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar i = DeferredDecls.erase(i); 472b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4734c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 4744c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 4754c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4764c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4823c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 5028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 5078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 5088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 5098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 5108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 5128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 5138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 5148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 5158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 5168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 5178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 5188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 5198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 5208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 5218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5245c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5255c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 5265c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) 52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 528bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 529bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 53073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) 53273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 53373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 53473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 53573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 53673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 53773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 53873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar assert(VD->isFileVarDecl() && "Invalid decl."); 53973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 54073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 54173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 54273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 54373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 54473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 54573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 54673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 54773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 5485e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar // Aliases are deferred until code for everything else has been 5495e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar // emitted. 5505e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (Global->getAttr<AliasAttr>()) { 5515e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar Aliases.push_back(Global); 5525e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar return; 5535e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar } 554219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 5555e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 55673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 55773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 55873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5590269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5600269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 561bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 562bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 56373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 5647542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 56573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5664c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5674c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 56873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Defer code generation when possible. 56973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 5700269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar DeferredDecls.push_back(Global); 571bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 572bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 573bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 574bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 575bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5764c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5774c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 578bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 579bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 580bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 581bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 582bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 583bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 584bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 585bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 586bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 587bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5889986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 58977ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 59077ba708819285931932ecd33691a672bb59d221aEli Friedman 591bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 592bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 5939986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 594bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5953c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5965f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 59749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 59849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 59949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 60049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 6016ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), &getModule(), 6025f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, ASTTy.getAddressSpace()); 60349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 60449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 60649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 60849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 60949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 61049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 61149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 612eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // FIXME: Merge with other attribute handling code. 613eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 61449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 61549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 616eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 6175e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 618e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 6193f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 6203f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 6213f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 6223f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // should not be munged. 6233f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar GV->setName("\01" + ALA->getLabel()); 6243f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar } 62549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 6263c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 6279986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 6289986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 629bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 630bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 631bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6328f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 63377ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 63477ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 63577ba708819285931932ecd33691a672bb59d221aEli Friedman 6368f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 637cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 638cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 639cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 640cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 641cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 642cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 643cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 644cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 645cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 646cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 647cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 648cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 64977ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 650bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 6516e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 652232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ErrorUnsupported(D, "static initializer"); 6536e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 6546e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 6556e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 6568f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 65777ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 6588e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 6595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 6603c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 6613c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 66277ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 66377ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 66477ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6656ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 6665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 667232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 668232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar } else if (GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { 669232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // If we already have this global and it has an initializer, then 670232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // we are in the rare situation where we emitted the defining 671232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // declaration of the global and are now being asked to emit a 672232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // definition which would be common. This occurs, for example, in 673232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // the following situation because statics can be emitted out of 674232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // order: 675232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 676232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x; 677232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int *y = &x; 678232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x = 10; 679232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // int **z = &y; 680232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 681232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // Bail here so we don't blow away the definition. Note that if we 682232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // can't distinguish here if we emitted a definition with a null 683232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // initializer, but this case is safe. 684232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar assert(!D->getInit() && "Emitting multiple definitions of a decl!"); 685232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar return; 686232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 6873c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 6883c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 68977ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 69077ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 69177ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 69277ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 69377ba708819285931932ecd33691a672bb59d221aEli Friedman // 69477ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 69577ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 69677ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 69777ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 698cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 699cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 700cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 701cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 70277ba708819285931932ecd33691a672bb59d221aEli Friedman 70377ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 70477ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 70577ba708819285931932ecd33691a672bb59d221aEli Friedman 70677ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 70777ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 70877ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 7096ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 7105f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 71177ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 71277ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 71377ba708819285931932ecd33691a672bb59d221aEli Friedman 71477ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 71577ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 71677ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 71777ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 71877ba708819285931932ecd33691a672bb59d221aEli Friedman 71977ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 72077ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 72177ba708819285931932ecd33691a672bb59d221aEli Friedman } 72277ba708819285931932ecd33691a672bb59d221aEli Friedman 7233c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 7249e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 7258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 7268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 7278bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 728f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 7298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 7308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 73188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 732b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 7330de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 73408d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 735ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 73641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 737ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 738a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 739a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 740a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 741a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 742a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 743a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 74488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 74588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7468fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7478fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 7488fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 749ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 750ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 751ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 7525e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 753286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 7548fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 755ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 756ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 757ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 7588fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 759ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 760ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 761ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 762ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 763ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 764ceb77d909f80d4949ee6177094510413c391cddcDuncan Sands GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 76598883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 76698883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 767ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 768ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 76949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 77049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 77149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 772ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 77349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 77449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 775ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 776ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 77788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 778686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 77917f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 78017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 78117f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 7825c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 7835c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7845c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 785686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 786686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 787686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 78866031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 789686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 790686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 79188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 793bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 794d5d31801fc87239436fa349c89dce7797cf13537Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D, 79542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar const llvm::Type *Ty) { 7962136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman bool DoSetAttributes = true; 7972136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (!Ty) { 798d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = getTypes().ConvertType(D->getType()); 7992136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (!isa<llvm::FunctionType>(Ty)) { 8002136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // This function doesn't have a complete type (for example, the return 8012136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // type is an incomplete struct). Use a fake type instead, and make 8022136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // sure not to try to set attributes. 8032136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman Ty = llvm::FunctionType::get(llvm::Type::VoidTy, 8042136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman std::vector<const llvm::Type*>(), false); 8052136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman DoSetAttributes = false; 8062136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman } 8072136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman } 80842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 80942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar llvm::Function::ExternalLinkage, 81042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar getMangledName(D), 81142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar &getModule()); 8122136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (DoSetAttributes) 8132136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman SetFunctionAttributes(D, F); 814219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 815bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 816bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 817bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 8189986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 8199986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 8209986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 8213c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 8223c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 8235f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 8243c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 825d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, 0); 826bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 8279986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 828bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 829bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 830bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 831d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::FunctionType *Ty = 832d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 833d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 834d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // As a special case, make sure that definitions of K&R function 835d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // "type foo()" aren't declared as varargs (which forces the backend 836d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // to do unnecessary work). 837d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar if (Ty->isVarArg() && Ty->getNumParams() == 0 && Ty->isVarArg()) 838d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = llvm::FunctionType::get(Ty->getReturnType(), 839d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar std::vector<const llvm::Type*>(), 840d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar false); 841d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 8425f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 8433c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 844d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, Ty); 84542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar } else { 84642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 84742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 84842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // Otherwise, we have a definition after a prototype with the 84942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // wrong type. F is the Function* for the one with the wrong 85042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // type, we must make a new Function* and update everything that 85142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // used F (a declaration) with the new Function* (which will be 85242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // a definition). 85342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // 85442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // This happens if there is a prototype for a function 85542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // (e.g. "int f()") and then a definition of a different type 85642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // (e.g. "int f(int x)"). Start by making a new function of the 85742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // correct type, RAUW, then steal the name. 85842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D, Ty); 85942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar NewFn->takeName(Entry); 86042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar 86142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // Replace uses of F with the Function we will endow with a body. 86242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar llvm::Constant *NewPtrForOldDecl = 86342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 86442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 86542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar 86642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // Ok, delete the old function now, which is dead. 86742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 86842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar Entry->eraseFromParent(); 86942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar 87042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar Entry = NewFn; 87142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar } 872bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 873bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 874219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 875219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8766379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 877219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 878219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 879219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 880219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 881219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 882219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 883bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 884bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 885bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 886f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 887f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 888f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 889f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 890f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 891f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 892b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar RuntimeGlobals.push_back(std::make_pair(Fn, Name)); 893f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 894f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 895f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 896b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbarllvm::GlobalVariable * 897b681b8ffab2aa016b3897916d5110927c34a584bDaniel DunbarCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 898b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar const std::string &Name) { 899b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalVariable *GV = 900b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar new llvm::GlobalVariable(Ty, /*Constant=*/false, 901b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalValue::ExternalLinkage, 902b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar 0, "", &TheModule); 903b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar RuntimeGlobals.push_back(std::make_pair(GV, Name)); 904b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar return GV; 905b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar} 906b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar 907c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 908c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 909c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 910d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 911d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 912d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 913bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 914c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 9151426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 9161426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 917bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9181426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 9191426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 9201426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 921c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump llvm::Value *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 922bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 923bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 924bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9253e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 9263e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 9273e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 928bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9293e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 9303e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 9313e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 9323e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 933bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 934bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 935370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor Builtin::Context::GetBuiltinTypeError Error; 936370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); 937370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); 938370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 939bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 940bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 941bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 942bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 943bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 944bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 945bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 946bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 947bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 948bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 949c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 950c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 951c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 952c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 953c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 954bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9554667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner llvm::GlobalValue *&ExistingFn = 9564667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner GlobalDeclMap[getContext().Idents.get(Name).getName()]; 9574667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner if (ExistingFn) 9584667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = llvm::ConstantExpr::getBitCast(ExistingFn, Ty); 959c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump 960bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 9614667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = ExistingFn = 9624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 9634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 964bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 965bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9667acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 9677acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 9687acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 9697acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 9707acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 971bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 9744e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9754e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 977c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9780c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 9790c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 9804e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9814e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 9820c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 9830c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 98441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 98541ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 9864e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9874e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 98841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 9897acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 990e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 991e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 99244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 99344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 994e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 995e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 996e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 997e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 998e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 99944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 1000e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1001e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 100244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 1003e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 1004e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 100544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 1006e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1007e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1008e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 1009e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 1010e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 1011e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 1012e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1013e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 1014e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1015e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 1016e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 10173e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 10183e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 1019bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 1020bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 1021c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 1022c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1023c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1024c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 1025c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 1026c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 10273e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 10283e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 1029c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1030c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1031c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1032c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 10333e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10343e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 10353e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 10363e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 10373e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 10383e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 10393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 1040c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 1041c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 1042c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 1043c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 10443e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10453e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 10463e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 10473e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1048c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1049c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1050e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 1051e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 10523e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1053e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1054e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1055e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1056e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 105744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 105844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1059c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 106044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 106144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 106244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 106344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1064c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1065c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 106644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 106744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10683e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 106944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 107044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 1071c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1072c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 107344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 107444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10753e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 1076c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 1077c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 1078e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 107944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1080e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 1081e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1082c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1083c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 108444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 108544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1086c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 108744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 108844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 1089c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1090c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 1091e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 10920c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 10930c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 10940c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 10950c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 10963e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10970c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 10980c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 10993e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 11000c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1101c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 110245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 11036143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 11041e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 11056143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 11061e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 11071e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 11081e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11091e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 11101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 11111e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 11121e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1113dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 11141e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 11151e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1116dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1117dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1118dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1119dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 11201e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 11211e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11221e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 11231e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 11241e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11256143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 11266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 11276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 11286143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 11296143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 11306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 11316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 11326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1133eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1134eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1135eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1136eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1137eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1138eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1139a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1140a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1141eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1142eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1143eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1144a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 114545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 114645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 11475fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 11485fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 11496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 11506143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 115145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 115245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 1153eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return new llvm::GlobalVariable(C->getType(), constant, 1154eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 1155eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C, GlobalName ? GlobalName : ".str", 1156eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner &CGM.getModule()); 115745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 115845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 11596143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 11606143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 11616143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 11626143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 11636143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 11646143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 11656143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 11666143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 11675fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 11685fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 116945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 117045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 11715fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 117245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 117345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 117445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 117545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 117645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1177eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 117845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 117945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 11805fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 118145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 118245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 118345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 11846143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 11856143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 11866143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 11876143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 11885fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 11895fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1190c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 11916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 119241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1193af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1194af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1195af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1196af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1197af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1198af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1199af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1200af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1201af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1202af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1203af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1204af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1205af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1206af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1207af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1208af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1209af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1210af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1211fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1212fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1213af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1214af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1215fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1216fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1217af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1218af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1219af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1220af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 122141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 122241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 122341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 122441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 122541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 122641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 122741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 122841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 122941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 123041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 123141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 123241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 123341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 123441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 123541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1236662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 123741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 123841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 123941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 124041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 124138e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 124241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 124341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 124441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 124538e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian 124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 124738e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian case Decl::ObjCCategory: 124838e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian case Decl::ObjCInterface: { 124938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian ObjCContainerDecl *OCD = cast<ObjCContainerDecl>(D); 125038e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian for (ObjCContainerDecl::tuvar_iterator i = OCD->tuvar_begin(), 125138e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian e = OCD->tuvar_end(); i != e; ++i) { 125238e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian VarDecl *VD = *i; 125338e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian EmitGlobal(VD); 125438e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian } 125538e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian if (D->getKind() == Decl::ObjCProtocol) 125638e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 125741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 125838e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian } 125941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 126041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1261af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1262af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1263af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 126441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 126541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 126641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1267af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1268af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1269af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1270af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 127141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1272af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 127341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 127441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 127541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 127641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 127741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 127841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 127941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 128041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1281305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 128241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 128341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 128441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 128541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 128641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1287488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 128841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 128941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 129041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 129141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 129241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 129341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 129441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 129541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 129641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 129741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 129841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 129941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 130041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 130141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 130241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 130341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 130441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 130541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 130641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 130741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 130841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 130941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 131041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 131141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 131241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 131341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1314