CodeGenModule.cpp revision 56ebe5082da7411fb37479e230b52735f77cff35
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h"
195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h"
20bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/Frontend/CompileOptions.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2321ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h"
242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h"
258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
27e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h"
28ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h"
29bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h"
3120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h"
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36bd3606426d389370616126af969904ec493cb105Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts,
37fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner                             llvm::Module &M, const llvm::TargetData &TD,
38bd3606426d389370616126af969904ec493cb105Chris Lattner                             Diagnostic &diags)
39bd3606426d389370616126af969904ec493cb105Chris Lattner  : BlockModule(C, M, TD, Types, *this), Context(C),
40bd3606426d389370616126af969904ec493cb105Chris Lattner    Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M),
412a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump    TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0),
422a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump    MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
43208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
443c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  if (!Features.ObjC1)
453c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = 0;
463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (!Features.NeXTRuntime)
473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateGNUObjCRuntime(*this);
483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (Features.ObjCNonFragileABI)
493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else
513c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacObjCRuntime(*this);
52e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
53e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  // If debug info generation is enabled, create the CGDebugInfo object.
54bd3606426d389370616126af969904ec493cb105Chris Lattner  DebugInfo = CompileOpts.DebugInfo ? new CGDebugInfo(this) : 0;
552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
562b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() {
58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete Runtime;
59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete DebugInfo;
60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek}
61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek
62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() {
6382227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner  EmitDeferred();
64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  if (Runtime)
65208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
66208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      AddGlobalCtor(ObjCInitFunction);
676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalCtors, "llvm.global_ctors");
686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalDtors, "llvm.global_dtors");
69532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  EmitAnnotations();
700269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitLLVMUsed();
71f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
73488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
742c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet.
7590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
7690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
7790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
7890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
79488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
8056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
812c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  std::string Msg = Type;
820a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
830a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    << Msg << S->getSourceRange();
842c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner}
8558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
86488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
87c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet.
8890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
8990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
9090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
9190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
92488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
9356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
94c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  std::string Msg = Type;
950a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
96c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
97c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
9804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarLangOptions::VisibilityMode
9904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const {
10004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
10104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    if (VD->getStorageClass() == VarDecl::PrivateExtern)
10204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Hidden;
10304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
10404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) {
10504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    switch (attr->getVisibility()) {
10604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    default: assert(0 && "Unknown visibility!");
10704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    case VisibilityAttr::DefaultVisibility:
10804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Default;
10904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    case VisibilityAttr::HiddenVisibility:
11004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Hidden;
11104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    case VisibilityAttr::ProtectedVisibility:
11204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Protected;
11304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    }
1147e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  }
1156ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar
11604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  return getLangOptions().getVisibilityMode();
1174f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman}
1184f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
11904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbarvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
12004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                        const Decl *D) const {
12104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  // Internal definitions always have default visibility.
122df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner  if (GV->hasLocalLinkage()) {
1237e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1246ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar    return;
1257e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  }
1266ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar
12704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  switch (getDeclVisibilityMode(D)) {
1287cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian  default: assert(0 && "Unknown visibility!");
12904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Default:
13004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
13104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Hidden:
13204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
13304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Protected:
13404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1357cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian  }
1367cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian}
1377cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian
1385f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration.
1395f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1405f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an
141c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name.  Otherwise, returns
142c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name.
1435f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1446ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) {
145c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  // In C, functions with no attributes never need to be mangled. Fastpath them.
146c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) {
147c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner    assert(ND->getIdentifier() && "Attempt to mangle unnamed decl.");
1483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return ND->getNameAsCString();
149c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  }
150c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner
1516ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::SmallString<256> Name;
1526ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::raw_svector_ostream Out(Name);
153fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  if (!mangleName(ND, Context, Out)) {
154fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar    assert(ND->getIdentifier() && "Attempt to mangle unnamed decl.");
1553c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return ND->getNameAsCString();
156fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  }
1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1586ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  Name += '\0';
15995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  return UniqueMangledName(Name.begin(), Name.end());
16095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson}
16195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
16295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlssonconst char *CodeGenModule::UniqueMangledName(const char *NameStart,
16395d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson                                             const char *NameEnd) {
16495d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!");
16595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
16695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData();
1675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor}
1685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1696d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before
1706d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs.
1716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
17249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
1736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalCtors.push_back(std::make_pair(Ctor, Priority));
1746d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
1756d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
1766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called
1776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded.
1786bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
17949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
1806bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalDtors.push_back(std::make_pair(Dtor, Priority));
1816bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
1826bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
1846bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Ctor function type is void()*.
1856bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::FunctionType* CtorFTy =
1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::FunctionType::get(llvm::Type::VoidTy,
1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            std::vector<const llvm::Type*>(),
1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            false);
1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Get the type of a ctor entry, { i32, void ()* }.
192572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::StructType* CtorStructTy =
1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::StructType::get(llvm::Type::Int32Ty,
1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                          llvm::PointerType::getUnqual(CtorFTy), NULL);
1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Construct the constructor and destructor arrays.
1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  std::vector<llvm::Constant*> Ctors;
1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    std::vector<llvm::Constant*> S;
2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false));
2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  if (!Ctors.empty()) {
2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    new llvm::GlobalVariable(AT, false,
208572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             llvm::GlobalValue::AppendingLinkage,
2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             llvm::ConstantArray::get(AT, Ctors),
2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             GlobalName,
211572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             &TheModule);
2126d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  }
2136d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
2146d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
215532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() {
216532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  if (Annotations.empty())
217532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman    return;
218532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
219532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create a new global variable for the ConstantStruct in the Module.
220532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::Constant *Array =
221532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
222532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                                                Annotations.size()),
223532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           Annotations);
224532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::GlobalValue *gv =
225532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  new llvm::GlobalVariable(Array->getType(), false,
226532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           llvm::GlobalValue::AppendingLinkage, Array,
227532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           "llvm.global.annotations", &TheModule);
228532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  gv->setSection("llvm.metadata");
229532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman}
230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
23186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattnerstatic CodeGenModule::GVALinkage
23286daeee2d4aa6523679f07f27a826bf4c42ca95dChris LattnerGetLinkageForFunction(const FunctionDecl *FD, const LangOptions &Features) {
2337c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // "static" and attr(always_inline) functions get internal linkage.
2347c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (FD->getStorageClass() == FunctionDecl::Static ||
2357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      FD->hasAttr<AlwaysInlineAttr>())
2367c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    return CodeGenModule::GVA_Internal;
2377c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
23886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  if (!FD->isInline())
239cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    return CodeGenModule::GVA_StrongExternal;
24086daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner
24186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  // If the inline function explicitly has the GNU inline attribute on it, then
242cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  // force to GNUC semantics (which is strong external), regardless of language.
24386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  if (FD->hasAttr<GNUCInlineAttr>())
244cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    return CodeGenModule::GVA_StrongExternal;
245cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner
24686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  // The definition of inline changes based on the language.  Note that we
24786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  // have already handled "static inline" above, with the GVA_Internal case.
248cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  if (Features.CPlusPlus)  // inline and extern inline.
24986daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    return CodeGenModule::GVA_CXXInline;
25086daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner
251cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  if (FD->getStorageClass() == FunctionDecl::Extern) {
252cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    // extern inline in C99 is a strong definition. In C89, it is extern inline.
253cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    if (Features.C99)
254cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      return CodeGenModule::GVA_StrongExternal;
255cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner
256cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    // In C89 mode, an 'extern inline' works like a C99 inline function.
257cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    return CodeGenModule::GVA_C99Inline;
258cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  }
259cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner
26086daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  if (Features.C99)
26186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    return CodeGenModule::GVA_C99Inline;
26286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner
26386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  // Otherwise, this is the GNU inline extension in K&R and GNU C89 mode.
264cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  return CodeGenModule::GVA_StrongExternal;
2657c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar}
2667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
2677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar/// SetFunctionDefinitionAttributes - Set attributes for a global.
268b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar///
269b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar/// FIXME: This is currently only done for aliases and functions, but
270b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar/// not for variables (these details are set in
271b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar/// EmitGlobalVarDefinition for variables).
2727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
2737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                                    llvm::GlobalValue *GV) {
27486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  GVALinkage Linkage = GetLinkageForFunction(D, Features);
2757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
27655d6f50b23a1fd0a04b568787a25beb7537e6c9bDaniel Dunbar  if (Linkage == GVA_Internal) {
2779f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
2789f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner  } else if (D->hasAttr<DLLExportAttr>()) {
2797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GV->setLinkage(llvm::Function::DLLExportLinkage);
28044b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner  } else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) {
28144b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner    GV->setLinkage(llvm::Function::WeakAnyLinkage);
282cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  } else if (Linkage == GVA_C99Inline) {
283cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    // In C99 mode, 'inline' functions are guaranteed to have a strong
284cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    // definition somewhere else, so we can use available_externally linkage.
285d9d049a93c55624908e81cf3927b7905efeba05fChris Lattner    GV->setLinkage(llvm::Function::AvailableExternallyLinkage);
28686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner  } else if (Linkage == GVA_CXXInline) {
28786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // In C++, the compiler has to emit a definition in every translation unit
28886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // that references the function.  We should use linkonce_odr because
28986daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // a) if all references in this translation unit are optimized away, we
29086daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // don't need to codegen it.  b) if the function persists, it needs to be
29186daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // merged with other definitions. c) C++ has the ODR, so we know the
29286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // definition is dependable.
29386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    GV->setLinkage(llvm::Function::LinkOnceODRLinkage);
2947c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  } else {
295cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    assert(Linkage == GVA_StrongExternal);
296cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    // Otherwise, we have strong external linkage.
2977c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GV->setLinkage(llvm::Function::ExternalLinkage);
2980dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  }
299d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
3007c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GV);
301d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes}
302d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
3037dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
3047dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar                                              const CGFunctionInfo &Info,
3057dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar                                              llvm::Function *F) {
306761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  AttributeListType AttributeList;
30788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  ConstructAttributeList(Info, D, AttributeList);
308c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman
309761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
310761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                        AttributeList.size()));
311ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
312ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  // Set the appropriate calling convention for the Function.
313b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (D->hasAttr<FastCallAttr>())
314f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_FastCall);
315f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov
316b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (D->hasAttr<StdCallAttr>())
317f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_StdCall);
318f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
319f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
3207c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
3217c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                                           llvm::Function *F) {
32274ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar  if (!Features.Exceptions && !Features.ObjCNonFragileABI)
323f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar    F->addFnAttr(llvm::Attribute::NoUnwind);
324af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
325b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (D->hasAttr<AlwaysInlineAttr>())
326af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar    F->addFnAttr(llvm::Attribute::AlwaysInline);
32781ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson
328b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (D->hasAttr<NoinlineAttr>())
32981ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    F->addFnAttr(llvm::Attribute::NoInline);
330f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
331f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
3327c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetCommonAttributes(const Decl *D,
3337c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                        llvm::GlobalValue *GV) {
3347c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  setGlobalVisibility(GV, D);
3357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3367c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (D->hasAttr<UsedAttr>())
3377c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    AddUsedGlobal(GV);
3387c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3397c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
3407c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GV->setSection(SA->getName());
3417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar}
3427c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3430e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
3440e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar                                                  llvm::Function *F,
3450e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar                                                  const CGFunctionInfo &FI) {
3460e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetLLVMFunctionAttributes(D, FI, F);
3470e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetLLVMFunctionAttributesForDefinition(D, F);
3487c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3497c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  F->setLinkage(llvm::Function::InternalLinkage);
3507c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3510e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetCommonAttributes(D, F);
352f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
353f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
354f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
355f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                                          llvm::Function *F) {
3567dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar  SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
35745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
3587c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // Only a few attributes are set on declarations; these may later be
3597c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // overridden by a definition.
3607c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3617c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (FD->hasAttr<DLLImportAttr>()) {
3627c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setLinkage(llvm::Function::DLLImportLinkage);
3637c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  } else if (FD->hasAttr<WeakAttr>() || FD->hasAttr<WeakImportAttr>()) {
3647c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    // "extern_weak" is overloaded in LLVM; we probably should have
3657c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    // separate linkage types for this.
3667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setLinkage(llvm::Function::ExternalWeakLinkage);
3677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  } else {
3687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setLinkage(llvm::Function::ExternalLinkage);
3697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  }
3707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
3727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setSection(SA->getName());
373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar}
374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
3750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
3760269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  assert(!GV->isDeclaration() &&
3770269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         "Only globals with definition can force usage.");
37835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  LLVMUsed.push_back(GV);
3790269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
3800269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
3810269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() {
3820269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Don't create llvm.used if there is no need.
3830269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  if (LLVMUsed.empty())
3840269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    return;
3850269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
38635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
38735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, LLVMUsed.size());
38835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner
38935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  // Convert LLVMUsed to what ConstantArray needs.
39035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  std::vector<llvm::Constant*> UsedArray;
39135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  UsedArray.resize(LLVMUsed.size());
39235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
39335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner    UsedArray[i] =
39435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner     llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), i8PTy);
39535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  }
39635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner
3970269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::GlobalVariable *GV =
3980269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    new llvm::GlobalVariable(ATy, false,
3990269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::GlobalValue::AppendingLinkage,
40035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner                             llvm::ConstantArray::get(ATy, UsedArray),
4010269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             "llvm.used", &getModule());
4020269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4030269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  GV->setSection("llvm.metadata");
4040269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
4050269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4060269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() {
40767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Emit code for any potentially referenced deferred decls.  Since a
40867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // previously unused static decl may become used during the generation of code
40967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // for a static function, iterate until no  changes are made.
41067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  while (!DeferredDeclsToEmit.empty()) {
41167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    const ValueDecl *D = DeferredDeclsToEmit.back();
41267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.pop_back();
41367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
41467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // The mangled name for the decl must have been emitted in GlobalDeclMap.
41567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Look it up to see if it was defined with a stronger definition (e.g. an
41667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // extern inline function with a strong function redefinition).  If so,
41767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // just ignore the deferred decl.
41867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)];
41967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    assert(CGRef && "Deferred decl wasn't referenced?");
420b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
42167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    if (!CGRef->isDeclaration())
42267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      continue;
42367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
42467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Otherwise, emit the definition and move on to the next one.
42567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    EmitGlobalDefinition(D);
42667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
42703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
4286463ef4caf2fb576d38b1b2e15a29ac9f2abb3e7Daniel Dunbar  // Emit any tentative definitions, in reverse order so the most
4296463ef4caf2fb576d38b1b2e15a29ac9f2abb3e7Daniel Dunbar  // important (merged) decl will be seen and emitted first.
4306463ef4caf2fb576d38b1b2e15a29ac9f2abb3e7Daniel Dunbar  for (std::vector<const VarDecl*>::reverse_iterator
4316463ef4caf2fb576d38b1b2e15a29ac9f2abb3e7Daniel Dunbar         it = TentativeDefinitions.rbegin(), ie = TentativeDefinitions.rend();
4326463ef4caf2fb576d38b1b2e15a29ac9f2abb3e7Daniel Dunbar       it != ie; ++it)
43303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    EmitTentativeDefinition(*it);
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4368bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
4378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue.  The annotation struct is
4388bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
4393c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated.  The second field is the constant string
4408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation.  The third field is a constant
4418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit.  The fourth field is
4428bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration.
4438bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4448bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc
4458bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///        appears to.
4468bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4478bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
4488bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                const AnnotateAttr *AA,
4498bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                unsigned LineNo) {
4508bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Module *M = &getModule();
4518bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4528bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // get [N x i8] constants for the annotation string, and the filename string
4538bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // which are the 2nd and 3rd elements of the global annotation structure.
4548bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
4558bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true);
4568bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(),
4578bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                  true);
4588bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4598bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Get the two global values corresponding to the ConstantArrays we just
4608bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // created to hold the bytes of the strings.
4618e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  const char *StringPrefix = getContext().Target.getStringSymbolPrefix(true);
4628bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *annoGV =
4638bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(anno->getType(), false,
4648bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, anno,
4658e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                           GV->getName() + StringPrefix, M);
4668bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // translation unit name string, emitted into the llvm.metadata section.
4678bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *unitGV =
4688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(unit->getType(), false,
4698e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                           llvm::GlobalValue::InternalLinkage, unit,
4708e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                           StringPrefix, M);
4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
47257d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar  // Create the ConstantStruct for the global annotation.
4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *Fields[4] = {
4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(GV, SBP),
4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(annoGV, SBP),
4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(unitGV, SBP),
4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  };
4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return llvm::ConstantStruct::get(Fields, 4, false);
4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
48273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
4835c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // Never defer when EmitAllDecls is specified or the decl has
4845c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // attribute used.
485b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>())
48673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    return false;
487bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
488bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
48973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Constructors and destructors should never be deferred.
490b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
49173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
49273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
49386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    GVALinkage Linkage = GetLinkageForFunction(FD, Features);
494dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner
495dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    // static, static inline, always_inline, and extern inline functions can
49686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    // always be deferred.  Normal inline functions can be deferred in C99/C++.
497cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
498cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner        Linkage == GVA_CXXInline)
499dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner      return true;
500dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    return false;
50173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  }
502dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner
503dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner  const VarDecl *VD = cast<VarDecl>(Global);
504dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner  assert(VD->isFileVarDecl() && "Invalid decl");
505dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner  return VD->getStorageClass() == VarDecl::Static;
50673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar}
50773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
50873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) {
509bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // If this is an alias definition (which otherwise looks like a declaration)
510bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // emit it now.
511b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  if (Global->hasAttr<AliasAttr>())
512bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    return EmitAliasDefinition(Global);
513219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
51467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Ignore declarations, they will be emitted on their first use.
5155e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
51673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
51773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (!FD->isThisDeclarationADefinition())
51873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
5190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  } else {
5200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
521bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
522bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
52303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // If this isn't a definition, defer code generation.
52403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    if (!VD->getInit()) {
52503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      // If this is a tentative definition, remember it so that we can
52603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      // emit the common definition if needed. It is important to
52703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      // defer tentative definitions, since they may have incomplete
52803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      // type.
52903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      if (!VD->hasExternalStorage())
53003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar        TentativeDefinitions.push_back(VD);
53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
53203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    }
5334c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
5344c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
53567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Defer code generation when possible if this is a static definition, inline
53667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // function etc.  These we only want to emit if they are used.
53773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  if (MayDeferGeneration(Global)) {
53867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // If the value has already been used, add it directly to the
53967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // DeferredDeclsToEmit list.
54067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    const char *MangledName = getMangledName(Global);
54167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    if (GlobalDeclMap.count(MangledName))
54267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      DeferredDeclsToEmit.push_back(Global);
54367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    else {
54467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      // Otherwise, remember that we saw a deferred decl with this name.  The
54567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      // first use of the mangled name will cause it to move into
54667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      // DeferredDeclsToEmit.
54767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      DeferredDecls[MangledName] = Global;
54867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
549bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    return;
550bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
551bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
552bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  // Otherwise emit the definition.
553bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  EmitGlobalDefinition(Global);
5544c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
5554c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
556bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) {
557bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
558bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalFunctionDefinition(FD);
559bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
560bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalVarDefinition(VD);
561bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else {
562bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(0 && "Invalid argument to EmitGlobalDefinition()");
563bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
564bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
565bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
56674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
56774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there
56874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially
56974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type.
57074391b48b4791cded373683a3baf67314f358d50Chris Lattner///
57174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this.  This is used
57274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created.
57374391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName,
57474391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                       const llvm::Type *Ty,
57574391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                       const FunctionDecl *D) {
5760558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // Lookup the entry, lazily creating it if necessary.
5770558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
5780558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (Entry) {
5790558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    if (Entry->getType()->getElementType() == Ty)
5800558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner      return Entry;
5810558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
5820558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    // Make sure the result is of the correct type.
5830558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
5840558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    return llvm::ConstantExpr::getBitCast(Entry, PTy);
5850558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
5860558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
58767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // This is the first use or definition of a mangled name.  If there is a
58867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // deferred decl with this name, remember that we need to emit it at the end
58967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // of the file.
59067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI =
59174391b48b4791cded373683a3baf67314f358d50Chris Lattner  DeferredDecls.find(MangledName);
59267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (DDI != DeferredDecls.end()) {
59367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
59467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // list, and remove it from DeferredDecls (since we don't need it anymore).
59567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.push_back(DDI->second);
59667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDecls.erase(DDI);
59767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
59867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
5990558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // This function doesn't have a complete type (for example, the return
6000558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // type is an incomplete struct). Use a fake type instead, and make
6010558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // sure not to try to set attributes.
6020558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  bool ShouldSetAttributes = true;
6030558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (!isa<llvm::FunctionType>(Ty)) {
6040558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Ty = llvm::FunctionType::get(llvm::Type::VoidTy,
6050558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                 std::vector<const llvm::Type*>(), false);
6060558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    ShouldSetAttributes = false;
6070558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
6080558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty),
6090558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                             llvm::Function::ExternalLinkage,
610d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner                                             "", &getModule());
611d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner  F->setName(MangledName);
61274391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (D && ShouldSetAttributes)
6130558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    SetFunctionAttributes(D, F);
6140558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  Entry = F;
6150558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  return F;
6160558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner}
6170558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
61874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function.  If Ty is
61974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to
62074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function).
62174391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D,
62274391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                 const llvm::Type *Ty) {
62374391b48b4791cded373683a3baf67314f358d50Chris Lattner  // If there was no specific requested type, just convert it now.
62474391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (!Ty)
62574391b48b4791cded373683a3baf67314f358d50Chris Lattner    Ty = getTypes().ConvertType(D->getType());
62674391b48b4791cded373683a3baf67314f358d50Chris Lattner  return GetOrCreateLLVMFunction(getMangledName(D), Ty, D);
62774391b48b4791cded373683a3baf67314f358d50Chris Lattner}
62877ba708819285931932ecd33691a672bb59d221aEli Friedman
62974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified
63074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name.
63174391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *
63274391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
63374391b48b4791cded373683a3baf67314f358d50Chris Lattner                                     const char *Name) {
63474391b48b4791cded373683a3baf67314f358d50Chris Lattner  // Convert Name to be a uniqued string from the IdentifierInfo table.
63574391b48b4791cded373683a3baf67314f358d50Chris Lattner  Name = getContext().Idents.get(Name).getName();
63674391b48b4791cded373683a3baf67314f358d50Chris Lattner  return GetOrCreateLLVMFunction(Name, FTy, 0);
63774391b48b4791cded373683a3baf67314f358d50Chris Lattner}
638bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
63974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
64074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type.  If there
64174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially
64274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type.
64374391b48b4791cded373683a3baf67314f358d50Chris Lattner///
64474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this.  This is used
64574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created.
64674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName,
64774391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                     const llvm::PointerType*Ty,
64874391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                     const VarDecl *D) {
6493c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
6505d4f5c724533b994de05df49ae259120482ec366Chris Lattner  llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
65199b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  if (Entry) {
65274391b48b4791cded373683a3baf67314f358d50Chris Lattner    if (Entry->getType() == Ty)
653570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      return Entry;
654570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
65599b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner    // Make sure the result is of the correct type.
65674391b48b4791cded373683a3baf67314f358d50Chris Lattner    return llvm::ConstantExpr::getBitCast(Entry, Ty);
65799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  }
65867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
65967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // This is the first use or definition of a mangled name.  If there is a
66067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // deferred decl with this name, remember that we need to emit it at the end
66167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // of the file.
66267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI =
66367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDecls.find(MangledName);
66467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (DDI != DeferredDecls.end()) {
66567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
66667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // list, and remove it from DeferredDecls (since we don't need it anymore).
66767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.push_back(DDI->second);
66867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDecls.erase(DDI);
66967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
67067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
67199b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  llvm::GlobalVariable *GV =
67274391b48b4791cded373683a3baf67314f358d50Chris Lattner    new llvm::GlobalVariable(Ty->getElementType(), false,
67399b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner                             llvm::GlobalValue::ExternalLinkage,
674d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner                             0, "", &getModule(),
67556ebe5082da7411fb37479e230b52735f77cff35Eli Friedman                             false, Ty->getAddressSpace());
676d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner  GV->setName(MangledName);
67749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
67899b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  // Handle things which are present even on external declarations.
67974391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (D) {
68074391b48b4791cded373683a3baf67314f358d50Chris Lattner    // FIXME: This code is overly simple and should be merged with
68174391b48b4791cded373683a3baf67314f358d50Chris Lattner    // other global handling.
68274391b48b4791cded373683a3baf67314f358d50Chris Lattner    GV->setConstant(D->getType().isConstant(Context));
68349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
68474391b48b4791cded373683a3baf67314f358d50Chris Lattner    // FIXME: Merge with other attribute handling code.
68574391b48b4791cded373683a3baf67314f358d50Chris Lattner    if (D->getStorageClass() == VarDecl::PrivateExtern)
68604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
68749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
688b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar    if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>())
68974391b48b4791cded373683a3baf67314f358d50Chris Lattner      GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
69056ebe5082da7411fb37479e230b52735f77cff35Eli Friedman
69156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman    GV->setThreadLocal(D->isThreadSpecified());
69274391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
69374391b48b4791cded373683a3baf67314f358d50Chris Lattner
69474391b48b4791cded373683a3baf67314f358d50Chris Lattner  return Entry = GV;
69574391b48b4791cded373683a3baf67314f358d50Chris Lattner}
696eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
697eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
69874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
69974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable.  If Ty is non-null and if the global doesn't exist,
70074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the
70174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be.
70274391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
70374391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                  const llvm::Type *Ty) {
70474391b48b4791cded373683a3baf67314f358d50Chris Lattner  assert(D->hasGlobalStorage() && "Not a global variable");
70574391b48b4791cded373683a3baf67314f358d50Chris Lattner  QualType ASTTy = D->getType();
70674391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (Ty == 0)
70774391b48b4791cded373683a3baf67314f358d50Chris Lattner    Ty = getTypes().ConvertTypeForMem(ASTTy);
70874391b48b4791cded373683a3baf67314f358d50Chris Lattner
70974391b48b4791cded373683a3baf67314f358d50Chris Lattner  const llvm::PointerType *PTy =
71074391b48b4791cded373683a3baf67314f358d50Chris Lattner    llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
71174391b48b4791cded373683a3baf67314f358d50Chris Lattner  return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D);
71274391b48b4791cded373683a3baf67314f358d50Chris Lattner}
7133f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar
71474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the
71574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name.
71674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *
71774391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
71874391b48b4791cded373683a3baf67314f358d50Chris Lattner                                     const char *Name) {
71974391b48b4791cded373683a3baf67314f358d50Chris Lattner  // Convert Name to be a uniqued string from the IdentifierInfo table.
72074391b48b4791cded373683a3baf67314f358d50Chris Lattner  Name = getContext().Idents.get(Name).getName();
72174391b48b4791cded373683a3baf67314f358d50Chris Lattner  return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0);
722bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
723bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
72403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
72503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  assert(!D->getInit() && "Cannot emit definite definitions here!");
72603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
72703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  // See if we have already defined this (as a variable), if so we do
72803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  // not need to do anything.
72903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  llvm::GlobalValue *GV = GlobalDeclMap[getMangledName(D)];
73003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  if (llvm::GlobalVariable *Var = dyn_cast_or_null<llvm::GlobalVariable>(GV))
73103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    if (Var->hasInitializer())
73203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      return;
73303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
73403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  EmitGlobalVarDefinition(D);
73503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar}
73603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
737bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
7388f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  llvm::Constant *Init = 0;
73977ba708819285931932ecd33691a672bb59d221aEli Friedman  QualType ASTTy = D->getType();
740b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner
7418f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  if (D->getInit() == 0) {
742cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // This is a tentative definition; tentative definitions are
74303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // implicitly initialized with { 0 }.
74403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    //
74503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // Note that tentative definitions are only emitted at the end of
74603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // a translation unit, so they should never have incomplete
74703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // type. In addition, EmitTentativeDefinition makes sure that we
74803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // never attempt to emit a tentative definition if a real one
74903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // exists. A use may still exists, however, so we still may need
75003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // to do a RAUW.
75103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
75203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    Init = llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(ASTTy));
75377ba708819285931932ecd33691a672bb59d221aEli Friedman  } else {
754e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson    Init = EmitConstantExpr(D->getInit(), D->getType());
7556e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    if (!Init) {
756232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar      ErrorUnsupported(D, "static initializer");
7576e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      QualType T = D->getInit()->getType();
7586e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      Init = llvm::UndefValue::get(getTypes().ConvertType(T));
7596e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    }
7608f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  }
7618e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel
7622d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner  const llvm::Type* InitType = Init->getType();
763570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
764570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
765570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // Strip off a bitcast if we got one back.
766570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
767570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    assert(CE->getOpcode() == llvm::Instruction::BitCast);
768570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Entry = CE->getOperand(0);
769570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  }
7703c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
771570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // Entry is now either a Function or GlobalVariable.
772570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry);
773570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
774570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // We have a definition after a declaration with the wrong type.
775570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // We must make a new GlobalVariable* and update everything that used OldGV
776570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // (a declaration or tentative definition) with the new GlobalVariable*
777570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // (which will be a definition).
778570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  //
779570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // This happens if there is a prototype for a global (e.g.
780570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // "extern int x[];") and then a definition of a different type (e.g.
781570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // "int x[10];"). This also happens when an initializer has a different type
782570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // from the type of the global (this happens with unions).
783570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (GV == 0 ||
784570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      GV->getType()->getElementType() != InitType ||
785570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) {
786570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
787570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    // Remove the old entry from GlobalDeclMap so that we'll create a new one.
788570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    GlobalDeclMap.erase(getMangledName(D));
789232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
790570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    // Make a new global with the correct type, this is now guaranteed to work.
791570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
7920558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    GV->takeName(cast<llvm::GlobalValue>(Entry));
7930558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
79477ba708819285931932ecd33691a672bb59d221aEli Friedman    // Replace all uses of the old global with the new global
79577ba708819285931932ecd33691a672bb59d221aEli Friedman    llvm::Constant *NewPtrForOldDecl =
796570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner        llvm::ConstantExpr::getBitCast(GV, Entry->getType());
797570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Entry->replaceAllUsesWith(NewPtrForOldDecl);
79877ba708819285931932ecd33691a672bb59d221aEli Friedman
79977ba708819285931932ecd33691a672bb59d221aEli Friedman    // Erase the old global, since it is no longer used.
800570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    cast<llvm::GlobalValue>(Entry)->eraseFromParent();
80177ba708819285931932ecd33691a672bb59d221aEli Friedman  }
80277ba708819285931932ecd33691a672bb59d221aEli Friedman
8038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
8048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    SourceManager &SM = Context.getSourceManager();
8058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    AddAnnotation(EmitAnnotateAttr(GV, AA,
806f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner                              SM.getInstantiationLineNumber(D->getLocation())));
8078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
8088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
80988a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  GV->setInitializer(Init);
810b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes  GV->setConstant(D->getType().isConstant(Context));
8110de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman  GV->setAlignment(getContext().getDeclAlignInBytes(D));
81208d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman
81388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  // Set the llvm linkage type as appropriate.
8148fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  if (D->getStorageClass() == VarDecl::Static)
8158fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
816b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  else if (D->hasAttr<DLLImportAttr>())
817ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLImportLinkage);
818b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  else if (D->hasAttr<DLLExportAttr>())
819ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLExportLinkage);
820b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar  else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>())
821286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump    GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
82204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  else if (!CompileOpts.NoCommon &&
82304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar           (!D->hasExternalStorage() && !D->getInit()))
82404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
8257e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  else
82604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
8277e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar
8287c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GV);
82904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
830686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  // Emit global variable debug information.
8312d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner  if (CGDebugInfo *DI = getDebugInfo()) {
83266031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar    DI->setLocation(D->getLocation());
833686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta    DI->EmitGlobalVariable(GV, D);
834686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  }
83588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner}
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
837bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
838bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) {
8392b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  const llvm::FunctionType *Ty;
8402b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson
8412b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
8422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    bool isVariadic = D->getType()->getAsFunctionProtoType()->isVariadic();
8432b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson
8442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic);
8452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  } else {
8462b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType()));
8472b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson
8482b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    // As a special case, make sure that definitions of K&R function
8492b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    // "type foo()" aren't declared as varargs (which forces the backend
8502b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    // to do unnecessary work).
8512b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (D->getType()->isFunctionNoProtoType()) {
8522b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      assert(Ty->isVarArg() && "Didn't lower type as expected");
8532b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Due to stret, the lowered function could have arguments.
8542b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Just create the same type as was lowered by ConvertType
8552b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // but strip off the varargs bit.
8562b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end());
8572b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false);
8582b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
859ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner  }
860d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
86134809507232bc4c3c4840c7d092c7440219fddafChris Lattner  // Get or create the prototype for teh function.
8620558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::Constant *Entry = GetAddrOfFunction(D, Ty);
86334809507232bc4c3c4840c7d092c7440219fddafChris Lattner
8640558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // Strip off a bitcast if we got one back.
8650558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
8660558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    assert(CE->getOpcode() == llvm::Instruction::BitCast);
8670558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Entry = CE->getOperand(0);
8680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
8690558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
8700558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
8710558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) {
87242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar    // If the types mismatch then we have to rewrite the definition.
8730558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    assert(cast<llvm::GlobalValue>(Entry)->isDeclaration() &&
8740558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner           "Shouldn't replace non-declaration");
87534809507232bc4c3c4840c7d092c7440219fddafChris Lattner
87662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // F is the Function* for the one with the wrong type, we must make a new
87762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Function* and update everything that used F (a declaration) with the new
87862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Function* (which will be a definition).
87962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    //
88062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // This happens if there is a prototype for a function
88162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // (e.g. "int f()") and then a definition of a different type
88262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // (e.g. "int f(int x)").  Start by making a new function of the
88362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // correct type, RAUW, then steal the name.
88434809507232bc4c3c4840c7d092c7440219fddafChris Lattner    GlobalDeclMap.erase(getMangledName(D));
8850558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(D, Ty));
8860558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    NewFn->takeName(cast<llvm::GlobalValue>(Entry));
88762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
88862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Replace uses of F with the Function we will endow with a body.
88962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    llvm::Constant *NewPtrForOldDecl =
8900558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner      llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
8910558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Entry->replaceAllUsesWith(NewPtrForOldDecl);
89262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
89362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Ok, delete the old function now, which is dead.
8940558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    cast<llvm::GlobalValue>(Entry)->eraseFromParent();
89562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
8960558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Entry = NewFn;
897bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
8980558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
8990558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::Function *Fn = cast<llvm::Function>(Entry);
900bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
901219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  CodeGenFunction(*this).GenerateCode(D, Fn);
9026379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar
9037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetFunctionDefinitionAttributes(D, Fn);
9047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetLLVMFunctionAttributesForDefinition(D, Fn);
905219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
90634809507232bc4c3c4840c7d092c7440219fddafChris Lattner  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
907219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalCtor(Fn, CA->getPriority());
90834809507232bc4c3c4840c7d092c7440219fddafChris Lattner  if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
909219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalDtor(Fn, DA->getPriority());
910bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
911bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
912bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) {
913bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  const AliasAttr *AA = D->getAttr<AliasAttr>();
914bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  assert(AA && "Not an alias?");
915bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
916bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
917bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
918bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Unique the name through the identifier table.
919bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  const char *AliaseeName = AA->getAliasee().c_str();
920bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  AliaseeName = getContext().Idents.get(AliaseeName).getName();
921bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
922bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Create a reference to the named value.  This ensures that it is emitted
923bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // if a deferred decl.
924bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  llvm::Constant *Aliasee;
925bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (isa<llvm::FunctionType>(DeclTy))
926bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, 0);
927bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  else
928bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Aliasee = GetOrCreateLLVMGlobal(AliaseeName,
929bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                                    llvm::PointerType::getUnqual(DeclTy), 0);
930bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
931bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Create the new alias itself, but don't set a name yet.
932bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  llvm::GlobalValue *GA =
933bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    new llvm::GlobalAlias(Aliasee->getType(),
934bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                          llvm::Function::ExternalLinkage,
935bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                          "", Aliasee, &getModule());
936bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
937bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // See if there is already something with the alias' name in the module.
938bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  const char *MangledName = getMangledName(D);
939bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
940bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
941bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (Entry && !Entry->isDeclaration()) {
942bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // If there is a definition in the module, then it wins over the alias.
943bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // This is dubious, but allow it to be safe.  Just ignore the alias.
944bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    GA->eraseFromParent();
945bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    return;
946bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
947bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
948bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (Entry) {
949bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // If there is a declaration in the module, then we had an extern followed
950bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // by the alias, as in:
951bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   extern int test6();
952bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   ...
953bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   int test6() __attribute__((alias("test7")));
954bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //
955bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // Remove it and replace uses of it with the alias.
956bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
957bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
958bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                                                          Entry->getType()));
959bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Entry->eraseFromParent();
960bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
961bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
962bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Now we know that there is no conflict, set the name.
963bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  Entry = GA;
964bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  GA->setName(MangledName);
965bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
9667c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // Set attributes which are particular to an alias; this is a
9677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // specialization of the attributes which may be set on a global
9687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // variable/function.
9697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (D->hasAttr<DLLExportAttr>()) {
9707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
9717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      // The dllexport attribute is ignored for undefined symbols.
9727297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor      if (FD->getBody(getContext()))
9737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        GA->setLinkage(llvm::Function::DLLExportLinkage);
9747c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    } else {
9757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      GA->setLinkage(llvm::Function::DLLExportLinkage);
9767c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    }
9777c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  } else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) {
9787c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GA->setLinkage(llvm::Function::WeakAnyLinkage);
9797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  }
9807c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
9817c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GA);
982bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner}
983bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
984b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like
985b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf".
986c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
9873e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
9883e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor          Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
9893e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor         "isn't a lib fn");
990bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9913e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  // Get the name, skip over the __builtin_ prefix (if necessary).
9923e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
9933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  if (Context.BuiltinInfo.isLibFunction(BuiltinID))
9943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor    Name += 10;
995bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
996bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // Get the type for the builtin.
997370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  Builtin::Context::GetBuiltinTypeError Error;
998370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error);
999370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  assert(Error == Builtin::Context::GE_None && "Can't get builtin type");
1000370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor
1001bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  const llvm::FunctionType *Ty =
1002bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    cast<llvm::FunctionType>(getTypes().ConvertType(Type));
1003bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
1004b808c952bbff821dce727dd801a1098d64394f98Chris Lattner  // Unique the name through the identifier table.
1005b808c952bbff821dce727dd801a1098d64394f98Chris Lattner  Name = getContext().Idents.get(Name).getName();
1006bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: param attributes for sext/zext etc.
1007b808c952bbff821dce727dd801a1098d64394f98Chris Lattner  return GetOrCreateLLVMFunction(Name, Ty, 0);
1008bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner}
1009bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
10107acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
10117acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                            unsigned NumTys) {
10127acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return llvm::Intrinsic::getDeclaration(&getModule(),
10137acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
10147acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner}
1015bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() {
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (MemCpyFn) return MemCpyFn;
10184e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
10194e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1);
10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1021c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
10220c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() {
10230c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman  if (MemMoveFn) return MemMoveFn;
10244e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
10254e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1);
10260c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman}
10270c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman
102841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() {
102941ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  if (MemSetFn) return MemSetFn;
10304e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
10314e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1);
103241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio}
10337acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner
1034e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM,
1035e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                  std::vector<llvm::Constant*>& Fields,
103644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  FieldDecl *FieldD, FieldDecl *NextFieldD,
103744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  llvm::Constant* Field,
10383c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner                                  RecordDecl* RD, const llvm::StructType *STy) {
1039e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append the field.
1040e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  Fields.push_back(Field);
1041e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
104244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD);
1043e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1044e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  int NextStructFieldNo;
104544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (!NextFieldD) {
1046e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    NextStructFieldNo = STy->getNumElements();
1047e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  } else {
104844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD);
1049e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
1050e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1051e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append padding
1052e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) {
1053e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    llvm::Constant *C =
1054e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson      llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1));
1055e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1056e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    Fields.push_back(C);
1057e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
1058e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson}
1059e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1060bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule::
10618d4141f83d9de379547cf05bd75d4c6cf894b189Steve NaroffGetAddrOfConstantCFString(const StringLiteral *Literal) {
1062b59212a6e494d2c364b54462f545833902c29158Steve Naroff  std::string str;
1063271474e32f38cc5bd735c1006e499996cbc6e2d1Chris Lattner  unsigned StringLength = 0;
1064b59212a6e494d2c364b54462f545833902c29158Steve Naroff
1065e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff  bool isUTF16 = false;
1066e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff  if (Literal->containsNonAsciiOrNull()) {
1067e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    // Convert from UTF-8 to UTF-16.
1068e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    llvm::SmallVector<UTF16, 128> ToBuf(Literal->getByteLength());
1069e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    const UTF8 *FromPtr = (UTF8 *)Literal->getStrData();
1070e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    UTF16 *ToPtr = &ToBuf[0];
1071e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff
1072e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    ConversionResult Result;
1073e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    Result = ConvertUTF8toUTF16(&FromPtr, FromPtr+Literal->getByteLength(),
1074e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff                                &ToPtr, ToPtr+Literal->getByteLength(),
1075e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff                                strictConversion);
1076aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff    if (Result == conversionOK) {
1077aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      // FIXME: Storing UTF-16 in a C string is a hack to test Unicode strings
1078aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      // without doing more surgery to this routine. Since we aren't explicitly
1079aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      // checking for endianness here, it's also a bug (when generating code for
1080aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      // a target that doesn't match the host endianness). Modeling this as an
1081aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      // i16 array is likely the cleanest solution.
1082aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      StringLength = ToPtr-&ToBuf[0];
1083aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      str.assign((char *)&ToBuf[0], StringLength*2);// Twice as many UTF8 chars.
1084aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      isUTF16 = true;
1085aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff    } else if (Result == sourceIllegal) {
1086fd942628abfe30e30427875db953222ae99b4325Steve Naroff      // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string.
1087aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      str.assign(Literal->getStrData(), Literal->getByteLength());
1088aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      StringLength = str.length();
1089aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff    } else
1090aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff      assert(Result == conversionOK && "UTF-8 to UTF-16 conversion failed");
1091b59212a6e494d2c364b54462f545833902c29158Steve Naroff
1092b59212a6e494d2c364b54462f545833902c29158Steve Naroff  } else {
1093b59212a6e494d2c364b54462f545833902c29158Steve Naroff    str.assign(Literal->getStrData(), Literal->getByteLength());
1094b59212a6e494d2c364b54462f545833902c29158Steve Naroff    StringLength = str.length();
1095e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff  }
1096c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::StringMapEntry<llvm::Constant *> &Entry =
1097c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
1098c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
10998e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (llvm::Constant *C = Entry.getValue())
11008e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    return C;
1101c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
11023e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
11033e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zeros[] = { Zero, Zero };
1104c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1105c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (!CFConstantStringClassRef) {
1106c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
1107c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    Ty = llvm::ArrayType::get(Ty, 0);
11083e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
11093e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // FIXME: This is fairly broken if
11103e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // __CFConstantStringClassReference is already defined, in that it
11113e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // will get renamed and the user will most likely see an opaque
11123e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // error message. This is a general issue with relying on
11133e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // particular names.
11143e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    llvm::GlobalVariable *GV =
1115c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      new llvm::GlobalVariable(Ty, false,
1116c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalVariable::ExternalLinkage, 0,
1117c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               "__CFConstantStringClassReference",
1118c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               &getModule());
11193e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
11203e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // Decay array -> ptr
11213e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    CFConstantStringClassRef =
11223e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1123c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
1124c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1125e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  QualType CFTy = getContext().getCFConstantStringType();
1126e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl();
11273e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
1128e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  const llvm::StructType *STy =
1129e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1130e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1131e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  std::vector<llvm::Constant*> Fields;
11326ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  RecordDecl::field_iterator Field = CFRD->field_begin(getContext());
113344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1134c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Class pointer.
113544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *CurField = *Field++;
113644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *NextField = *Field++;
113744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
113844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        CFConstantStringClassRef, CFRD, STy);
1139c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1140c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Flags.
114144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
114244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
11433e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
114444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
1145e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff                        isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0)
1146e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff                                : llvm::ConstantInt::get(Ty, 0x07C8),
1147e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff                        CFRD, STy);
1148c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1149c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String pointer.
115044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
115144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
11523e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str);
1153a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar
1154a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  const char *Sect, *Prefix;
1155a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  bool isConstant;
1156a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  if (isUTF16) {
1157a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    Prefix = getContext().Target.getUnicodeStringSymbolPrefix();
1158a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    Sect = getContext().Target.getUnicodeStringSection();
1159a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    // FIXME: Why does GCC not set constant here?
1160a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    isConstant = false;
1161a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  } else {
1162a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    Prefix = getContext().Target.getStringSymbolPrefix(true);
1163a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    Sect = getContext().Target.getCFStringDataSection();
1164a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    // FIXME: -fwritable-strings should probably affect this, but we
1165a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    // are following gcc here.
1166a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    isConstant = true;
1167a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  }
11688e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  llvm::GlobalVariable *GV =
1169a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    new llvm::GlobalVariable(C->getType(), isConstant,
11708e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                             llvm::GlobalValue::InternalLinkage,
1171a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar                             C, Prefix, &getModule());
1172a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  if (Sect)
11738e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    GV->setSection(Sect);
1174a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  if (isUTF16) {
1175a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8;
1176a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    GV->setAlignment(Align);
1177a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  }
117844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
11798e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                        llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2),
1180e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                        CFRD, STy);
1181c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1182c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String length.
118344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
118444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = 0;
1185c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().LongTy);
118644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
1187b59212a6e494d2c364b54462f545833902c29158Steve Naroff                        llvm::ConstantInt::get(Ty, StringLength), CFRD, STy);
1188c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1189c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // The struct.
1190e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  C = llvm::ConstantStruct::get(STy, Fields);
11918e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  GV = new llvm::GlobalVariable(C->getType(), true,
11928e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                llvm::GlobalVariable::InternalLinkage, C,
11938e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                getContext().Target.getCFStringSymbolPrefix(),
11948e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                &getModule());
11958e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (const char *Sect = getContext().Target.getCFStringSection())
11968e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    GV->setSection(Sect);
11970c67829763b98bc670062b553897a851fab17401Anders Carlsson  Entry.setValue(GV);
11983e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
11990c67829763b98bc670062b553897a851fab17401Anders Carlsson  return GV;
1200c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson}
120145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
12026143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a
12031e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type.
12046143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
12051e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const char *StrData = E->getStrData();
12061e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  unsigned Len = E->getByteLength();
12071e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
12081e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const ConstantArrayType *CAT =
12091e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    getContext().getAsConstantArrayType(E->getType());
12101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  assert(CAT && "String isn't pointer or array!");
12111e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
1212dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  // Resize the string to the right size.
12131e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  std::string Str(StrData, StrData+Len);
12141e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  uint64_t RealLen = CAT->getSize().getZExtValue();
1215dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
1216dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  if (E->isWide())
1217dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    RealLen *= getContext().Target.getWCharWidth()/8;
1218dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
12191e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  Str.resize(RealLen, '\0');
12201e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
12211e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  return Str;
12221e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar}
12231e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
12246143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
12256143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal.
12266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant *
12276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
12286143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // FIXME: This can be more efficient.
12296143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  return GetAddrOfConstantString(GetStringForStringLiteral(S));
12306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
12316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
1232eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1233eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node.
1234eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant *
1235eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1236eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  std::string Str;
1237eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1238a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman
1239a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman  return GetAddrOfConstantCString(Str);
1240eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner}
1241eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1242eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1243a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal.
124445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str,
124545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                             bool constant,
12465fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             CodeGenModule &CGM,
12475fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             const char *GlobalName) {
12486143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // Create Constant for this string literal. Don't add a '\0'.
12496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str, false);
125045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
125145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this string
1252eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  return new llvm::GlobalVariable(C->getType(), constant,
1253eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  llvm::GlobalValue::InternalLinkage,
12548e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                  C, GlobalName, &CGM.getModule());
125545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
125645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
12576143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array
12586143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the
12596143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically;
12606143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is
12616143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on
12626143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings.
12636143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar///
12646143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type.
12655fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
12665fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                       const char *GlobalName) {
12678e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  bool IsConstant = !Features.WritableStrings;
12688e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar
12698e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  // Get the default prefix if a name wasn't specified.
12708e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (!GlobalName)
12718e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    GlobalName = getContext().Target.getStringSymbolPrefix(IsConstant);
12728e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar
12738e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  // Don't share any string literals if strings aren't constant.
12748e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (!IsConstant)
12755fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    return GenerateStringLiteral(str, false, *this, GlobalName);
127645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
127745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::StringMapEntry<llvm::Constant *> &Entry =
127845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
127945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
128045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Entry.getValue())
1281eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner    return Entry.getValue();
128245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
128345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this.
12845fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
128545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  Entry.setValue(C);
128645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
128745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
12886143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
12896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character
12906143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-'
12916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type.
12925fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
12935fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                        const char *GlobalName){
1294c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner  return GetAddrOfConstantString(str + '\0', GlobalName);
12956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
129641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1297af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized
1298af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation.
1299af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const
1300af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                                                    ObjCImplementationDecl *D) {
1301af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(),
1302af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         e = D->propimpl_end(); i != e; ++i) {
1303af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCPropertyImplDecl *PID = *i;
1304af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1305af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Dynamic is just for type-checking.
1306af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1307af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      ObjCPropertyDecl *PD = PID->getPropertyDecl();
1308af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1309af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // Determine which methods need to be implemented, some may have
1310af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // been overridden. Note that ::isSynthesized is not the method
1311af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // we want, that just indicates if the decl came from a
1312af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // property. What we want to know is if the method is defined in
1313af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // this implementation.
1314af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!D->getInstanceMethod(PD->getGetterName()))
1315fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCGetter(
1316fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1317af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!PD->isReadOnly() &&
1318af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar          !D->getInstanceMethod(PD->getSetterName()))
1319fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCSetter(
1320fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1321af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    }
1322af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
1323af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
1324af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
132591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace.
1326984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
13276ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (RecordDecl::decl_iterator I = ND->decls_begin(getContext()),
13286ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor         E = ND->decls_end(getContext());
1329984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson       I != E; ++I)
1330984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson    EmitTopLevelDecl(*I);
1331984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson}
1332984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson
133391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec.
133491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
133591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  if (LSD->getLanguage() != LinkageSpecDecl::lang_c) {
133691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    ErrorUnsupported(LSD, "linkage spec");
133791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    return;
133891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  }
133991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
13406ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (RecordDecl::decl_iterator I = LSD->decls_begin(getContext()),
13416ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor         E = LSD->decls_end(getContext());
134291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson       I != E; ++I)
134391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    EmitTopLevelDecl(*I);
134491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson}
134591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
134641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration.
134741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) {
134841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // If an error has occurred, stop code generation, but continue
134941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // parsing and semantic analysis (to ensure all warnings and errors
135041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // are emitted).
135141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  if (Diags.hasErrorOccurred())
135241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
135341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
135441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  switch (D->getKind()) {
13552b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  case Decl::CXXMethod:
135641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Function:
135741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Var:
135841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    EmitGlobal(cast<ValueDecl>(D));
135941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
136041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
136195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  // C++ Decls
136241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Namespace:
1363984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson    EmitNamespace(cast<NamespaceDecl>(D));
136441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
136595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  case Decl::CXXConstructor:
136695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    EmitCXXConstructors(cast<CXXConstructorDecl>(D));
136795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    break;
136827ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson  case Decl::CXXDestructor:
136927ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson    EmitCXXDestructors(cast<CXXDestructorDecl>(D));
137027ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson    break;
137195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
137295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  // Objective-C Decls
137341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
137438e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  // Forward declarations, no (immediate) code generation.
137541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCClass:
137641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCForwardProtocol:
1377b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian  case Decl::ObjCCategory:
1378285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner    break;
1379b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian  case Decl::ObjCInterface:
1380285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner    // If we already laid out this interface due to an @class, and if we
1381285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner    // codegen'd a reference it, update the 'opaque' type to be a real type now.
1382285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner    Types.UpdateCompletedType(cast<ObjCInterfaceDecl>(D));
138341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
1384285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner
138541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCProtocol:
1386b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian    Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
138741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
138841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
138941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCategoryImpl:
1390af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Categories have properties but don't support synthesize so we
1391af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // can ignore them here.
139241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
139341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
139441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1395af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  case Decl::ObjCImplementation: {
1396af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1397af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    EmitObjCPropertyImplementations(OMD);
1398af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    Runtime->GenerateClass(OMD);
139941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
1400af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
140141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCMethod: {
140241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
140341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // If this is not a prototype, emit the body.
14047297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor    if (OMD->getBody(getContext()))
140541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      CodeGenFunction(*this).GenerateObjCMethod(OMD);
140641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
140741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
140841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCompatibleAlias:
1409305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian    // compatibility-alias is a directive and has no code gen.
141041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
141141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
141291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  case Decl::LinkageSpec:
141391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    EmitLinkageSpec(cast<LinkageSpecDecl>(D));
141441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
141541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
141641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::FileScopeAsm: {
141741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
141841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    std::string AsmString(AD->getAsmString()->getStrData(),
141941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                          AD->getAsmString()->getByteLength());
142041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
142141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    const std::string &S = getModule().getModuleInlineAsm();
142241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (S.empty())
142341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(AsmString);
142441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    else
142541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(S + '\n' + AsmString);
142641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
142741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
142841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
142941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  default:
143041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Make sure we handled everything we should, every other kind is
143141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // a non-top-level decl.  FIXME: Would be nice to have an
143241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // isTopLevelDeclKind function. Need to recode Decl::Kind to do
143341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // that easily.
143441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    assert(isa<TypeDecl>(D) && "Unsupported decl kind");
143541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
143641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar}
1437