CodeGenModule.cpp revision dbb1ecc32ca122b07b7c98fd0a8f6f53985adacc
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)
37fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner  : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
38208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0),
39ab695143861b520f5c9f8f982534a71d355396f1Mike Stump    CFConstantStringClassRef(0), NSConcreteGlobalBlock(0),
409b8a7977109604d573b49d517e98badbbb9d5ac7Mike Stump    BlockDescriptorType(0), GenericBlockLiteralType(0) {
41208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
42208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  if (Features.ObjC1) {
43f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar    if (Features.NeXTRuntime) {
4430bc57187be7535c57ef1ca8ff3e765653e94332Fariborz Jahanian      Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this)
45ee0af74d1e0990c7b66d32657f3e4e54b8691552Fariborz Jahanian                                       : CreateMacObjCRuntime(*this);
46208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    } else {
47208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      Runtime = CreateGNUObjCRuntime(*this);
48208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    }
49c17a4d3b16a2624a76de5d7508805534545bd3bfDaniel Dunbar  }
50e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
51e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  // If debug info generation is enabled, create the CGDebugInfo object.
5226efc3388adb010984da2f70e1f24e8286e6476dMike Stump  DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0;
5326efc3388adb010984da2f70e1f24e8286e6476dMike Stump
5426efc3388adb010984da2f70e1f24e8286e6476dMike Stump  Block.GlobalUniqueCount = 0;
552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
562b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() {
58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete Runtime;
59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete DebugInfo;
60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek}
61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek
62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() {
630269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitDeferred();
64219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  EmitAliases();
65208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  if (Runtime)
66208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
67208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      AddGlobalCtor(ObjCInitFunction);
686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalCtors, "llvm.global_ctors");
696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalDtors, "llvm.global_dtors");
70532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  EmitAnnotations();
710269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitLLVMUsed();
72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  BindRuntimeFunctions();
732b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
75f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarvoid CodeGenModule::BindRuntimeFunctions() {
76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  // Deal with protecting runtime function names.
77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) {
78f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    llvm::Function *Fn = RuntimeFunctions[i].first;
79f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    const std::string &Name = RuntimeFunctions[i].second;
80f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar    // Discard unused runtime functions.
820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar    if (Fn->use_empty()) {
830293d540baafbe070c1035611787a81001a4118eDaniel Dunbar      Fn->eraseFromParent();
840293d540baafbe070c1035611787a81001a4118eDaniel Dunbar      continue;
850293d540baafbe070c1035611787a81001a4118eDaniel Dunbar    }
860293d540baafbe070c1035611787a81001a4118eDaniel Dunbar
87f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    // See if there is a conflict against a function.
88f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    llvm::Function *Conflict = TheModule.getFunction(Name);
89f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    if (Conflict) {
90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      // Decide which version to take. If the conflict is a definition
91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      // we are forced to take that, otherwise assume the runtime
92f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      // knows best.
93f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      if (!Conflict->isDeclaration()) {
94f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        llvm::Value *Casted =
95f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar          llvm::ConstantExpr::getBitCast(Conflict, Fn->getType());
96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        Fn->replaceAllUsesWith(Casted);
97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        Fn->eraseFromParent();
98f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      } else {
99f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        Fn->takeName(Conflict);
100f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        llvm::Value *Casted =
101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar          llvm::ConstantExpr::getBitCast(Fn, Conflict->getType());
102f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        Conflict->replaceAllUsesWith(Casted);
103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        Conflict->eraseFromParent();
104f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      }
105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    } else {
106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      // FIXME: There still may be conflicts with aliases and
107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      // variables.
108f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar      Fn->setName(Name);
109f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar    }
110f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  }
111f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
112f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
113488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
1142c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet.
11590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
11690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
11790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
11890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
119488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
12056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
1212c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  std::string Msg = Type;
1220a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
1230a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    << Msg << S->getSourceRange();
1242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner}
12558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
126488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
127c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet.
12890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
12990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
13090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
13190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
132488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
13356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
134c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  std::string Msg = Type;
1350a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
136c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
137c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
13841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM
13941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value.
14041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV,
14141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                                VisibilityAttr::VisibilityTypes Vis) {
1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  switch (Vis) {
1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  default: assert(0 && "Unknown visibility!");
1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::DefaultVisibility:
1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::HiddenVisibility:
1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  case VisibilityAttr::ProtectedVisibility:
1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1524f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman    break;
1534f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  }
1544f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman}
1554f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration.
1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an
1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// IdentifierInfo* containing the mangled name. Otherwise, returns
1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// the name of the declaration as an identifier.
1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We
1635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a
1645f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require
165b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// changes to the GlobalDeclMap, too. (I disagree, I think what we
166b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// actually need is for Sema to provide some notion of which Decls
167b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// refer to the same semantic decl. We shouldn't need to mangle the
168b5da3e94b72a0106abd69c2a84bf650e773fa4acDaniel Dunbar/// names and see what comes out the same to figure this out. - DWD)
1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor///
1705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start
1715f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above
1725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first.
1736ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) {
1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::SmallString<256> Name;
1756ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::raw_svector_ostream Out(Name);
1765f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  if (!mangleName(ND, Context, Out))
1776ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor    return ND->getIdentifier()->getName();
1785f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1796ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  Name += '\0';
1806ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  return MangledNames.GetOrCreateValue(Name.begin(), Name.end())
1816ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor           .getKeyData();
1825f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor}
1835f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1846d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before
1856d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs.
1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
18749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalCtors.push_back(std::make_pair(Ctor, Priority));
1896d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
1906d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called
1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded.
1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
19449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalDtors.push_back(std::make_pair(Dtor, Priority));
1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Ctor function type is void()*.
2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::FunctionType* CtorFTy =
2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::FunctionType::get(llvm::Type::VoidTy,
2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            std::vector<const llvm::Type*>(),
2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            false);
2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Get the type of a ctor entry, { i32, void ()* }.
207572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::StructType* CtorStructTy =
2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::StructType::get(llvm::Type::Int32Ty,
2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                          llvm::PointerType::getUnqual(CtorFTy), NULL);
2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Construct the constructor and destructor arrays.
2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  std::vector<llvm::Constant*> Ctors;
2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    std::vector<llvm::Constant*> S;
2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false));
2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  if (!Ctors.empty()) {
2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    new llvm::GlobalVariable(AT, false,
223572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             llvm::GlobalValue::AppendingLinkage,
2246bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             llvm::ConstantArray::get(AT, Ctors),
2256bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                             GlobalName,
226572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             &TheModule);
2276d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  }
2286d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
2296d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() {
231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  if (Annotations.empty())
232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman    return;
233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create a new global variable for the ConstantStruct in the Module.
235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::Constant *Array =
236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
237532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                                                Annotations.size()),
238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           Annotations);
239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::GlobalValue *gv =
240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  new llvm::GlobalVariable(Array->getType(), false,
241532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           llvm::GlobalValue::AppendingLinkage, Array,
242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           "llvm.global.annotations", &TheModule);
243532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  gv->setSection("llvm.metadata");
244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman}
245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
2465c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D,
2475c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool IsInternal,
2485c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool IsInline,
2495c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             llvm::GlobalValue *GV,
2505c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                             bool ForDefinition) {
25149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Set up linkage and many other things.  Note, this is a simple
252d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes  // approximation of what we really want.
253219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (!ForDefinition) {
254219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // Only a few attributes are set on declarations.
2552f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov    if (D->getAttr<DLLImportAttr>()) {
2562f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      // The dllimport attribute is overridden by a subsequent declaration as
2572f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      // dllexport.
2583ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl      if (!D->getAttr<DLLExportAttr>()) {
2592f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        // dllimport attribute can be applied only to function decls, not to
2602f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        // definitions.
2612f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2622f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          if (!FD->getBody())
2632f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov            GV->setLinkage(llvm::Function::DLLImportLinkage);
2642f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        } else
2652f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          GV->setLinkage(llvm::Function::DLLImportLinkage);
2663ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl      }
267eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar    } else if (D->getAttr<WeakAttr>())
268eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar      GV->setLinkage(llvm::Function::ExternalWeakLinkage);
269219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else {
270219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (IsInternal) {
271219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      GV->setLinkage(llvm::Function::InternalLinkage);
272219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    } else {
2732f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      if (D->getAttr<DLLExportAttr>()) {
2742f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2752f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          // The dllexport attribute is ignored for undefined symbols.
2762f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          if (FD->getBody())
2772f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov            GV->setLinkage(llvm::Function::DLLExportLinkage);
2782f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov        } else
2792f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov          GV->setLinkage(llvm::Function::DLLExportLinkage);
2802f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov      } else if (D->getAttr<WeakAttr>() || IsInline)
281219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar        GV->setLinkage(llvm::Function::WeakLinkage);
282219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
2830dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  }
284d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
28549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Figure out the relative priority of the attribute,
28649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // -fvisibility, and private_extern.
2870dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
28841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    setGlobalVisibility(GV, attr->getVisibility());
289d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes  // FIXME: else handle -fvisibility
290a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar
291eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  // Prefaced with special LLVM marker to indicate that the name
292eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  // should not be munged.
293eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>())
294a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    GV->setName("\01" + ALA->getLabel());
29517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
29617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
29717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar    GV->setSection(SA->getName());
2985c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar
2995c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // Only add to llvm.used when we see a definition, otherwise we
3005c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // might add multiple times or risk the value being replaced by a
3015c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // subsequent RAUW.
3025c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (ForDefinition) {
3035c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar    if (D->getAttr<UsedAttr>())
3045c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar      AddUsedGlobal(GV);
3055c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  }
306d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes}
307d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
308761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D,
30945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar                                          const CGFunctionInfo &Info,
310b768807c49a1c7085def099b848631856af766faDaniel Dunbar                                          llvm::Function *F) {
311761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  AttributeListType AttributeList;
31288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  ConstructAttributeList(Info, D, AttributeList);
313c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman
314761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
315761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                        AttributeList.size()));
316ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
317ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  // Set the appropriate calling convention for the Function.
31845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  if (D->getAttr<FastCallAttr>())
319f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_FastCall);
320f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov
321f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov  if (D->getAttr<StdCallAttr>())
322f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov    F->setCallingConv(llvm::CallingConv::X86_StdCall);
323f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
324f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
325f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes
326f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition.
327219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D,
328219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                                                       llvm::Function *F) {
329219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (isa<ObjCMethodDecl>(D)) {
330219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(D, true, false, F, true);
331219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else {
332219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const FunctionDecl *FD = cast<FunctionDecl>(D);
333219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
334219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                             FD->isInline(), F, true);
335219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  }
336219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
337f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar  if (!Features.Exceptions)
338f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar    F->addFnAttr(llvm::Attribute::NoUnwind);
339af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
340af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar  if (D->getAttr<AlwaysInlineAttr>())
341af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar    F->addFnAttr(llvm::Attribute::AlwaysInline);
34281ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson
34381ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson  if (D->getAttr<NoinlineAttr>())
34481ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    F->addFnAttr(llvm::Attribute::NoInline);
345f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
346f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
347f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD,
348f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                                        llvm::Function *F) {
349541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F);
350f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
351219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetFunctionAttributesForDefinition(MD, F);
352f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
353f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
354f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
355f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                                          llvm::Function *F) {
356541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
35745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
358219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
359219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                           FD->isInline(), F, false);
360219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar}
361219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
36245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
363219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() {
364219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
365219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const FunctionDecl *D = Aliases[i];
366219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const AliasAttr *AA = D->getAttr<AliasAttr>();
367219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
368219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // This is something of a hack, if the FunctionDecl got overridden
369219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // then its attributes will be moved to the new declaration. In
370219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // this case the current decl has no alias attribute, but we will
371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // eventually see it.
372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (!AA)
373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      continue;
374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    const std::string& aliaseeName = AA->getAliasee();
376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    llvm::Function *aliasee = getModule().getFunction(aliaseeName);
377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (!aliasee) {
378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // FIXME: This isn't unsupported, this is just an error, which
379219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // sema should catch, but...
380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      ErrorUnsupported(D, "alias referencing a missing function");
381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      continue;
382219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    llvm::GlobalValue *GA =
385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      new llvm::GlobalAlias(aliasee->getType(),
386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                            llvm::Function::ExternalLinkage,
3876ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                            getMangledName(D), aliasee,
3885f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                            &getModule());
389219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
3905f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor    llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
391219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (Entry) {
392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // If we created a dummy function for this then replace it.
393219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      GA->takeName(Entry);
394219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
395219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      llvm::Value *Casted =
396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar        llvm::ConstantExpr::getBitCast(GA, Entry->getType());
397219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry->replaceAllUsesWith(Casted);
398219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry->eraseFromParent();
399ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
400219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry = GA;
401219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
402219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
403219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // Alias should never be internal or inline.
404219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    SetGlobalValueAttributes(D, false, false, GA, true);
405219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  }
406ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman}
407ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman
4080269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
4090269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  assert(!GV->isDeclaration() &&
4100269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         "Only globals with definition can force usage.");
4110269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
4120269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy));
4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() {
4160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Don't create llvm.used if there is no need.
4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  if (LLVMUsed.empty())
4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    return;
4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(),
4210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                                              LLVMUsed.size());
4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  llvm::GlobalVariable *GV =
4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    new llvm::GlobalVariable(ATy, false,
4240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::GlobalValue::AppendingLinkage,
4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::ConstantArray::get(ATy, LLVMUsed),
4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             "llvm.used", &getModule());
4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4280269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  GV->setSection("llvm.metadata");
4290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
4300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() {
4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Emit code for any deferred decl which was used.  Since a
4330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // previously unused static decl may become used during the
4340269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // generation of code for a static function, iterate until no
4350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // changes are made.
4364c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  bool Changed;
4374c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  do {
4384c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    Changed = false;
439b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(),
4410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         e = DeferredDecls.end(); i != e; ) {
442b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson      const ValueDecl *D = *i;
443b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4446f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // Check if we have used a decl with the same name
4456f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // FIXME: The AST should have some sort of aggregate decls or
4466f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman      // global symbol map.
447219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      // FIXME: This is missing some important cases. For example, we
44873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      // need to check for uses in an alias.
4495f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor      if (!GlobalDeclMap.count(getMangledName(D))) {
450232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar        ++i;
451a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar        continue;
452b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson      }
453b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
454bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      // Emit the definition.
455bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      EmitGlobalDefinition(D);
456bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
4574c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Erase the used decl from the list.
4580269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar      i = DeferredDecls.erase(i);
459b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
4604c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Remember that we made a change.
4614c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      Changed = true;
4624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    }
4634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  } while (Changed);
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4668bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
4678bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue.  The annotation struct is
4688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
4693c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated.  The second field is the constant string
4708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation.  The third field is a constant
4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit.  The fourth field is
4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration.
4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc
4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///        appears to.
4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                const AnnotateAttr *AA,
4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                unsigned LineNo) {
4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Module *M = &getModule();
4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // get [N x i8] constants for the annotation string, and the filename string
4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // which are the 2nd and 3rd elements of the global annotation structure.
4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true);
4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(),
4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                  true);
4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Get the two global values corresponding to the ConstantArrays we just
4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // created to hold the bytes of the strings.
4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *annoGV =
4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(anno->getType(), false,
4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, anno,
4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           GV->getName() + ".str", M);
4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // translation unit name string, emitted into the llvm.metadata section.
4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *unitGV =
4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(unit->getType(), false,
4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, unit, ".str", M);
4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Create the ConstantStruct that is the global annotion.
5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *Fields[4] = {
5028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(GV, SBP),
5038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(annoGV, SBP),
5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(unitGV, SBP),
5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  };
5078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return llvm::ConstantStruct::get(Fields, 4, false);
5088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
5098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
51073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
5115c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // Never defer when EmitAllDecls is specified or the decl has
5125c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  // attribute used.
5135c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (Features.EmitAllDecls || Global->getAttr<UsedAttr>())
51473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    return false;
515bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
516bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
51773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Constructors and destructors should never be deferred.
51873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>())
51973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
52073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
52173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (FD->getStorageClass() != FunctionDecl::Static)
52273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  } else {
52473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
52573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    assert(VD->isFileVarDecl() && "Invalid decl.");
52673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (VD->getStorageClass() != VarDecl::Static)
52873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return false;
52973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  }
53073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  return true;
53273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar}
53373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
53473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) {
53573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
536219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // Aliases are deferred until code for everything else has been
537219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    // emitted.
538219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    if (FD->getAttr<AliasAttr>()) {
539219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      assert(!FD->isThisDeclarationADefinition() &&
540219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar             "Function alias cannot have a definition!");
541219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Aliases.push_back(FD);
542219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      return;
543219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    }
544219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
54573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
54673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (!FD->isThisDeclarationADefinition())
54773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
5480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  } else {
5490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
550bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
551bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
55273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
5537542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar    if (!VD->getInit() && VD->hasExternalStorage())
55473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
5554c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
5564c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
55773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  // Defer code generation when possible.
55873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  if (MayDeferGeneration(Global)) {
5590269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    DeferredDecls.push_back(Global);
560bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    return;
561bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
562bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
563bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  // Otherwise emit the definition.
564bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  EmitGlobalDefinition(Global);
5654c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
5664c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
567bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) {
568bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
569bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalFunctionDefinition(FD);
570bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
571bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    EmitGlobalVarDefinition(VD);
572bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else {
573bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(0 && "Invalid argument to EmitGlobalDefinition()");
574bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
575bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
576bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
5779986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) {
57877ba708819285931932ecd33691a672bb59d221aEli Friedman  assert(D->hasGlobalStorage() && "Not a global variable");
57977ba708819285931932ecd33691a672bb59d221aEli Friedman
580bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  QualType ASTTy = D->getType();
581bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
5829986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
583bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
5843c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
5855f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
58649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  if (!Entry) {
58749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    llvm::GlobalVariable *GV =
58849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      new llvm::GlobalVariable(Ty, false,
58949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar                               llvm::GlobalValue::ExternalLinkage,
5906ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                               0, getMangledName(D), &getModule(),
5915f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                               0, ASTTy.getAddressSpace());
59249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    Entry = GV;
59349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
59449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // Handle things which are present even on external declarations.
59549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
59649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // FIXME: This code is overly simple and should be merged with
59749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    // other global handling.
59849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
59949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    GV->setConstant(D->getType().isConstant(Context));
60049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
601eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar    // FIXME: Merge with other attribute handling code.
602eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
60349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    if (D->getStorageClass() == VarDecl::PrivateExtern)
60449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility);
605eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
606eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar    if (D->getAttr<WeakAttr>())
607eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar      GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
60849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  }
6093c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
6109986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  // Make sure the result is of the correct type.
6119986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  return llvm::ConstantExpr::getBitCast(Entry, PTy);
612bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
613bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
614bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
6158f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  llvm::Constant *Init = 0;
61677ba708819285931932ecd33691a672bb59d221aEli Friedman  QualType ASTTy = D->getType();
61777ba708819285931932ecd33691a672bb59d221aEli Friedman  const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy);
61877ba708819285931932ecd33691a672bb59d221aEli Friedman
6198f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  if (D->getInit() == 0) {
620cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // This is a tentative definition; tentative definitions are
621cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // implicitly initialized with { 0 }
622cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    const llvm::Type* InitTy;
623cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    if (ASTTy->isIncompleteArrayType()) {
624cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      // An incomplete array is normally [ TYPE x 0 ], but we need
625cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      // to fix it to [ TYPE x 1 ].
626cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy);
627cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      InitTy = llvm::ArrayType::get(ATy->getElementType(), 1);
628cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    } else {
629cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      InitTy = VarTy;
630cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    }
631cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    Init = llvm::Constant::getNullValue(InitTy);
63277ba708819285931932ecd33691a672bb59d221aEli Friedman  } else {
633bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    Init = EmitConstantExpr(D->getInit());
6346e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    if (!Init) {
635232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar      ErrorUnsupported(D, "static initializer");
6366e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      QualType T = D->getInit()->getType();
6376e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman      Init = llvm::UndefValue::get(getTypes().ConvertType(T));
6386e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    }
6398f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  }
64077ba708819285931932ecd33691a672bb59d221aEli Friedman  const llvm::Type* InitType = Init->getType();
6418e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel
6425f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
6433c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry);
6443c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
64577ba708819285931932ecd33691a672bb59d221aEli Friedman  if (!GV) {
64677ba708819285931932ecd33691a672bb59d221aEli Friedman    GV = new llvm::GlobalVariable(InitType, false,
64777ba708819285931932ecd33691a672bb59d221aEli Friedman                                  llvm::GlobalValue::ExternalLinkage,
6486ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                                  0, getMangledName(D),
6495f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                                  &getModule(), 0, ASTTy.getAddressSpace());
650232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
651232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar  } else if (GV->hasInitializer() && !GV->getInitializer()->isNullValue()) {
652232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // If we already have this global and it has an initializer, then
653232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // we are in the rare situation where we emitted the defining
654232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // declaration of the global and are now being asked to emit a
655232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // definition which would be common. This occurs, for example, in
656232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // the following situation because statics can be emitted out of
657232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // order:
658232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //
659232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int x;
660232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int *y = &x;
661232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  static int x = 10;
662232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //  int **z = &y;
663232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    //
664232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // Bail here so we don't blow away the definition. Note that if we
665232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // can't distinguish here if we emitted a definition with a null
666232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    // initializer, but this case is safe.
667232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    assert(!D->getInit() && "Emitting multiple definitions of a decl!");
668232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    return;
669232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
6703c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  } else if (GV->getType() !=
6713c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar             llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) {
67277ba708819285931932ecd33691a672bb59d221aEli Friedman    // We have a definition after a prototype with the wrong type.
67377ba708819285931932ecd33691a672bb59d221aEli Friedman    // We must make a new GlobalVariable* and update everything that used OldGV
67477ba708819285931932ecd33691a672bb59d221aEli Friedman    // (a declaration or tentative definition) with the new GlobalVariable*
67577ba708819285931932ecd33691a672bb59d221aEli Friedman    // (which will be a definition).
67677ba708819285931932ecd33691a672bb59d221aEli Friedman    //
67777ba708819285931932ecd33691a672bb59d221aEli Friedman    // This happens if there is a prototype for a global (e.g. "extern int x[];")
67877ba708819285931932ecd33691a672bb59d221aEli Friedman    // and then a definition of a different type (e.g. "int x[10];"). This also
67977ba708819285931932ecd33691a672bb59d221aEli Friedman    // happens when an initializer has a different type from the type of the
68077ba708819285931932ecd33691a672bb59d221aEli Friedman    // global (this happens with unions).
681cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    //
682cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // FIXME: This also ends up happening if there's a definition followed by
683cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // a tentative definition!  (Although Sema rejects that construct
684cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // at the moment.)
68577ba708819285931932ecd33691a672bb59d221aEli Friedman
68677ba708819285931932ecd33691a672bb59d221aEli Friedman    // Save the old global
68777ba708819285931932ecd33691a672bb59d221aEli Friedman    llvm::GlobalVariable *OldGV = GV;
68877ba708819285931932ecd33691a672bb59d221aEli Friedman
68977ba708819285931932ecd33691a672bb59d221aEli Friedman    // Make a new global with the correct type
69077ba708819285931932ecd33691a672bb59d221aEli Friedman    GV = new llvm::GlobalVariable(InitType, false,
69177ba708819285931932ecd33691a672bb59d221aEli Friedman                                  llvm::GlobalValue::ExternalLinkage,
6926ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                                  0, getMangledName(D),
6935f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                                  &getModule(), 0, ASTTy.getAddressSpace());
69477ba708819285931932ecd33691a672bb59d221aEli Friedman    // Steal the name of the old global
69577ba708819285931932ecd33691a672bb59d221aEli Friedman    GV->takeName(OldGV);
69677ba708819285931932ecd33691a672bb59d221aEli Friedman
69777ba708819285931932ecd33691a672bb59d221aEli Friedman    // Replace all uses of the old global with the new global
69877ba708819285931932ecd33691a672bb59d221aEli Friedman    llvm::Constant *NewPtrForOldDecl =
69977ba708819285931932ecd33691a672bb59d221aEli Friedman        llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
70077ba708819285931932ecd33691a672bb59d221aEli Friedman    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
70177ba708819285931932ecd33691a672bb59d221aEli Friedman
70277ba708819285931932ecd33691a672bb59d221aEli Friedman    // Erase the old global, since it is no longer used.
70377ba708819285931932ecd33691a672bb59d221aEli Friedman    OldGV->eraseFromParent();
70477ba708819285931932ecd33691a672bb59d221aEli Friedman  }
70577ba708819285931932ecd33691a672bb59d221aEli Friedman
7063c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  Entry = GV;
7079e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel
7088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
7098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    SourceManager &SM = Context.getSourceManager();
7108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    AddAnnotation(EmitAnnotateAttr(GV, AA,
711f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner                              SM.getInstantiationLineNumber(D->getLocation())));
7128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
7138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
71488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  GV->setInitializer(Init);
715b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes  GV->setConstant(D->getType().isConstant(Context));
716ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
717cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman  // FIXME: This is silly; getTypeAlign should just work for incomplete arrays
718cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman  unsigned Align;
719c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType* IAT =
720c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner        Context.getAsIncompleteArrayType(D->getType()))
721cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    Align = Context.getTypeAlign(IAT->getElementType());
722cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman  else
723cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    Align = Context.getTypeAlign(D->getType());
7246aee306b1c653bc7d86ae6cb2b905e8b8d0ac27fDaniel Dunbar  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
72508d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman    Align = std::max(Align, AA->getAlignment());
72608d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman  GV->setAlignment(Align / 8);
72708d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman
728ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
72941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    setGlobalVisibility(GV, attr->getVisibility());
730ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  // FIXME: else handle -fvisibility
731a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar
732a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
733a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    // Prefaced with special LLVM marker to indicate that the name
734a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    // should not be munged.
735a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar    GV->setName("\01" + ALA->getLabel());
736a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar  }
73788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner
73888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  // Set the llvm linkage type as appropriate.
7398fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  if (D->getStorageClass() == VarDecl::Static)
7408fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
7418fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else if (D->getAttr<DLLImportAttr>())
742ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLImportLinkage);
743ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  else if (D->getAttr<DLLExportAttr>())
744ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLExportLinkage);
7458fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else if (D->getAttr<WeakAttr>())
746ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
7478fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else {
748ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // FIXME: This isn't right.  This should handle common linkage and other
749ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // stuff.
750ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    switch (D->getStorageClass()) {
7518fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    case VarDecl::Static: assert(0 && "This case handled above");
752ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Auto:
753ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Register:
754ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      assert(0 && "Can't have auto or register globals");
755ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::None:
756ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      if (!D->getInit())
757a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman        GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
75898883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson      else
75998883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson        GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
760ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
761ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Extern:
76249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      // FIXME: common
76349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      break;
76449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
765ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::PrivateExtern:
76649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
76749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar      // FIXME: common
768ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
769ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    }
77088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  }
771686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta
77217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
77317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar    GV->setSection(SA->getName());
77417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
7755c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  if (D->getAttr<UsedAttr>())
7765c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar    AddUsedGlobal(GV);
7775c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar
778686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  // Emit global variable debug information.
779686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  CGDebugInfo *DI = getDebugInfo();
780686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  if(DI) {
78166031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar    DI->setLocation(D->getLocation());
782686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta    DI->EmitGlobalVariable(GV, D);
783686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  }
78488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner}
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
786bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue *
787d5d31801fc87239436fa349c89dce7797cf13537Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D,
788d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar                                             const llvm::Type *Ty) {
789d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  if (!Ty)
790d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Ty = getTypes().ConvertType(D->getType());
791219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty),
792219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                                             llvm::Function::ExternalLinkage,
7936ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor                                             getMangledName(D),
7945f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor                                             &getModule());
795219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetFunctionAttributes(D, F);
796219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  return F;
797bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
798bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
799bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) {
8009986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  QualType ASTTy = D->getType();
8019986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
8029986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
8033c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
8043c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
8055f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
8063c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  if (!Entry)
807d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Entry = EmitForwardFunctionDefinition(D, 0);
808bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
8099986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar  return llvm::ConstantExpr::getBitCast(Entry, PTy);
810bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
811bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
812bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) {
813d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  const llvm::FunctionType *Ty =
814d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    cast<llvm::FunctionType>(getTypes().ConvertType(D->getType()));
815d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
816d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // As a special case, make sure that definitions of K&R function
817d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // "type foo()" aren't declared as varargs (which forces the backend
818d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  // to do unnecessary work).
819d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar  if (Ty->isVarArg() && Ty->getNumParams() == 0 && Ty->isVarArg())
820d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Ty = llvm::FunctionType::get(Ty->getReturnType(),
821d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar                                 std::vector<const llvm::Type*>(),
822d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar                                 false);
823d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
8245f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
8253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  if (!Entry) {
826d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    Entry = EmitForwardFunctionDefinition(D, Ty);
827bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  } else {
8283c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar    // If the types mismatch then we have to rewrite the definition.
8293c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar    if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) {
830d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // Otherwise, we have a definition after a prototype with the
831d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // wrong type.  F is the Function* for the one with the wrong
832d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // type, we must make a new Function* and update everything that
833d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // used F (a declaration) with the new Function* (which will be
834d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // a definition).
835bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      //
836d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // This happens if there is a prototype for a function
837d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // (e.g. "int f()") and then a definition of a different type
838d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // (e.g. "int f(int x)").  Start by making a new function of the
839d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      // correct type, RAUW, then steal the name.
840d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar      llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D, Ty);
8413c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar      NewFn->takeName(Entry);
842bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
843bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      // Replace uses of F with the Function we will endow with a body.
844bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      llvm::Constant *NewPtrForOldDecl =
8453c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar        llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
8463c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar      Entry->replaceAllUsesWith(NewPtrForOldDecl);
8473c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar
848bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      // Ok, delete the old function now, which is dead.
8493c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar      assert(Entry->isDeclaration() && "Shouldn't replace non-declaration");
850219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      Entry->eraseFromParent();
851bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
852bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      Entry = NewFn;
853bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    }
854bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
855bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
856219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  llvm::Function *Fn = cast<llvm::Function>(Entry);
857219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  CodeGenFunction(*this).GenerateCode(D, Fn);
8586379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar
859219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  SetFunctionAttributesForDefinition(D, Fn);
860219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
861219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) {
862219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalCtor(Fn, CA->getPriority());
863219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) {
864219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalDtor(Fn, DA->getPriority());
865bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
866bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
867bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
868f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function *
869f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
870f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                     const std::string &Name) {
871f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  llvm::Function *Fn = llvm::Function::Create(FTy,
872f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                              llvm::Function::ExternalLinkage,
873f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                              "", &TheModule);
874f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  RuntimeFunctions.push_back(std::make_pair(Fn, Name));
875f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar  return Fn;
876f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
877f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
878c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
879c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  // Make sure that this type is translated.
880c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  Types.UpdateCompletedType(TD);
881d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
882d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
883d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
884bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction
885bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
8861426fecdf90dd1986751b9940422e675880ff671Chris Lattner  if (BuiltinID > BuiltinFunctions.size())
8871426fecdf90dd1986751b9940422e675880ff671Chris Lattner    BuiltinFunctions.resize(BuiltinID);
888bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
8891426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // Cache looked up functions.  Since builtin id #0 is invalid we don't reserve
8901426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // a slot for it.
8911426fecdf90dd1986751b9940422e675880ff671Chris Lattner  assert(BuiltinID && "Invalid Builtin ID");
8921426fecdf90dd1986751b9940422e675880ff671Chris Lattner  llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
893bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  if (FunctionSlot)
894bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    return FunctionSlot;
895bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
8963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
8973e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor          Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
8983e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor         "isn't a lib fn");
899bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9003e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  // Get the name, skip over the __builtin_ prefix (if necessary).
9013e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
9023e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  if (Context.BuiltinInfo.isLibFunction(BuiltinID))
9033e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor    Name += 10;
904bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
905bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // Get the type for the builtin.
906370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  Builtin::Context::GetBuiltinTypeError Error;
907370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error);
908370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  assert(Error == Builtin::Context::GE_None && "Can't get builtin type");
909370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor
910bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  const llvm::FunctionType *Ty =
911bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    cast<llvm::FunctionType>(getTypes().ConvertType(Type));
912bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
913bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: This has a serious problem with code like this:
914bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //  void abs() {}
915bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //    ... __builtin_abs(x);
916bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // The two versions of abs will collide.  The fix is for the builtin to win,
917bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // and for the existing one to be turned into a constantexpr cast of the
918bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // builtin.  In the case where the existing one is a static function, it
919bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // should just be renamed.
920c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  if (llvm::Function *Existing = getModule().getFunction(Name)) {
921c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
922c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner      return FunctionSlot = Existing;
923c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    assert(Existing == 0 && "FIXME: Name collision");
924c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  }
925bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
926bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: param attributes for sext/zext etc.
9274c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  return FunctionSlot =
9284c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name,
9294c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman                           &getModule());
930bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner}
931bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9327acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
9337acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                            unsigned NumTys) {
9347acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return llvm::Intrinsic::getDeclaration(&getModule(),
9357acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
9367acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner}
937bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() {
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (MemCpyFn) return MemCpyFn;
9404e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9414e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1);
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
943c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
9440c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() {
9450c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman  if (MemMoveFn) return MemMoveFn;
9464e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9474e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1);
9480c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman}
9490c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman
95041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() {
95141ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  if (MemSetFn) return MemSetFn;
9524e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
9534e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner  return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1);
95441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio}
9557acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner
956e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM,
957e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                  std::vector<llvm::Constant*>& Fields,
95844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  FieldDecl *FieldD, FieldDecl *NextFieldD,
95944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                  llvm::Constant* Field,
960e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                  RecordDecl* RD, const llvm::StructType *STy)
961e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{
962e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append the field.
963e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  Fields.push_back(Field);
964e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
96544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD);
966e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
967e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  int NextStructFieldNo;
96844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (!NextFieldD) {
969e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    NextStructFieldNo = STy->getNumElements();
970e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  } else {
97144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD);
972e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
973e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
974e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  // Append padding
975e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) {
976e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    llvm::Constant *C =
977e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson      llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1));
978e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
979e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    Fields.push_back(C);
980e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  }
981e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson}
982e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
9833e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16.
9843e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215>
985bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule::
986bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) {
987c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::StringMapEntry<llvm::Constant *> &Entry =
988c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
989c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
990c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (Entry.getValue())
991c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    return Entry.getValue();
992c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
9933e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
9943e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zeros[] = { Zero, Zero };
995c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
996c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (!CFConstantStringClassRef) {
997c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
998c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    Ty = llvm::ArrayType::get(Ty, 0);
9993e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10003e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // FIXME: This is fairly broken if
10013e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // __CFConstantStringClassReference is already defined, in that it
10023e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // will get renamed and the user will most likely see an opaque
10033e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // error message. This is a general issue with relying on
10043e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // particular names.
10053e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    llvm::GlobalVariable *GV =
1006c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      new llvm::GlobalVariable(Ty, false,
1007c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalVariable::ExternalLinkage, 0,
1008c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               "__CFConstantStringClassReference",
1009c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               &getModule());
10103e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10113e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // Decay array -> ptr
10123e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    CFConstantStringClassRef =
10133e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1014c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
1015c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1016e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  QualType CFTy = getContext().getCFConstantStringType();
1017e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl();
10183e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
1019e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  const llvm::StructType *STy =
1020e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1021e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
1022e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  std::vector<llvm::Constant*> Fields;
102344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  RecordDecl::field_iterator Field = CFRD->field_begin();
102444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1025c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Class pointer.
102644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *CurField = *Field++;
102744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  FieldDecl *NextField = *Field++;
102844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
102944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        CFConstantStringClassRef, CFRD, STy);
1030c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1031c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Flags.
103244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
103344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
10343e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
103544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
103644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy);
1037c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1038c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String pointer.
103944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
104044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = *Field++;
10413e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str);
1042c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = new llvm::GlobalVariable(C->getType(), true,
1043c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalValue::InternalLinkage,
1044e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                               C, ".str", &getModule());
104544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
1046e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                        llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2),
1047e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                        CFRD, STy);
1048c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1049c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String length.
105044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  CurField = NextField;
105144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  NextField = 0;
1052c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().LongTy);
105344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  appendFieldAndPadding(*this, Fields, CurField, NextField,
105444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                        llvm::ConstantInt::get(Ty, str.length()), CFRD, STy);
1055c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
1056c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // The struct.
1057e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  C = llvm::ConstantStruct::get(STy, Fields);
10580c67829763b98bc670062b553897a851fab17401Anders Carlsson  llvm::GlobalVariable *GV =
10590c67829763b98bc670062b553897a851fab17401Anders Carlsson    new llvm::GlobalVariable(C->getType(), true,
10600c67829763b98bc670062b553897a851fab17401Anders Carlsson                             llvm::GlobalVariable::InternalLinkage,
10610c67829763b98bc670062b553897a851fab17401Anders Carlsson                             C, "", &getModule());
10623e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10630c67829763b98bc670062b553897a851fab17401Anders Carlsson  GV->setSection("__DATA,__cfstring");
10640c67829763b98bc670062b553897a851fab17401Anders Carlsson  Entry.setValue(GV);
10653e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
10660c67829763b98bc670062b553897a851fab17401Anders Carlsson  return GV;
1067c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson}
106845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
10696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a
10701e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type.
10716143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
10721e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const char *StrData = E->getStrData();
10731e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  unsigned Len = E->getByteLength();
10741e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
10751e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const ConstantArrayType *CAT =
10761e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    getContext().getAsConstantArrayType(E->getType());
10771e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  assert(CAT && "String isn't pointer or array!");
10781e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
1079dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  // Resize the string to the right size.
10801e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  std::string Str(StrData, StrData+Len);
10811e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  uint64_t RealLen = CAT->getSize().getZExtValue();
1082dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
1083dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  if (E->isWide())
1084dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    RealLen *= getContext().Target.getWCharWidth()/8;
1085dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner
10861e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  Str.resize(RealLen, '\0');
10871e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
10881e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  return Str;
10891e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar}
10901e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
10916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
10926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal.
10936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant *
10946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
10956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // FIXME: This can be more efficient.
10966143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  return GetAddrOfConstantString(GetStringForStringLiteral(S));
10976143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
10986143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
1099eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1100eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node.
1101eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant *
1102eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1103eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  std::string Str;
1104eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1105eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1106eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  llvm::Constant *C = llvm::ConstantArray::get(Str);
1107eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  C = new llvm::GlobalVariable(C->getType(), true,
1108eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                               llvm::GlobalValue::InternalLinkage,
1109eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                               C, ".str", &getModule());
1110eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  return C;
1111eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner}
1112eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1113eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1114a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal.
111545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str,
111645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                             bool constant,
11175fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             CodeGenModule &CGM,
11185fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             const char *GlobalName) {
11196143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // Create Constant for this string literal. Don't add a '\0'.
11206143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  llvm::Constant *C = llvm::ConstantArray::get(str, false);
112145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
112245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this string
1123eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  return new llvm::GlobalVariable(C->getType(), constant,
1124eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  llvm::GlobalValue::InternalLinkage,
1125eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  C, GlobalName ? GlobalName : ".str",
1126eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                  &CGM.getModule());
112745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
112845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
11296143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array
11306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the
11316143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically;
11326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is
11336143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on
11346143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings.
11356143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar///
11366143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type.
11375fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
11385fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                       const char *GlobalName) {
113945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Don't share any string literals if writable-strings is turned on.
114045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Features.WritableStrings)
11415fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    return GenerateStringLiteral(str, false, *this, GlobalName);
114245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
114345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::StringMapEntry<llvm::Constant *> &Entry =
114445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
114545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
114645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Entry.getValue())
1147eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner    return Entry.getValue();
114845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
114945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this.
11505fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
115145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  Entry.setValue(C);
115245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
115345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
11546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
11556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character
11566143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-'
11576143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type.
11585fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
11595fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                        const char *GlobalName){
1160c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner  return GetAddrOfConstantString(str + '\0', GlobalName);
11616143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
116241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1163af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized
1164af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation.
1165af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const
1166af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                                                    ObjCImplementationDecl *D) {
1167af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(),
1168af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         e = D->propimpl_end(); i != e; ++i) {
1169af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCPropertyImplDecl *PID = *i;
1170af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1171af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Dynamic is just for type-checking.
1172af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1173af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      ObjCPropertyDecl *PD = PID->getPropertyDecl();
1174af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1175af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // Determine which methods need to be implemented, some may have
1176af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // been overridden. Note that ::isSynthesized is not the method
1177af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // we want, that just indicates if the decl came from a
1178af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // property. What we want to know is if the method is defined in
1179af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // this implementation.
1180af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!D->getInstanceMethod(PD->getGetterName()))
1181fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCGetter(
1182fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1183af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!PD->isReadOnly() &&
1184af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar          !D->getInstanceMethod(PD->getSetterName()))
1185fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCSetter(
1186fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1187af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    }
1188af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
1189af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
1190af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
119141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration.
119241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) {
119341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // If an error has occurred, stop code generation, but continue
119441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // parsing and semantic analysis (to ensure all warnings and errors
119541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // are emitted).
119641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  if (Diags.hasErrorOccurred())
119741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
119841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
119941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  switch (D->getKind()) {
120041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Function:
120141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Var:
120241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    EmitGlobal(cast<ValueDecl>(D));
120341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
120441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
120541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Namespace:
1206662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar    ErrorUnsupported(D, "namespace");
120741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
120841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
120941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Objective-C Decls
121041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
121141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Forward declarations, no (immediate) code generation.
121241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCClass:
121341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCategory:
121441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCForwardProtocol:
121541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCInterface:
121641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
121741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
121841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCProtocol:
121941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
122041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
122141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
122241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCategoryImpl:
1223af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Categories have properties but don't support synthesize so we
1224af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // can ignore them here.
1225af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
122641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
122741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
122841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1229af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  case Decl::ObjCImplementation: {
1230af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1231af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    EmitObjCPropertyImplementations(OMD);
1232af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    Runtime->GenerateClass(OMD);
123341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
1234af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
123541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCMethod: {
123641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
123741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // If this is not a prototype, emit the body.
123841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (OMD->getBody())
123941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      CodeGenFunction(*this).GenerateObjCMethod(OMD);
124041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
124141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
124241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCompatibleAlias:
1243305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian    // compatibility-alias is a directive and has no code gen.
124441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
124541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
124641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::LinkageSpec: {
124741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D);
124841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
1249488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar      ErrorUnsupported(LSD, "linkage spec");
125041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // FIXME: implement C++ linkage, C linkage works mostly by C
125141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // language reuse already.
125241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
125341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
125441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
125541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::FileScopeAsm: {
125641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
125741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    std::string AsmString(AD->getAsmString()->getStrData(),
125841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                          AD->getAsmString()->getByteLength());
125941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
126041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    const std::string &S = getModule().getModuleInlineAsm();
126141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (S.empty())
126241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(AsmString);
126341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    else
126441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(S + '\n' + AsmString);
126541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
126641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
126741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
126841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  default:
126941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Make sure we handled everything we should, every other kind is
127041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // a non-top-level decl.  FIXME: Would be nice to have an
127141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // isTopLevelDeclKind function. Need to recode Decl::Kind to do
127241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // that easily.
127341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    assert(isa<TypeDecl>(D) && "Unsupported decl kind");
127441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
127541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar}
1276