CodeGenModule.cpp revision 35f38a2c22d68c22e2dbe8e9ee84c120c8f327bb
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 20bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/Frontend/CompileOptions.h" 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2321ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 27ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 28bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 3020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 35bd3606426d389370616126af969904ec493cb105Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts, 36fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 37bd3606426d389370616126af969904ec493cb105Chris Lattner Diagnostic &diags) 38bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 39bd3606426d389370616126af969904ec493cb105Chris Lattner Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M), 402a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), 412a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) { 42208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 433c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 443c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 453c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 51e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 52e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 53bd3606426d389370616126af969904ec493cb105Chris Lattner DebugInfo = CompileOpts.DebugInfo ? new CGDebugInfo(this) : 0; 542b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 562b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 57815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 6282227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 63208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 65208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 68532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 690269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 70f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 71f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 72488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 732c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 7490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 7590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 7690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 7790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 78488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 7956b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 802c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 810a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 820a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 832c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 8458c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 85488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 86c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 8790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 8890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 8990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 9090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 91488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 9256b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 93c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 940a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 95c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 96c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 9741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 9841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 9941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 10041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1014f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1024f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1034f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1044f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1054f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1064f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1074f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1084f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1094f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1104f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1114f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1124f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1134f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1144f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1155f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1165f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1175f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 118c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 119c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1205f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1216ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 122c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner // In C, functions with no attributes never need to be mangled. Fastpath them. 123c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { 124c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1253c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 126c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 127c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner 1286ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1296ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 130fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar if (!mangleName(ND, Context, Out)) { 131fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1323c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 133fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar } 1345f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1356ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 1363c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return MangledNames.GetOrCreateValue(Name.begin(), Name.end()).getKeyData(); 1375f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1385f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1396d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1406d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1416bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 14249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1436bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1446d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1456d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1466bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1476bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1486bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 14949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1506bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1516bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1526bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1536bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1546bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1556bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1566bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1576bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 1586bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 1596bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 1606bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1616bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 162572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 1636bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 1646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 1656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 1676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 1686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 1696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 1706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 1716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 1726bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 1736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 1746bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 1766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 1776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 178572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 1796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 1806bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 181572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 1826d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 1836d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1846d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 185532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 186532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 187532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 188532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 189532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 190532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 191532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 192532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 193532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 194532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 195532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 196532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 197532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 198532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 199532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 200532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2015c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 2025c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2035c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2045c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2055c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 20649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 207d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 208219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 209219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2102f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2112f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2122f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2133ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2142f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2152f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2162f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2172f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2182f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2192f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2202f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2213ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2225e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || 2235e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar D->getAttr<WeakImportAttr>()) { 2245e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 2255e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // separate linkage types for this. 226e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands GV->setLinkage(llvm::Function::ExternalWeakLinkage); 2275e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } 228219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 229219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 230219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 231219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2322f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2332f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2342f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2352f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2362f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2372f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2382f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2395e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() || 2405e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar IsInline) 241286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump GV->setLinkage(llvm::Function::WeakAnyLinkage); 242219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2430dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 244d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 24549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 24649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2470dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 24841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 249d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 250a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 25117f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 25217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 2535c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 2545c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 2555c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // might add multiple times or risk the value being replaced by a 2565c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // subsequent RAUW. 2575c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (ForDefinition) { 2585c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 2595c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 2605c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 261d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 262d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 263761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 26445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 265b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 266761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 26788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 268c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 269761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 270761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 271ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 272ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 27345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 274f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 275f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 276f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 277f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 278ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian 279ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian if (D->getAttr<RegparmAttr>()) 280ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian ErrorUnsupported(D, "regparm attribute"); 281f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 282f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 283f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 284f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 285219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 286219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 287219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 288219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 289219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 290219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 291219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 292219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 293219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 294219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 29574ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 296f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 297af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 298af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 299af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 30081ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 30181ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson if (D->getAttr<NoinlineAttr>()) 30281ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 303f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 304f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 305f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 306f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 307541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 308f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 309219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 310f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 311f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 312f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 313f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 314541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 31545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 316219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 317219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 318219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 319219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 3210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 3220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 32335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 3240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 3250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 3270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 3280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 3290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 3300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 33135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 33235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, LLVMUsed.size()); 33335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 33435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 33535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 33635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 33735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 33835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray[i] = 33935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), i8PTy); 34035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 34135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 3420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 3430269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 3440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 34535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ConstantArray::get(ATy, UsedArray), 3460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 3470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 3490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 3500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 35267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 35367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 35467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 35567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner while (!DeferredDeclsToEmit.empty()) { 35667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner const ValueDecl *D = DeferredDeclsToEmit.back(); 35767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 35867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 35967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // The mangled name for the decl must have been emitted in GlobalDeclMap. 36067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Look it up to see if it was defined with a stronger definition (e.g. an 36167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // extern inline function with a strong function redefinition). If so, 36267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // just ignore the deferred decl. 36367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; 36467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 365b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 36667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 36767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 36867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 36967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 37067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 37167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 3758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 3768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 3773c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 3788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 3798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 3808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 3818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 3828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 3838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 3848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 3858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 3868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 3878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 3888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 3898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 3908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 3918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 3928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 3938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 3948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 3958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 3968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 3978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 3988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 3998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 4038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 4078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 4118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 4128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 4138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 4148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 4158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 4168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 4178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 41873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 4195c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 4205c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 4215c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) 42273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 423bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 424bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 42573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 42673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) 42773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 42873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 42999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // FIXME: What about inline, and/or extern inline? 43073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 43173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 43273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 43373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 43499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 43573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 43673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 43773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 43873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 43973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 44073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 44173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 44273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 44373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 444bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 445bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 446bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Global->getAttr<AliasAttr>()) 447bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return EmitAliasDefinition(Global); 448219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 44967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 4505e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 45173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 45273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 45373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 4540269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 4550269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 456bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 457bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 45873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 4597542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 46073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 4614c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 46367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 46467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 46573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 46667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // If the value has already been used, add it directly to the 46767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit list. 46867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner const char *MangledName = getMangledName(Global); 46967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (GlobalDeclMap.count(MangledName)) 47067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(Global); 47167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner else { 47267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 47367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // first use of the mangled name will cause it to move into 47467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit. 47567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls[MangledName] = Global; 47667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 477bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 478bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 479bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 480bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 481bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 4824c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 4834c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 484bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 485bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 486bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 487bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 488bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 489bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 490bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 491bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 492bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 493bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 49474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 49574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 49674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 49774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 49874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 49974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 50074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 50174391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, 50274391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty, 50374391b48b4791cded373683a3baf67314f358d50Chris Lattner const FunctionDecl *D) { 5040558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 5050558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 5060558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 5070558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 5080558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 5090558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 5100558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 5110558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 5120558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return llvm::ConstantExpr::getBitCast(Entry, PTy); 5130558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 5140558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 51567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 51667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 51767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 51867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = 51974391b48b4791cded373683a3baf67314f358d50Chris Lattner DeferredDecls.find(MangledName); 52067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 52167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 52267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 52367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 52467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 52567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 52667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 5270558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // This function doesn't have a complete type (for example, the return 5280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // type is an incomplete struct). Use a fake type instead, and make 5290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // sure not to try to set attributes. 5300558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner bool ShouldSetAttributes = true; 5310558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (!isa<llvm::FunctionType>(Ty)) { 5320558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Ty = llvm::FunctionType::get(llvm::Type::VoidTy, 5330558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner std::vector<const llvm::Type*>(), false); 5340558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner ShouldSetAttributes = false; 5350558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 5360558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 5370558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function::ExternalLinkage, 538d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner "", &getModule()); 539d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner F->setName(MangledName); 54074391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D && ShouldSetAttributes) 5410558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner SetFunctionAttributes(D, F); 5420558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = F; 5430558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return F; 5440558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 5450558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 54674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 54774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 54874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 54974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D, 55074391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 55174391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 55274391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 55374391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertType(D->getType()); 55474391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMFunction(getMangledName(D), Ty, D); 55574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 55677ba708819285931932ecd33691a672bb59d221aEli Friedman 55774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 55874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 55974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 56074391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 56174391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 56274391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 56374391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 56474391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, 0); 56574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 566bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 56774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 56874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 56974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 57074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 57174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 57274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 57374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 57474391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, 57574391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType*Ty, 57674391b48b4791cded373683a3baf67314f358d50Chris Lattner const VarDecl *D) { 5773c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5785d4f5c724533b994de05df49ae259120482ec366Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 57999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 58074391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 581570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 582570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 58399b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 58474391b48b4791cded373683a3baf67314f358d50Chris Lattner return llvm::ConstantExpr::getBitCast(Entry, Ty); 58599b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 58667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 58767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 58867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 58967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 59067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = 59167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.find(MangledName); 59267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 59367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 59467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 59567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 59667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 59767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 59867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 59999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalVariable *GV = 60074391b48b4791cded373683a3baf67314f358d50Chris Lattner new llvm::GlobalVariable(Ty->getElementType(), false, 60199b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 602d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner 0, "", &getModule(), 60374391b48b4791cded373683a3baf67314f358d50Chris Lattner 0, Ty->getAddressSpace()); 604d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner GV->setName(MangledName); 60549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 60699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 60774391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 60874391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: This code is overly simple and should be merged with 60974391b48b4791cded373683a3baf67314f358d50Chris Lattner // other global handling. 61074391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setConstant(D->getType().isConstant(Context)); 61149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 61274391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 61374391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 61474391b48b4791cded373683a3baf67314f358d50Chris Lattner setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 61549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 61674391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 61774391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 61874391b48b4791cded373683a3baf67314f358d50Chris Lattner } 61974391b48b4791cded373683a3baf67314f358d50Chris Lattner 62074391b48b4791cded373683a3baf67314f358d50Chris Lattner return Entry = GV; 62174391b48b4791cded373683a3baf67314f358d50Chris Lattner} 622eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 623eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 62474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 62574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 62674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 62774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 62874391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 62974391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 63074391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 63174391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 63274391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 63374391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 63474391b48b4791cded373683a3baf67314f358d50Chris Lattner 63574391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType *PTy = 63674391b48b4791cded373683a3baf67314f358d50Chris Lattner llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 63774391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); 63874391b48b4791cded373683a3baf67314f358d50Chris Lattner} 6393f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 64074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 64174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 64274391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 64374391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 64474391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 64574391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 64674391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 64774391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 648bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 649bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 650bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6518f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 65277ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 65377ba708819285931932ecd33691a672bb59d221aEli Friedman 6548f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 655cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 656cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 657570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner const llvm::Type *InitTy = getTypes().ConvertTypeForMem(ASTTy); 658cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 659cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 660cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 661570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner const llvm::ArrayType* ATy = cast<llvm::ArrayType>(InitTy); 662cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 663cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 664cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 66577ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 666bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 6676e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 668232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ErrorUnsupported(D, "static initializer"); 6696e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 6706e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 6716e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 6728f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 6738e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 6742d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 675570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 676570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 677570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 678570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 679570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 680570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 681570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 6823c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 683570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 684570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 685570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 686570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // If we already have this global and it has an initializer, then 687570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // we are in the rare situation where we emitted the defining 688570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // declaration of the global and are now being asked to emit a 689570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // definition which would be common. This occurs, for example, in 690570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // the following situation because statics can be emitted out of 691570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // order: 692570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 693570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int x; 694570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int *y = &x; 695570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int x = 10; 696570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // int **z = &y; 697570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 698570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Bail here so we don't blow away the definition. Note that if we 699570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // can't distinguish here if we emitted a definition with a null 700570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // initializer, but this case is safe. 701570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV && GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { 702232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar assert(!D->getInit() && "Emitting multiple definitions of a decl!"); 703232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar return; 704570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 705570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 706570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 707570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 708570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 709570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 710570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 711570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 712570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 713570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 714570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 715570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 716570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // FIXME: This also ends up happening if there's a definition followed by 717570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // a tentative definition! (Although Sema rejects that construct 718570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // at the moment.) 719570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 720570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 721570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 722570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 723570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Remove the old entry from GlobalDeclMap so that we'll create a new one. 724570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GlobalDeclMap.erase(getMangledName(D)); 725232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 726570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 727570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 7280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner GV->takeName(cast<llvm::GlobalValue>(Entry)); 7290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 73077ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 73177ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 732570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 733570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 73477ba708819285931932ecd33691a672bb59d221aEli Friedman 73577ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 736570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 73777ba708819285931932ecd33691a672bb59d221aEli Friedman } 73877ba708819285931932ecd33691a672bb59d221aEli Friedman 7398bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 7408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 7418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 742f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 7438bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 7448bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 74588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 746b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 7470de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 74808d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 749ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 75041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 751ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 752a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 75388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7548fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7558fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 7568fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 757ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 758ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 759ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 7605e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) 761286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 7628fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 763ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 764ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 765ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 7668fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 767ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 768ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 769ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 770ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 771bd3606426d389370616126af969904ec493cb105Chris Lattner if (!D->getInit() && !CompileOpts.NoCommon) 772ceb77d909f80d4949ee6177094510413c391cddcDuncan Sands GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 77398883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 77498883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 775ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 776ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 77749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 77849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 77949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 780ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 78149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 78249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 783ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 784ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 78588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 786686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 78717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 78817f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 78917f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 7905c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 7915c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7925c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 793686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 7942d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 79566031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 796686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 797686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 79888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 800bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 801bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 802d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::FunctionType *Ty = 803d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 804d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 805d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // As a special case, make sure that definitions of K&R function 806d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // "type foo()" aren't declared as varargs (which forces the backend 807d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar // to do unnecessary work). 808ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner if (D->getType()->isFunctionNoProtoType()) { 809ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner assert(Ty->isVarArg() && "Didn't lower type as expected"); 810ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner // Due to stret, the lowered function could have arguments. Just create the 811ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner // same type as was lowered by ConvertType but strip off the varargs bit. 812ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); 813ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); 814ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner } 815d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 81634809507232bc4c3c4840c7d092c7440219fddafChris Lattner // Get or create the prototype for teh function. 8170558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(D, Ty); 81834809507232bc4c3c4840c7d092c7440219fddafChris Lattner 8190558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 8200558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 8210558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 8220558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 8230558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 8240558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8250558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8260558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 82742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 8280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(cast<llvm::GlobalValue>(Entry)->isDeclaration() && 8290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 83034809507232bc4c3c4840c7d092c7440219fddafChris Lattner 83162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 83262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 83362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 83462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 83562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 83662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 83762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f(int x)"). Start by making a new function of the 83862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // correct type, RAUW, then steal the name. 83934809507232bc4c3c4840c7d092c7440219fddafChris Lattner GlobalDeclMap.erase(getMangledName(D)); 8400558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(D, Ty)); 8410558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner NewFn->takeName(cast<llvm::GlobalValue>(Entry)); 84262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 84362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 84462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner llvm::Constant *NewPtrForOldDecl = 8450558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 8460558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 84762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 84862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 8490558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 85062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 8510558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 852bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 8530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8540558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 855bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 856219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8576379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 858219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 859219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 86034809507232bc4c3c4840c7d092c7440219fddafChris Lattner if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 861219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 86234809507232bc4c3c4840c7d092c7440219fddafChris Lattner if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 863219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 864bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 865bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 866bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { 867bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const AliasAttr *AA = D->getAttr<AliasAttr>(); 868bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 869bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 870bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 871bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 872bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Unique the name through the identifier table. 873bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *AliaseeName = AA->getAliasee().c_str(); 874bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner AliaseeName = getContext().Idents.get(AliaseeName).getName(); 875bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 876bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 877bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 878bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 879bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 880bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, 0); 881bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 882bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMGlobal(AliaseeName, 883bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::PointerType::getUnqual(DeclTy), 0); 884bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 885bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 886bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *GA = 887bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 888bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 889bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 890bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 891bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // See if there is already something with the alias' name in the module. 892bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *MangledName = getMangledName(D); 893bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 894bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 895bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry && !Entry->isDeclaration()) { 896bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a definition in the module, then it wins over the alias. 897bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // This is dubious, but allow it to be safe. Just ignore the alias. 898bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->eraseFromParent(); 899bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return; 900bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 901bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 902bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 903bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 904bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 905bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 906bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 907bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 908bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 909bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 910bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 911bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 912bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 913bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 914bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 915bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 916bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Now we know that there is no conflict, set the name. 917bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry = GA; 918bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->setName(MangledName); 919bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 920bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Alias should never be internal or inline. 921bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner SetGlobalValueAttributes(D, false, false, GA, true); 922bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 923bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 924c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 925c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 926c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 927d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 928d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 929d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 930b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 931b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 932c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 9333e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 9343e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 9353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 936bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 9383e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 9393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 9403e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 941bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 942bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 943370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor Builtin::Context::GetBuiltinTypeError Error; 944370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); 945370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); 946370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 947bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 948bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 949bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 950b808c952bbff821dce727dd801a1098d64394f98Chris Lattner // Unique the name through the identifier table. 951b808c952bbff821dce727dd801a1098d64394f98Chris Lattner Name = getContext().Idents.get(Name).getName(); 952bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 953b808c952bbff821dce727dd801a1098d64394f98Chris Lattner return GetOrCreateLLVMFunction(Name, Ty, 0); 954bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 955bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9567acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 9577acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 9587acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 9597acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 9607acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 961bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 9644e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9654e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 967c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9680c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 9690c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 9704e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9714e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 9720c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 9730c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 97441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 97541ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 9764e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9774e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 97841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 9797acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 980e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 981e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 98244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 98344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 9843c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner RecordDecl* RD, const llvm::StructType *STy) { 985e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 986e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 987e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 98844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 989e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 990e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 99144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 992e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 993e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 99444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 995e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 996e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 997e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 998e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 999e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 1000e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 1001e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1002e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 1003e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1004e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 1005e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 10063e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 10073e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 1008bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 1009bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 1010c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 1011c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1012c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1013c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 1014c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 1015c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 10163e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 10173e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 1018c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1019c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1020c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1021c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 10223e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10233e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 10243e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 10253e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 10263e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 10273e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 10283e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 1029c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 1030c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 1031c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 1032c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 10333e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10343e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 10353e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 10363e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1037c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1038c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1039e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 1040e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 10413e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1042e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1043e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1044e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1045e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 104644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 104744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1048c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 104944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 105044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 105144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 105244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1053c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1054c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 105544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 105644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10573e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 105844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 105944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 1060c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1061c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 106244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 106344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10643e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 1065c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 1066c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 1067e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 106844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1069e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 1070e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1071c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1072c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 107344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 107444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1075c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 107644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 107744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 1078c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1079c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 1080e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 10810c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 10820c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 10830c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 10840c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 10853e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10860c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 10870c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 10883e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10890c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1090c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 109145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 10931e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 10946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 10951e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 10961e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 10971e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10981e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 10991e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 11001e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 11011e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1102dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 11031e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 11041e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1105dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1106dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1107dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1108dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 11091e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 11101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11111e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 11121e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 11131e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11146143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 11156143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 11166143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 11176143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 11186143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 11196143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 11206143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 11216143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1122eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1123eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1124eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1125eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1126eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1127eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1128a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1129a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1130eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1131eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1132eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1133a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 113445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 113545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 11365fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 11375fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 11386143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 11396143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 114045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 114145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 1142eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return new llvm::GlobalVariable(C->getType(), constant, 1143eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 1144eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner C, GlobalName ? GlobalName : ".str", 1145eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner &CGM.getModule()); 114645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 114745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 11486143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 11496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 11506143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 11516143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 11526143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 11536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 11546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 11556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 11565fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 11575fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 115845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 115945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 11605fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 116145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 116245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 116345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 116445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 116545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1166eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 116745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 116845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 11695fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 117045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 117145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 117245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 11736143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 11746143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 11756143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 11766143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 11775fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 11785fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1179c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 11806143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 118141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1182af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1183af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1184af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1185af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1186af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1187af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1188af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1189af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1190af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1191af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1192af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1193af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1194af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1195af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1196af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1197af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1198af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1199af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1200fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1201fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1202af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1203af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1204fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1205fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1206af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1207af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1208af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1209af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 121041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 121141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 121241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 121341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 121441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 121541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 121641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 121741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 121841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 121941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 122041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 122141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 122241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 122341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 122441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1225662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 122641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 122741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 122841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 122941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 123038e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 123141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 123241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1233b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1234b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 123541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 123638e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian 123741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1238b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 123941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 124041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 124141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1242af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1243af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1244af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 124541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 124741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1248af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1249af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1250af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1251af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 125241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1253af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 125441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 125541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 125641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 125741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 125841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 125941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 126041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 126141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1262305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 126341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 126441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 126541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 126641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 126741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1268488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 126941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 127041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 127141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 127241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 127341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 127441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 127541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 127641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 127741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 127841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 127941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 128041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 128141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 128241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 128341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 128441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 128541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 128641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 128741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 128841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 128941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 129041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 129141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 129241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 129341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 129441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1295