CodeGenModule.cpp revision 90a904309c79977fcba2ff0542e2e4cd8e3c3faf
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(); 69f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar BindRuntimeFunctions(); 702b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarvoid CodeGenModule::BindRuntimeFunctions() { 73f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Deal with protecting runtime function names. 74f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { 75f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = RuntimeFunctions[i].first; 76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name = RuntimeFunctions[i].second; 77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 780293d540baafbe070c1035611787a81001a4118eDaniel Dunbar // Discard unused runtime functions. 790293d540baafbe070c1035611787a81001a4118eDaniel Dunbar if (Fn->use_empty()) { 800293d540baafbe070c1035611787a81001a4118eDaniel Dunbar Fn->eraseFromParent(); 810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar continue; 820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar } 830293d540baafbe070c1035611787a81001a4118eDaniel Dunbar 84f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // See if there is a conflict against a function. 85f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Conflict = TheModule.getFunction(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. 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (!Conflict->isDeclaration()) { 91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 92f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); 93f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->replaceAllUsesWith(Casted); 94f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->eraseFromParent(); 95f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->takeName(Conflict); 97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 98f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); 99f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->replaceAllUsesWith(Casted); 100f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->eraseFromParent(); 101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 102f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // FIXME: There still may be conflicts with aliases and 104f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // variables. 105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->setName(Name); 106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 108f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 109f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 110488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1112c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 11490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 116488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 11756b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1182c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1190a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1200a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1212c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12258c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 123488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 124c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 12590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 12690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 12790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 12890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 129488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 13056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 131c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1320a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 133c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 134c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 13541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 13641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 13741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 13841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1394f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1404f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1414f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1524f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1535f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// IdentifierInfo* containing the mangled name. Otherwise, returns 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// the name of the declaration as an identifier. 1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We 1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require 162b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// changes to the GlobalDeclMap, too. (I disagree, I think what we 163b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// actually need is for Sema to provide some notion of which Decls 164b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// refer to the same semantic decl. We shouldn't need to mangle the 165b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// names and see what comes out the same to figure this out. - DWD) 1665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start 1685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above 1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first. 1706ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 1716ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1726ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!mangleName(ND, Context, Out)) 1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return ND->getIdentifier()->getName(); 1755f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1766ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 1776ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor return MangledNames.GetOrCreateValue(Name.begin(), Name.end()) 1786ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor .getKeyData(); 1795f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1805f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1816d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1826d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 18449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1856bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1866d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1876d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 19149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 204572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 220572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 223572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2246d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2256d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2266d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 227532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 228532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 229532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 237532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 241532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2435c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 2445c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2455c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2465c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2475c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 24849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 249d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 250219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 251219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2522f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2532f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2542f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2553ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2562f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2572f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2582f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2592f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2602f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2612f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2622f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2633ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 264eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar } else if (D->getAttr<WeakAttr>()) 265eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar GV->setLinkage(llvm::Function::ExternalWeakLinkage); 266219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 267219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 268219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 269219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2702f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2712f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2722f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2732f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2742f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2752f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2762f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2772f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else if (D->getAttr<WeakAttr>() || IsInline) 278219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::WeakLinkage); 279219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2800dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 281d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 28249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 28349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2840dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 28541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 286d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 287a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 288eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 289eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // should not be munged. 290eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) 291a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 29217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 29317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 29417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 2955c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 2965c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 2975c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // might add multiple times or risk the value being replaced by a 2985c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // subsequent RAUW. 2995c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (ForDefinition) { 3005c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 3015c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 3025c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 303d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 304d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 305761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 30645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 307b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 308761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 30988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 310c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 311761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 312761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 313ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 314ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 31545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 316f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 317f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 318f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 319f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 320f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 321f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 322f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 323f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 324219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 325219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 326219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 327219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 328219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 329219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 330219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 331219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 332219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 333219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 33474ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 335f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 336af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 337af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 338af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 33981ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 34081ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson if (D->getAttr<NoinlineAttr>()) 34181ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 342f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 343f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 344f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 345f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 346541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 347f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 348219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 349f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 350f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 351f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 352f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 353541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 35445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 355219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 356219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 357219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 358219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 35945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 360219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 361219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 362219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *D = Aliases[i]; 363219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 364219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 365219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 366219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 367219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 368219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 369219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 370219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *aliasee = getModule().getFunction(aliaseeName); 374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 379219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 382219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 3846ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor getMangledName(D), aliasee, 3855f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3875f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 388219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 389219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 391219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 393219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 394219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 395219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 396ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 397219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 399219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 400219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 401219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 402219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 403ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 404ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 4050269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4060269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 4070269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 4080269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4090269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); 4100269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4110269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4120269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), 4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.size()); 4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 4210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ConstantArray::get(ATy, LLVMUsed), 4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 4240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 4290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Emit code for any deferred decl which was used. Since a 4300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // previously unused static decl may become used during the 4310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // generation of code for a static function, iterate until no 4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // changes are made. 4334c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 4344c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 4354c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 436b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4370269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(), 4380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar e = DeferredDecls.end(); i != e; ) { 439b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 440b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4416f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 4426f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 4436f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 444219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 44573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // need to check for uses in an alias. 4465f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!GlobalDeclMap.count(getMangledName(D))) { 447232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ++i; 448a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 449b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 450b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 451bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 452bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 453bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4544c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 4550269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar i = DeferredDecls.erase(i); 456b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4574c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 4584c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 4594c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4604c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4638bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4648bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4658bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4663c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4678bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4698bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 5028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 50773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5085c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5095c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 5105c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) 51173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 512bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 513bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 51473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 51573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) 51673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 51773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 51873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 51973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 52173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 52273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar assert(VD->isFileVarDecl() && "Invalid decl."); 52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 52573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 52973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 53073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 53273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 533219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Aliases are deferred until code for everything else has been 534219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // emitted. 535219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (FD->getAttr<AliasAttr>()) { 536219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar assert(!FD->isThisDeclarationADefinition() && 537219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar "Function alias cannot have a definition!"); 538219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Aliases.push_back(FD); 539219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return; 540219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 541219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 54273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 54373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 54473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 547bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 548bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 54973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 5507542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 55173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5524c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5534c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 55473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Defer code generation when possible. 55573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 5560269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar DeferredDecls.push_back(Global); 557bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 558bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 559bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 560bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 561bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 564bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 565bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 566bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 567bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 568bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 569bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 570bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 571bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 572bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 573bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5749986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 57577ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 57677ba708819285931932ecd33691a672bb59d221aEli Friedman 577bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 578bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 5799986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 580bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5813c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5825f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 58349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 58449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 58549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 58649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 5876ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), &getModule(), 5885f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, ASTTy.getAddressSpace()); 58949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 59049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 59149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 59249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 59349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 59449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 59549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 59649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 59749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 598eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar // FIXME: Merge with other attribute handling code. 599eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 60049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 60149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 602eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 603eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar if (D->getAttr<WeakAttr>()) 604eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 6053f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 6063f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 6073f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // Prefaced with special LLVM marker to indicate that the name 6083f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar // should not be munged. 6093f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar GV->setName("\01" + ALA->getLabel()); 6103f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar } 61149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 6123c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 6139986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 6149986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 615bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 616bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 617bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6188f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 61977ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 62077ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 62177ba708819285931932ecd33691a672bb59d221aEli Friedman 6228f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 623cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 624cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 625cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 626cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 627cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 628cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 629cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 630cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 631cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 632cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 633cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 634cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 63577ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 636bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 6376e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 638232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ErrorUnsupported(D, "static initializer"); 6396e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 6406e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 6416e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 6428f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 64377ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 6448e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 6455f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 6463c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 6473c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 64877ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 64977ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 65077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6516ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 6525f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 653232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 654232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar } else if (GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { 655232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // If we already have this global and it has an initializer, then 656232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // we are in the rare situation where we emitted the defining 657232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // declaration of the global and are now being asked to emit a 658232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // definition which would be common. This occurs, for example, in 659232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // the following situation because statics can be emitted out of 660232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // order: 661232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 662232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x; 663232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int *y = &x; 664232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // static int x = 10; 665232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // int **z = &y; 666232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // 667232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // Bail here so we don't blow away the definition. Note that if we 668232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // can't distinguish here if we emitted a definition with a null 669232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar // initializer, but this case is safe. 670232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar assert(!D->getInit() && "Emitting multiple definitions of a decl!"); 671232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar return; 672232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 6733c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 6743c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 67577ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 67677ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 67777ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 67877ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 67977ba708819285931932ecd33691a672bb59d221aEli Friedman // 68077ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 68177ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 68277ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 68377ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 684cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 685cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 686cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 687cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 68877ba708819285931932ecd33691a672bb59d221aEli Friedman 68977ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 69077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 69177ba708819285931932ecd33691a672bb59d221aEli Friedman 69277ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 69377ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 69477ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6956ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 0, getMangledName(D), 6965f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 69777ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 69877ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 69977ba708819285931932ecd33691a672bb59d221aEli Friedman 70077ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 70177ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 70277ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 70377ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 70477ba708819285931932ecd33691a672bb59d221aEli Friedman 70577ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 70677ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 70777ba708819285931932ecd33691a672bb59d221aEli Friedman } 70877ba708819285931932ecd33691a672bb59d221aEli Friedman 7093c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 7109e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 7118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 7128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 7138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 714f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 7158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 7168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 71788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 718b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 7190de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 72008d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 721ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 72241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 723ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 724a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 725a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 726a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 727a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 728a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 729a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 73088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 73188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7328fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7338fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 7348fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 735ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 736ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 737ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 7388fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<WeakAttr>()) 739ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakLinkage); 7408fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 741ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 742ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 743ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 7448fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 745ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 746ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 747ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 748ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 749ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 750a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 75198883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 75298883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 753ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 754ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 75549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 75649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 75749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 758ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 75949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 76049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 761ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 762ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 76388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 764686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 76517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 76617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 76717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 7685c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 7695c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7705c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 771686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 772686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 773686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 77466031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 775686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 776686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 77788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 779bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 780d5d31801fc87239436fa349c89dce7797cf13537Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D, 781d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::Type *Ty) { 782d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar if (!Ty) 783d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = getTypes().ConvertType(D->getType()); 784219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 785219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 7866ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor getMangledName(D), 7875f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 788219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributes(D, F); 789219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 790bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 791bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 792bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 7939986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 7949986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 7959986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 7963c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 7973c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 7985f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 7993c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 800d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, 0); 801bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 8029986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 803bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 804bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 805bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 806d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::FunctionType *Ty = 807d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 808d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 809d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // As a special case, make sure that definitions of K&R function 810d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // "type foo()" aren't declared as varargs (which forces the backend 811d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // to do unnecessary work). 812d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar if (Ty->isVarArg() && Ty->getNumParams() == 0 && Ty->isVarArg()) 813d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Ty = llvm::FunctionType::get(Ty->getReturnType(), 814d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar std::vector<const llvm::Type*>(), 815d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar false); 816d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 8175f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 8183c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 819d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar Entry = EmitForwardFunctionDefinition(D, Ty); 820bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 8213c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 8223c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 823d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // Otherwise, we have a definition after a prototype with the 824d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // wrong type. F is the Function* for the one with the wrong 825d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // type, we must make a new Function* and update everything that 826d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // used F (a declaration) with the new Function* (which will be 827d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // a definition). 828bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // 829d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // This happens if there is a prototype for a function 830d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // (e.g. "int f()") and then a definition of a different type 831d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // (e.g. "int f(int x)"). Start by making a new function of the 832d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // correct type, RAUW, then steal the name. 833d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D, Ty); 8343c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar NewFn->takeName(Entry); 835bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 836bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Replace uses of F with the Function we will endow with a body. 837bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *NewPtrForOldDecl = 8383c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 8393c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 8403c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 841bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Ok, delete the old function now, which is dead. 8423c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 843219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 844bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 845bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = NewFn; 846bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 847bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 848bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 849219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 850219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8516379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 852219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 853219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 854219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 855219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 856219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 857219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 858bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 859bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 860bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 861f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 862f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 863f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 864f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 865f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 866f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 867f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar RuntimeFunctions.push_back(std::make_pair(Fn, Name)); 868f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 869f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 870f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 871c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 872c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 873c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 874d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 875d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 876d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 877bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 878c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 8791426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 8801426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 881bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8821426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 8831426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 8841426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 885c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump llvm::Value *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 886bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 887bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 888bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8893e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 8903e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 8913e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 892bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 8943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 8953e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 8963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 897bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 898bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 899370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor Builtin::Context::GetBuiltinTypeError Error; 900370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); 901370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); 902370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 903bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 904bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 905bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 906bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 907bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 908bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 909bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 910bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 911bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 912bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 913c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 914c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 915c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 916c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 917c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 918bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9194667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner llvm::GlobalValue *&ExistingFn = 9204667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner GlobalDeclMap[getContext().Idents.get(Name).getName()]; 9214667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner if (ExistingFn) 9224667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = llvm::ConstantExpr::getBitCast(ExistingFn, Ty); 923c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump 924bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 9254667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner return FunctionSlot = ExistingFn = 9264c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 9274c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 928bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 929bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9307acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 9317acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 9327acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 9337acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 9347acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 935bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 9384e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9394e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 941c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9420c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 9430c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 9444e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9454e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 9460c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 9470c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 94841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 94941ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 9504e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9514e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 95241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 9537acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 954e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 955e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 95644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 95744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 958e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 959e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 960e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 961e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 962e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 96344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 964e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 965e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 96644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 967e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 968e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 96944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 970e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 971e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 972e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 973e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 974e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 975e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 976e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 977e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 978e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 979e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 980e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 9813e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 9823e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 983bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 984bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 985c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 986c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 987c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 988c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 989c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 990c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9913e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 9923e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 993c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 994c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 995c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 996c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 9973e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9983e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 9993e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 10003e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 10013e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 10023e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 10033e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 1004c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 1005c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 1006c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 1007c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 10083e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10093e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 10103e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 10113e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1012c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1013c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1014e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 1015e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 10163e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1017e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1018e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1019e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1020e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 102144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 102244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1023c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 102444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 102544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 102644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 102744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1028c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1029c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 103044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 103144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10323e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 103344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 103444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 1035c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1036c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 103744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 103844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 1040c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 1041c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 1042e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 104344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1044e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 1045e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1046c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1047c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 104844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 104944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1050c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 105144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 105244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 1053c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1054c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 1055e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 10560c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 10570c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 10580c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 10590c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 10603e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10610c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 10620c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 10633e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10640c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1065c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 106645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10676143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 10681e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 10696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 10701e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 10711e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 10721e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10731e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 10741e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 10751e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 10761e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1077dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 10781e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 10791e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1080dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1081dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1082dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1083dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 10841e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 10851e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10861e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 10871e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 10881e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 10906143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 10916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 10926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 10936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 10946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 10956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 10966143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1097eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1098eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1099eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1100eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1101eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1102eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1103eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1104eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::Constant *C = llvm::ConstantArray::get(Str); 1105eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C = new llvm::GlobalVariable(C->getType(), true, 1106eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 1107eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C, ".str", &getModule()); 1108eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return C; 1109eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1110eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1111eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1112a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 111345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 111445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 11155fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 11165fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 11176143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 11186143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 111945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 112045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 1121eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return new llvm::GlobalVariable(C->getType(), constant, 1122eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 1123eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C, GlobalName ? GlobalName : ".str", 1124eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner &CGM.getModule()); 112545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 112645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 11276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 11286143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 11296143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 11306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 11316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 11326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 11336143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 11346143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 11355fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 11365fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 113745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 113845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 11395fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 114045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 114145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 114245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 114345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 114445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1145eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 114645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 114745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 11485fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 114945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 115045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 115145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 11526143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 11536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 11546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 11556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 11565fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 11575fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1158c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 11596143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 116041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1161af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1162af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1163af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1164af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1165af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1166af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1167af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1168af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1169af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1170af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1171af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1172af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1173af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1174af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1175af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1176af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1177af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1178af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1179fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1180fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1181af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1182af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1183fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1184fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1185af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1186af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1187af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1188af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 118941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 119041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 119141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 119241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 119341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 119441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 119541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 119641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 119741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 119841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 119941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 120041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 120141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 120241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 120341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1204662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 120541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 120641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 120741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 120841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 120941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Forward declarations, no (immediate) code generation. 121041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 121141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategory: 121241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 121341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCInterface: 121441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 121541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 121641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 121741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 121841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 121941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 122041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1221af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1222af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1223af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 122441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 122541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 122641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1227af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1228af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1229af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1230af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 123141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1232af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 123341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 123441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 123541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 123641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 123741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 123841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 123941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 124041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1241305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 124241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 124341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 124441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 124541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1247488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 124841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 124941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 125041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 125141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 125241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 125341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 125441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 125541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 125641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 125741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 125841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 125941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 126041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 126141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 126241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 126341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 126441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 126541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 126641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 126741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 126841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 126941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 127041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 127141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 127241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 127341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1274