CodeGenModule.cpp revision b681b8ffab2aa016b3897916d5110927c34a584b
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 1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// IdentifierInfo* containing the mangled name. Otherwise, returns 1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// the name of the declaration as an identifier. 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) { 1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1756ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 176fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar if (!mangleName(ND, Context, Out)) { 177fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1786ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return ND->getIdentifier()->getName(); 179fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar } 1805f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1816ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 1826ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return MangledNames.GetOrCreateValue(Name.begin(), Name.end()) 1836ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor .getKeyData(); 1845f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1855f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1866d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1876d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 18949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1916d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1926d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 19649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 209572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 2236bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2246bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 225572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2266bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2276bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 228572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2296d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2306d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2316d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 237532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 241532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 243532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 247532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2485c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 2495c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2505c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2515c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2525c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 25349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 254d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 255219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 256219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2572f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2582f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2592f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2603ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2612f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2622f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2632f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2642f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2652f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2662f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2672f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2683ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2695e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || 2705e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar D->getAttr<WeakImportAttr>()) { 2715e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 2725e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // separate linkage types for this. 273eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar GV->setLinkage(llvm::Function::ExternalWeakLinkage); 2745e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } 275219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 276219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 277219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 278219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2792f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2802f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2812f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2822f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2832f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2842f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2852f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2865e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() || 2875e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar IsInline) 288219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::WeakLinkage); 289219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2900dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 291d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 29249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 29349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2940dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 29541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 296d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 297a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 298eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 299eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // should not be munged. 300eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) 301a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 30217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 30317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 30417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 3055c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 3065c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 3075c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // might add multiple times or risk the value being replaced by a 3085c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // subsequent RAUW. 3095c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (ForDefinition) { 3105c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 3115c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 3125c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 313d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 314d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 315761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 31645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 317b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 318761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 31988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 320c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 321761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 322761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 323ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 324ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 32545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 326f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 327f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 328f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 329f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 330f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 331f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 332f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 333f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 334219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 335219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 336219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 337219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 338219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 339219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 340219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 341219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 342219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 343219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 34474ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 345f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 346af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 347af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 348af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 34981ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 35081ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson if (D->getAttr<NoinlineAttr>()) 35181ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 352f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 353f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 354f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 355f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 356541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 357f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 358219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 359f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 360f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 361f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 362f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 363541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 36445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 365219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 366219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 367219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 368219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 36945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 370219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *D = Aliases[i]; 373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 379219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 382219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *aliasee = getModule().getFunction(aliaseeName); 384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 387219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 388219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 389219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 391219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 393219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 3946ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor getMangledName(D), aliasee, 3955f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3975f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 399219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 400219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 401219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 402219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 403219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 404219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 405219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 406ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 407219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 408219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 409219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 410219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 411219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 412219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 413ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 414ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); 4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 4240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), 4280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.size()); 4290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 4300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 4310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ConstantArray::get(ATy, LLVMUsed), 4330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 4340269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4360269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4370269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 4390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Emit code for any deferred decl which was used. Since a 4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // previously unused static decl may become used during the 4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // generation of code for a static function, iterate until no 4420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // changes are made. 4434c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 4444c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 4454c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 446b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(), 4480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar e = DeferredDecls.end(); i != e; ) { 449b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 450b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4516f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 4526f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 4536f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 454219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 45573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // need to check for uses in an alias. 4565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!GlobalDeclMap.count(getMangledName(D))) { 457232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ++i; 458a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 459b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 460b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 461bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 462bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 463bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4644c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 4650269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar i = DeferredDecls.erase(i); 466b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4674c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 4684c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 4694c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4704c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4763c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 5028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 5078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 5088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 5098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 5108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 5128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 5138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 5148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 5158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 51773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5185c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5195c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 5205c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) 52173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 522bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 523bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 52473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 52573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) 52673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 52973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 53073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 53273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar assert(VD->isFileVarDecl() && "Invalid decl."); 53373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 53473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 53573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 53673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 53773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 53873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 53973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 54073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 54173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 54273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 543219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Aliases are deferred until code for everything else has been 544219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // emitted. 545219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (FD->getAttr<AliasAttr>()) { 546219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar assert(!FD->isThisDeclarationADefinition() && 547219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar "Function alias cannot have a definition!"); 548219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Aliases.push_back(FD); 549219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return; 550219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 551219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 55273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 55373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 55473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5550269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5560269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 557bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 558bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 55973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 5607542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 56173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 56473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Defer code generation when possible. 56573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 5660269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar DeferredDecls.push_back(Global); 567bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 568bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 569bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 570bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 571bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5724c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5734c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 574bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 575bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 576bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 577bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 578bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 579bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 580bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 581bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 582bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 583bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5849986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 58577ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 58677ba708819285931932ecd33691a672bb59d221aEli Friedman 587bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 588bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 5899986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 590bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5913c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5925f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 59349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 59449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 59549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 59649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 5976ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), &getModule(), 5985f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, ASTTy.getAddressSpace()); 59949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 60049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 60249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 60449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 60549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 60749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 608eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // FIXME: Merge with other attribute handling code. 609eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 61049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 61149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 612eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 6135e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 614eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 6153f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 6163f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 6173f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 6183f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // should not be munged. 6193f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar GV->setName("\01" + ALA->getLabel()); 6203f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar } 62149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 6223c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 6239986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 6249986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 625bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 626bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 627bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6288f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 62977ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 63077ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 63177ba708819285931932ecd33691a672bb59d221aEli Friedman 6328f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 633cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 634cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 635cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 636cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 637cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 638cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 639cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 640cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 641cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 642cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 643cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 644cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 64577ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 646bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 6476e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 648232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ErrorUnsupported(D, "static initializer"); 6496e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 6506e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 6516e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 6528f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 65377ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 6548e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 6555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 6563c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 6573c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 65877ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 65977ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 66077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6616ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 6625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 663232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 664232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar } else if (GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { 665232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // If we already have this global and it has an initializer, then 666232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // we are in the rare situation where we emitted the defining 667232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // declaration of the global and are now being asked to emit a 668232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // definition which would be common. This occurs, for example, in 669232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // the following situation because statics can be emitted out of 670232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // order: 671232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 672232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x; 673232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int *y = &x; 674232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x = 10; 675232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // int **z = &y; 676232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 677232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // Bail here so we don't blow away the definition. Note that if we 678232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // can't distinguish here if we emitted a definition with a null 679232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // initializer, but this case is safe. 680232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar assert(!D->getInit() && "Emitting multiple definitions of a decl!"); 681232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar return; 682232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 6833c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 6843c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 68577ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 68677ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 68777ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 68877ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 68977ba708819285931932ecd33691a672bb59d221aEli Friedman // 69077ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 69177ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 69277ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 69377ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 694cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 695cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 696cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 697cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 69877ba708819285931932ecd33691a672bb59d221aEli Friedman 69977ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 70077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 70177ba708819285931932ecd33691a672bb59d221aEli Friedman 70277ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 70377ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 70477ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 7056ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 7065f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 70777ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 70877ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 70977ba708819285931932ecd33691a672bb59d221aEli Friedman 71077ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 71177ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 71277ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 71377ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 71477ba708819285931932ecd33691a672bb59d221aEli Friedman 71577ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 71677ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 71777ba708819285931932ecd33691a672bb59d221aEli Friedman } 71877ba708819285931932ecd33691a672bb59d221aEli Friedman 7193c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 7209e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 7218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 7228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 7238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 724f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 7258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 7268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 72788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 728b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 7290de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 73008d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 731ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 73241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 733ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 734a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 735a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 736a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 737a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 738a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 739a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 74088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 74188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7428fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7438fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 7448fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 745ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 746ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 747ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 7485e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 749ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakLinkage); 7508fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 751ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 752ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 753ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 7548fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 755ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 756ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 757ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 758ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 759ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 760a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 76198883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 76298883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 763ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 764ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 76549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 76649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 76749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 768ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 76949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 77049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 771ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 772ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 77388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 774686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 77517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 77617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 77717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 7785c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 7795c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7805c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 781686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 782686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 783686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 78466031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 785686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 786686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 78788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 789bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 790d5d31801fc87239436fa349c89dce7797cf13537Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D, 791d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::Type *Ty) { 7922136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman bool DoSetAttributes = true; 7932136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (!Ty) { 794d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = getTypes().ConvertType(D->getType()); 7952136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (!isa<llvm::FunctionType>(Ty)) { 7962136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // This function doesn't have a complete type (for example, the return 7972136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // type is an incomplete struct). Use a fake type instead, and make 7982136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman // sure not to try to set attributes. 7992136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman Ty = llvm::FunctionType::get(llvm::Type::VoidTy, 8002136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman std::vector<const llvm::Type*>(), false); 8012136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman DoSetAttributes = false; 8022136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman } 8032136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman } 804219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 805219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 8066ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor getMangledName(D), 8075f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 8082136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman if (DoSetAttributes) 8092136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman SetFunctionAttributes(D, F); 810219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 811bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 812bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 813bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 8149986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 8159986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 8169986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 8173c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 8183c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 8195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 8203c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 821d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, 0); 822bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 8239986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 824bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 825bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 826bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 827d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::FunctionType *Ty = 828d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 829d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 830d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // As a special case, make sure that definitions of K&R function 831d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // "type foo()" aren't declared as varargs (which forces the backend 832d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // to do unnecessary work). 833d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar if (Ty->isVarArg() && Ty->getNumParams() == 0 && Ty->isVarArg()) 834d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = llvm::FunctionType::get(Ty->getReturnType(), 835d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar std::vector<const llvm::Type*>(), 836d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar false); 837d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 8385f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 8393c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 840d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, Ty); 841bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 8423c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 8433c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 844d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // Otherwise, we have a definition after a prototype with the 845d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // wrong type. F is the Function* for the one with the wrong 846d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // type, we must make a new Function* and update everything that 847d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // used F (a declaration) with the new Function* (which will be 848d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // a definition). 849bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // 850d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // This happens if there is a prototype for a function 851d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // (e.g. "int f()") and then a definition of a different type 852d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // (e.g. "int f(int x)"). Start by making a new function of the 853d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // correct type, RAUW, then steal the name. 854d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D, Ty); 8553c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar NewFn->takeName(Entry); 856bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 857bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Replace uses of F with the Function we will endow with a body. 858bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *NewPtrForOldDecl = 8593c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 8603c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 8613c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 862bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Ok, delete the old function now, which is dead. 8633c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 864219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 865bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 866bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = NewFn; 867bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 868bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 869bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 870219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 871219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8726379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 873219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 874219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 875219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 876219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 877219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 878219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 879bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 880bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 881bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 882f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 883f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 884f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 885f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 886f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 887f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 888b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar RuntimeGlobals.push_back(std::make_pair(Fn, Name)); 889f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 890f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 891f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 892b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbarllvm::GlobalVariable * 893b681b8ffab2aa016b3897916d5110927c34a584bDaniel DunbarCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 894b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar const std::string &Name) { 895b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalVariable *GV = 896b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar new llvm::GlobalVariable(Ty, /*Constant=*/false, 897b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalValue::ExternalLinkage, 898b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar 0, "", &TheModule); 899b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar RuntimeGlobals.push_back(std::make_pair(GV, Name)); 900b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar return GV; 901b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar} 902b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar 903c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 904c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 905c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 906d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 907d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 908d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 909bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 910c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 9111426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 9121426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 913bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9141426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 9151426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 9161426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 917c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump llvm::Value *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 918bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 919bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 920bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9213e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 9223e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 9233e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 924bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9253e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 9263e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 9273e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 9283e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 929bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 930bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 931370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor Builtin::Context::GetBuiltinTypeError Error; 932370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); 933370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); 934370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 935bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 936bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 937bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 938bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 939bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 940bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 941bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 942bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 943bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 944bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 945c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 946c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 947c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 948c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 949c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 950bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9514667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner llvm::GlobalValue *&ExistingFn = 9524667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner GlobalDeclMap[getContext().Idents.get(Name).getName()]; 9534667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner if (ExistingFn) 9544667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = llvm::ConstantExpr::getBitCast(ExistingFn, Ty); 955c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump 956bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 9574667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = ExistingFn = 9584c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 9594c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 960bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 961bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9627acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 9637acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 9647acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 9657acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 9667acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 967bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 9704e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9714e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 973c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9740c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 9750c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 9764e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9774e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 9780c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 9790c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 98041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 98141ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 9824e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9834e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 98441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 9857acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 986e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 987e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 98844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 98944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 990e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 991e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 992e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 993e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 994e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 99544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 996e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 997e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 99844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 999e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 1000e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 100144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 1002e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1003e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1004e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 1005e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 1006e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 1007e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 1008e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1009e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 1010e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1011e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 1012e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 10133e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 10143e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 1015bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 1016bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 1017c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 1018c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1019c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1020c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 1021c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 1022c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 10233e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 10243e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 1025c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1026c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1027c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1028c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 10293e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10303e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 10313e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 10323e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 10333e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 10343e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 10353e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 1036c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 1037c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 1038c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 1039c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 10403e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10413e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 10423e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 10433e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1044c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1045c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1046e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 1047e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 10483e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1049e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1050e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1051e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1052e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 105344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 105444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1055c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 105644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 105744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 105844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 105944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1060c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1061c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 106244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 106344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10643e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 106544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 106644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 1067c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1068c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 106944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 107044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10713e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 1072c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 1073c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 1074e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 107544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1076e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 1077e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1078c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1079c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 108044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 108144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1082c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 108344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 108444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 1085c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1086c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 1087e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 10880c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 10890c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 10900c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 10910c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 10923e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10930c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 10940c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 10953e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10960c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1097c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 109845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10996143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 11001e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 11016143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 11021e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 11031e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 11041e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11051e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 11061e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 11071e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 11081e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1109dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 11101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 11111e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1112dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1113dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1114dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1115dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 11161e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 11171e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11181e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 11191e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 11201e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11216143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 11226143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 11236143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 11246143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 11256143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 11266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 11276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 11286143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1129eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1130eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1131eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1132eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1133eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1134eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1135eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1136eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::Constant *C = llvm::ConstantArray::get(Str); 1137eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C = new llvm::GlobalVariable(C->getType(), true, 1138eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 1139eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C, ".str", &getModule()); 1140eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return C; 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 124141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Forward declarations, no (immediate) code generation. 124241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 124341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategory: 124441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 124541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCInterface: 124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 124741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 124841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 124941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 125041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 125141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 125241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1253af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1254af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1255af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 125641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 125741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 125841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1259af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1260af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1261af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1262af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 126341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1264af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 126541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 126641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 126741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 126841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 126941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 127041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 127141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 127241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1273305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 127441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 127541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 127641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 127741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 127841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1279488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 128041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 128141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 128241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 128341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 128441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 128541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 128641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 128741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 128841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 128941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 129041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 129141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 129241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 129341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 129441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 129541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 129641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 129741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 129841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 129941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 130041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 130141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 130241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 130341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 130441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 130541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1306