CodeGenModule.cpp revision e21c4b829c9e69c20f4baab4916e05cdb786d9ae
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h"
195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2221ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h"
232c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h"
248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
26ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h"
27bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h"
2920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
35fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner                             llvm::Module &M, const llvm::TargetData &TD,
36f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar                             Diagnostic &diags, bool GenerateDebugInfo)
3790a904309c79977fcba2ff0542e2e4cd8e3c3fafMike Stump  : BlockModule(C, M, TD, Types, *this), Context(C), Features(LO), TheModule(M),
382a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump    TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0),
392a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump    MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
40208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
413c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  if (!Features.ObjC1)
423c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = 0;
433c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (!Features.NeXTRuntime)
443c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateGNUObjCRuntime(*this);
453c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (Features.ObjCNonFragileABI)
463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else
483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacObjCRuntime(*this);
49e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
50e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  // If debug info generation is enabled, create the CGDebugInfo object.
5126efc3388adb010984da2f70e1f24e8286e6476dMike Stump  DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0;
522b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
532b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
542b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() {
55815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete Runtime;
56815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete DebugInfo;
57815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek}
58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek
59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() {
600269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitDeferred();
61219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  EmitAliases();
62208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  if (Runtime)
63208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      AddGlobalCtor(ObjCInitFunction);
656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalCtors, "llvm.global_ctors");
666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalDtors, "llvm.global_dtors");
67532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  EmitAnnotations();
680269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitLLVMUsed();
69b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  BindRuntimeGlobals();
702b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
72b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbarvoid CodeGenModule::BindRuntimeGlobals() {
73f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  // Deal with protecting runtime function names.
74b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  for (unsigned i = 0, e = RuntimeGlobals.size(); i < e; ++i) {
75b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    llvm::GlobalValue *GV = RuntimeGlobals[i].first;
76b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    const std::string &Name = RuntimeGlobals[i].second;
77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
78b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    // Discard unused runtime declarations.
79b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    if (GV->isDeclaration() && GV->use_empty()) {
80b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar      GV->eraseFromParent();
810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar      continue;
820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar    }
830293d540baafbe070c1035611787a81001a4118eDaniel Dunbar
84e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // See if there is a conflict against a function by setting the name and
85e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // seeing if we got the desired name.
86e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    GV->setName(Name);
87e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    if (GV->isName(Name.c_str()))
88e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      continue;  // Yep, it worked!
89e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner
90e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    GV->setName(""); // Zap the bogus name until we work out the conflict.
91b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    llvm::GlobalValue *Conflict = TheModule.getNamedValue(Name);
92e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    assert(Conflict && "Must have conflicted!");
93e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner
94e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // Decide which version to take. If the conflict is a definition
95e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // we are forced to take that, otherwise assume the runtime
96e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // knows best.
97b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar
98e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // FIXME: This will fail phenomenally when the conflict is the
99e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // wrong type of value. Just bail on it for now. This should
100e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    // really reuse something inside the LLVM Linker code.
101e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    assert(GV->getValueID() == Conflict->getValueID() &&
102e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner           "Unable to resolve conflict between globals of different types.");
103e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    if (!Conflict->isDeclaration()) {
104e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      llvm::Value *Casted =
105e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner        llvm::ConstantExpr::getBitCast(Conflict, GV->getType());
106e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      GV->replaceAllUsesWith(Casted);
107e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      GV->eraseFromParent();
108e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    } else {
109e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      GV->takeName(Conflict);
110e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      llvm::Value *Casted =
111e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner        llvm::ConstantExpr::getBitCast(GV, Conflict->getType());
112e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      Conflict->replaceAllUsesWith(Casted);
113e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner      Conflict->eraseFromParent();
114e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner    }
115f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  }
116f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
117f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
118488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
1192c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet.
12090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
12190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
12290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
12390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
124488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
12556b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
1262c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  std::string Msg = Type;
1270a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
1280a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    << Msg << S->getSourceRange();
1292c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner}
13058c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
131488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
132c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet.
13390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
13490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
13590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
13690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
137488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
13856b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
139c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  std::string Msg = Type;
1400a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
141c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
142c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
14341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM
14441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value.
14541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV,
14641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                                VisibilityAttr::VisibilityTypes Vis) {
1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  switch (Vis) {
1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  default: assert(0 && "Unknown visibility!");
1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::DefaultVisibility:
1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1524f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::HiddenVisibility:
1534f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1544f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1554f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::ProtectedVisibility:
1564f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1574f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1584f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  }
1594f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman}
1604f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration.
1625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an
164c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name.  Otherwise, returns
165c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name.
1665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We
1685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a
1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require
170b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// changes to the GlobalDeclMap, too. (I disagree, I think what we
171b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// actually need is for Sema to provide some notion of which Decls
172b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// refer to the same semantic decl. We shouldn't need to mangle the
173b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// names and see what comes out the same to figure this out. - DWD)
1745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1755f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start
1765f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above
1775f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first.
1786ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) {
179c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  // In C, functions with no attributes never need to be mangled. Fastpath them.
180c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) {
181c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner    assert(ND->getIdentifier() && "Attempt to mangle unnamed decl.");
1823c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return ND->getNameAsCString();
183c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  }
184c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner
1856ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::SmallString<256> Name;
1866ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::raw_svector_ostream Out(Name);
187fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  if (!mangleName(ND, Context, Out)) {
188fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar    assert(ND->getIdentifier() && "Attempt to mangle unnamed decl.");
1893c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return ND->getNameAsCString();
190fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  }
1915f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1926ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  Name += '\0';
1933c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  return MangledNames.GetOrCreateValue(Name.begin(), Name.end()).getKeyData();
1945f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor}
1955f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1966d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before
1976d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs.
1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
19949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalCtors.push_back(std::make_pair(Ctor, Priority));
2016d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
2026d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called
2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded.
2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
20649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalDtors.push_back(std::make_pair(Dtor, Priority));
2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Ctor function type is void()*.
2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::FunctionType* CtorFTy =
2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::FunctionType::get(llvm::Type::VoidTy,
2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            std::vector<const llvm::Type*>(),
2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            false);
2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Get the type of a ctor entry, { i32, void ()* }.
219572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::StructType* CtorStructTy =
2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::StructType::get(llvm::Type::Int32Ty,
2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                          llvm::PointerType::getUnqual(CtorFTy), NULL);
2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2236bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Construct the constructor and destructor arrays.
2246bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  std::vector<llvm::Constant*> Ctors;
2256bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
2266bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    std::vector<llvm::Constant*> S;
2276bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false));
2286bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
2296bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
2306bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
2316bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2326bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  if (!Ctors.empty()) {
2336bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
2346bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    new llvm::GlobalVariable(AT, false,
235572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             llvm::GlobalValue::AppendingLinkage,
2366bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             llvm::ConstantArray::get(AT, Ctors),
2376bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             GlobalName,
238572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             &TheModule);
2396d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  }
2406d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
2416d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() {
243532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  if (Annotations.empty())
244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman    return;
245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create a new global variable for the ConstantStruct in the Module.
247532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::Constant *Array =
248532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
249532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                                                Annotations.size()),
250532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           Annotations);
251532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::GlobalValue *gv =
252532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  new llvm::GlobalVariable(Array->getType(), false,
253532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           llvm::GlobalValue::AppendingLinkage, Array,
254532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           "llvm.global.annotations", &TheModule);
255532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  gv->setSection("llvm.metadata");
256532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman}
257532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
2585c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D,
2595c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool IsInternal,
2605c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool IsInline,
2615c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             llvm::GlobalValue *GV,
2625c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool ForDefinition) {
26349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Set up linkage and many other things.  Note, this is a simple
264d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes  // approximation of what we really want.
265219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (!ForDefinition) {
266219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // Only a few attributes are set on declarations.
2672f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov    if (D->getAttr<DLLImportAttr>()) {
2682f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      // The dllimport attribute is overridden by a subsequent declaration as
2692f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      // dllexport.
2703ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl      if (!D->getAttr<DLLExportAttr>()) {
2712f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        // dllimport attribute can be applied only to function decls, not to
2722f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        // definitions.
2732f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2742f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          if (!FD->getBody())
2752f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov            GV->setLinkage(llvm::Function::DLLImportLinkage);
2762f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        } else
2772f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          GV->setLinkage(llvm::Function::DLLImportLinkage);
2783ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl      }
2795e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar    } else if (D->getAttr<WeakAttr>() ||
2805e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar               D->getAttr<WeakImportAttr>()) {
2815e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar      // "extern_weak" is overloaded in LLVM; we probably should have
2825e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar      // separate linkage types for this.
283e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands      GV->setLinkage(llvm::Function::ExternalWeakLinkage);
2845e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar   }
285219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else {
286219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (IsInternal) {
287219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      GV->setLinkage(llvm::Function::InternalLinkage);
288219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    } else {
2892f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      if (D->getAttr<DLLExportAttr>()) {
2902f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2912f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          // The dllexport attribute is ignored for undefined symbols.
2922f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          if (FD->getBody())
2932f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov            GV->setLinkage(llvm::Function::DLLExportLinkage);
2942f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        } else
2952f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          GV->setLinkage(llvm::Function::DLLExportLinkage);
2965e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar      } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() ||
2975e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar                 IsInline)
298286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump        GV->setLinkage(llvm::Function::WeakAnyLinkage);
299219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
3000dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  }
301d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
30249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Figure out the relative priority of the attribute,
30349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // -fvisibility, and private_extern.
3040dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
30541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    setGlobalVisibility(GV, attr->getVisibility());
306d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes  // FIXME: else handle -fvisibility
307a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar
308eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  // Prefaced with special LLVM marker to indicate that the name
309eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  // should not be munged.
310eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>())
311a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    GV->setName("\01" + ALA->getLabel());
31217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
31317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
31417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar    GV->setSection(SA->getName());
3155c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar
3165c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // Only add to llvm.used when we see a definition, otherwise we
3175c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // might add multiple times or risk the value being replaced by a
3185c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // subsequent RAUW.
3195c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (ForDefinition) {
3205c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar    if (D->getAttr<UsedAttr>())
3215c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar      AddUsedGlobal(GV);
3225c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  }
323d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes}
324d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
325761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D,
32645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar                                          const CGFunctionInfo &Info,
327b768807c49a1c7085def099b848631856af766faDaniel Dunbar                                          llvm::Function *F) {
328761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  AttributeListType AttributeList;
32988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  ConstructAttributeList(Info, D, AttributeList);
330c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman
331761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
332761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                        AttributeList.size()));
333ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
334ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  // Set the appropriate calling convention for the Function.
33545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  if (D->getAttr<FastCallAttr>())
336f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_FastCall);
337f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov
338f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov  if (D->getAttr<StdCallAttr>())
339f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_StdCall);
340f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
341f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
342f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes
343f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition.
344219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D,
345219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                                                       llvm::Function *F) {
346219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (isa<ObjCMethodDecl>(D)) {
347219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(D, true, false, F, true);
348219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else {
349219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const FunctionDecl *FD = cast<FunctionDecl>(D);
350219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
351219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                             FD->isInline(), F, true);
352219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  }
353219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
35474ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar  if (!Features.Exceptions && !Features.ObjCNonFragileABI)
355f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar    F->addFnAttr(llvm::Attribute::NoUnwind);
356af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
357af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar  if (D->getAttr<AlwaysInlineAttr>())
358af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar    F->addFnAttr(llvm::Attribute::AlwaysInline);
35981ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson
36081ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson  if (D->getAttr<NoinlineAttr>())
36181ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    F->addFnAttr(llvm::Attribute::NoInline);
362f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
363f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
364f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD,
365f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                                        llvm::Function *F) {
366541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F);
367f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
368219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetFunctionAttributesForDefinition(MD, F);
369f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
370f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
371f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
372f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                                          llvm::Function *F) {
373541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
37445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                           FD->isInline(), F, false);
377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar}
378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
37945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() {
381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
3825e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar    const ValueDecl *D = Aliases[i];
383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const AliasAttr *AA = D->getAttr<AliasAttr>();
384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // This is something of a hack, if the FunctionDecl got overridden
386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // then its attributes will be moved to the new declaration. In
387219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // this case the current decl has no alias attribute, but we will
388219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // eventually see it.
389219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (!AA)
390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      continue;
391219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const std::string& aliaseeName = AA->getAliasee();
3935e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar    llvm::GlobalValue *aliasee = getModule().getNamedValue(aliaseeName);
394219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (!aliasee) {
395219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // FIXME: This isn't unsupported, this is just an error, which
396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // sema should catch, but...
397219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      ErrorUnsupported(D, "alias referencing a missing function");
398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      continue;
399219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
400219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
401219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    llvm::GlobalValue *GA =
402219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      new llvm::GlobalAlias(aliasee->getType(),
403219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                            llvm::Function::ExternalLinkage,
4046ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                            getMangledName(D), aliasee,
4055f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                            &getModule());
406219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
4075f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor    llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
408219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (Entry) {
409219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // If we created a dummy function for this then replace it.
410219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      GA->takeName(Entry);
411219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
412219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      llvm::Value *Casted =
413219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar        llvm::ConstantExpr::getBitCast(GA, Entry->getType());
414219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry->replaceAllUsesWith(Casted);
415219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry->eraseFromParent();
416ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
417219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry = GA;
418219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
419219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
420219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // Alias should never be internal or inline.
421219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(D, false, false, GA, true);
422219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  }
423ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman}
424ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  assert(!GV->isDeclaration() &&
4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         "Only globals with definition can force usage.");
4280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
4290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy));
4300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
4310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() {
4330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Don't create llvm.used if there is no need.
4340269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  if (LLVMUsed.empty())
4350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    return;
4360269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4370269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(),
4380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                                              LLVMUsed.size());
4390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::GlobalVariable *GV =
4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    new llvm::GlobalVariable(ATy, false,
4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::GlobalValue::AppendingLinkage,
4420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::ConstantArray::get(ATy, LLVMUsed),
4430269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             "llvm.used", &getModule());
4440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  GV->setSection("llvm.metadata");
4460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() {
4490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Emit code for any deferred decl which was used.  Since a
4500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // previously unused static decl may become used during the
4510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // generation of code for a static function, iterate until no
4520269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // changes are made.
4534c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  bool Changed;
4544c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  do {
4554c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    Changed = false;
456b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4570269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(),
4580269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         e = DeferredDecls.end(); i != e; ) {
459b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson      const ValueDecl *D = *i;
460b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4616f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // Check if we have used a decl with the same name
4626f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // FIXME: The AST should have some sort of aggregate decls or
4636f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // global symbol map.
464219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // FIXME: This is missing some important cases. For example, we
46573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      // need to check for uses in an alias.
4665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor      if (!GlobalDeclMap.count(getMangledName(D))) {
467232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar        ++i;
468a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar        continue;
469b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson      }
470b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
471bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      // Emit the definition.
472bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      EmitGlobalDefinition(D);
473bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
4744c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Erase the used decl from the list.
4750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar      i = DeferredDecls.erase(i);
476b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4774c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Remember that we made a change.
4784c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      Changed = true;
4794c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    }
4804c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  } while (Changed);
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue.  The annotation struct is
4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
4863c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated.  The second field is the constant string
4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation.  The third field is a constant
4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit.  The fourth field is
4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration.
4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc
4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///        appears to.
4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                const AnnotateAttr *AA,
4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                unsigned LineNo) {
4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Module *M = &getModule();
4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // get [N x i8] constants for the annotation string, and the filename string
5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // which are the 2nd and 3rd elements of the global annotation structure.
5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
5028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true);
5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(),
5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                  true);
5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Get the two global values corresponding to the ConstantArrays we just
5078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // created to hold the bytes of the strings.
5088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *annoGV =
5098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(anno->getType(), false,
5108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, anno,
5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           GV->getName() + ".str", M);
5128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // translation unit name string, emitted into the llvm.metadata section.
5138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *unitGV =
5148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(unit->getType(), false,
5158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, unit, ".str", M);
5168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
5178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Create the ConstantStruct that is the global annotion.
5188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *Fields[4] = {
5198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(GV, SBP),
5208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(annoGV, SBP),
5218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(unitGV, SBP),
5228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
5238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  };
5248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return llvm::ConstantStruct::get(Fields, 4, false);
5258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
5268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
5285c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // Never defer when EmitAllDecls is specified or the decl has
5295c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // attribute used.
5305c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (Features.EmitAllDecls || Global->getAttr<UsedAttr>())
53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    return false;
532bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
533bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
53473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Constructors and destructors should never be deferred.
53573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>())
53673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
53773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
53873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (FD->getStorageClass() != FunctionDecl::Static)
53973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
54073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  } else {
54173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
54273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    assert(VD->isFileVarDecl() && "Invalid decl.");
54373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
54473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (VD->getStorageClass() != VarDecl::Static)
54573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
54673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  }
54773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
54873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  return true;
54973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar}
55073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
55173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) {
5525e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  // Aliases are deferred until code for everything else has been
5535e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  // emitted.
5545e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  if (Global->getAttr<AliasAttr>()) {
5555e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar    Aliases.push_back(Global);
5565e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar    return;
5575e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  }
558219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
5595e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
56073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
56173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (!FD->isThisDeclarationADefinition())
56273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
5630269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  } else {
5640269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
565bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
566bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
56773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
5687542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar    if (!VD->getInit() && VD->hasExternalStorage())
56973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
5704c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
5714c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
57273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  // Defer code generation when possible.
57373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  if (MayDeferGeneration(Global)) {
5740269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    DeferredDecls.push_back(Global);
575bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    return;
576bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
577bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
578bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  // Otherwise emit the definition.
579bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  EmitGlobalDefinition(Global);
5804c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
5814c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
582bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) {
583bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
584bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalFunctionDefinition(FD);
585bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
586bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalVarDefinition(VD);
587bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else {
588bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(0 && "Invalid argument to EmitGlobalDefinition()");
589bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
590bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
591bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
5929986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) {
59377ba708819285931932ecd33691a672bb59d221aEli Friedman  assert(D->hasGlobalStorage() && "Not a global variable");
59477ba708819285931932ecd33691a672bb59d221aEli Friedman
595bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  QualType ASTTy = D->getType();
596bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
5979986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
598bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
5993c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
6005f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
60149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  if (!Entry) {
60249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    llvm::GlobalVariable *GV =
60349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      new llvm::GlobalVariable(Ty, false,
60449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar                               llvm::GlobalValue::ExternalLinkage,
6056ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                               0, getMangledName(D), &getModule(),
6065f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                               0, ASTTy.getAddressSpace());
60749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    Entry = GV;
60849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
60949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // Handle things which are present even on external declarations.
61049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
61149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // FIXME: This code is overly simple and should be merged with
61249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // other global handling.
61349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
61449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    GV->setConstant(D->getType().isConstant(Context));
61549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
616eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar    // FIXME: Merge with other attribute handling code.
617eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
61849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    if (D->getStorageClass() == VarDecl::PrivateExtern)
61949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility);
620eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
6215e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar    if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>())
622e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands      GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6233f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar
6243f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar    if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
6253f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar      // Prefaced with special LLVM marker to indicate that the name
6263f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar      // should not be munged.
6273f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar      GV->setName("\01" + ALA->getLabel());
6283f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar    }
62949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  }
6303c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
6319986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  // Make sure the result is of the correct type.
6329986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  return llvm::ConstantExpr::getBitCast(Entry, PTy);
633bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
634bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
635bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
6368f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  llvm::Constant *Init = 0;
63777ba708819285931932ecd33691a672bb59d221aEli Friedman  QualType ASTTy = D->getType();
63877ba708819285931932ecd33691a672bb59d221aEli Friedman  const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy);
63977ba708819285931932ecd33691a672bb59d221aEli Friedman
6408f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  if (D->getInit() == 0) {
641cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // This is a tentative definition; tentative definitions are
642cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // implicitly initialized with { 0 }
643cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    const llvm::Type* InitTy;
644cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    if (ASTTy->isIncompleteArrayType()) {
645cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      // An incomplete array is normally [ TYPE x 0 ], but we need
646cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      // to fix it to [ TYPE x 1 ].
647cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy);
648cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      InitTy = llvm::ArrayType::get(ATy->getElementType(), 1);
649cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    } else {
650cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      InitTy = VarTy;
651cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    }
652cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    Init = llvm::Constant::getNullValue(InitTy);
65377ba708819285931932ecd33691a672bb59d221aEli Friedman  } else {
654bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    Init = EmitConstantExpr(D->getInit());
6556e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    if (!Init) {
656232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar      ErrorUnsupported(D, "static initializer");
6576e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      QualType T = D->getInit()->getType();
6586e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      Init = llvm::UndefValue::get(getTypes().ConvertType(T));
6596e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    }
6608f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  }
66177ba708819285931932ecd33691a672bb59d221aEli Friedman  const llvm::Type* InitType = Init->getType();
6628e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel
6635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
6643c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry);
6653c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
66677ba708819285931932ecd33691a672bb59d221aEli Friedman  if (!GV) {
66777ba708819285931932ecd33691a672bb59d221aEli Friedman    GV = new llvm::GlobalVariable(InitType, false,
66877ba708819285931932ecd33691a672bb59d221aEli Friedman                                  llvm::GlobalValue::ExternalLinkage,
6696ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                                  0, getMangledName(D),
6705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                                  &getModule(), 0, ASTTy.getAddressSpace());
671232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
672232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar  } else if (GV->hasInitializer() && !GV->getInitializer()->isNullValue()) {
673232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // If we already have this global and it has an initializer, then
674232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // we are in the rare situation where we emitted the defining
675232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // declaration of the global and are now being asked to emit a
676232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // definition which would be common. This occurs, for example, in
677232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // the following situation because statics can be emitted out of
678232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // order:
679232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //
680232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int x;
681232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int *y = &x;
682232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int x = 10;
683232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  int **z = &y;
684232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //
685232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // Bail here so we don't blow away the definition. Note that if we
686232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // can't distinguish here if we emitted a definition with a null
687232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // initializer, but this case is safe.
688232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    assert(!D->getInit() && "Emitting multiple definitions of a decl!");
689232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    return;
690232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
6913c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  } else if (GV->getType() !=
6923c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar             llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) {
69377ba708819285931932ecd33691a672bb59d221aEli Friedman    // We have a definition after a prototype with the wrong type.
69477ba708819285931932ecd33691a672bb59d221aEli Friedman    // We must make a new GlobalVariable* and update everything that used OldGV
69577ba708819285931932ecd33691a672bb59d221aEli Friedman    // (a declaration or tentative definition) with the new GlobalVariable*
69677ba708819285931932ecd33691a672bb59d221aEli Friedman    // (which will be a definition).
69777ba708819285931932ecd33691a672bb59d221aEli Friedman    //
69877ba708819285931932ecd33691a672bb59d221aEli Friedman    // This happens if there is a prototype for a global (e.g. "extern int x[];")
69977ba708819285931932ecd33691a672bb59d221aEli Friedman    // and then a definition of a different type (e.g. "int x[10];"). This also
70077ba708819285931932ecd33691a672bb59d221aEli Friedman    // happens when an initializer has a different type from the type of the
70177ba708819285931932ecd33691a672bb59d221aEli Friedman    // global (this happens with unions).
702cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    //
703cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // FIXME: This also ends up happening if there's a definition followed by
704cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // a tentative definition!  (Although Sema rejects that construct
705cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // at the moment.)
70677ba708819285931932ecd33691a672bb59d221aEli Friedman
70777ba708819285931932ecd33691a672bb59d221aEli Friedman    // Save the old global
70877ba708819285931932ecd33691a672bb59d221aEli Friedman    llvm::GlobalVariable *OldGV = GV;
70977ba708819285931932ecd33691a672bb59d221aEli Friedman
71077ba708819285931932ecd33691a672bb59d221aEli Friedman    // Make a new global with the correct type
71177ba708819285931932ecd33691a672bb59d221aEli Friedman    GV = new llvm::GlobalVariable(InitType, false,
71277ba708819285931932ecd33691a672bb59d221aEli Friedman                                  llvm::GlobalValue::ExternalLinkage,
7136ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                                  0, getMangledName(D),
7145f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                                  &getModule(), 0, ASTTy.getAddressSpace());
71577ba708819285931932ecd33691a672bb59d221aEli Friedman    // Steal the name of the old global
71677ba708819285931932ecd33691a672bb59d221aEli Friedman    GV->takeName(OldGV);
71777ba708819285931932ecd33691a672bb59d221aEli Friedman
71877ba708819285931932ecd33691a672bb59d221aEli Friedman    // Replace all uses of the old global with the new global
71977ba708819285931932ecd33691a672bb59d221aEli Friedman    llvm::Constant *NewPtrForOldDecl =
72077ba708819285931932ecd33691a672bb59d221aEli Friedman        llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
72177ba708819285931932ecd33691a672bb59d221aEli Friedman    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
72277ba708819285931932ecd33691a672bb59d221aEli Friedman
72377ba708819285931932ecd33691a672bb59d221aEli Friedman    // Erase the old global, since it is no longer used.
72477ba708819285931932ecd33691a672bb59d221aEli Friedman    OldGV->eraseFromParent();
72577ba708819285931932ecd33691a672bb59d221aEli Friedman  }
72677ba708819285931932ecd33691a672bb59d221aEli Friedman
7273c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  Entry = GV;
7289e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel
7298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
7308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    SourceManager &SM = Context.getSourceManager();
7318bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    AddAnnotation(EmitAnnotateAttr(GV, AA,
732f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner                              SM.getInstantiationLineNumber(D->getLocation())));
7338bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
7348bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
73588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  GV->setInitializer(Init);
736b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes  GV->setConstant(D->getType().isConstant(Context));
7370de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman  GV->setAlignment(getContext().getDeclAlignInBytes(D));
73808d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman
739ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
74041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    setGlobalVisibility(GV, attr->getVisibility());
741ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  // FIXME: else handle -fvisibility
742a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar
743a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
744a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    // Prefaced with special LLVM marker to indicate that the name
745a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    // should not be munged.
746a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    GV->setName("\01" + ALA->getLabel());
747a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar  }
74888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner
74988a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  // Set the llvm linkage type as appropriate.
7508fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  if (D->getStorageClass() == VarDecl::Static)
7518fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
7528fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else if (D->getAttr<DLLImportAttr>())
753ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLImportLinkage);
754ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  else if (D->getAttr<DLLExportAttr>())
755ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLExportLinkage);
7565e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar  else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>())
757286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump    GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
7588fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else {
759ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // FIXME: This isn't right.  This should handle common linkage and other
760ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // stuff.
761ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    switch (D->getStorageClass()) {
7628fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    case VarDecl::Static: assert(0 && "This case handled above");
763ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Auto:
764ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Register:
765ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      assert(0 && "Can't have auto or register globals");
766ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::None:
767ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      if (!D->getInit())
768ceb77d909f80d4949ee6177094510413c391cddcDuncan Sands        GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
76998883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson      else
77098883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson        GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
771ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
772ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Extern:
77349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      // FIXME: common
77449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      break;
77549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
776ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::PrivateExtern:
77749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
77849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      // FIXME: common
779ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
780ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    }
78188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  }
782686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta
78317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
78417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar    GV->setSection(SA->getName());
78517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
7865c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (D->getAttr<UsedAttr>())
7875c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar    AddUsedGlobal(GV);
7885c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar
789686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  // Emit global variable debug information.
790686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  CGDebugInfo *DI = getDebugInfo();
791686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  if(DI) {
79266031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar    DI->setLocation(D->getLocation());
793686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta    DI->EmitGlobalVariable(GV, D);
794686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  }
79588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner}
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
797bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue *
798d5d31801fc87239436fa349c89dce7797cf13537Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D,
79942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar                                             const llvm::Type *Ty) {
8002136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman  bool DoSetAttributes = true;
8012136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman  if (!Ty) {
802d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Ty = getTypes().ConvertType(D->getType());
8032136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman    if (!isa<llvm::FunctionType>(Ty)) {
8042136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman      // This function doesn't have a complete type (for example, the return
8052136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman      // type is an incomplete struct). Use a fake type instead, and make
8062136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman      // sure not to try to set attributes.
8072136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman      Ty = llvm::FunctionType::get(llvm::Type::VoidTy,
8082136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman                                   std::vector<const llvm::Type*>(), false);
8092136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman      DoSetAttributes = false;
8102136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman    }
8112136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman  }
81242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty),
81342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar                                             llvm::Function::ExternalLinkage,
81442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar                                             getMangledName(D),
81542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar                                             &getModule());
8162136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman  if (DoSetAttributes)
8172136b2e9345c23c26426110873dd97ef5d761ef2Eli Friedman    SetFunctionAttributes(D, F);
818219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  return F;
819bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
820bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
821bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) {
8229986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  QualType ASTTy = D->getType();
8239986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
8249986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
8253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
8263c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
8275f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
8283c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  if (!Entry)
829d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Entry = EmitForwardFunctionDefinition(D, 0);
830bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
8318f431a554367d2828b82612b456c3a2ce737e799Chris Lattner  if (Entry->getType() != PTy)
8328f431a554367d2828b82612b456c3a2ce737e799Chris Lattner    return llvm::ConstantExpr::getBitCast(Entry, PTy);
8338f431a554367d2828b82612b456c3a2ce737e799Chris Lattner  return Entry;
834bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
835bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
836bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) {
837d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  const llvm::FunctionType *Ty =
838d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    cast<llvm::FunctionType>(getTypes().ConvertType(D->getType()));
839d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
840d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // As a special case, make sure that definitions of K&R function
841d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // "type foo()" aren't declared as varargs (which forces the backend
842d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // to do unnecessary work).
843d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  if (Ty->isVarArg() && Ty->getNumParams() == 0 && Ty->isVarArg())
844d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Ty = llvm::FunctionType::get(Ty->getReturnType(),
845d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar                                 std::vector<const llvm::Type*>(),
846d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar                                 false);
847d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
8485f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
8493c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  if (!Entry) {
850d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Entry = EmitForwardFunctionDefinition(D, Ty);
85142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  } else {
85242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar    // If the types mismatch then we have to rewrite the definition.
85342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar    if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) {
85442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // Otherwise, we have a definition after a prototype with the
85542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // wrong type.  F is the Function* for the one with the wrong
85642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // type, we must make a new Function* and update everything that
85742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // used F (a declaration) with the new Function* (which will be
85842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // a definition).
85942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      //
86042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // This happens if there is a prototype for a function
86142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // (e.g. "int f()") and then a definition of a different type
86242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // (e.g. "int f(int x)").  Start by making a new function of the
86342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // correct type, RAUW, then steal the name.
86442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D, Ty);
86542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      NewFn->takeName(Entry);
86642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar
86742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // Replace uses of F with the Function we will endow with a body.
86842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      llvm::Constant *NewPtrForOldDecl =
86942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar        llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
87042745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      Entry->replaceAllUsesWith(NewPtrForOldDecl);
87142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar
87242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      // Ok, delete the old function now, which is dead.
87342745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      assert(Entry->isDeclaration() && "Shouldn't replace non-declaration");
87442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      Entry->eraseFromParent();
87542745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar
87642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar      Entry = NewFn;
87742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar    }
878bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
879bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
880219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  llvm::Function *Fn = cast<llvm::Function>(Entry);
881219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  CodeGenFunction(*this).GenerateCode(D, Fn);
8826379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar
883219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetFunctionAttributesForDefinition(D, Fn);
884219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
885219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) {
886219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalCtor(Fn, CA->getPriority());
887219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) {
888219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalDtor(Fn, DA->getPriority());
889bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
890bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
891bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
892f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function *
893f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
894f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                     const std::string &Name) {
895f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  llvm::Function *Fn = llvm::Function::Create(FTy,
896f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                              llvm::Function::ExternalLinkage,
897f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                              "", &TheModule);
898b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  RuntimeGlobals.push_back(std::make_pair(Fn, Name));
899f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  return Fn;
900f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
901f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
902b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbarllvm::GlobalVariable *
903b681b8ffab2aa016b3897916d5110927c34a584bDaniel DunbarCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
904b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar                                     const std::string &Name) {
905b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  llvm::GlobalVariable *GV =
906b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar    new llvm::GlobalVariable(Ty, /*Constant=*/false,
907b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar                             llvm::GlobalValue::ExternalLinkage,
908b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar                             0, "", &TheModule);
909b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  RuntimeGlobals.push_back(std::make_pair(GV, Name));
910b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  return GV;
911b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar}
912b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar
913c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
914c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  // Make sure that this type is translated.
915c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  Types.UpdateCompletedType(TD);
916d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
917d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
918d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
919bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction
920c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
9211426fecdf90dd1986751b9940422e675880ff671Chris Lattner  if (BuiltinID > BuiltinFunctions.size())
9221426fecdf90dd1986751b9940422e675880ff671Chris Lattner    BuiltinFunctions.resize(BuiltinID);
923bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9241426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // Cache looked up functions.  Since builtin id #0 is invalid we don't reserve
9251426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // a slot for it.
9261426fecdf90dd1986751b9940422e675880ff671Chris Lattner  assert(BuiltinID && "Invalid Builtin ID");
927c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump  llvm::Value *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
928bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  if (FunctionSlot)
929bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    return FunctionSlot;
930bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9313e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
9323e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor          Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
9333e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor         "isn't a lib fn");
934bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  // Get the name, skip over the __builtin_ prefix (if necessary).
9363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
9373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  if (Context.BuiltinInfo.isLibFunction(BuiltinID))
9383e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor    Name += 10;
939bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
940bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // Get the type for the builtin.
941370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  Builtin::Context::GetBuiltinTypeError Error;
942370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error);
943370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  assert(Error == Builtin::Context::GE_None && "Can't get builtin type");
944370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor
945bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  const llvm::FunctionType *Ty =
946bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    cast<llvm::FunctionType>(getTypes().ConvertType(Type));
947bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
948bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: This has a serious problem with code like this:
949bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //  void abs() {}
950bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //    ... __builtin_abs(x);
951bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // The two versions of abs will collide.  The fix is for the builtin to win,
952bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // and for the existing one to be turned into a constantexpr cast of the
953bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // builtin.  In the case where the existing one is a static function, it
954bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // should just be renamed.
955c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  if (llvm::Function *Existing = getModule().getFunction(Name)) {
956c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
957c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner      return FunctionSlot = Existing;
958c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    assert(Existing == 0 && "FIXME: Name collision");
959c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  }
960bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9614667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner  llvm::GlobalValue *&ExistingFn =
9624667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner    GlobalDeclMap[getContext().Idents.get(Name).getName()];
9635b60a0e369521825f892d755150255645d923646Chris Lattner  assert(!ExistingFn && "Asking for the same builtin multiple times?");
964c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump
965bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: param attributes for sext/zext etc.
9664667c4a309f7d49f858e14c4c2462fe595bcc536Chris Lattner  return FunctionSlot = ExistingFn =
9674c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name,
9684c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman                           &getModule());
969bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner}
970bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9717acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
9727acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                            unsigned NumTys) {
9737acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return llvm::Intrinsic::getDeclaration(&getModule(),
9747acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
9757acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner}
976bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() {
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (MemCpyFn) return MemCpyFn;
9794e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9804e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1);
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
982c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
9830c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() {
9840c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman  if (MemMoveFn) return MemMoveFn;
9854e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9864e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1);
9870c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman}
9880c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman
98941ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() {
99041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  if (MemSetFn) return MemSetFn;
9914e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9924e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1);
99341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio}
9947acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner
995e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM,
996e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                  std::vector<llvm::Constant*>& Fields,
99744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  FieldDecl *FieldD, FieldDecl *NextFieldD,
99844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  llvm::Constant* Field,
9993c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner                                  RecordDecl* RD, const llvm::StructType *STy) {
1000e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append the field.
1001e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  Fields.push_back(Field);
1002e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
100344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD);
1004e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1005e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  int NextStructFieldNo;
100644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (!NextFieldD) {
1007e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    NextStructFieldNo = STy->getNumElements();
1008e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  } else {
100944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD);
1010e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
1011e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1012e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append padding
1013e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) {
1014e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    llvm::Constant *C =
1015e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson      llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1));
1016e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1017e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    Fields.push_back(C);
1018e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
1019e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson}
1020e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
10213e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16.
10223e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215>
1023bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule::
1024bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) {
1025c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::StringMapEntry<llvm::Constant *> &Entry =
1026c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
1027c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1028c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (Entry.getValue())
1029c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    return Entry.getValue();
1030c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
10313e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
10323e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zeros[] = { Zero, Zero };
1033c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1034c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (!CFConstantStringClassRef) {
1035c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
1036c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    Ty = llvm::ArrayType::get(Ty, 0);
10373e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10383e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // FIXME: This is fairly broken if
10393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // __CFConstantStringClassReference is already defined, in that it
10403e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // will get renamed and the user will most likely see an opaque
10413e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // error message. This is a general issue with relying on
10423e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // particular names.
10433e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    llvm::GlobalVariable *GV =
1044c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      new llvm::GlobalVariable(Ty, false,
1045c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalVariable::ExternalLinkage, 0,
1046c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               "__CFConstantStringClassReference",
1047c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               &getModule());
10483e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10493e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // Decay array -> ptr
10503e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    CFConstantStringClassRef =
10513e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1052c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
1053c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1054e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  QualType CFTy = getContext().getCFConstantStringType();
1055e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl();
10563e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
1057e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  const llvm::StructType *STy =
1058e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1059e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1060e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  std::vector<llvm::Constant*> Fields;
106144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  RecordDecl::field_iterator Field = CFRD->field_begin();
106244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1063c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Class pointer.
106444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *CurField = *Field++;
106544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *NextField = *Field++;
106644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
106744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        CFConstantStringClassRef, CFRD, STy);
1068c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1069c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Flags.
107044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
107144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
10723e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
107344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
107444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy);
1075c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1076c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String pointer.
107744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
107844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
10793e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str);
1080c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = new llvm::GlobalVariable(C->getType(), true,
1081c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalValue::InternalLinkage,
1082e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                               C, ".str", &getModule());
108344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
1084e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                        llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2),
1085e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                        CFRD, STy);
1086c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1087c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String length.
108844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
108944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = 0;
1090c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().LongTy);
109144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
109244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        llvm::ConstantInt::get(Ty, str.length()), CFRD, STy);
1093c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1094c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // The struct.
1095e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  C = llvm::ConstantStruct::get(STy, Fields);
10960c67829763b98bc670062b553897a851fab17401Anders Carlsson  llvm::GlobalVariable *GV =
10970c67829763b98bc670062b553897a851fab17401Anders Carlsson    new llvm::GlobalVariable(C->getType(), true,
10980c67829763b98bc670062b553897a851fab17401Anders Carlsson                             llvm::GlobalVariable::InternalLinkage,
10990c67829763b98bc670062b553897a851fab17401Anders Carlsson                             C, "", &getModule());
11003e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
11010c67829763b98bc670062b553897a851fab17401Anders Carlsson  GV->setSection("__DATA,__cfstring");
11020c67829763b98bc670062b553897a851fab17401Anders Carlsson  Entry.setValue(GV);
11033e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
11040c67829763b98bc670062b553897a851fab17401Anders Carlsson  return GV;
1105c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson}
110645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
11076143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a
11081e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type.
11096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
11101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const char *StrData = E->getStrData();
11111e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  unsigned Len = E->getByteLength();
11121e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
11131e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const ConstantArrayType *CAT =
11141e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    getContext().getAsConstantArrayType(E->getType());
11151e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  assert(CAT && "String isn't pointer or array!");
11161e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
1117dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  // Resize the string to the right size.
11181e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  std::string Str(StrData, StrData+Len);
11191e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  uint64_t RealLen = CAT->getSize().getZExtValue();
1120dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
1121dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  if (E->isWide())
1122dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    RealLen *= getContext().Target.getWCharWidth()/8;
1123dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
11241e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  Str.resize(RealLen, '\0');
11251e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
11261e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  return Str;
11271e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar}
11281e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
11296143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
11306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal.
11316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant *
11326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
11336143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // FIXME: This can be more efficient.
11346143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  return GetAddrOfConstantString(GetStringForStringLiteral(S));
11356143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
11366143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
1137eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1138eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node.
1139eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant *
1140eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1141eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  std::string Str;
1142eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1143a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman
1144a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman  return GetAddrOfConstantCString(Str);
1145eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner}
1146eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1147eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1148a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal.
114945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str,
115045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                             bool constant,
11515fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             CodeGenModule &CGM,
11525fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             const char *GlobalName) {
11536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // Create Constant for this string literal. Don't add a '\0'.
11546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str, false);
115545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
115645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this string
1157eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  return new llvm::GlobalVariable(C->getType(), constant,
1158eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  llvm::GlobalValue::InternalLinkage,
1159eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  C, GlobalName ? GlobalName : ".str",
1160eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  &CGM.getModule());
116145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
116245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
11636143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array
11646143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the
11656143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically;
11666143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is
11676143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on
11686143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings.
11696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar///
11706143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type.
11715fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
11725fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                       const char *GlobalName) {
117345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Don't share any string literals if writable-strings is turned on.
117445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Features.WritableStrings)
11755fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    return GenerateStringLiteral(str, false, *this, GlobalName);
117645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
117745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::StringMapEntry<llvm::Constant *> &Entry =
117845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
117945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
118045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Entry.getValue())
1181eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner    return Entry.getValue();
118245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
118345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this.
11845fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
118545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  Entry.setValue(C);
118645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
118745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
11886143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
11896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character
11906143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-'
11916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type.
11925fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
11935fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                        const char *GlobalName){
1194c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner  return GetAddrOfConstantString(str + '\0', GlobalName);
11956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
119641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1197af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized
1198af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation.
1199af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const
1200af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                                                    ObjCImplementationDecl *D) {
1201af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(),
1202af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         e = D->propimpl_end(); i != e; ++i) {
1203af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCPropertyImplDecl *PID = *i;
1204af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1205af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Dynamic is just for type-checking.
1206af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1207af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      ObjCPropertyDecl *PD = PID->getPropertyDecl();
1208af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1209af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // Determine which methods need to be implemented, some may have
1210af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // been overridden. Note that ::isSynthesized is not the method
1211af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // we want, that just indicates if the decl came from a
1212af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // property. What we want to know is if the method is defined in
1213af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // this implementation.
1214af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!D->getInstanceMethod(PD->getGetterName()))
1215fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCGetter(
1216fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1217af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!PD->isReadOnly() &&
1218af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar          !D->getInstanceMethod(PD->getSetterName()))
1219fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCSetter(
1220fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1221af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    }
1222af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
1223af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
1224af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
122541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration.
122641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) {
122741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // If an error has occurred, stop code generation, but continue
122841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // parsing and semantic analysis (to ensure all warnings and errors
122941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // are emitted).
123041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  if (Diags.hasErrorOccurred())
123141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
123241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
123341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  switch (D->getKind()) {
123441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Function:
123541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Var:
123641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    EmitGlobal(cast<ValueDecl>(D));
123741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
123841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
123941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Namespace:
1240662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar    ErrorUnsupported(D, "namespace");
124141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
124241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
124341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Objective-C Decls
124441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
124538e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  // Forward declarations, no (immediate) code generation.
124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCClass:
124741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCForwardProtocol:
124841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
124938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian
125041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCProtocol:
125138e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  case Decl::ObjCCategory:
125238e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  case Decl::ObjCInterface: {
125338e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian    ObjCContainerDecl *OCD = cast<ObjCContainerDecl>(D);
125438e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian    for (ObjCContainerDecl::tuvar_iterator i = OCD->tuvar_begin(),
125538e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian         e = OCD->tuvar_end(); i != e; ++i) {
125638e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian        VarDecl *VD = *i;
125738e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian        EmitGlobal(VD);
125838e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian    }
125938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian    if (D->getKind() == Decl::ObjCProtocol)
126038e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian      Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
126141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
126238e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  }
126341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
126441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCategoryImpl:
1265af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Categories have properties but don't support synthesize so we
1266af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // can ignore them here.
1267af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
126841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
126941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
127041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1271af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  case Decl::ObjCImplementation: {
1272af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1273af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    EmitObjCPropertyImplementations(OMD);
1274af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    Runtime->GenerateClass(OMD);
127541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
1276af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
127741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCMethod: {
127841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
127941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // If this is not a prototype, emit the body.
128041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (OMD->getBody())
128141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      CodeGenFunction(*this).GenerateObjCMethod(OMD);
128241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
128341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
128441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCompatibleAlias:
1285305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian    // compatibility-alias is a directive and has no code gen.
128641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
128741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
128841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::LinkageSpec: {
128941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D);
129041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
1291488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar      ErrorUnsupported(LSD, "linkage spec");
129241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // FIXME: implement C++ linkage, C linkage works mostly by C
129341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // language reuse already.
129441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
129541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
129641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
129741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::FileScopeAsm: {
129841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
129941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    std::string AsmString(AD->getAsmString()->getStrData(),
130041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                          AD->getAsmString()->getByteLength());
130141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
130241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    const std::string &S = getModule().getModuleInlineAsm();
130341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (S.empty())
130441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(AsmString);
130541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    else
130641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(S + '\n' + AsmString);
130741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
130841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
130941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
131041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  default:
131141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Make sure we handled everything we should, every other kind is
131241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // a non-top-level decl.  FIXME: Would be nice to have an
131341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // isTopLevelDeclKind function. Need to recode Decl::Kind to do
131441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // that easily.
131541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    assert(isa<TypeDecl>(D) && "Unsupported decl kind");
131641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
131741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar}
1318