CodeGenModule.cpp revision 541b63b1a9db77e4a8670e9823711c2c12e58afb
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h" 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2121ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 222c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 25ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 26bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 2820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, 34fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 35f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar Diagnostic &diags, bool GenerateDebugInfo) 36fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), 37208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), 380c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman CFConstantStringClassRef(0) { 39208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 40208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Features.ObjC1) { 41f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar if (Features.NeXTRuntime) { 4230bc57187be7535c57ef1ca8ff3e765653e94332Fariborz Jahanian Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) 43ee0af74d1e0990c7b66d32657f3e4e54b8691552Fariborz Jahanian : CreateMacObjCRuntime(*this); 44208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } else { 45208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Runtime = CreateGNUObjCRuntime(*this); 46208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } 47c17a4d3b16a2624a76de5d7508805534545bd3bfDaniel Dunbar } 48e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 49e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 50815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; 512b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 522b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 532b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 54815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 55815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 56815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 57815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 5920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov EmitStatics(); 60219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar EmitAliases(); 61208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 62208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 63208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 66532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 67f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar BindRuntimeFunctions(); 682b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 70f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarvoid CodeGenModule::BindRuntimeFunctions() { 71f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Deal with protecting runtime function names. 72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { 73f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = RuntimeFunctions[i].first; 74f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name = RuntimeFunctions[i].second; 75f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 760293d540baafbe070c1035611787a81001a4118eDaniel Dunbar // Discard unused runtime functions. 770293d540baafbe070c1035611787a81001a4118eDaniel Dunbar if (Fn->use_empty()) { 780293d540baafbe070c1035611787a81001a4118eDaniel Dunbar Fn->eraseFromParent(); 790293d540baafbe070c1035611787a81001a4118eDaniel Dunbar continue; 800293d540baafbe070c1035611787a81001a4118eDaniel Dunbar } 810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar 82f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // See if there is a conflict against a function. 83f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Conflict = TheModule.getFunction(Name); 84f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (Conflict) { 85f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Decide which version to take. If the conflict is a definition 86f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // we are forced to take that, otherwise assume the runtime 87f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // knows best. 88f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (!Conflict->isDeclaration()) { 89f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); 91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->replaceAllUsesWith(Casted); 92f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->eraseFromParent(); 93f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 94f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->takeName(Conflict); 95f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); 97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->replaceAllUsesWith(Casted); 98f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->eraseFromParent(); 99f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 100f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // FIXME: There still may be conflicts with aliases and 102f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // variables. 103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->setName(Name); 104f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 108488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1092c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 11290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 114488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 1152c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner "cannot codegen this %0 yet"); 1162c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1170a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1180a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1192c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12058c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 121488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 122c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 12390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 12490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 12590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 12690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 127488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 128c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner "cannot codegen this %0 yet"); 129c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1300a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 131c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 132c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 13341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 13441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 13541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 13641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1374f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1384f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1394f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1404f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1414f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1516d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1526d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1536bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 15449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1556bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1566d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1576d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1586bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1596bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1606bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 16149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1626bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1636bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 1706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 1716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 1726bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 174572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 1756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 1766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 1776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1786bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 1796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 1806bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 1816bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 1826bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 1846bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 1856bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 190572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 193572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 1946d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 1956d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1966d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 197532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 198532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 199532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 200532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 201532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 202532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 203532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 204532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 205532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 206532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 207532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 208532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 209532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 210532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 211532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 212532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarstatic void SetGlobalValueAttributes(const Decl *D, 2140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar bool IsInternal, 2150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar bool IsInline, 216219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GV, 217219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar bool ForDefinition) { 21849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 219d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 220219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 221219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2222f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2232f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2242f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2253ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2262f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2272f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2282f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2292f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2302f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2312f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2322f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2333ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2342f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } 235219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 236219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 237219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 238219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2392f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2402f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2412f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2422f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2432f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2442f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2452f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2462f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else if (D->getAttr<WeakAttr>() || IsInline) 247219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::WeakLinkage); 248219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2490dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 250d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 25149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 25249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2530dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 25441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 255d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 256a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 2570dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 258a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 259a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 260a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 261a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 262d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 263d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 264761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 26545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 266b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 267761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 26888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 269c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 270761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 271761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 272ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 273ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 27445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 275f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 276f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 277f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 278f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 279f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 280f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 281f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 282f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 283219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 284219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 285219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 286219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 287219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 288219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 289219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 290219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 291219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 292219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 293f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar if (!Features.Exceptions) 294f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 295af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 296af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 297af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 298f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 299f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 300f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 301f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 302541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 303f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 304219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 305f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 306f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 307f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 308f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 309541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 31045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 311219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 312219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 313219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 314219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 31545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 316219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 317219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 318219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *D = Aliases[i]; 319219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 320219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 321219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 322219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 323219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 324219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 325219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 326219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 327219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 328219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 329219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *aliasee = getModule().getFunction(aliaseeName); 330219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 331219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 332219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 333219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 334219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 335219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 336219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 337219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 338219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 339219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 340d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner D->getNameAsString(), aliasee, &getModule()); 341219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 342219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; 343219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 344219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 345219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 346219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 347219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 348219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 349219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 350219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 351ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 352219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 353219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 354219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 355219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 356219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 357219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 358ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 359ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 3604c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begemanvoid CodeGenModule::EmitStatics() { 3614c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Emit code for each used static decl encountered. Since a previously unused 3624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // static decl may become used during the generation of code for a static 3634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // function, iterate until no changes are made. 3644c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 3654c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 3664c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 367b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 368b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(), 369b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson e = StaticDecls.end(); i != e; ) { 370b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 371b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 3726f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 3736f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 3746f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // need to check for uses in an alias and in a constructor. 377b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson if (!GlobalDeclMap.count(D->getIdentifier())) { 378b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson i++; 379a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 380b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 381b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 382bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 383bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 384bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 3854c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 386b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson i = StaticDecls.erase(i); 387b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 3884c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 3894c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 3904c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 3914c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 3958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 3968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 3973c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 3988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 3998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 4238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 4278bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4288bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 4318bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 4328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 4338bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 4348bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 4358bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 4368bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 4378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 438bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 439bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar bool isDef, isStatic; 440bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 441bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 442219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Aliases are deferred until code for everything else has been 443219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // emitted. 444219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (FD->getAttr<AliasAttr>()) { 445219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar assert(!FD->isThisDeclarationADefinition() && 446219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar "Function alias cannot have a definition!"); 447219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Aliases.push_back(FD); 448219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return; 449219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 450219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 451219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar isDef = FD->isThisDeclarationADefinition(); 452bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar isStatic = FD->getStorageClass() == FunctionDecl::Static; 453bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = cast<VarDecl>(Global)) { 454bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 455bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 45649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar isDef = !((VD->getStorageClass() == VarDecl::Extern || 45749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar VD->getStorageClass() == VarDecl::PrivateExtern) && 45849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar VD->getInit() == 0); 459bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar isStatic = VD->getStorageClass() == VarDecl::Static; 460bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 461bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobal"); 4624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman return; 4634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4644c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 465bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Forward declarations are emitted lazily on first use. 466bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (!isDef) 46788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner return; 468bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 469bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // If the global is a static, defer code generation until later so 470bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // we can easily omit unused statics. 471bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (isStatic) { 472bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar StaticDecls.push_back(Global); 473bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 474bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 475bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 476bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 477bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 4784c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 4794c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 480bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 481bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 482bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 483bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 484bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 485bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 486bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 487bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 488bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 489bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4909986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 49177ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 49277ba708819285931932ecd33691a672bb59d221aEli Friedman 493bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 494bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 4959986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 496bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4973c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 49890db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; 49949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 50049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 50149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 50249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 50349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 0, D->getNameAsString(), &getModule(), 0, 50449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar ASTTy.getAddressSpace()); 50549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 50649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 50749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 50849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 50949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 51049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 51149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 51249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 51349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 51449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 51549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 51649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 5173c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 5189986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 5199986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 520bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 521bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 522bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 5238f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 52477ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 52577ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 52677ba708819285931932ecd33691a672bb59d221aEli Friedman 5278f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 528cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 529cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 530cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 531cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 532cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 533cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 534cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 535cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 536cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 537cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 538cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 539cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 54077ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 541bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 5428f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 54377ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 5448e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 54590db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; 5463c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 5473c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 54877ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 54977ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 55077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 551d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner 0, D->getNameAsString(), &getModule(), 0, 55277ba708819285931932ecd33691a672bb59d221aEli Friedman ASTTy.getAddressSpace()); 5533c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 5543c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 55577ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 55677ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 55777ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 55877ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 55977ba708819285931932ecd33691a672bb59d221aEli Friedman // 56077ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 56177ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 56277ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 56377ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 564cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 565cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 566cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 567cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 56877ba708819285931932ecd33691a672bb59d221aEli Friedman 56977ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 57077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 57177ba708819285931932ecd33691a672bb59d221aEli Friedman 57277ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 57377ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 57477ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 575d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner 0, D->getNameAsString(), &getModule(), 0, 57677ba708819285931932ecd33691a672bb59d221aEli Friedman ASTTy.getAddressSpace()); 57777ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 57877ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 57977ba708819285931932ecd33691a672bb59d221aEli Friedman 58077ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 58177ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 58277ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 58377ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 58477ba708819285931932ecd33691a672bb59d221aEli Friedman 58577ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 58677ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 58777ba708819285931932ecd33691a672bb59d221aEli Friedman } 58877ba708819285931932ecd33691a672bb59d221aEli Friedman 5893c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 5909e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 5918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 5928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 5938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 594f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 5958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 5968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 59788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 598b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 599ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 600cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This is silly; getTypeAlign should just work for incomplete arrays 601cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman unsigned Align; 602c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const IncompleteArrayType* IAT = 603c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Context.getAsIncompleteArrayType(D->getType())) 604cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(IAT->getElementType()); 605cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman else 606cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(D->getType()); 60708d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { 60808d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman Align = std::max(Align, AA->getAlignment()); 60908d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman } 61008d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman GV->setAlignment(Align / 8); 61108d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 612ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 61341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 614ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 615a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 616a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 617a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 618a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 619a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 620a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 62188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 62288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 6238fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 6248fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 6258fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 626ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 627ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 628ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 6298fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<WeakAttr>()) 630ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakLinkage); 6318fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 632ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 633ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 634ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 6358fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 636ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 637ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 638ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 639ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 640ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 641a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 64298883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 64398883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 644ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 645ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 64649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 64749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 64849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 649ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 65049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 65149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 652ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 653ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 65488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 655686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 656686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 657686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 658686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 65966031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 660686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 661686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 66288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 664bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 665bd012ff1fa088181646a784f385b28867372d434Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { 666219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 667219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 668219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 669d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner D->getNameAsString(),&getModule()); 670219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributes(D, F); 671219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 672bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 673bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 674bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 6759986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 6769986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 6779986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 6783c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 6793c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 68090db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; 6813c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 6823c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = EmitForwardFunctionDefinition(D); 683bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 6849986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 685bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 686bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 687bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 68890db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; 6893c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 690bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = EmitForwardFunctionDefinition(D); 691bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 6923c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 6933c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 6943c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 695bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise, we have a definition after a prototype with the wrong type. 696bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // F is the Function* for the one with the wrong type, we must make a new 697bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* and update everything that used F (a declaration) with the new 698bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* (which will be a definition). 699bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // 700bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // This happens if there is a prototype for a function (e.g. "int f()") and 701bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // then a definition of a different type (e.g. "int f(int x)"). Start by 702bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // making a new function of the correct type, RAUW, then steal the name. 7033c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); 7043c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar NewFn->takeName(Entry); 705bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 706bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Replace uses of F with the Function we will endow with a body. 707bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *NewPtrForOldDecl = 7083c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 7093c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 7103c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 711bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Ok, delete the old function now, which is dead. 7123c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 713219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 714bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 715bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = NewFn; 716bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 717bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 718bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 719219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 720219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 7216379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 722219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 723219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 724219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 725219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 726219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 727219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 728bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 729bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 730bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 731f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 732f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 733f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 734f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 735f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 736f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 737f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar RuntimeFunctions.push_back(std::make_pair(Fn, Name)); 738f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 739f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 740f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 741c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 742c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 743c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 744d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 745d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 746d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 747bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 748bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 7491426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 7501426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 751bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 7521426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 7531426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 7541426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 7551426fecdf90dd1986751b9940422e675880ff671Chris Lattner llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 756bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 757bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 758bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 759bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); 760bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 761bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the name, skip over the __builtin_ prefix. 762bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; 763bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 764bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 765bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); 766bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 767bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 768bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 769bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 770bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 771bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 772bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 773bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 774bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 775bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 776c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 777c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 778c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 779c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 780c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 781bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 782bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 7834c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman return FunctionSlot = 7844c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 7854c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 786bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 787bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 7887acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 7897acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 7907acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 7917acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 7927acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 793bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 7964e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 7974e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 799c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 8000c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 8010c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 8024e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8034e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 8040c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 8050c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 80641ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 80741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 8084e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8094e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 81041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 8117acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 812e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 813e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 81444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 81544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 816e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 817e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 818e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 819e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 820e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 82144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 822e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 823e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 82444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 825e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 826e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 82744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 828e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 829e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 830e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 831e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 832e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 833e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 834e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 835e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 836e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 837e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 838e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 8393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 8403e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 841bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 842bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 843c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 844c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 845c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 846c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 847c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 848c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 8493e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 8503e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 851c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 852c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 853c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 854c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 8553e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 8563e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 8573e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 8583e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 8593e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 8603e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 8613e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 862c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 863c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 864c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 865c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 8663e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 8673e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 8683e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 8693e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 870c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 871c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 872e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 873e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 8743e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 875e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 876e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 877e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 878e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 87944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 88044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 881c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 88244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 88344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 88444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 88544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 886c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 887c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 88844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 88944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 8903e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 89144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 89244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 893c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 894c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 89544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 89644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 8973e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 898c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 899c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 900e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 90144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 902e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 903e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 904c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 905c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 90644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 90744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 908c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 90944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 91044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 911c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 912c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 913e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 9140c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 9150c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 9160c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 9170c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 9183e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9190c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 9200c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 9213e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9220c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 923c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 92445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 9256143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 9261e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 9276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 928662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar if (E->isWide()) { 929662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(E, "wide string"); 930662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar return "FIXME"; 931662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar } 932662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar 9331e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 9341e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 9351e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9361e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 9371e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 9381e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 9391e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9401e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // Resize the string to the right size 9411e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // FIXME: What about wchar_t strings? 9421e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 9431e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 9441e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 9451e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9461e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 9471e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 9481e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 9506143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 9516143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 9526143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 9536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 9546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 9556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 9566143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 957a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 95845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 95945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 9605fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 9615fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 9626143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 9636143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 96445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 96545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 96645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner C = new llvm::GlobalVariable(C->getType(), constant, 96745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::GlobalValue::InternalLinkage, 9685fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar C, 9695fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar GlobalName ? GlobalName : ".str", 9705fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar &CGM.getModule()); 9716143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 97245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 97345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 97445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 9756143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 9766143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 9776143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 9786143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 9796143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 9806143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 9816143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 9826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 9835fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 9845fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 98545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 98645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 9875fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 98845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 98945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 99045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 99145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 99245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 99345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return Entry.getValue(); 99445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 99545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 9965fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 99745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 99845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 99945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 10006143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 10016143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 10026143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 10036143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 10045fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 10055fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1006c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 10076143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 100841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1009af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1010af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1011af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1012af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1013af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1014af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1015af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1016af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1017af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1018af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1019af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1020af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1021af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1022af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1023af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1024af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1025af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1026af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1027fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1028fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1029af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1030af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1031fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1032fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1033af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1034af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1035af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1036af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 103741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 103841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 103941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 104041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 104141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 104241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 104341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 104441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 104541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 104641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 104741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 104841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 104941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 105041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 105141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1052662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 105341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 105441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 105541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 105641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 105741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Forward declarations, no (immediate) code generation. 105841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 105941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategory: 106041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 106141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCInterface: 106241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 106341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 106441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 106541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 106641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 106741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 106841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1069af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1070af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1071af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 107241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 107341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 107441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1075af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1076af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1077af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1078af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 107941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1080af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 108141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 108241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 108341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 108441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 108541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 108641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 108741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 108841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1089305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 109041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 109141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 109241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 109341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 109441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1095488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 109641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 109741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 109841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 109941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 110041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 110141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 110241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 110341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 110441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 110541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 110641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 110741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 110841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 110941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 111041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 111141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 111241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 111341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 111441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 111541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 111641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 111741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 111841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 111941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 112041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 112141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 112241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1123