CodeGenModule.cpp revision 744016dde06fcffd50931e94a98c850f8b12cd87
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 2082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h" 2106057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h" 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 23687cc4a850b59116efee061018f0d8df50728b82Ken Dyck#include "clang/AST/CharUnits.h" 24c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2521ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 26af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor#include "clang/AST/DeclTemplate.h" 271a5e0d7f18485e4fb958f96dcddff3e4486a4069Anders Carlsson#include "clang/AST/RecordLayout.h" 281b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 292c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 32e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h" 33ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 34bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 36d5b8902c0329d80a216803b58dc3b689349a0c11Chris Lattner#include "llvm/LLVMContext.h" 376374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall#include "llvm/ADT/Triple.h" 3820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 396ba728d9687b2617793f5afd410650a8d6c71080Gabor Greif#include "llvm/Support/CallSite.h" 4078f7ece00e2ddfb64d4ed72a7be770b5b9b805e3Chris Lattner#include "llvm/Support/ErrorHandling.h" 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 452811ccf48d6d898c42cc4cfad37abedb36236d20Chandler CarruthCodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 46468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall llvm::Module &M, const llvm::TargetData &TD, 47468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall Diagnostic &diags) 48bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 492811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 50468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), 5182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()), 523a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis VTables(*this), Runtime(0), ABI(0), 536ae1f35cedb5a847f15f86bdb227b7bbe574a344John McCall CFConstantStringClassRef(0), 542bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian NSConstantStringClassRef(0), 55a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson VMContext(M.getContext()) { 56208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 573c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 583c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 593c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 603c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 613c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 623c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 633c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 643c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 65e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 663a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis if (!Features.CPlusPlus) 673a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis ABI = 0; 683a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis else createCXXABI(); 693a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 70e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 7120f12a20ba9cfa6f8d53c8304e24f50903c45184Anders Carlsson DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0; 722b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 732b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 742b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 75815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 763a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis delete ABI; 77815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 78815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 79815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 800d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallvoid CodeGenModule::createObjCRuntime() { 810d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall if (!Features.NeXTRuntime) 820d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateGNUObjCRuntime(*this); 830d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall else if (Features.ObjCNonFragileABI) 840d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateMacNonFragileABIObjCRuntime(*this); 850d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall else 860d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateMacObjCRuntime(*this); 870d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall} 880d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall 893a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisvoid CodeGenModule::createCXXABI() { 9098b7c5c496dfccb39287b8f7d8f1444594936d10Charles Davis if (Context.Target.getCXXABI() == "microsoft") 9198b7c5c496dfccb39287b8f7d8f1444594936d10Charles Davis ABI = CreateMicrosoftCXXABI(*this); 9298b7c5c496dfccb39287b8f7d8f1444594936d10Charles Davis else 9398b7c5c496dfccb39287b8f7d8f1444594936d10Charles Davis ABI = CreateItaniumCXXABI(*this); 943a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis} 953a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 96815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 9782227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 986c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman EmitCXXGlobalInitFunc(); 99efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar EmitCXXGlobalDtorFunc(); 100208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 101208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 102208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 1036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 1046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 105532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 1060269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 107744016dde06fcffd50931e94a98c850f8b12cd87John McCall 108744016dde06fcffd50931e94a98c850f8b12cd87John McCall if (getCodeGenOpts().EmitDeclMetadata) 109744016dde06fcffd50931e94a98c850f8b12cd87John McCall EmitDeclMetadata(); 110f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 111f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 1126374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool CodeGenModule::isTargetDarwin() const { 1136374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin; 1146374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall} 1156374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall 116488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1172c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 12090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 12190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 12356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1250a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1260a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1272c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12858c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 129488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 130c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 13190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 13290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 13390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 13490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 13656b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 137c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1380a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 139c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 140c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 1411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLangOptions::VisibilityMode 14204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const { 14304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 14404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (VD->getStorageClass() == VarDecl::PrivateExtern) 14504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 14604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 14740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 14804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (attr->getVisibility()) { 14904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar default: assert(0 && "Unknown visibility!"); 1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case VisibilityAttr::DefaultVisibility: 15104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Default; 15204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::HiddenVisibility: 15304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 15404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::ProtectedVisibility: 15504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Protected; 15604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar } 1577e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1587cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor 159af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (getLangOptions().CPlusPlus) { 160af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor // Entities subject to an explicit instantiation declaration get default 161af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor // visibility. 162af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 163af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (Function->getTemplateSpecializationKind() 164af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor == TSK_ExplicitInstantiationDeclaration) 165af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor return LangOptions::Default; 166af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor } else if (const ClassTemplateSpecializationDecl *ClassSpec 167af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 168af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (ClassSpec->getSpecializationKind() 169af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor == TSK_ExplicitInstantiationDeclaration) 170af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor return LangOptions::Default; 171af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor } else if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 172af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (Record->getTemplateSpecializationKind() 173af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor == TSK_ExplicitInstantiationDeclaration) 174af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor return LangOptions::Default; 175af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor } else if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 176af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (Var->isStaticDataMember() && 177af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor (Var->getTemplateSpecializationKind() 178af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor == TSK_ExplicitInstantiationDeclaration)) 179af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor return LangOptions::Default; 180af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor } 181af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor 182af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor // If -fvisibility-inlines-hidden was provided, then inline C++ member 183af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor // functions get "hidden" visibility by default. 184af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (getLangOptions().InlineVisibilityHidden) 185af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 186af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor if (Method->isInlined()) 187af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor return LangOptions::Hidden; 188af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor } 189af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor 19039de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson // This decl should have the same visibility as its parent. 19139de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson if (const DeclContext *DC = D->getDeclContext()) 19239de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson return getDeclVisibilityMode(cast<Decl>(DC)); 19339de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson 19404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return getLangOptions().getVisibilityMode(); 1954f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1964f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 19804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar const Decl *D) const { 19904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Internal definitions always have default visibility. 200df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner if (GV->hasLocalLinkage()) { 2017e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 2026ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar return; 2037e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 2046ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 20504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (getDeclVisibilityMode(D)) { 2067cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian default: assert(0 && "Unknown visibility!"); 20704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Default: 20804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 20904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Hidden: 21004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 21104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Protected: 21204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 2137cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian } 2147cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian} 2157cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian 216793a990774826a0c20b0da66cec0991badfb8b63Anders Carlssonllvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) { 217793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 218793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 219793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; 220793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson if (!Str.empty()) 221793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson return Str; 222793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 223793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson if (!getMangleContext().shouldMangleDeclName(ND)) { 224793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson IdentifierInfo *II = ND->getIdentifier(); 225793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson assert(II && "Attempt to mangle unnamed decl."); 226793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 227793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson Str = II->getName(); 228793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson return Str; 229793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson } 230793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 231793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson llvm::SmallString<256> Buffer; 232793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 233793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Buffer); 234793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 235793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer); 236793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) 237564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian getMangleContext().mangleBlock(GD, BD, Buffer); 238793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson else 239793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson getMangleContext().mangleName(ND, Buffer); 240793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 241793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson // Allocate space for the mangled name. 242793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson size_t Length = Buffer.size(); 243793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson char *Name = MangledNamesAllocator.Allocate<char>(Length); 244793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson std::copy(Buffer.begin(), Buffer.end(), Name); 245793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 246793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson Str = llvm::StringRef(Name, Length); 247793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 248793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson return Str; 249793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson} 250793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson 251564360be450b319aeafc26698be9811837bfb826Fariborz Jahanianvoid CodeGenModule::getMangledName(GlobalDecl GD, MangleBuffer &Buffer, 252564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian const BlockDecl *BD) { 253564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian getMangleContext().mangleBlock(GD, BD, Buffer.getBuffer()); 2549a8822bb154b792cdb18fe4cfb34480ca0ec7661Anders Carlsson} 2559a8822bb154b792cdb18fe4cfb34480ca0ec7661Anders Carlsson 256f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) { 257f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return getModule().getNamedValue(Name); 2585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 2595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 2606d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 2616d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 2626bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 26349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 2646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 2656d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2666d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 2676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 2686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 2696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 27049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 2716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 2726bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 2736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2746bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 2756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 2766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 2786bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 28096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2816bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2826bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType* CtorStructTy = 2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), 28596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(CtorFTy), NULL); 2866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2920032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson I->second, false)); 2933c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 29408e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 29896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2991c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(TheModule, AT, false, 300572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 3017db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(AT, Ctors), 3021c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson GlobalName); 3036d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 3046d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 3056d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 306532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 307532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 308532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 309532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 310532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 311532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 31296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 313532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 314532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *gv = 3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(TheModule, Array->getType(), false, 3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::AppendingLinkage, Array, 3181c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.global.annotations"); 319532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 320532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 321532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 32286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattnerstatic CodeGenModule::GVALinkage 3231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpGetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, 32468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor const LangOptions &Features) { 3250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; 3269aeed32282fe8a775c24c01c923717ca86695685John McCall 3270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor Linkage L = FD->getLinkage(); 3280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 3290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor FD->getType()->getLinkage() == UniqueExternalLinkage) 3300b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor L = UniqueExternalLinkage; 3317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 3320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (L) { 3330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case NoLinkage: 3340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case InternalLinkage: 3350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case UniqueExternalLinkage: 3360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor return CodeGenModule::GVA_Internal; 337548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson 3380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case ExternalLinkage: 3390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (FD->getTemplateSpecializationKind()) { 3400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_Undeclared: 3410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitSpecialization: 3420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor External = CodeGenModule::GVA_StrongExternal; 3430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 3440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 3450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitInstantiationDefinition: 3468f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor return CodeGenModule::GVA_ExplicitTemplateInstantiation; 3470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 3480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 3490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ImplicitInstantiation: 350548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson External = CodeGenModule::GVA_TemplateInstantiation; 3510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 3520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 353548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson } 3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3557ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor if (!FD->isInlined()) 3561fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 3570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 3581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { 3591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // GNU or C99 inline semantics. Determine whether this symbol should be 3601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // externally visible. 3611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (FD->isInlineDefinitionExternallyVisible()) 3621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return External; 363cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner 3641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // C99 inline semantics, where the symbol is not externally visible. 365b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor return CodeGenModule::GVA_C99Inline; 3661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor } 367b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor 3687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // C++0x [temp.explicit]p9: 3697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // [ Note: The intent is that an inline function that is the subject of 3707d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // an explicit instantiation declaration will still be implicitly 3717d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // instantiated when used so that the body can be considered for 3727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // inlining, but that no out-of-line copy of the inline function would be 3737d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // generated in the translation unit. -- end note ] 374f075b229643aa07389a6055352ade7446241c3caRafael Espindola if (FD->getTemplateSpecializationKind() 375f075b229643aa07389a6055352ade7446241c3caRafael Espindola == TSK_ExplicitInstantiationDeclaration) 3767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return CodeGenModule::GVA_C99Inline; 377b8cab18e8562994b87f8d7c06f8c122beb712677Rafael Espindola 3781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return CodeGenModule::GVA_CXXInline; 3797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 3807c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 381d46f98573ba104eda102dd3224b2dca69f1c6336John McCallllvm::GlobalValue::LinkageTypes 382d46f98573ba104eda102dd3224b2dca69f1c6336John McCallCodeGenModule::getFunctionLinkage(const FunctionDecl *D) { 38368584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); 3847c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 385f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (Linkage == GVA_Internal) 386d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::InternalLinkage; 387f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 388f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (D->hasAttr<DLLExportAttr>()) 389d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::DLLExportLinkage; 390f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 391f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (D->hasAttr<WeakAttr>()) 392d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::WeakAnyLinkage; 393f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 394f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // In C99 mode, 'inline' functions are guaranteed to have a strong 395f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // definition somewhere else, so we can use available_externally linkage. 396f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (Linkage == GVA_C99Inline) 397d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::AvailableExternallyLinkage; 398f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 399f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // In C++, the compiler has to emit a definition in every translation unit 400f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // that references the function. We should use linkonce_odr because 401f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // a) if all references in this translation unit are optimized away, we 402f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // don't need to codegen it. b) if the function persists, it needs to be 403f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // merged with other definitions. c) C++ has the ODR, so we know the 404f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // definition is dependable. 405f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 406d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::LinkOnceODRLinkage; 407f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 408f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // An explicit instantiation of a template has weak linkage, since 409f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // explicit instantiations can occur in multiple translation units 410f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // and must all be equivalent. However, we are not allowed to 411f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // throw away these explicit instantiations. 412f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner if (Linkage == GVA_ExplicitTemplateInstantiation) 4138f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor return llvm::Function::WeakODRLinkage; 414f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner 415f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner // Otherwise, we have strong external linkage. 416f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner assert(Linkage == GVA_StrongExternal); 417f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner return llvm::Function::ExternalLinkage; 418d46f98573ba104eda102dd3224b2dca69f1c6336John McCall} 419d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 420d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 421d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// SetFunctionDefinitionAttributes - Set attributes for a global. 422d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// 423d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// FIXME: This is currently only done for aliases and functions, but not for 424d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// variables (these details are set in EmitGlobalVarDefinition for variables). 425d46f98573ba104eda102dd3224b2dca69f1c6336John McCallvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 426d46f98573ba104eda102dd3224b2dca69f1c6336John McCall llvm::GlobalValue *GV) { 4277c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 428d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 429d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 4307dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const CGFunctionInfo &Info, 4327dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar llvm::Function *F) { 433ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned CallingConv; 434761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 435ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar ConstructAttributeList(Info, D, AttributeList, CallingConv); 436761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 437ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar AttributeList.size())); 438ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 439f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 440f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 4417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 4427c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::Function *F) { 44374ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 4441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->addFnAttr(llvm::Attribute::NoUnwind); 445af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 44640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<AlwaysInlineAttr>()) 447af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4491feade8e520be483293dbf55eb57a51720899589Mike Stump if (D->hasAttr<NoInlineAttr>()) 45081ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 451f55314dce992fd60816ba337ad151a2fb7c42239Mike Stump 452fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson if (Features.getStackProtectorMode() == LangOptions::SSPOn) 453fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtect); 454fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 455fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtectReq); 456fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson 457bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) { 458bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt unsigned width = Context.Target.getCharWidth(); 459bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(AA->getAlignment() / width); 460bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt while ((AA = AA->getNext<AlignedAttr>())) 461bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width)); 462bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 463fb51ddfafcd5f8536d0312b3daa3c0b74b90ab5bMike Stump // C++ ABI requires 2-byte alignment for member functions. 464bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 465bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump F->setAlignment(2); 466f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 467f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::SetCommonAttributes(const Decl *D, 4697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 4707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar setGlobalVisibility(GV, D); 4717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 47240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<UsedAttr>()) 4737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar AddUsedGlobal(GV); 4747c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 47540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 4767c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setSection(SA->getName()); 47782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov 47882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); 4797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 4807c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4810e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 4820e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar llvm::Function *F, 4830e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar const CGFunctionInfo &FI) { 4840e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributes(D, FI, F); 4850e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, F); 4867c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4877c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::InternalLinkage); 4887c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4890e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetCommonAttributes(D, F); 490f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 491f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 492b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonvoid CodeGenModule::SetFunctionAttributes(GlobalDecl GD, 493c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman llvm::Function *F, 494c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman bool IsIncompleteFunction) { 495b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 496b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 497c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman if (!IsIncompleteFunction) 498b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5007c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Only a few attributes are set on declarations; these may later be 5017c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // overridden by a definition. 5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 50340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (FD->hasAttr<DLLImportAttr>()) { 5047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::DLLImportLinkage); 5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (FD->hasAttr<WeakAttr>() || 50640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<WeakImportAttr>()) { 5077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // separate linkage types for this. 5097c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::ExternalWeakLinkage); 5107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->setLinkage(llvm::Function::ExternalLinkage); 5127c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 5137c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 51440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 5157c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setSection(SA->getName()); 516219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 517219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 5180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!GV->isDeclaration() && 5200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 52135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 5220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 5230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 5240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 5250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 526ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner if (LLVMUsed.empty()) 5270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 5280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 5293c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 53235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 53335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 53435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump UsedArray[i] = 5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 537a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson i8PTy); 53835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 540c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian if (UsedArray.empty()) 541c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian return; 54296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), ATy, false, 5460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 5477db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(ATy, UsedArray), 5481c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.used"); 5490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 5500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 5510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 5520269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 5530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 55467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 55567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 55667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 557bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola 558046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { 559046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson if (!DeferredVTables.empty()) { 560046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson const CXXRecordDecl *RD = DeferredVTables.back(); 561046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson DeferredVTables.pop_back(); 562046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson getVTables().GenerateClassData(getVTableLinkage(RD), RD); 563bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola continue; 564bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola } 565bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola 5662a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson GlobalDecl D = DeferredDeclsToEmit.back(); 56767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 56867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 569c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // Check to see if we've already emitted this. This is necessary 570c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // for a couple of reasons: first, decls can end up in the 571c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // deferred-decls queue multiple times, and second, decls can end 572c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // up with definitions in unusual ways (e.g. by an extern inline 573c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // function acquiring a strong function redefinition). Just 574c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // ignore these cases. 575c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // 576c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // TODO: That said, looking this up multiple times is very wasteful. 5779a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef Name = getMangledName(D); 578f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *CGRef = GetGlobalValue(Name); 57967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 58167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 58267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 584c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // GlobalAlias::isDeclaration() defers to the aliasee, but for our 585c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall // purposes an alias counts as a definition. 586c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall if (isa<llvm::GlobalAlias>(CGRef)) 587c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall continue; 588c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall 58967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 59067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 59167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 5958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// GlobalValue being annotated. The second field is the constant string 5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// created from the AnnotateAttr's annotation. The third field is a constant 5998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 6008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 6018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 6028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 6038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 6048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 6068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 6078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 6088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 6098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 6108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 6118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 6123c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 6140032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson AA->getAnnotation(), true); 6150032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 6160032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson M->getModuleIdentifier(), 6178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 6188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 6198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 6208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *annoGV = 62295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, anno->getType(), false, 62395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, anno, 62495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GV->getName()); 6258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 6268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 62795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, unit->getType(), false, 6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::PrivateLinkage, unit, 62995b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ".str"); 6308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 63157d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar // Create the ConstantStruct for the global annotation. 6328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 6333c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, SBP), 6343c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(annoGV, SBP), 6353c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(unitGV, SBP), 6360032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 6378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 63847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 6398bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 6408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 64161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedmanstatic CodeGenModule::GVALinkage 64261c6c91930de9c0659027f019062b4a5636c1c2aEli FriedmanGetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { 64361c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman // If this is a static data member, compute the kind of template 64461c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman // specialization. Otherwise, this variable is not part of a 64561c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman // template. 64661c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman TemplateSpecializationKind TSK = TSK_Undeclared; 64761c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman if (VD->isStaticDataMember()) 64861c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman TSK = VD->getTemplateSpecializationKind(); 64961c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 65061c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman Linkage L = VD->getLinkage(); 65161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 65261c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman VD->getType()->getLinkage() == UniqueExternalLinkage) 65361c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman L = UniqueExternalLinkage; 65461c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 65561c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman switch (L) { 65661c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case NoLinkage: 65761c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case InternalLinkage: 65861c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case UniqueExternalLinkage: 65961c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return CodeGenModule::GVA_Internal; 66061c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 66161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case ExternalLinkage: 66261c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman switch (TSK) { 66361c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case TSK_Undeclared: 66461c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case TSK_ExplicitSpecialization: 66561c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return CodeGenModule::GVA_StrongExternal; 66661c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 66761c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case TSK_ExplicitInstantiationDeclaration: 66861c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman llvm_unreachable("Variable should not be instantiated"); 66961c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman // Fall through to treat this like any other instantiation. 67061c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 67161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case TSK_ExplicitInstantiationDefinition: 67261c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return CodeGenModule::GVA_ExplicitTemplateInstantiation; 67361c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 67461c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman case TSK_ImplicitInstantiation: 67561c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return CodeGenModule::GVA_TemplateInstantiation; 67661c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman } 67761c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman } 67861c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 67961c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return CodeGenModule::GVA_StrongExternal; 68061c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman} 68161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman 68273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 6835c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 6845c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 68540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 68673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 687bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 688bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 68973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FD->hasAttr<ConstructorAttr>() || 69140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<DestructorAttr>()) 69273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 69373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 69461eab8872168af6eb1e0047a82901096cf145e27Eli Friedman // The key function for a class must never be deferred. 69561eab8872168af6eb1e0047a82901096cf145e27Eli Friedman if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Global)) { 69661eab8872168af6eb1e0047a82901096cf145e27Eli Friedman const CXXRecordDecl *RD = MD->getParent(); 69761eab8872168af6eb1e0047a82901096cf145e27Eli Friedman if (MD->isOutOfLine() && RD->isDynamicClass()) { 69861eab8872168af6eb1e0047a82901096cf145e27Eli Friedman const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD); 699bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor if (KeyFunction && 700bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) 70161eab8872168af6eb1e0047a82901096cf145e27Eli Friedman return false; 70261eab8872168af6eb1e0047a82901096cf145e27Eli Friedman } 70361eab8872168af6eb1e0047a82901096cf145e27Eli Friedman } 70461eab8872168af6eb1e0047a82901096cf145e27Eli Friedman 70568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); 7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 707dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner // static, static inline, always_inline, and extern inline functions can 70886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // always be deferred. Normal inline functions can be deferred in C99/C++. 7098f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor // Implicit template instantiations can also be deferred in C++. 710cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || 711ab189950908fc08dd2533692f0e8253e807c73acEli Friedman Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 712dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return true; 713dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return false; 71473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 716dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner const VarDecl *VD = cast<VarDecl>(Global); 717dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 718b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 71974d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // We never want to defer structs that have non-trivial constructors or 72074d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // destructors. 72174d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 72274d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // FIXME: Handle references. 72374d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { 72474d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 72574d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) 72674d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson return false; 72774d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 72874d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 72974d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 73061c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman GVALinkage L = GetLinkageForVariable(getContext(), VD); 73161c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman if (L == GVA_Internal || L == GVA_TemplateInstantiation) { 73261c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman if (!(VD->getInit() && VD->getInit()->HasSideEffects(Context))) 73361c6c91930de9c0659027f019062b4a5636c1c2aEli Friedman return true; 734393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian } 7350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 736393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return false; 73773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 73873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 7396a836706c40a31c716952b74785102c90fd6afa7Rafael Espindolallvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { 7406a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola const AliasAttr *AA = VD->getAttr<AliasAttr>(); 7416a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola assert(AA && "No alias?"); 7426a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 7436a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); 7446a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 7456a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola // See if there is already something with the target's name in the module. 746f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); 7476a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 7486a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola llvm::Constant *Aliasee; 7496a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (isa<llvm::FunctionType>(DeclTy)) 750f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 7516a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola else 752f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 7536a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola llvm::PointerType::getUnqual(DeclTy), 0); 7546a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (!Entry) { 7556a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); 7566a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola F->setLinkage(llvm::Function::ExternalWeakLinkage); 7576a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola WeakRefReferences.insert(F); 7586a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola } 7596a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 7606a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola return Aliasee; 7616a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola} 7626a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 763b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobal(GlobalDecl GD) { 7644a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7666a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola // Weak references don't produce any output by themselves. 7676a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (Global->hasAttr<WeakRefAttr>()) 7686a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola return; 7696a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 770bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 771bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 77240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Global->hasAttr<AliasAttr>()) 773f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return EmitAliasDefinition(GD); 774219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 77567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 7765e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 77773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 77873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 77973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 7800269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 7810269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 782bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 783bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 784a9a55c063e9e59c6ab0a6d7a21302660f7bde9f9Douglas Gregor if (VD->isThisDeclarationADefinition() != VarDecl::Definition) 78573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 7864c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 7874c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 78867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 78967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 7904357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner if (!MayDeferGeneration(Global)) { 7914357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // Emit the definition if it can't be deferred. 7924357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner EmitGlobalDefinition(GD); 793bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 794bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 7954357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner 7964357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // If the value has already been used, add it directly to the 7974357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // DeferredDeclsToEmit list. 7989a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef MangledName = getMangledName(GD); 7994357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner if (GetGlobalValue(MangledName)) 8004357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner DeferredDeclsToEmit.push_back(GD); 8014357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner else { 8024357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 8034357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // first use of the mangled name will cause it to move into 8044357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner // DeferredDeclsToEmit. 8054357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner DeferredDecls[MangledName] = GD; 8064357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner } 8074c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 8084c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 809b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 8104a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 812cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), 8138e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson Context.getSourceManager(), 8148e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson "Generating code for declaration"); 8158e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson 8166fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 8176fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor if (Method->isVirtual()) 8186fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor getVTables().EmitThunks(GD); 8197270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson 8202a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 8214357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner return EmitCXXConstructor(CD, GD.getCtorType()); 8224357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner 8234357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 8244357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner return EmitCXXDestructor(DD, GD.getDtorType()); 825b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner 826b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner if (isa<FunctionDecl>(D)) 827b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner return EmitGlobalFunctionDefinition(GD); 828b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner 829b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 830b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner return EmitGlobalVarDefinition(VD); 8314357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner 8324357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner assert(0 && "Invalid argument to EmitGlobalDefinition()"); 833bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 834bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 83574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 83674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 83774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 83874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 83974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 84074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 84174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 842f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::Constant * 843f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallCodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName, 844f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall const llvm::Type *Ty, 845f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall GlobalDecl D) { 8460558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 847f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 8480558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 8496a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (WeakRefReferences.count(Entry)) { 8506a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); 8516a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (FD && !FD->hasAttr<WeakAttr>()) 8527270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson Entry->setLinkage(llvm::Function::ExternalLinkage); 8536a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 8546a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola WeakRefReferences.erase(Entry); 8556a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola } 8566a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 8570558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 8580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8600558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 86196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 8623c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, PTy); 8630558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 865654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // This function doesn't have a complete type (for example, the return 866654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // type is an incomplete struct). Use a fake type instead, and make 867654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // sure not to try to set attributes. 868654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman bool IsIncompleteFunction = false; 869784f21121a6c9418ebd86baa6814e36e1176c410John McCall 870784f21121a6c9418ebd86baa6814e36e1176c410John McCall const llvm::FunctionType *FTy; 871784f21121a6c9418ebd86baa6814e36e1176c410John McCall if (isa<llvm::FunctionType>(Ty)) { 872784f21121a6c9418ebd86baa6814e36e1176c410John McCall FTy = cast<llvm::FunctionType>(Ty); 873784f21121a6c9418ebd86baa6814e36e1176c410John McCall } else { 874784f21121a6c9418ebd86baa6814e36e1176c410John McCall FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 875784f21121a6c9418ebd86baa6814e36e1176c410John McCall std::vector<const llvm::Type*>(), false); 876654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman IsIncompleteFunction = true; 877654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman } 878bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner 879784f21121a6c9418ebd86baa6814e36e1176c410John McCall llvm::Function *F = llvm::Function::Create(FTy, 880654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman llvm::Function::ExternalLinkage, 881f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall MangledName, &getModule()); 882f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall assert(F->getName() == MangledName && "name was uniqued!"); 883654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman if (D.getDecl()) 884b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson SetFunctionAttributes(D, F, IsIncompleteFunction); 885654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman 88667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 88767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 88867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 889f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 89067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 89167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 89267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 89367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 89467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 895b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { 8960c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // If this the first reference to a C++ inline function in a class, queue up 8970c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // the deferred function body for emission. These are not seen as 8980c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // top-level declarations. 899b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) 900b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner DeferredDeclsToEmit.push_back(D); 901c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // A called constructor which has no definition or declaration need be 902c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // synthesized. 903c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 9047b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola if (CD->isImplicit()) { 905d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer assert(CD->isUsed() && "Sema doesn't consider constructor as used."); 90615233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman DeferredDeclsToEmit.push_back(D); 9077b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola } 90815233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { 9097b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola if (DD->isImplicit()) { 910d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer assert(DD->isUsed() && "Sema doesn't consider destructor as used."); 91115233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman DeferredDeclsToEmit.push_back(D); 9127b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola } 91315233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 9147b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola if (MD->isCopyAssignment() && MD->isImplicit()) { 915d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer assert(MD->isUsed() && "Sema doesn't consider CopyAssignment as used."); 916c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian DeferredDeclsToEmit.push_back(D); 9177b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola } 918c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian } 91967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 921784f21121a6c9418ebd86baa6814e36e1176c410John McCall // Make sure the result is of the requested type. 922784f21121a6c9418ebd86baa6814e36e1176c410John McCall if (!IsIncompleteFunction) { 923784f21121a6c9418ebd86baa6814e36e1176c410John McCall assert(F->getType()->getElementType() == Ty); 924784f21121a6c9418ebd86baa6814e36e1176c410John McCall return F; 925784f21121a6c9418ebd86baa6814e36e1176c410John McCall } 926784f21121a6c9418ebd86baa6814e36e1176c410John McCall 927784f21121a6c9418ebd86baa6814e36e1176c410John McCall const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 928784f21121a6c9418ebd86baa6814e36e1176c410John McCall return llvm::ConstantExpr::getBitCast(F, PTy); 9290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 9300558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 93174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 93274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 93374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 934b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 93574391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 93674391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 93774391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 9384a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 939bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner 9409a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef MangledName = getMangledName(GD); 941f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return GetOrCreateLLVMFunction(MangledName, Ty, GD); 94274391b48b4791cded373683a3baf67314f358d50Chris Lattner} 94377ba708819285931932ecd33691a672bb59d221aEli Friedman 94474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 94574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 94674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 94774391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 948f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::StringRef Name) { 949b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 95074391b48b4791cded373683a3baf67314f358d50Chris Lattner} 951bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 95220e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedmanstatic bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { 95388786862424cb2d478516d911709dc19c962af9cJohn McCall if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) 95420e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return false; 95520e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman if (Context.getLangOptions().CPlusPlus && 95620e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman Context.getBaseElementType(D->getType())->getAs<RecordType>()) { 95720e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman // FIXME: We should do something fancier here! 95820e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return false; 95920e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman } 96020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return true; 96120e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman} 96220e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman 96374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 96474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 96574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 96674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 96774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 96874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 96974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 970f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::Constant * 971f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallCodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, 972f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall const llvm::PointerType *Ty, 973f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall const VarDecl *D) { 9743c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 975f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 97699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 9776a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (WeakRefReferences.count(Entry)) { 9786a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola if (D && !D->hasAttr<WeakAttr>()) 9797270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson Entry->setLinkage(llvm::Function::ExternalLinkage); 9806a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 9816a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola WeakRefReferences.erase(Entry); 9826a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola } 9836a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola 98474391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 985570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 98799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 9883c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, Ty); 98999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 99167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 99267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 99367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 994f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 99567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 99667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 99767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 99867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 99967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 100067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 100499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 1005f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall 0, MangledName, 0, 100656ebe5082da7411fb37479e230b52735f77cff35Eli Friedman false, Ty->getAddressSpace()); 100749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 100899b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 100974391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 1010f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This code is overly simple and should be merged with other global 1011f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // handling. 101220e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman GV->setConstant(DeclIsConstantGlobal(Context, D)); 101349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 101474391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 101574391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 101604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 101749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (D->hasAttr<WeakAttr>() || 101940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) 102074391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 102156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman 102256ebe5082da7411fb37479e230b52735f77cff35Eli Friedman GV->setThreadLocal(D->isThreadSpecified()); 102374391b48b4791cded373683a3baf67314f358d50Chris Lattner } 10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1025f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return GV; 102674391b48b4791cded373683a3baf67314f358d50Chris Lattner} 1027eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 1028eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 102974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 103074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 103174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 103274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 103374391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 103474391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 103574391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 103674391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 103774391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 103874391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::PointerType *PTy = 104196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 1042f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall 10439a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef MangledName = getMangledName(D); 1044f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return GetOrCreateLLVMGlobal(MangledName, PTy, D); 104574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 10463f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 104774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 104874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 104974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 105074391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 1051f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::StringRef Name) { 105296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 1053bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 1054bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 105503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 105603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!D->getInit() && "Cannot emit definite definitions here!"); 105703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 10587520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (MayDeferGeneration(D)) { 10597520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // If we have not seen a reference to this variable yet, place it 10607520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // into the deferred declarations table to be emitted if needed 10617520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // later. 10629a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef MangledName = getMangledName(D); 1063f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall if (!GetGlobalValue(MangledName)) { 1064555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson DeferredDecls[MangledName] = D; 106503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar return; 10667520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 10677520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 10687520bd1de12af10ea08c662440565adbdf589317Douglas Gregor 10697520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // The tentative definition is the only definition. 107003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar EmitGlobalVarDefinition(D); 107103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar} 107203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 10736fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregorvoid CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { 10746fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor if (DefinitionRequired) 10756fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor getVTables().GenerateClassData(getVTableLinkage(Class), Class); 10766fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor} 10776fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor 10784b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregorllvm::GlobalVariable::LinkageTypes 1079046c294a43024874ff35656c6e785b64e72f1f36Anders CarlssonCodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 1080dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) 1081dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor return llvm::GlobalVariable::InternalLinkage; 1082dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 1083dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor if (const CXXMethodDecl *KeyFunction 1084dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor = RD->getASTContext().getKeyFunction(RD)) { 1085dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // If this class has a key function, use that to determine the linkage of 1086dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // the vtable. 10874b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor const FunctionDecl *Def = 0; 10884b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor if (KeyFunction->getBody(Def)) 10894b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor KeyFunction = cast<CXXMethodDecl>(Def); 1090dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 10914b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor switch (KeyFunction->getTemplateSpecializationKind()) { 10924b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_Undeclared: 10934b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitSpecialization: 10944b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor if (KeyFunction->isInlined()) 10954b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 10964b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 10974b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::ExternalLinkage; 10984b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 10994b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ImplicitInstantiation: 11004b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitInstantiationDefinition: 11014b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 11024b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 11034b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 11044b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // FIXME: Use available_externally linkage. However, this currently 11054b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // breaks LLVM's build due to undefined symbols. 11064b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // return llvm::GlobalVariable::AvailableExternallyLinkage; 11074b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 11084b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor } 1109dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor } 1110dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 1111dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor switch (RD->getTemplateSpecializationKind()) { 1112dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_Undeclared: 1113dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitSpecialization: 1114dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ImplicitInstantiation: 1115dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitInstantiationDefinition: 1116dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 1117dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 1118dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitInstantiationDeclaration: 1119dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // FIXME: Use available_externally linkage. However, this currently 1120dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // breaks LLVM's build due to undefined symbols. 1121dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // return llvm::GlobalVariable::AvailableExternallyLinkage; 11224b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 11234b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor } 11244b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 11254b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // Silence GCC warning. 11264b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 11274b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor} 11284b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 1129687cc4a850b59116efee061018f0d8df50728b82Ken DyckCharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const { 1130687cc4a850b59116efee061018f0d8df50728b82Ken Dyck return CharUnits::fromQuantity( 1131687cc4a850b59116efee061018f0d8df50728b82Ken Dyck TheTargetData.getTypeStoreSizeInBits(Ty) / Context.getCharWidth()); 1132687cc4a850b59116efee061018f0d8df50728b82Ken Dyck} 1133687cc4a850b59116efee061018f0d8df50728b82Ken Dyck 1134bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 11358f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 113677ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 11376c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman bool NonConstInit = false; 11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl const Expr *InitExpr = D->getAnyInitializer(); 11403bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson 11413bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson if (!InitExpr) { 1142cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 114303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // implicitly initialized with { 0 }. 114403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // 114503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // Note that tentative definitions are only emitted at the end of 114603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // a translation unit, so they should never have incomplete 114703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // type. In addition, EmitTentativeDefinition makes sure that we 114803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // never attempt to emit a tentative definition if a real one 114903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // exists. A use may still exists, however, so we still may need 115003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // to do a RAUW. 115103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1152b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson Init = EmitNullConstant(D->getType()); 115377ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 1154c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor Init = EmitConstantExpr(InitExpr, D->getType()); 11556e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 11563bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson QualType T = InitExpr->getType(); 1157c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor if (D->getType()->isReferenceType()) 1158c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor T = D->getType(); 1159c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor 116089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson if (getLangOptions().CPlusPlus) { 11616c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman EmitCXXGlobalVarDeclInitFunc(D); 116289ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = EmitNullConstant(T); 11636c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman NonConstInit = true; 116489ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } else { 116589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson ErrorUnsupported(D, "static initializer"); 116689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 116789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } 11686e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 11698f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 11708e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 11712d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 1172570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1174570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 1175570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 11769d4a15fd3b85434c43ea27562793de63a793321aChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast || 11779d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // all zero index gep. 11789d4a15fd3b85434c43ea27562793de63a793321aChris Lattner CE->getOpcode() == llvm::Instruction::GetElementPtr); 1179570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 1180570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1182570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 1183570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1185570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 1186570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 1187570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 1188570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 1189570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 1190570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 1191570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 1192570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 1193570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 1194570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 1195570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 1196570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1198f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall // Move the old entry aside so that we'll create a new one. 1199f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Entry->setName(llvm::StringRef()); 1200232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 1201570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 1202570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 12030558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 120477ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 12063c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1207570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 120877ba708819285931932ecd33691a672bb59d221aEli Friedman 120977ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 1210570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 121177ba708819285931932ecd33691a672bb59d221aEli Friedman } 121277ba708819285931932ecd33691a672bb59d221aEli Friedman 121340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 12148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 12158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 1216f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 12178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 12188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 121988a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 1220e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner 1221e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // If it is safe to mark the global 'constant', do so now. 1222e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 12236c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman if (!NonConstInit && DeclIsConstantGlobal(Context, D)) 1224e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(true); 12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12268b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); 122708d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 122888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 12291028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GVALinkage Linkage = GetLinkageForVariable(getContext(), D); 1230a0f00a71fcb0f98298709d5e5318339acf7958acDouglas Gregor if (Linkage == GVA_Internal) 12318fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 123240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLImportAttr>()) 1233ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 123440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLExportAttr>()) 1235ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 1236e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else if (D->hasAttr<WeakAttr>()) { 1237e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (GV->isConstant()) 1238e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); 1239e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else 1240e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 12418f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor } else if (Linkage == GVA_TemplateInstantiation || 12428f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor Linkage == GVA_ExplicitTemplateInstantiation) 12438f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor // FIXME: It seems like we can provide more specific linkage here 12448f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor // (LinkOnceODR, WeakODR). 12451028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 12461bc5c03a7f08b04f4ac26abd705418aaf4109062Daniel Dunbar else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon && 1247309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner !D->hasExternalStorage() && !D->getInit() && 1248e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner !D->getAttr<SectionAttr>()) { 124904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 1250e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // common vars aren't constant even if declared const. 1251e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 1252e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } else 125304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 12547e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar 12557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 125604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 1257686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 12582d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 125966031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 1260686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 1261686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 126288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1264bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1265bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// implement a function with no prototype, e.g. "int foo() {}". If there are 1266bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// existing call uses of the old function in the module, this adjusts them to 1267bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// call the new function directly. 1268bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// 1269bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// This is not just a cleanup: the always_inline pass requires direct calls to 1270bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// functions to be able to inline them. If there is a bitcast in the way, it 1271bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// won't inline them. Instcombine normally deletes these calls, but it isn't 1272bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// run at -O0. 1273bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1274bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *NewFn) { 1275bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If we're redefining a global as a function, don't transform it. 1276bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1277bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (OldFn == 0) return; 12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1279bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner const llvm::Type *NewRetTy = NewFn->getReturnType(); 1280bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::SmallVector<llvm::Value*, 4> ArgList; 1281bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1282bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1283dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer UI != E; ) { 1284bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // TODO: Do invokes ever occur in C code? If so, we should handle them too. 1285dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. 1286dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); 128735db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif llvm::CallSite CS(CI); 1288dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer if (!CI || !CS.isCallee(I)) continue; 12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1290bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the return types don't match exactly, and if the call isn't dead, then 1291bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // we can't transform this call. 1292bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getType() != NewRetTy && !CI->use_empty()) 1293bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 1294bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1295bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the function was passed too few arguments, don't transform. If extra 1296bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // arguments were passed, we silently drop them. If any of the types 1297bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // mismatch, we don't transform. 1298bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner unsigned ArgNo = 0; 1299bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner bool DontTransform = false; 1300bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1301bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 130235db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif if (CS.arg_size() == ArgNo || 130335db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif CS.getArgument(ArgNo)->getType() != AI->getType()) { 1304bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner DontTransform = true; 1305bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner break; 1306bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1307bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1308bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (DontTransform) 1309bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1311bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Okay, we can transform this. Create the new call instruction and copy 1312bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // over the required information. 13136ba728d9687b2617793f5afd410650a8d6c71080Gabor Greif ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); 1314bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1315bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.end(), "", CI); 1316bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.clear(); 1317ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer if (!NewCall->getType()->isVoidTy()) 1318bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->takeName(CI); 1319bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->setAttributes(CI->getAttributes()); 1320ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar NewCall->setCallingConv(CI->getCallingConv()); 1321bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1322bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Finally, remove the old call, replacing any uses with the new one. 132300549fcec0490b2daf27543e532f94adbb186063Chris Lattner if (!CI->use_empty()) 132400549fcec0490b2daf27543e532f94adbb186063Chris Lattner CI->replaceAllUsesWith(NewCall); 13253b122bc5f1203615e2128e0c1a63da438865b1ccDevang Patel 1326c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner // Copy debug location attached to CI. 1327c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner if (!CI->getDebugLoc().isUnknown()) 1328c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner NewCall->setDebugLoc(CI->getDebugLoc()); 1329bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->eraseFromParent(); 1330bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1331bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner} 1332bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1333bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1334b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 1335b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 1336c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD); 13374819ac44c0e5222ebfe8e199c51f653f52177336Fariborz Jahanian getMangleContext().mangleInitDiscriminator(); 13389fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner // Get or create the prototype for the function. 1339b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13410558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 13420558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 13430558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 13440558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 13450558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13480558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1349bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 135142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 1352bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner assert(OldFn->isDeclaration() && 13530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 135434809507232bc4c3c4840c7d092c7440219fddafChris Lattner 135562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 135662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 135762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 135862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 135962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 136062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 1361f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall // (e.g. "int f(int x)"). Move the old function aside so that it 1362f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall // doesn't interfere with GetAddrOfFunction. 1363f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall OldFn->setName(llvm::StringRef()); 1364b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1366bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If this is an implementation of a function without a prototype, try to 1367bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // replace any existing uses of the function (which may be calls) with uses 1368bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // of the new function 13699fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner if (D->getType()->isFunctionNoProtoType()) { 1370bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 13719fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner OldFn->removeDeadConstantUsers(); 13729fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner } 13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 137462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 1375bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (!Entry->use_empty()) { 13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 13773c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1378bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 1379bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 138162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 1382bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner OldFn->eraseFromParent(); 13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13840558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 1385bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13870558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 13888b2423361648c39a7d8a3c5e8129e12006deac32John McCall setFunctionLinkage(D, Fn); 1389bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 13916379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 13927c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetFunctionDefinitionAttributes(D, Fn); 13937c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, Fn); 13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 139540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 139740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 1399bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 1400bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1401f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallvoid CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 1402f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 140340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis const AliasAttr *AA = D->getAttr<AliasAttr>(); 1404bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 1405bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 14069a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson llvm::StringRef MangledName = getMangledName(GD); 1407f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall 1408f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall // If there is a definition in the module, then it wins over the alias. 1409f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall // This is dubious, but allow it to be safe. Just ignore the alias. 1410f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 1411f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall if (Entry && !Entry->isDeclaration()) 1412f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall return; 14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1414f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 1415bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1416bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 1417bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 1418bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 1419bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 1420f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 1421bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 1422f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 142396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(DeclTy), 0); 1424bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1425bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *GA = 1427bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 1428bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 1429bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1431bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 1432f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall assert(Entry->isDeclaration()); 1433f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall 1434bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 1435bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 1436bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 1437bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 1438bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 1439bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 1440bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 1441f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall GA->takeName(Entry); 14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14433c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1444bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 1445bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 1446f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall } else { 14479a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson GA->setName(MangledName); 1448bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14507c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Set attributes which are particular to an alias; this is a 14517c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // specialization of the attributes which may be set on a global 14527c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // variable/function. 145340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<DLLExportAttr>()) { 14547c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 14557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // The dllexport attribute is ignored for undefined symbols. 14566fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (FD->getBody()) 14577c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 14587c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 14597c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 14607c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (D->hasAttr<WeakAttr>() || 146211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola D->hasAttr<WeakRefAttr>() || 146340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) { 14647c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::WeakAnyLinkage); 14657c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 14667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 14677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GA); 1468bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 1469bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1470b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 1471b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 147234771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbarllvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 147334771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar unsigned BuiltinID) { 14743e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 14763e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14783e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 14793e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 14803e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 14813e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::FunctionType *Ty = 1484386ca78d0b03b1fb519e60d1a14cd12a220364a6Eli Friedman cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); 1485bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 148634771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); 1487bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 1488bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 14897acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 14907acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 14917acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 14927acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 14937acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 1494bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 14953ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang 14963ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemCpyFn(const llvm::Type *DestType, 14973ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *SrcType, 14983ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *SizeType) { 14993ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType }; 15003ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang return getIntrinsic(llvm::Intrinsic::memcpy, ArgTypes, 3); 15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1502c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 15033ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemMoveFn(const llvm::Type *DestType, 15043ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *SrcType, 15053ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *SizeType) { 15063ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType }; 15073ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang return getIntrinsic(llvm::Intrinsic::memmove, ArgTypes, 3); 15080c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 15090c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 15103ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemSetFn(const llvm::Type *DestType, 15113ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *SizeType) { 15123ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang const llvm::Type *ArgTypes[2] = { DestType, SizeType }; 15133ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang return getIntrinsic(llvm::Intrinsic::memset, ArgTypes, 2); 151441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 15157acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 15161d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarstatic llvm::StringMapEntry<llvm::Constant*> & 15171d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarGetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 15181d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const StringLiteral *Literal, 151970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar bool TargetIsLSB, 15201d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool &IsUTF16, 15211d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned &StringLength) { 1522df4eee9b035cc4b95b1ea72635e7429d06b0ecc8Daniel Dunbar unsigned NumBytes = Literal->getByteLength(); 15231d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 1524f015b034159d40e7033309e50036804eb1971787Daniel Dunbar // Check for simple case. 1525f015b034159d40e7033309e50036804eb1971787Daniel Dunbar if (!Literal->containsNonAsciiOrNull()) { 1526f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1527f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1528f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1529f015b034159d40e7033309e50036804eb1971787Daniel Dunbar } 1530f015b034159d40e7033309e50036804eb1971787Daniel Dunbar 15311d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Otherwise, convert the UTF8 literals into a byte string. 15321d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 15331d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 15341d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar UTF16 *ToPtr = &ToBuf[0]; 15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 15371d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar &ToPtr, ToPtr + NumBytes, 15381d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar strictConversion); 15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15401d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Check for conversion failure. 15411d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (Result != conversionOK) { 15421d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove 15431d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // this duplicate code. 15441d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); 1545f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1546f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1547f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1548e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff } 15491d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 155070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // ConvertUTF8toUTF16 returns the length in ToPtr. 15511d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = ToPtr - &ToBuf[0]; 155270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 155370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // Render the UTF-16 string into a byte array and convert to the target byte 155470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // order. 155570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // 155670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // FIXME: This isn't something we should need to do here. 155770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar llvm::SmallString<128> AsBytes; 155870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.reserve(StringLength * 2); 155970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar for (unsigned i = 0; i != StringLength; ++i) { 156070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar unsigned short Val = ToBuf[i]; 156170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar if (TargetIsLSB) { 156270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 156370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 156470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } else { 156570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 156670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 156770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 156870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 1569434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // Append one extra null character, the second is automatically added by our 1570434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // caller. 1571434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar AsBytes.push_back(0); 157270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 15731d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar IsUTF16 = true; 157470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 15751d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar} 15761d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 15771d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarllvm::Constant * 15781d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarCodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 15791d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned StringLength = 0; 15801d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool isUTF16 = false; 15811d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::StringMapEntry<llvm::Constant*> &Entry = 15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GetConstantCFStringEntry(CFConstantStringMap, Literal, 158370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar getTargetData().isLittleEndian(), 158470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar isUTF16, StringLength); 15851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15861d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (llvm::Constant *C = Entry.getValue()) 15871d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return C; 15881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15890032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *Zero = 15900032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 15913e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 15921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15939d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // If we don't already have it, get __CFConstantStringClassReference. 1594c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1595c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 159696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::ArrayType::get(Ty, 0); 15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *GV = CreateRuntimeVariable(Ty, 15989d4a15fd3b85434c43ea27562793de63a793321aChris Lattner "__CFConstantStringClassReference"); 15993e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 16003e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 16013c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1602c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1604e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 16053e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::StructType *STy = 1607e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1608e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 16095add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson std::vector<llvm::Constant*> Fields(4); 161044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1611c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 16125add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[0] = CFConstantStringClassRef; 16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1614c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 16153e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 16175add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson llvm::ConstantInt::get(Ty, 0x07C8); 16185add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1619c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 16200032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1621a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar 162295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::LinkageTypes Linkage; 1623278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner bool isConstant; 1624a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1625278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // FIXME: why do utf strings get "_" labels instead of "L" labels? 162695b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::InternalLinkage; 1627278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1628278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // does make plain ascii ones writable. 1629278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = true; 1630a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } else { 163195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::PrivateLinkage; 1632278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = !Features.WritableStrings; 1633a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 1634278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner 16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 1636278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1637278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner ".str"); 1638a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 16394da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 16404da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck GV->setAlignment(Align.getQuantity()); 1641a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 16425add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 16435add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1644c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 1645c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 16465add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1648c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 164908e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson C = llvm::ConstantStruct::get(STy, Fields); 16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable::PrivateLinkage, C, 165295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner "_unnamed_cfstring_"); 16538e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (const char *Sect = getContext().Target.getCFStringSection()) 16548e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 16551d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar Entry.setValue(GV); 16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16570c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1658c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 165945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 166033e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanianllvm::Constant * 166133e982bf782d851bfe5767acb1336fcf3419ac6bFariborz JahanianCodeGenModule::GetAddrOfConstantNSString(const StringLiteral *Literal) { 16622bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian unsigned StringLength = 0; 16632bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian bool isUTF16 = false; 16642bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::StringMapEntry<llvm::Constant*> &Entry = 16652bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian GetConstantCFStringEntry(CFConstantStringMap, Literal, 16662bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian getTargetData().isLittleEndian(), 16672bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian isUTF16, StringLength); 16682bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16692bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian if (llvm::Constant *C = Entry.getValue()) 16702bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian return C; 16712bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16722bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::Constant *Zero = 16732bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 16742bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::Constant *Zeros[] = { Zero, Zero }; 16752bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 1676ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian // If we don't already have it, get _NSConstantStringClassReference. 16772bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian if (!NSConstantStringClassRef) { 16782bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 16792bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Ty = llvm::ArrayType::get(Ty, 0); 1680ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian llvm::Constant *GV = CreateRuntimeVariable(Ty, 1681ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian Features.ObjCNonFragileABI ? 1682ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian "OBJC_CLASS_$_NSConstantString" : 1683ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian "_NSConstantStringClassReference"); 16842bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // Decay array -> ptr 16852bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian NSConstantStringClassRef = 16862bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 16872bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian } 16882bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16892bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian QualType NSTy = getContext().getNSConstantStringType(); 16902bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16912bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian const llvm::StructType *STy = 16922bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian cast<llvm::StructType>(getTypes().ConvertType(NSTy)); 16932bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16942bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian std::vector<llvm::Constant*> Fields(3); 16952bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16962bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // Class pointer. 16972bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Fields[0] = NSConstantStringClassRef; 16982bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 16992bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // String pointer. 17002bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 17012bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 17022bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::GlobalValue::LinkageTypes Linkage; 17032bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian bool isConstant; 17042bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian if (isUTF16) { 17052bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // FIXME: why do utf strings get "_" labels instead of "L" labels? 17062bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Linkage = llvm::GlobalValue::InternalLinkage; 17072bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // Note: -fwritable-strings doesn't make unicode NSStrings writable, but 17082bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // does make plain ascii ones writable. 17092bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian isConstant = true; 17102bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian } else { 17112bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Linkage = llvm::GlobalValue::PrivateLinkage; 17122bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian isConstant = !Features.WritableStrings; 17132bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian } 17142bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 17152bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::GlobalVariable *GV = 17162bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 17172bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian ".str"); 17182bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian if (isUTF16) { 17192bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 17202bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian GV->setAlignment(Align.getQuantity()); 17212bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian } 17222bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 17232bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 17242bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // String length. 17252bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 17262bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Fields[2] = llvm::ConstantInt::get(Ty, StringLength); 17272bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 17282bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // The struct. 17292bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian C = llvm::ConstantStruct::get(STy, Fields); 17302bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 17312bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian llvm::GlobalVariable::PrivateLinkage, C, 17322bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian "_unnamed_nsstring_"); 17332bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian // FIXME. Fix section. 1734ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian if (const char *Sect = 1735ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian Features.ObjCNonFragileABI 1736ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian ? getContext().Target.getNSStringNonFragileABISection() 1737ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian : getContext().Target.getNSStringSection()) 17382bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian GV->setSection(Sect); 17392bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian Entry.setValue(GV); 17402bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian 17412bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian return GV; 174233e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanian} 174333e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanian 17446143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 17451e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 17466143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 17471e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 17481e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 17491e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 17501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 17511e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 17521e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1754dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 17551e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 17561e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1758dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1759dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 17601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17611e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17631e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 17641e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 17651e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 17666143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 17676143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 17686143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 17696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 17706143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 17717eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman // FIXME: We shouldn't need to bitcast the constant in the wide string case. 17727eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 17737eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman if (S->isWide()) { 17747eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Type *DestTy = 17757eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 17767eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman C = llvm::ConstantExpr::getBitCast(C, DestTy); 17777eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman } 17787eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman return C; 17796143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 17806143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1781eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1782eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1783eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1784eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1785eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1786eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1787a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1788a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1789eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1790eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1791eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1792a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 179445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 17955fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 17965fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 17976143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 17980032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = 17990032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 18001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 180145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 18021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 180395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, 18041c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson C, GlobalName); 180545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 180645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 18076143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 18086143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 18096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 18106143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 18116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 18126143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 18136143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 18146143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 18155fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 18165fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 18178e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar bool IsConstant = !Features.WritableStrings; 18188e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 18198e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Get the default prefix if a name wasn't specified. 18208e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!GlobalName) 182195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GlobalName = ".str"; 18228e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 18238e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Don't share any string literals if strings aren't constant. 18248e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!IsConstant) 18255fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 18261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StringMapEntry<llvm::Constant *> &Entry = 182895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 182945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 183045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1831eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 183245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 183345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 18345fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 183545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 183645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 183745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 18386143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 18396143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 18406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 18416143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 18425fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 18435fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1844c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 18456143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 184641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1847af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1848af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 18491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::EmitObjCPropertyImplementations(const 1850af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 18511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (ObjCImplementationDecl::propimpl_iterator 185217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1853af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1855af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1856af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1857af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1858af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1859af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1860af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1861af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1862af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1863af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 186417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis if (!D->getInstanceMethod(PD->getGetterName())) 1865fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1866fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1867af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 186817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis !D->getInstanceMethod(PD->getSetterName())) 1869fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1870fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1871af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1872af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1873af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1874af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1875109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian/// EmitObjCIvarInitializations - Emit information for ivar initialization 1876109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian/// for an implementation. 1877109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 1878109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0) 1879109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian return; 1880109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D)); 1881109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian assert(DC && "EmitObjCIvarInitializations - null DeclContext"); 1882109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 1883109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 1884109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(), 1885109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->getLocation(), 1886109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->getLocation(), cxxSelector, 1887109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian getContext().VoidTy, 0, 1888109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian DC, true, false, true, 1889109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl::Required); 1890109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->addInstanceMethod(DTORMethod); 1891109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 1892109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 1893109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian II = &getContext().Idents.get(".cxx_construct"); 1894109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian cxxSelector = getContext().Selectors.getSelector(0, &II); 1895109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // The constructor returns 'self'. 1896109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), 1897109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->getLocation(), 1898109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->getLocation(), cxxSelector, 1899109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian getContext().getObjCIdType(), 0, 1900109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian DC, true, false, true, 1901109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl::Required); 1902109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian D->addInstanceMethod(CTORMethod); 1903109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 1904109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 1905109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 1906109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1907109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 190891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace. 1909984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 191017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1911984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson I != E; ++I) 1912984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitTopLevelDecl(*I); 1913984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson} 1914984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson 191591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec. 191691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1917f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1918f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 191991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson ErrorUnsupported(LSD, "linkage spec"); 192091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson return; 192191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson } 192291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 192317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 192491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson I != E; ++I) 192591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitTopLevelDecl(*I); 192691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson} 192791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 192841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 192941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 193041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 193141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 193241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 193341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 193441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 193541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 193616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Ignore dependent declarations. 193716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 193816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 19391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 194041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 1941293361afd4199c92aabff9267fddea890943c586Anders Carlsson case Decl::CXXConversion: 19422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson case Decl::CXXMethod: 194341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 194416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Skip function templates 194516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 194616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 19471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1948555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<FunctionDecl>(D)); 1949555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson break; 1950555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson 195141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 1952555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<VarDecl>(D)); 195341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 195441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 195595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // C++ Decls 195641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1957984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitNamespace(cast<NamespaceDecl>(D)); 195841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 19599cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // No code generation needed. 19609d0c6613ec040ad8d952556be909232a7f25dedeJohn McCall case Decl::UsingShadow: 19619cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor case Decl::Using: 1962dd9967a6374c9a44be4af02aaeee340ffb82848fDouglas Gregor case Decl::UsingDirective: 1963127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::ClassTemplate: 1964127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::FunctionTemplate: 1965018837b081bd1a725bae1c020eb1649975b04d67Anders Carlsson case Decl::NamespaceAlias: 19669cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor break; 196795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson case Decl::CXXConstructor: 19681fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson // Skip function templates 19691fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 19701fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson return; 19711fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson 197295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 197395d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson break; 197427ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson case Decl::CXXDestructor: 197527ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 197627ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson break; 197736674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 197836674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson case Decl::StaticAssert: 197936674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson // Nothing to do. 198036674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson break; 198136674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 198295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // Objective-C Decls 19831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 198438e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 198541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 198641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1987b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1988b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 198941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1990285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner 199141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1992b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 199341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 199441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 199541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1996af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1997af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 199841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 199941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 200041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 2001af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 2002af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 2003af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 2004109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitObjCIvarInitializations(OMD); 2005af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 200641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 20071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 200841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 200941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 201041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 20116fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (OMD->getBody()) 201241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 201341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 201441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 20151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Decl::ObjCCompatibleAlias: 2016305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 201741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 201841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 201991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson case Decl::LinkageSpec: 202091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 202141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 202241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 202341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 202441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 20258d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer llvm::StringRef AsmString = AD->getAsmString()->getString(); 20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 202741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 202841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 202941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 203041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 20318d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); 203241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 203341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 20341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump default: 2036f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Make sure we handled everything we should, every other kind is a 2037f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 2038f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // function. Need to recode Decl::Kind to do that easily. 203941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 204041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 204141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 2042744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2043744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Turns the given pointer into a constant. 2044744016dde06fcffd50931e94a98c850f8b12cd87John McCallstatic llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, 2045744016dde06fcffd50931e94a98c850f8b12cd87John McCall const void *Ptr) { 2046744016dde06fcffd50931e94a98c850f8b12cd87John McCall uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); 2047744016dde06fcffd50931e94a98c850f8b12cd87John McCall const llvm::Type *i64 = llvm::Type::getInt64Ty(Context); 2048744016dde06fcffd50931e94a98c850f8b12cd87John McCall return llvm::ConstantInt::get(i64, PtrInt); 2049744016dde06fcffd50931e94a98c850f8b12cd87John McCall} 2050744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2051744016dde06fcffd50931e94a98c850f8b12cd87John McCallstatic void EmitGlobalDeclMetadata(CodeGenModule &CGM, 2052744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::NamedMDNode *&GlobalMetadata, 2053744016dde06fcffd50931e94a98c850f8b12cd87John McCall GlobalDecl D, 2054744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::GlobalValue *Addr) { 2055744016dde06fcffd50931e94a98c850f8b12cd87John McCall if (!GlobalMetadata) 2056744016dde06fcffd50931e94a98c850f8b12cd87John McCall GlobalMetadata = 2057744016dde06fcffd50931e94a98c850f8b12cd87John McCall CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); 2058744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2059744016dde06fcffd50931e94a98c850f8b12cd87John McCall // TODO: should we report variant information for ctors/dtors? 2060744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::Value *Ops[] = { 2061744016dde06fcffd50931e94a98c850f8b12cd87John McCall Addr, 2062744016dde06fcffd50931e94a98c850f8b12cd87John McCall GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) 2063744016dde06fcffd50931e94a98c850f8b12cd87John McCall }; 2064744016dde06fcffd50931e94a98c850f8b12cd87John McCall GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops, 2)); 2065744016dde06fcffd50931e94a98c850f8b12cd87John McCall} 2066744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2067744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Emits metadata nodes associating all the global values in the 2068744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// current module with the Decls they came from. This is useful for 2069744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// projects using IR gen as a subroutine. 2070744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// 2071744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Since there's currently no way to associate an MDNode directly 2072744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// with an llvm::GlobalValue, we create a global named metadata 2073744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// with the name 'clang.global.decl.ptrs'. 2074744016dde06fcffd50931e94a98c850f8b12cd87John McCallvoid CodeGenModule::EmitDeclMetadata() { 2075744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::NamedMDNode *GlobalMetadata = 0; 2076744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2077744016dde06fcffd50931e94a98c850f8b12cd87John McCall // StaticLocalDeclMap 2078744016dde06fcffd50931e94a98c850f8b12cd87John McCall for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator 2079744016dde06fcffd50931e94a98c850f8b12cd87John McCall I = MangledDeclNames.begin(), E = MangledDeclNames.end(); 2080744016dde06fcffd50931e94a98c850f8b12cd87John McCall I != E; ++I) { 2081744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); 2082744016dde06fcffd50931e94a98c850f8b12cd87John McCall EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); 2083744016dde06fcffd50931e94a98c850f8b12cd87John McCall } 2084744016dde06fcffd50931e94a98c850f8b12cd87John McCall} 2085744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2086744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Emits metadata nodes for all the local variables in the current 2087744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// function. 2088744016dde06fcffd50931e94a98c850f8b12cd87John McCallvoid CodeGenFunction::EmitDeclMetadata() { 2089744016dde06fcffd50931e94a98c850f8b12cd87John McCall if (LocalDeclMap.empty()) return; 2090744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2091744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::LLVMContext &Context = getLLVMContext(); 2092744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2093744016dde06fcffd50931e94a98c850f8b12cd87John McCall // Find the unique metadata ID for this name. 2094744016dde06fcffd50931e94a98c850f8b12cd87John McCall unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); 2095744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2096744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::NamedMDNode *GlobalMetadata = 0; 2097744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2098744016dde06fcffd50931e94a98c850f8b12cd87John McCall for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator 2099744016dde06fcffd50931e94a98c850f8b12cd87John McCall I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { 2100744016dde06fcffd50931e94a98c850f8b12cd87John McCall const Decl *D = I->first; 2101744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::Value *Addr = I->second; 2102744016dde06fcffd50931e94a98c850f8b12cd87John McCall 2103744016dde06fcffd50931e94a98c850f8b12cd87John McCall if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { 2104744016dde06fcffd50931e94a98c850f8b12cd87John McCall llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); 2105744016dde06fcffd50931e94a98c850f8b12cd87John McCall Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, &DAddr, 1)); 2106744016dde06fcffd50931e94a98c850f8b12cd87John McCall } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) { 2107744016dde06fcffd50931e94a98c850f8b12cd87John McCall GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); 2108744016dde06fcffd50931e94a98c850f8b12cd87John McCall EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); 2109744016dde06fcffd50931e94a98c850f8b12cd87John McCall } 2110744016dde06fcffd50931e94a98c850f8b12cd87John McCall } 2111744016dde06fcffd50931e94a98c850f8b12cd87John McCall} 2112