CodeGenModule.cpp revision 4c73307c74764ba99e1379677fe92af72f676531
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
173d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman#include "CodeGenTBAA.h"
180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h"
20af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h"
215f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h"
2282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h"
2306057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
25687cc4a850b59116efee061018f0d8df50728b82Ken Dyck#include "clang/AST/CharUnits.h"
26c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2721ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h"
28af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor#include "clang/AST/DeclTemplate.h"
291a5e0d7f18485e4fb958f96dcddff3e4486a4069Anders Carlsson#include "clang/AST/RecordLayout.h"
301b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
312c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h"
328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h"
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
34e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h"
35ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h"
36bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h"
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h"
38d5b8902c0329d80a216803b58dc3b689349a0c11Chris Lattner#include "llvm/LLVMContext.h"
396374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall#include "llvm/ADT/Triple.h"
4020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h"
416ba728d9687b2617793f5afd410650a8d6c71080Gabor Greif#include "llvm/Support/CallSite.h"
4278f7ece00e2ddfb64d4ed72a7be770b5b9b805e3Chris Lattner#include "llvm/Support/ErrorHandling.h"
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
46f16aa103d3afd42fbca2ab346f191bf745cec092John McCallstatic CGCXXABI &createCXXABI(CodeGenModule &CGM) {
47f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  switch (CGM.getContext().Target.getCXXABI()) {
48f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  case CXXABI_ARM: return *CreateARMCXXABI(CGM);
49f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM);
50f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM);
51f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  }
52f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
53f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  llvm_unreachable("invalid C++ ABI kind");
54f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return *CreateItaniumCXXABI(CGM);
55f16aa103d3afd42fbca2ab346f191bf745cec092John McCall}
56f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
582811ccf48d6d898c42cc4cfad37abedb36236d20Chandler CarruthCodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
59468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall                             llvm::Module &M, const llvm::TargetData &TD,
60468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall                             Diagnostic &diags)
61bd3606426d389370616126af969904ec493cb105Chris Lattner  : BlockModule(C, M, TD, Types, *this), Context(C),
622811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth    Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M),
63468ec6c0266e48fccb26ce50d5b915c645bb3c7bJohn McCall    TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
64f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    ABI(createCXXABI(*this)),
65f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI),
663d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman    TBAA(0),
67f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    VTables(*this), Runtime(0),
684c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    CFConstantStringClassRef(0), ConstantStringClassRef(0),
69673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    VMContext(M.getContext()),
70754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0),
71673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
72754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0),
73673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    BlockObjectAssign(0), BlockObjectDispose(0){
74208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
753c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  if (!Features.ObjC1)
763c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = 0;
773c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (!Features.NeXTRuntime)
783c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateGNUObjCRuntime(*this);
793c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else if (Features.ObjCNonFragileABI)
803c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
813c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  else
823c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    Runtime = CreateMacObjCRuntime(*this);
83e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
843d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman  // Enable TBAA unless it's suppressed.
853d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman  if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)
860b5c4fc2ae3b503c2b1f354bf52b718aa50a6aeeDan Gohman    TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(),
870b5c4fc2ae3b503c2b1f354bf52b718aa50a6aeeDan Gohman                           ABI.getMangleContext());
883d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman
89e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  // If debug info generation is enabled, create the CGDebugInfo object.
9020f12a20ba9cfa6f8d53c8304e24f50903c45184Anders Carlsson  DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0;
912b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
922b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
932b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() {
94815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete Runtime;
95f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  delete &ABI;
964376c85fb0ac9e7fd779d246efc77e1169179138Dan Gohman  delete TBAA;
97815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  delete DebugInfo;
98815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek}
99815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek
1000d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallvoid CodeGenModule::createObjCRuntime() {
1010d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  if (!Features.NeXTRuntime)
1020d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    Runtime = CreateGNUObjCRuntime(*this);
1030d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  else if (Features.ObjCNonFragileABI)
1040d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
1050d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  else
1060d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    Runtime = CreateMacObjCRuntime(*this);
1070d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall}
1080d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall
109815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() {
11082227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner  EmitDeferred();
1116c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman  EmitCXXGlobalInitFunc();
112efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  EmitCXXGlobalDtorFunc();
113208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  if (Runtime)
114208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
115208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      AddGlobalCtor(ObjCInitFunction);
1166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalCtors, "llvm.global_ctors");
1176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  EmitCtorList(GlobalDtors, "llvm.global_dtors");
118532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  EmitAnnotations();
1190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  EmitLLVMUsed();
120744016dde06fcffd50931e94a98c850f8b12cd87John McCall
121b25938303de0976b9f189363d43033e5788e3d36John McCall  SimplifyPersonality();
122b25938303de0976b9f189363d43033e5788e3d36John McCall
123744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (getCodeGenOpts().EmitDeclMetadata)
124744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
125f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar}
126f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
1273d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohmanllvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
1283d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman  if (!TBAA)
1293d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman    return 0;
1303d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman  return TBAA->getTBAAInfo(QTy);
1313d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman}
1323d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman
1333d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohmanvoid CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
1343d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                        llvm::MDNode *TBAAInfo) {
1353d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman  Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
1363d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman}
1373d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman
1386374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool CodeGenModule::isTargetDarwin() const {
1396374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin;
1406374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall}
1416374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
142488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
1432c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet.
14490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
14590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
14690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
14790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
14956b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
1502c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  std::string Msg = Type;
1510a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
1520a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    << Msg << S->getSourceRange();
1532c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner}
15458c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
155488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
156c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet.
15790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
15890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                     bool OmitOnError) {
15990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  if (OmitOnError && getDiags().hasErrorOccurred())
16090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar    return;
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
16256b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar                                               "cannot compile this %0 yet");
163c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  std::string Msg = Type;
1640a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
165c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
166c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLangOptions::VisibilityMode
16804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const {
16904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
170d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (VD->getStorageClass() == SC_PrivateExtern)
17104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Hidden;
17204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
17340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) {
17404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    switch (attr->getVisibility()) {
17504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    default: assert(0 && "Unknown visibility!");
176cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    case VisibilityAttr::Default:
17704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Default;
178cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    case VisibilityAttr::Hidden:
17904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Hidden;
180cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    case VisibilityAttr::Protected:
18104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      return LangOptions::Protected;
18204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    }
1837e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  }
1847cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
185af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor  if (getLangOptions().CPlusPlus) {
186af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    // Entities subject to an explicit instantiation declaration get default
187af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    // visibility.
188af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
189af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor      if (Function->getTemplateSpecializationKind()
190af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor                                        == TSK_ExplicitInstantiationDeclaration)
191af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor        return LangOptions::Default;
192af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    } else if (const ClassTemplateSpecializationDecl *ClassSpec
193af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor                              = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
194af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor      if (ClassSpec->getSpecializationKind()
195af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor                                        == TSK_ExplicitInstantiationDeclaration)
196af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor        return LangOptions::Default;
197af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    } else if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
198af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor      if (Record->getTemplateSpecializationKind()
199af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor                                        == TSK_ExplicitInstantiationDeclaration)
200af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor        return LangOptions::Default;
201af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    } else if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
202af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor      if (Var->isStaticDataMember() &&
203af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor          (Var->getTemplateSpecializationKind()
204af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor                                      == TSK_ExplicitInstantiationDeclaration))
205af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor        return LangOptions::Default;
206af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    }
207af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor
208af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    // If -fvisibility-inlines-hidden was provided, then inline C++ member
209af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    // functions get "hidden" visibility by default.
210af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor    if (getLangOptions().InlineVisibilityHidden)
211af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor      if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
212af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor        if (Method->isInlined())
213af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor          return LangOptions::Hidden;
214af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor  }
215af896897f7485176f43d40c4adced7efb0fb2b06Douglas Gregor
216aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  // If this decl is contained in a class, it should have the same visibility
217aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  // as the parent class.
21839de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson  if (const DeclContext *DC = D->getDeclContext())
219aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    if (DC->isRecord())
220aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman      return getDeclVisibilityMode(cast<Decl>(DC));
22139de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson
22204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  return getLangOptions().getVisibilityMode();
2234f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman}
2244f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
22604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                        const Decl *D) const {
22704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  // Internal definitions always have default visibility.
228df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner  if (GV->hasLocalLinkage()) {
2297e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2306ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar    return;
2317e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  }
2326ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar
23304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  switch (getDeclVisibilityMode(D)) {
2347cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian  default: assert(0 && "Unknown visibility!");
23504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Default:
23604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
23704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Hidden:
23804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
23904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  case LangOptions::Protected:
24004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
2417cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian  }
2427cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian}
2437cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian
244cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall/// Set the symbol visibility of type information (vtable and RTTI)
245cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall/// associated with the given type.
246cbfe50224b19119e759802bd0c1463269dffd09eJohn McCallvoid CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV,
247cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall                                      const CXXRecordDecl *RD,
248cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall                                      bool IsForRTTI) const {
249cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  setGlobalVisibility(GV, RD);
250cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
251279b5eb6910d64a293e9c0e2887a05c65d8737d7John McCall  if (!CodeGenOpts.HiddenWeakVTables)
252279b5eb6910d64a293e9c0e2887a05c65d8737d7John McCall    return;
253279b5eb6910d64a293e9c0e2887a05c65d8737d7John McCall
254cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // We want to drop the visibility to hidden for weak type symbols.
255cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // This isn't possible if there might be unresolved references
256cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // elsewhere that rely on this symbol being visible.
257cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
2587a536907da776bdc47a704e7cafd641e8150e653John McCall  // This should be kept roughly in sync with setThunkVisibility
2597a536907da776bdc47a704e7cafd641e8150e653John McCall  // in CGVTables.cpp.
2607a536907da776bdc47a704e7cafd641e8150e653John McCall
261cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // Preconditions.
262cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  if (GV->getLinkage() != llvm::GlobalVariable::WeakODRLinkage ||
263cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall      GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
264cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    return;
265cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
266cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // Don't override an explicit visibility attribute.
267cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  if (RD->hasAttr<VisibilityAttr>())
268cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    return;
269cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
270cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  switch (RD->getTemplateSpecializationKind()) {
271cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // We have to disable the optimization if this is an EI definition
272cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // because there might be EI declarations in other shared objects.
273cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  case TSK_ExplicitInstantiationDefinition:
274cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  case TSK_ExplicitInstantiationDeclaration:
275cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    return;
276cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
2777a536907da776bdc47a704e7cafd641e8150e653John McCall  // Every use of a non-template class's type information has to emit it.
278cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  case TSK_Undeclared:
279cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    break;
280cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
2817a536907da776bdc47a704e7cafd641e8150e653John McCall  // In theory, implicit instantiations can ignore the possibility of
2827a536907da776bdc47a704e7cafd641e8150e653John McCall  // an explicit instantiation declaration because there necessarily
2837a536907da776bdc47a704e7cafd641e8150e653John McCall  // must be an EI definition somewhere with default visibility.  In
2847a536907da776bdc47a704e7cafd641e8150e653John McCall  // practice, it's possible to have an explicit instantiation for
2857a536907da776bdc47a704e7cafd641e8150e653John McCall  // an arbitrary template class, and linkers aren't necessarily able
2867a536907da776bdc47a704e7cafd641e8150e653John McCall  // to deal with mixed-visibility symbols.
2877a536907da776bdc47a704e7cafd641e8150e653John McCall  case TSK_ExplicitSpecialization:
288cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  case TSK_ImplicitInstantiation:
289279b5eb6910d64a293e9c0e2887a05c65d8737d7John McCall    if (!CodeGenOpts.HiddenWeakTemplateVTables)
2907a536907da776bdc47a704e7cafd641e8150e653John McCall      return;
291cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    break;
292cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  }
293cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
294cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // If there's a key function, there may be translation units
295cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // that don't have the key function's definition.  But ignore
296cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // this if we're emitting RTTI under -fno-rtti.
297cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  if (!IsForRTTI || Features.RTTI)
298cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall    if (Context.getKeyFunction(RD))
299cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall      return;
300cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
301cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  // Otherwise, drop the visibility to hidden.
302cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall  GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
303cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall}
304cbfe50224b19119e759802bd0c1463269dffd09eJohn McCall
305793a990774826a0c20b0da66cec0991badfb8b63Anders Carlssonllvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
306793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
307793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
308793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
309793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  if (!Str.empty())
310793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson    return Str;
311793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
3124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
313793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson    IdentifierInfo *II = ND->getIdentifier();
314793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson    assert(II && "Attempt to mangle unnamed decl.");
315793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
316793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson    Str = II->getName();
317793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson    return Str;
318793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  }
319793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
320793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  llvm::SmallString<256> Buffer;
321793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
3224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Buffer);
323793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
3244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer);
325793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND))
3264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getCXXABI().getMangleContext().mangleBlock(GD, BD, Buffer);
327793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  else
3284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getCXXABI().getMangleContext().mangleName(ND, Buffer);
329793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
330793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  // Allocate space for the mangled name.
331793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  size_t Length = Buffer.size();
332793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  char *Name = MangledNamesAllocator.Allocate<char>(Length);
333793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  std::copy(Buffer.begin(), Buffer.end(), Name);
334793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
335793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  Str = llvm::StringRef(Name, Length);
336793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
337793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson  return Str;
338793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson}
339793a990774826a0c20b0da66cec0991badfb8b63Anders Carlsson
340564360be450b319aeafc26698be9811837bfb826Fariborz Jahanianvoid CodeGenModule::getMangledName(GlobalDecl GD, MangleBuffer &Buffer,
341564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian                                   const BlockDecl *BD) {
3424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  getCXXABI().getMangleContext().mangleBlock(GD, BD, Buffer.getBuffer());
3439a8822bb154b792cdb18fe4cfb34480ca0ec7661Anders Carlsson}
3449a8822bb154b792cdb18fe4cfb34480ca0ec7661Anders Carlsson
345f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) {
346f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  return getModule().getNamedValue(Name);
3475f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor}
3485f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
3496d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before
3506d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs.
3516bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
35249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
3536bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalCtors.push_back(std::make_pair(Ctor, Priority));
3546d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
3556d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
3566bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called
3576bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded.
3586bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
35949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  // FIXME: Type coercion of void()* types.
3606bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  GlobalDtors.push_back(std::make_pair(Dtor, Priority));
3616bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
3626bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
3636bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
3646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Ctor function type is void()*.
3656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  llvm::FunctionType* CtorFTy =
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
3676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            std::vector<const llvm::Type*>(),
3686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                            false);
36996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
3706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
3716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Get the type of a ctor entry, { i32, void ()* }.
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::StructType* CtorStructTy =
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext),
37496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                          llvm::PointerType::getUnqual(CtorFTy), NULL);
3756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
3766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // Construct the constructor and destructor arrays.
3776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  std::vector<llvm::Constant*> Ctors;
3786bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
3796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    std::vector<llvm::Constant*> S;
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3810032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                I->second, false));
3823c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
38308e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
3846bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
3856bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
3866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  if (!Ctors.empty()) {
38796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
3881c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    new llvm::GlobalVariable(TheModule, AT, false,
389572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             llvm::GlobalValue::AppendingLinkage,
3907db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson                             llvm::ConstantArray::get(AT, Ctors),
3911c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                             GlobalName);
3926d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  }
3936d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
3946d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
395532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() {
396532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  if (Annotations.empty())
397532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman    return;
398532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
399532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create a new global variable for the ConstantStruct in the Module.
400532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::Constant *Array =
40196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
402532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                                                Annotations.size()),
403532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           Annotations);
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalValue *gv =
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  new llvm::GlobalVariable(TheModule, Array->getType(), false,
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                           llvm::GlobalValue::AppendingLinkage, Array,
4071c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                           "llvm.global.annotations");
408532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  gv->setSection("llvm.metadata");
409532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman}
410532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
411d46f98573ba104eda102dd3224b2dca69f1c6336John McCallllvm::GlobalValue::LinkageTypes
412d46f98573ba104eda102dd3224b2dca69f1c6336John McCallCodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
41390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
4147c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
415f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (Linkage == GVA_Internal)
416d46f98573ba104eda102dd3224b2dca69f1c6336John McCall    return llvm::Function::InternalLinkage;
417f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
418f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (D->hasAttr<DLLExportAttr>())
419d46f98573ba104eda102dd3224b2dca69f1c6336John McCall    return llvm::Function::DLLExportLinkage;
420f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
421f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (D->hasAttr<WeakAttr>())
422d46f98573ba104eda102dd3224b2dca69f1c6336John McCall    return llvm::Function::WeakAnyLinkage;
423f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
424f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // In C99 mode, 'inline' functions are guaranteed to have a strong
425f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // definition somewhere else, so we can use available_externally linkage.
426f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (Linkage == GVA_C99Inline)
427d46f98573ba104eda102dd3224b2dca69f1c6336John McCall    return llvm::Function::AvailableExternallyLinkage;
428f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
429f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // In C++, the compiler has to emit a definition in every translation unit
430f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // that references the function.  We should use linkonce_odr because
431f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // a) if all references in this translation unit are optimized away, we
432f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // don't need to codegen it.  b) if the function persists, it needs to be
433f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // merged with other definitions. c) C++ has the ODR, so we know the
434f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // definition is dependable.
435f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
436d46f98573ba104eda102dd3224b2dca69f1c6336John McCall    return llvm::Function::LinkOnceODRLinkage;
437f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
438f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // An explicit instantiation of a template has weak linkage, since
439f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // explicit instantiations can occur in multiple translation units
440f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // and must all be equivalent. However, we are not allowed to
441f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // throw away these explicit instantiations.
442f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  if (Linkage == GVA_ExplicitTemplateInstantiation)
4438f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor    return llvm::Function::WeakODRLinkage;
444f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner
445f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  // Otherwise, we have strong external linkage.
446f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  assert(Linkage == GVA_StrongExternal);
447f815306571385e2892e079a409f1b5b11f5e5cbbChris Lattner  return llvm::Function::ExternalLinkage;
448d46f98573ba104eda102dd3224b2dca69f1c6336John McCall}
449d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
450d46f98573ba104eda102dd3224b2dca69f1c6336John McCall
451d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// SetFunctionDefinitionAttributes - Set attributes for a global.
452d46f98573ba104eda102dd3224b2dca69f1c6336John McCall///
453d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// FIXME: This is currently only done for aliases and functions, but not for
454d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// variables (these details are set in EmitGlobalVarDefinition for variables).
455d46f98573ba104eda102dd3224b2dca69f1c6336John McCallvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
456d46f98573ba104eda102dd3224b2dca69f1c6336John McCall                                                    llvm::GlobalValue *GV) {
4577c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GV);
458d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes}
459d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
4607dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              const CGFunctionInfo &Info,
4627dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar                                              llvm::Function *F) {
463ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  unsigned CallingConv;
464761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  AttributeListType AttributeList;
465ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  ConstructAttributeList(Info, D, AttributeList, CallingConv);
466761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
467ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar                                          AttributeList.size()));
468ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
469f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
470f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
4717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
4727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                                           llvm::Function *F) {
47374ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar  if (!Features.Exceptions && !Features.ObjCNonFragileABI)
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    F->addFnAttr(llvm::Attribute::NoUnwind);
475af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
47640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (D->hasAttr<AlwaysInlineAttr>())
477af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar    F->addFnAttr(llvm::Attribute::AlwaysInline);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
479dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar  if (D->hasAttr<NakedAttr>())
480dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar    F->addFnAttr(llvm::Attribute::Naked);
481dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar
4821feade8e520be483293dbf55eb57a51720899589Mike Stump  if (D->hasAttr<NoInlineAttr>())
48381ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    F->addFnAttr(llvm::Attribute::NoInline);
484f55314dce992fd60816ba337ad151a2fb7c42239Mike Stump
485fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson  if (Features.getStackProtectorMode() == LangOptions::SSPOn)
486fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson    F->addFnAttr(llvm::Attribute::StackProtect);
487fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson  else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
488fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson    F->addFnAttr(llvm::Attribute::StackProtectReq);
489fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson
490cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
491cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (alignment)
492cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    F->setAlignment(alignment);
493cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
494fb51ddfafcd5f8536d0312b3daa3c0b74b90ab5bMike Stump  // C++ ABI requires 2-byte alignment for member functions.
495bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump  if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
496bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump    F->setAlignment(2);
497f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
498f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::SetCommonAttributes(const Decl *D,
5007c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                        llvm::GlobalValue *GV) {
5017c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  setGlobalVisibility(GV, D);
5027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
50340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (D->hasAttr<UsedAttr>())
5047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    AddUsedGlobal(GV);
5057c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
50640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
5077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GV->setSection(SA->getName());
50882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
50982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this);
5107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar}
5117c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
5120e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
5130e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar                                                  llvm::Function *F,
5140e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar                                                  const CGFunctionInfo &FI) {
5150e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetLLVMFunctionAttributes(D, FI, F);
5160e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetLLVMFunctionAttributesForDefinition(D, F);
5177c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
5187c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  F->setLinkage(llvm::Function::InternalLinkage);
5197c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
5200e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  SetCommonAttributes(D, F);
521f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar}
522f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
523b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonvoid CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
524c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman                                          llvm::Function *F,
525c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman                                          bool IsIncompleteFunction) {
526b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
527b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
528c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman  if (!IsIncompleteFunction)
529b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F);
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5317c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // Only a few attributes are set on declarations; these may later be
5327c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // overridden by a definition.
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (FD->hasAttr<DLLImportAttr>()) {
5357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setLinkage(llvm::Function::DLLImportLinkage);
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (FD->hasAttr<WeakAttr>() ||
53740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis             FD->hasAttr<WeakImportAttr>()) {
5387c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    // "extern_weak" is overloaded in LLVM; we probably should have
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // separate linkage types for this.
5407c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setLinkage(llvm::Function::ExternalWeakLinkage);
5417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  } else {
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    F->setLinkage(llvm::Function::ExternalLinkage);
5437c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  }
5447c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
54540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
5467c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    F->setSection(SA->getName());
547219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar}
548219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
5490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!GV->isDeclaration() &&
5510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar         "Only globals with definition can force usage.");
55235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  LLVMUsed.push_back(GV);
5530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
5540269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
5550269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() {
5560269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Don't create llvm.used if there is no need.
557ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner  if (LLVMUsed.empty())
5580269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    return;
5590269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
5603c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer  const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  // Convert LLVMUsed to what ConstantArray needs.
56335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  std::vector<llvm::Constant*> UsedArray;
56435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  UsedArray.resize(LLVMUsed.size());
56535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    UsedArray[i] =
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump     llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
568a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                                      i8PTy);
56935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  }
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
571c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian  if (UsedArray.empty())
572c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian    return;
57396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size());
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalVariable *GV =
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    new llvm::GlobalVariable(getModule(), ATy, false,
5770269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                             llvm::GlobalValue::AppendingLinkage,
5787db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson                             llvm::ConstantArray::get(ATy, UsedArray),
5791c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                             "llvm.used");
5800269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
5810269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  GV->setSection("llvm.metadata");
5820269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar}
5830269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
5840269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() {
58567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Emit code for any potentially referenced deferred decls.  Since a
58667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // previously unused static decl may become used during the generation of code
58767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // for a static function, iterate until no  changes are made.
588bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola
589046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson  while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) {
590046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson    if (!DeferredVTables.empty()) {
591046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson      const CXXRecordDecl *RD = DeferredVTables.back();
592046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson      DeferredVTables.pop_back();
593046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson      getVTables().GenerateClassData(getVTableLinkage(RD), RD);
594bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola      continue;
595bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola    }
596bbf58bb1b8dd8c5e0f07547a6c20ffd55385fcf6Rafael Espindola
5972a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    GlobalDecl D = DeferredDeclsToEmit.back();
59867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.pop_back();
59967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
600c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // Check to see if we've already emitted this.  This is necessary
601c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // for a couple of reasons: first, decls can end up in the
602c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // deferred-decls queue multiple times, and second, decls can end
603c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // up with definitions in unusual ways (e.g. by an extern inline
604c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // function acquiring a strong function redefinition).  Just
605c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // ignore these cases.
606c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    //
607c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // TODO: That said, looking this up multiple times is very wasteful.
6089a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson    llvm::StringRef Name = getMangledName(D);
609f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    llvm::GlobalValue *CGRef = GetGlobalValue(Name);
61067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    assert(CGRef && "Deferred decl wasn't referenced?");
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    if (!CGRef->isDeclaration())
61367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      continue;
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
615c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // GlobalAlias::isDeclaration() defers to the aliasee, but for our
616c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    // purposes an alias counts as a definition.
617c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall    if (isa<llvm::GlobalAlias>(CGRef))
618c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall      continue;
619c76702cc80c1ef8a94d82b62ddcb4ed4f67d5b8cJohn McCall
62067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Otherwise, emit the definition and move on to the next one.
62167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    EmitGlobalDefinition(D);
62267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
6268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue.  The annotation struct is
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// GlobalValue being annotated.  The second field is the constant string
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// created from the AnnotateAttr's annotation.  The third field is a constant
6308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit.  The fourth field is
6318bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration.
6328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
6338bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc
6348bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///        appears to.
6358bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
6378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                const AnnotateAttr *AA,
6388bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                unsigned LineNo) {
6398bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Module *M = &getModule();
6408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
6418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // get [N x i8] constants for the annotation string, and the filename string
6428bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // which are the 2nd and 3rd elements of the global annotation structure.
6433c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer  const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext);
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Constant *anno = llvm::ConstantArray::get(VMContext,
6450032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                  AA->getAnnotation(), true);
6460032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Constant *unit = llvm::ConstantArray::get(VMContext,
6470032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                  M->getModuleIdentifier(),
6488bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                  true);
6498bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
6508bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Get the two global values corresponding to the ConstantArrays we just
6518bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // created to hold the bytes of the strings.
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalValue *annoGV =
65395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    new llvm::GlobalVariable(*M, anno->getType(), false,
65495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                             llvm::GlobalValue::PrivateLinkage, anno,
65595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                             GV->getName());
6568bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // translation unit name string, emitted into the llvm.metadata section.
6578bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *unitGV =
65895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    new llvm::GlobalVariable(*M, unit->getType(), false,
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             llvm::GlobalValue::PrivateLinkage, unit,
66095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                             ".str");
6618bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
66257d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar  // Create the ConstantStruct for the global annotation.
6638bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *Fields[4] = {
6643c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(GV, SBP),
6653c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(annoGV, SBP),
6663c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(unitGV, SBP),
6670032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo)
6688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  };
66947a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  return llvm::ConstantStruct::get(VMContext, Fields, 4, false);
6708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
6718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
672a6d6af308bfc9b72467b432a045a9fc6673e3821Argyrios Kyrtzidisbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
67390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Never defer when EmitAllDecls is specified.
67490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (Features.EmitAllDecls)
675a6d6af308bfc9b72467b432a045a9fc6673e3821Argyrios Kyrtzidis    return false;
6760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
6774ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidis  return !getContext().DeclMustBeEmitted(Global);
67873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar}
67973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
6806a836706c40a31c716952b74785102c90fd6afa7Rafael Espindolallvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
6816a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  const AliasAttr *AA = VD->getAttr<AliasAttr>();
6826a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  assert(AA && "No alias?");
6836a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
6846a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
6856a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
6866a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  // See if there is already something with the target's name in the module.
687f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
6886a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
6896a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  llvm::Constant *Aliasee;
6906a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  if (isa<llvm::FunctionType>(DeclTy))
691f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl());
6926a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  else
693f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
6946a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola                                    llvm::PointerType::getUnqual(DeclTy), 0);
6956a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  if (!Entry) {
6966a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
6976a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    F->setLinkage(llvm::Function::ExternalWeakLinkage);
6986a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    WeakRefReferences.insert(F);
6996a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  }
7006a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
7016a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  return Aliasee;
7026a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola}
7036a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
704b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobal(GlobalDecl GD) {
7054a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson  const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7076a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  // Weak references don't produce any output by themselves.
7086a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola  if (Global->hasAttr<WeakRefAttr>())
7096a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    return;
7106a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
711bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // If this is an alias definition (which otherwise looks like a declaration)
712bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // emit it now.
71340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (Global->hasAttr<AliasAttr>())
714f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    return EmitAliasDefinition(GD);
715219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
71667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Ignore declarations, they will be emitted on their first use.
7175e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
718754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    if (FD->getIdentifier()) {
719754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      llvm::StringRef Name = FD->getName();
720754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      if (Name == "_Block_object_assign") {
721754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar        BlockObjectAssignDecl = FD;
722754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      } else if (Name == "_Block_object_dispose") {
723754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar        BlockObjectDisposeDecl = FD;
724754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      }
725754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    }
726754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
72773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Forward declarations are emitted lazily on first use.
72873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (!FD->isThisDeclarationADefinition())
72973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
7300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  } else {
7310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    const VarDecl *VD = cast<VarDecl>(Global);
732bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
733bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
734754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    if (VD->getIdentifier()) {
735754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      llvm::StringRef Name = VD->getName();
736754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      if (Name == "_NSConcreteGlobalBlock") {
737754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar        NSConcreteGlobalBlockDecl = VD;
738754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      } else if (Name == "_NSConcreteStackBlock") {
739754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar        NSConcreteStackBlockDecl = VD;
740754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      }
741754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    }
742754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
743754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
744a9a55c063e9e59c6ab0a6d7a21302660f7bde9f9Douglas Gregor    if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
74573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      return;
7464c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
7474c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
74867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // Defer code generation when possible if this is a static definition, inline
74967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // function etc.  These we only want to emit if they are used.
7504357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  if (!MayDeferGeneration(Global)) {
7514357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    // Emit the definition if it can't be deferred.
7524357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    EmitGlobalDefinition(GD);
753bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    return;
754bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
755bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall
756bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall  // If we're deferring emission of a C++ variable with an
757bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall  // initializer, remember the order in which it appeared in the file.
758bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall  if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) &&
759bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall      cast<VarDecl>(Global)->hasInit()) {
760bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall    DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
761bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall    CXXGlobalInits.push_back(0);
762bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall  }
7634357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner
7644357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  // If the value has already been used, add it directly to the
7654357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  // DeferredDeclsToEmit list.
7669a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson  llvm::StringRef MangledName = getMangledName(GD);
7674357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  if (GetGlobalValue(MangledName))
7684357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    DeferredDeclsToEmit.push_back(GD);
7694357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  else {
7704357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    // Otherwise, remember that we saw a deferred decl with this name.  The
7714357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    // first use of the mangled name will cause it to move into
7724357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    // DeferredDeclsToEmit.
7734357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner    DeferredDecls[MangledName] = GD;
7744357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  }
7754c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
7764c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
777b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) {
7784a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson  const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
780cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman  PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
7818e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson                                 Context.getSourceManager(),
7828e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson                                 "Generating code for declaration");
7838e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson
78444eac33ae12df384f3f002102f919f603bee330fDouglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
78544eac33ae12df384f3f002102f919f603bee330fDouglas Gregor    // At -O0, don't generate IR for functions with available_externally
78644eac33ae12df384f3f002102f919f603bee330fDouglas Gregor    // linkage.
7877feaeeed1789fbf357fc81072966fe8f62fb4a81Douglas Gregor    if (CodeGenOpts.OptimizationLevel == 0 &&
7887feaeeed1789fbf357fc81072966fe8f62fb4a81Douglas Gregor        !Function->hasAttr<AlwaysInlineAttr>() &&
78944eac33ae12df384f3f002102f919f603bee330fDouglas Gregor        getFunctionLinkage(Function)
79044eac33ae12df384f3f002102f919f603bee330fDouglas Gregor                                  == llvm::Function::AvailableExternallyLinkage)
79144eac33ae12df384f3f002102f919f603bee330fDouglas Gregor      return;
79244eac33ae12df384f3f002102f919f603bee330fDouglas Gregor
79344eac33ae12df384f3f002102f919f603bee330fDouglas Gregor    if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
79444eac33ae12df384f3f002102f919f603bee330fDouglas Gregor      if (Method->isVirtual())
79544eac33ae12df384f3f002102f919f603bee330fDouglas Gregor        getVTables().EmitThunks(GD);
7967270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson
79744eac33ae12df384f3f002102f919f603bee330fDouglas Gregor      if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method))
79844eac33ae12df384f3f002102f919f603bee330fDouglas Gregor        return EmitCXXConstructor(CD, GD.getCtorType());
7994357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner
80044eac33ae12df384f3f002102f919f603bee330fDouglas Gregor      if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Method))
80144eac33ae12df384f3f002102f919f603bee330fDouglas Gregor        return EmitCXXDestructor(DD, GD.getDtorType());
80244eac33ae12df384f3f002102f919f603bee330fDouglas Gregor    }
803b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner
804b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner    return EmitGlobalFunctionDefinition(GD);
80544eac33ae12df384f3f002102f919f603bee330fDouglas Gregor  }
806b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner
807b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
808b5e8156ebd6accd27daeaae6971597c45d5e5139Chris Lattner    return EmitGlobalVarDefinition(VD);
8094357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner
8104357a8291d759f6f9c36d3edeee8476d3eaf0804Chris Lattner  assert(0 && "Invalid argument to EmitGlobalDefinition()");
811bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
812bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
81374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
81474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there
81574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially
81674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type.
81774391b48b4791cded373683a3baf67314f358d50Chris Lattner///
81874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this.  This is used
81974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created.
820f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::Constant *
821f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallCodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName,
822f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                       const llvm::Type *Ty,
823f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                       GlobalDecl D) {
8240558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // Lookup the entry, lazily creating it if necessary.
825f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
8260558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (Entry) {
8276a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    if (WeakRefReferences.count(Entry)) {
8286a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola      const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl());
8296a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola      if (FD && !FD->hasAttr<WeakAttr>())
8307270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson        Entry->setLinkage(llvm::Function::ExternalLinkage);
8316a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
8326a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola      WeakRefReferences.erase(Entry);
8336a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    }
8346a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
8350558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    if (Entry->getType()->getElementType() == Ty)
8360558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner      return Entry;
8371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8380558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    // Make sure the result is of the correct type.
83996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
8403c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    return llvm::ConstantExpr::getBitCast(Entry, PTy);
8410558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
843654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  // This function doesn't have a complete type (for example, the return
844654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  // type is an incomplete struct). Use a fake type instead, and make
845654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  // sure not to try to set attributes.
846654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  bool IsIncompleteFunction = false;
847784f21121a6c9418ebd86baa6814e36e1176c410John McCall
848784f21121a6c9418ebd86baa6814e36e1176c410John McCall  const llvm::FunctionType *FTy;
849784f21121a6c9418ebd86baa6814e36e1176c410John McCall  if (isa<llvm::FunctionType>(Ty)) {
850784f21121a6c9418ebd86baa6814e36e1176c410John McCall    FTy = cast<llvm::FunctionType>(Ty);
851784f21121a6c9418ebd86baa6814e36e1176c410John McCall  } else {
852784f21121a6c9418ebd86baa6814e36e1176c410John McCall    FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
853784f21121a6c9418ebd86baa6814e36e1176c410John McCall                                  std::vector<const llvm::Type*>(), false);
854654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman    IsIncompleteFunction = true;
855654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  }
856bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner
857784f21121a6c9418ebd86baa6814e36e1176c410John McCall  llvm::Function *F = llvm::Function::Create(FTy,
858654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman                                             llvm::Function::ExternalLinkage,
859f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                             MangledName, &getModule());
860f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  assert(F->getName() == MangledName && "name was uniqued!");
861654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman  if (D.getDecl())
862b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    SetFunctionAttributes(D, F, IsIncompleteFunction);
863654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman
86467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // This is the first use or definition of a mangled name.  If there is a
86567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // deferred decl with this name, remember that we need to emit it at the end
86667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // of the file.
867f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
86867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (DDI != DeferredDecls.end()) {
86967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
87067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // list, and remove it from DeferredDecls (since we don't need it anymore).
87167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.push_back(DDI->second);
87267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDecls.erase(DDI);
873b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) {
8740c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner    // If this the first reference to a C++ inline function in a class, queue up
8750c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner    // the deferred function body for emission.  These are not seen as
8760c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner    // top-level declarations.
877b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD))
878b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner      DeferredDeclsToEmit.push_back(D);
879c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    // A called constructor which has no definition or declaration need be
880c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    // synthesized.
881c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
8827b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola      if (CD->isImplicit()) {
883d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer        assert(CD->isUsed() && "Sema doesn't consider constructor as used.");
88415233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman        DeferredDeclsToEmit.push_back(D);
8857b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola      }
88615233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman    } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
8877b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola      if (DD->isImplicit()) {
888d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer        assert(DD->isUsed() && "Sema doesn't consider destructor as used.");
88915233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman        DeferredDeclsToEmit.push_back(D);
8907b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola      }
89115233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman    } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
8923e9438b5251a547253d64169863c2909b9b2772aDouglas Gregor      if (MD->isImplicit() && MD->isCopyAssignmentOperator()) {
893d3a344c55b89bad933c04ac59650f7afb95423e9Benjamin Kramer        assert(MD->isUsed() && "Sema doesn't consider CopyAssignment as used.");
894c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian        DeferredDeclsToEmit.push_back(D);
8957b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola      }
896c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    }
89767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
899784f21121a6c9418ebd86baa6814e36e1176c410John McCall  // Make sure the result is of the requested type.
900784f21121a6c9418ebd86baa6814e36e1176c410John McCall  if (!IsIncompleteFunction) {
901784f21121a6c9418ebd86baa6814e36e1176c410John McCall    assert(F->getType()->getElementType() == Ty);
902784f21121a6c9418ebd86baa6814e36e1176c410John McCall    return F;
903784f21121a6c9418ebd86baa6814e36e1176c410John McCall  }
904784f21121a6c9418ebd86baa6814e36e1176c410John McCall
905784f21121a6c9418ebd86baa6814e36e1176c410John McCall  const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
906784f21121a6c9418ebd86baa6814e36e1176c410John McCall  return llvm::ConstantExpr::getBitCast(F, PTy);
9070558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner}
9080558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
90974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function.  If Ty is
91074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to
91174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function).
912b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
91374391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                 const llvm::Type *Ty) {
91474391b48b4791cded373683a3baf67314f358d50Chris Lattner  // If there was no specific requested type, just convert it now.
91574391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (!Ty)
9164a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson    Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
917bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner
9189a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson  llvm::StringRef MangledName = getMangledName(GD);
919f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  return GetOrCreateLLVMFunction(MangledName, Ty, GD);
92074391b48b4791cded373683a3baf67314f358d50Chris Lattner}
92177ba708819285931932ecd33691a672bb59d221aEli Friedman
92274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified
92374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name.
92474391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *
92574391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
926f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                     llvm::StringRef Name) {
927b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl());
92874391b48b4791cded373683a3baf67314f358d50Chris Lattner}
929bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
93020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedmanstatic bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) {
93188786862424cb2d478516d911709dc19c962af9cJohn McCall  if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType())
93220e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman    return false;
93320e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman  if (Context.getLangOptions().CPlusPlus &&
93420e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman      Context.getBaseElementType(D->getType())->getAs<RecordType>()) {
93520e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman    // FIXME: We should do something fancier here!
93620e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman    return false;
93720e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman  }
93820e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman  return true;
93920e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman}
94020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman
94174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
94274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type.  If there
94374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially
94474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type.
94574391b48b4791cded373683a3baf67314f358d50Chris Lattner///
94674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this.  This is used
94774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created.
948f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallllvm::Constant *
949f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallCodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
950f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                     const llvm::PointerType *Ty,
951f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                     const VarDecl *D) {
9523c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar  // Lookup the entry, lazily creating it if necessary.
953f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
95499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  if (Entry) {
9556a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    if (WeakRefReferences.count(Entry)) {
9566a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola      if (D && !D->hasAttr<WeakAttr>())
9577270ee4cd4794281c09dfb6931a98bbb2581ef02Anders Carlsson        Entry->setLinkage(llvm::Function::ExternalLinkage);
9586a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
9596a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola      WeakRefReferences.erase(Entry);
9606a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola    }
9616a836706c40a31c716952b74785102c90fd6afa7Rafael Espindola
96274391b48b4791cded373683a3baf67314f358d50Chris Lattner    if (Entry->getType() == Ty)
963570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      return Entry;
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96599b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner    // Make sure the result is of the correct type.
9663c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    return llvm::ConstantExpr::getBitCast(Entry, Ty);
96799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  }
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // This is the first use or definition of a mangled name.  If there is a
97067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // deferred decl with this name, remember that we need to emit it at the end
97167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  // of the file.
972f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
97367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (DDI != DeferredDecls.end()) {
97467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
97567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // list, and remove it from DeferredDecls (since we don't need it anymore).
97667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDeclsToEmit.push_back(DDI->second);
97767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    DeferredDecls.erase(DDI);
97867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalVariable *GV =
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
98299b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner                             llvm::GlobalValue::ExternalLinkage,
983f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                             0, MangledName, 0,
98456ebe5082da7411fb37479e230b52735f77cff35Eli Friedman                             false, Ty->getAddressSpace());
98549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
98699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  // Handle things which are present even on external declarations.
98774391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (D) {
988f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: This code is overly simple and should be merged with other global
989f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // handling.
99020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman    GV->setConstant(DeclIsConstantGlobal(Context, D));
99149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
99274391b48b4791cded373683a3baf67314f358d50Chris Lattner    // FIXME: Merge with other attribute handling code.
993d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (D->getStorageClass() == SC_PrivateExtern)
99404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
99549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
9962c6899fa1a748fe211280de3dfb478be56a9be6aNAKAMURA Takumi    if (D->hasAttr<DLLImportAttr>())
9972c6899fa1a748fe211280de3dfb478be56a9be6aNAKAMURA Takumi      GV->setLinkage(llvm::GlobalValue::DLLImportLinkage);
9982c6899fa1a748fe211280de3dfb478be56a9be6aNAKAMURA Takumi    else if (D->hasAttr<WeakAttr>() ||
99940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis        D->hasAttr<WeakImportAttr>())
100074391b48b4791cded373683a3baf67314f358d50Chris Lattner      GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
100156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman
100256ebe5082da7411fb37479e230b52735f77cff35Eli Friedman    GV->setThreadLocal(D->isThreadSpecified());
100374391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1005f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  return GV;
100674391b48b4791cded373683a3baf67314f358d50Chris Lattner}
1007eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
1008eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar
100974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
101074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable.  If Ty is non-null and if the global doesn't exist,
101174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the
101274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be.
101374391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
101474391b48b4791cded373683a3baf67314f358d50Chris Lattner                                                  const llvm::Type *Ty) {
101574391b48b4791cded373683a3baf67314f358d50Chris Lattner  assert(D->hasGlobalStorage() && "Not a global variable");
101674391b48b4791cded373683a3baf67314f358d50Chris Lattner  QualType ASTTy = D->getType();
101774391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (Ty == 0)
101874391b48b4791cded373683a3baf67314f358d50Chris Lattner    Ty = getTypes().ConvertTypeForMem(ASTTy);
10191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::PointerType *PTy =
102196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
1022f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall
10239a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson  llvm::StringRef MangledName = getMangledName(D);
1024f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  return GetOrCreateLLVMGlobal(MangledName, PTy, D);
102574391b48b4791cded373683a3baf67314f358d50Chris Lattner}
10263f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar
102774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the
102874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name.
102974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *
103074391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
1031f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall                                     llvm::StringRef Name) {
103296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0);
1033bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
1034bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
103503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
103603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  assert(!D->getInit() && "Cannot emit definite definitions here!");
103703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
10387520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  if (MayDeferGeneration(D)) {
10397520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    // If we have not seen a reference to this variable yet, place it
10407520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    // into the deferred declarations table to be emitted if needed
10417520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    // later.
10429a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson    llvm::StringRef MangledName = getMangledName(D);
1043f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    if (!GetGlobalValue(MangledName)) {
1044555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson      DeferredDecls[MangledName] = D;
104503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      return;
10467520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    }
10477520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  }
10487520bd1de12af10ea08c662440565adbdf589317Douglas Gregor
10497520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  // The tentative definition is the only definition.
105003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  EmitGlobalVarDefinition(D);
105103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar}
105203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
10536fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregorvoid CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) {
10546fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  if (DefinitionRequired)
10556fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    getVTables().GenerateClassData(getVTableLinkage(Class), Class);
10566fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor}
10576fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
10584b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregorllvm::GlobalVariable::LinkageTypes
1059046c294a43024874ff35656c6e785b64e72f1f36Anders CarlssonCodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1060dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
1061dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    return llvm::GlobalVariable::InternalLinkage;
1062dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor
1063dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  if (const CXXMethodDecl *KeyFunction
1064dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor                                    = RD->getASTContext().getKeyFunction(RD)) {
1065dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    // If this class has a key function, use that to determine the linkage of
1066dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    // the vtable.
10674b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor    const FunctionDecl *Def = 0;
106806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (KeyFunction->hasBody(Def))
10694b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      KeyFunction = cast<CXXMethodDecl>(Def);
1070dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor
10714b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor    switch (KeyFunction->getTemplateSpecializationKind()) {
10724b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      case TSK_Undeclared:
10734b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      case TSK_ExplicitSpecialization:
10744b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        if (KeyFunction->isInlined())
10754b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor          return llvm::GlobalVariable::WeakODRLinkage;
10764b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor
10774b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        return llvm::GlobalVariable::ExternalLinkage;
10784b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor
10794b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      case TSK_ImplicitInstantiation:
10804b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      case TSK_ExplicitInstantiationDefinition:
10814b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        return llvm::GlobalVariable::WeakODRLinkage;
10824b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor
10834b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor      case TSK_ExplicitInstantiationDeclaration:
10844b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        // FIXME: Use available_externally linkage. However, this currently
10854b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        // breaks LLVM's build due to undefined symbols.
10864b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        //      return llvm::GlobalVariable::AvailableExternallyLinkage;
10874b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor        return llvm::GlobalVariable::WeakODRLinkage;
10884b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor    }
1089dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  }
1090dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor
1091dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  switch (RD->getTemplateSpecializationKind()) {
1092dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  case TSK_Undeclared:
1093dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  case TSK_ExplicitSpecialization:
1094dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  case TSK_ImplicitInstantiation:
1095dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
1096dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    return llvm::GlobalVariable::WeakODRLinkage;
1097dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor
1098dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
1099dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    // FIXME: Use available_externally linkage. However, this currently
1100dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    // breaks LLVM's build due to undefined symbols.
1101dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor    //   return llvm::GlobalVariable::AvailableExternallyLinkage;
11024b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor    return llvm::GlobalVariable::WeakODRLinkage;
11034b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor  }
11044b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor
11054b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor  // Silence GCC warning.
11064b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor  return llvm::GlobalVariable::WeakODRLinkage;
11074b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor}
11084b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor
1109687cc4a850b59116efee061018f0d8df50728b82Ken DyckCharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const {
1110687cc4a850b59116efee061018f0d8df50728b82Ken Dyck    return CharUnits::fromQuantity(
1111687cc4a850b59116efee061018f0d8df50728b82Ken Dyck      TheTargetData.getTypeStoreSizeInBits(Ty) / Context.getCharWidth());
1112687cc4a850b59116efee061018f0d8df50728b82Ken Dyck}
1113687cc4a850b59116efee061018f0d8df50728b82Ken Dyck
1114bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
11158f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  llvm::Constant *Init = 0;
111677ba708819285931932ecd33691a672bb59d221aEli Friedman  QualType ASTTy = D->getType();
11176c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman  bool NonConstInit = false;
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  const Expr *InitExpr = D->getAnyInitializer();
11203bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson
11213bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson  if (!InitExpr) {
1122cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman    // This is a tentative definition; tentative definitions are
112303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // implicitly initialized with { 0 }.
112403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    //
112503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // Note that tentative definitions are only emitted at the end of
112603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // a translation unit, so they should never have incomplete
112703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // type. In addition, EmitTentativeDefinition makes sure that we
112803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // never attempt to emit a tentative definition if a real one
112903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // exists. A use may still exists, however, so we still may need
113003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    // to do a RAUW.
113103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
1132b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson    Init = EmitNullConstant(D->getType());
113377ba708819285931932ecd33691a672bb59d221aEli Friedman  } else {
1134c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor    Init = EmitConstantExpr(InitExpr, D->getType());
11356e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    if (!Init) {
11363bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson      QualType T = InitExpr->getType();
1137c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor      if (D->getType()->isReferenceType())
1138c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor        T = D->getType();
1139c446d1816f46a4b6d2337102dfc001f55fc18211Douglas Gregor
114089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson      if (getLangOptions().CPlusPlus) {
11416c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman        EmitCXXGlobalVarDeclInitFunc(D);
114289ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson        Init = EmitNullConstant(T);
11436c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman        NonConstInit = true;
114489ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson      } else {
114589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson        ErrorUnsupported(D, "static initializer");
114689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson        Init = llvm::UndefValue::get(getTypes().ConvertType(T));
114789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson      }
1148bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall    } else {
1149bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall      // We don't need an initializer, so remove the entry for the delayed
1150bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall      // initializer position (just in case this entry was delayed).
1151bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall      if (getLangOptions().CPlusPlus)
1152bf40cb518312dde1c07e44fcae59bc4eec65589cJohn McCall        DelayedCXXInitPosition.erase(D);
11536e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    }
11548f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  }
11558e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel
11562d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner  const llvm::Type* InitType = Init->getType();
1157570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1159570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // Strip off a bitcast if we got one back.
1160570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
11619d4a15fd3b85434c43ea27562793de63a793321aChris Lattner    assert(CE->getOpcode() == llvm::Instruction::BitCast ||
11629d4a15fd3b85434c43ea27562793de63a793321aChris Lattner           // all zero index gep.
11639d4a15fd3b85434c43ea27562793de63a793321aChris Lattner           CE->getOpcode() == llvm::Instruction::GetElementPtr);
1164570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Entry = CE->getOperand(0);
1165570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  }
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1167570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // Entry is now either a Function or GlobalVariable.
1168570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry);
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1170570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // We have a definition after a declaration with the wrong type.
1171570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // We must make a new GlobalVariable* and update everything that used OldGV
1172570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // (a declaration or tentative definition) with the new GlobalVariable*
1173570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // (which will be a definition).
1174570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  //
1175570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // This happens if there is a prototype for a global (e.g.
1176570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // "extern int x[];") and then a definition of a different type (e.g.
1177570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // "int x[10];"). This also happens when an initializer has a different type
1178570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // from the type of the global (this happens with unions).
1179570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (GV == 0 ||
1180570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      GV->getType()->getElementType() != InitType ||
1181570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) {
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1183f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    // Move the old entry aside so that we'll create a new one.
1184f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    Entry->setName(llvm::StringRef());
1185232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar
1186570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    // Make a new global with the correct type, this is now guaranteed to work.
1187570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
11880558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
118977ba708819285931932ecd33691a672bb59d221aEli Friedman    // Replace all uses of the old global with the new global
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Constant *NewPtrForOldDecl =
11913c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(GV, Entry->getType());
1192570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Entry->replaceAllUsesWith(NewPtrForOldDecl);
119377ba708819285931932ecd33691a672bb59d221aEli Friedman
119477ba708819285931932ecd33691a672bb59d221aEli Friedman    // Erase the old global, since it is no longer used.
1195570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    cast<llvm::GlobalValue>(Entry)->eraseFromParent();
119677ba708819285931932ecd33691a672bb59d221aEli Friedman  }
119777ba708819285931932ecd33691a672bb59d221aEli Friedman
119840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
11998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    SourceManager &SM = Context.getSourceManager();
12008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    AddAnnotation(EmitAnnotateAttr(GV, AA,
1201f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner                              SM.getInstantiationLineNumber(D->getLocation())));
12028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
12038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
120488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  GV->setInitializer(Init);
1205e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner
1206e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  // If it is safe to mark the global 'constant', do so now.
1207e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  GV->setConstant(false);
12086c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman  if (!NonConstInit && DeclIsConstantGlobal(Context, D))
1209e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner    GV->setConstant(true);
12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12118b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
121208d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman
121388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  // Set the llvm linkage type as appropriate.
121490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage Linkage = getContext().GetGVALinkageForVariable(D);
1215a0f00a71fcb0f98298709d5e5318339acf7958acDouglas Gregor  if (Linkage == GVA_Internal)
12168fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
121740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  else if (D->hasAttr<DLLImportAttr>())
1218ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLImportLinkage);
121940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  else if (D->hasAttr<DLLExportAttr>())
1220ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLExportLinkage);
1221e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  else if (D->hasAttr<WeakAttr>()) {
1222e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner    if (GV->isConstant())
1223e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner      GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage);
1224e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner    else
1225e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner      GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
12268f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor  } else if (Linkage == GVA_TemplateInstantiation ||
12278f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor             Linkage == GVA_ExplicitTemplateInstantiation)
12288f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor    // FIXME: It seems like we can provide more specific linkage here
12298f51a4f2d00b0abda3cde7f3828fb2e2b9beafb5Douglas Gregor    // (LinkOnceODR, WeakODR).
12301028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
12311bc5c03a7f08b04f4ac26abd705418aaf4109062Daniel Dunbar  else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon &&
1232309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner           !D->hasExternalStorage() && !D->getInit() &&
123391f31dc234bbc98f3dd20e6741a7b0b98c7916bfEric Christopher           !D->getAttr<SectionAttr>() && !D->isThreadSpecified()) {
123491f31dc234bbc98f3dd20e6741a7b0b98c7916bfEric Christopher    // Thread local vars aren't considered common linkage.
123504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
1236e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner    // common vars aren't constant even if declared const.
1237e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner    GV->setConstant(false);
1238e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  } else
123904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
12407e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar
12417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GV);
124204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
1243686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  // Emit global variable debug information.
12442d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner  if (CGDebugInfo *DI = getDebugInfo()) {
124566031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar    DI->setLocation(D->getLocation());
1246686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta    DI->EmitGlobalVariable(GV, D);
1247686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  }
124888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner}
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1250bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
1251bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// implement a function with no prototype, e.g. "int foo() {}".  If there are
1252bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// existing call uses of the old function in the module, this adjusts them to
1253bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// call the new function directly.
1254bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner///
1255bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// This is not just a cleanup: the always_inline pass requires direct calls to
1256bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// functions to be able to inline them.  If there is a bitcast in the way, it
1257bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// won't inline them.  Instcombine normally deletes these calls, but it isn't
1258bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// run at -O0.
1259bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
1260bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                                      llvm::Function *NewFn) {
1261bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // If we're redefining a global as a function, don't transform it.
1262bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  llvm::Function *OldFn = dyn_cast<llvm::Function>(Old);
1263bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (OldFn == 0) return;
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1265bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  const llvm::Type *NewRetTy = NewFn->getReturnType();
1266bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  llvm::SmallVector<llvm::Value*, 4> ArgList;
1267bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1268bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end();
1269dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer       UI != E; ) {
1270bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // TODO: Do invokes ever occur in C code?  If so, we should handle them too.
1271dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer    llvm::Value::use_iterator I = UI++; // Increment before the CI is erased.
1272dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer    llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I);
12738670cd3ea4dfbf86caacd270ec2441e9949bbb6aGabor Greif    if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I)
127435db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif    llvm::CallSite CS(CI);
1275dbf02bccc9fc1115cb7dd45c84df77252d68f220Benjamin Kramer    if (!CI || !CS.isCallee(I)) continue;
12761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1277bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // If the return types don't match exactly, and if the call isn't dead, then
1278bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // we can't transform this call.
1279bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    if (CI->getType() != NewRetTy && !CI->use_empty())
1280bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      continue;
1281bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1282bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // If the function was passed too few arguments, don't transform.  If extra
1283bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // arguments were passed, we silently drop them.  If any of the types
1284bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // mismatch, we don't transform.
1285bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    unsigned ArgNo = 0;
1286bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    bool DontTransform = false;
1287bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    for (llvm::Function::arg_iterator AI = NewFn->arg_begin(),
1288bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner         E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) {
128935db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif      if (CS.arg_size() == ArgNo ||
129035db3b9aad1829a1279b9e213ddee36395314a0bGabor Greif          CS.getArgument(ArgNo)->getType() != AI->getType()) {
1291bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner        DontTransform = true;
1292bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner        break;
1293bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      }
1294bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    }
1295bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    if (DontTransform)
1296bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      continue;
12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1298bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // Okay, we can transform this.  Create the new call instruction and copy
1299bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // over the required information.
13006ba728d9687b2617793f5afd410650a8d6c71080Gabor Greif    ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo);
1301bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(),
1302bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                                     ArgList.end(), "", CI);
1303bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    ArgList.clear();
1304ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer    if (!NewCall->getType()->isVoidTy())
1305bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      NewCall->takeName(CI);
1306bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    NewCall->setAttributes(CI->getAttributes());
1307ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    NewCall->setCallingConv(CI->getCallingConv());
1308bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1309bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // Finally, remove the old call, replacing any uses with the new one.
131000549fcec0490b2daf27543e532f94adbb186063Chris Lattner    if (!CI->use_empty())
131100549fcec0490b2daf27543e532f94adbb186063Chris Lattner      CI->replaceAllUsesWith(NewCall);
13123b122bc5f1203615e2128e0c1a63da438865b1ccDevang Patel
1313c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner    // Copy debug location attached to CI.
1314c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner    if (!CI->getDebugLoc().isUnknown())
1315c60346388d60b1756f3675dabb60dc58da0728ecChris Lattner      NewCall->setDebugLoc(CI->getDebugLoc());
1316bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    CI->eraseFromParent();
1317bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  }
1318bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner}
1319bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1320bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
1321b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) {
1322b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
1323c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD);
13244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  getCXXABI().getMangleContext().mangleInitDiscriminator();
13259fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner  // Get or create the prototype for the function.
1326b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  llvm::Constant *Entry = GetAddrOfFunction(GD, Ty);
13271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  // Strip off a bitcast if we got one back.
13290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
13300558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    assert(CE->getOpcode() == llvm::Instruction::BitCast);
13310558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Entry = CE->getOperand(0);
13320558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
13331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13350558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) {
1336bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry);
13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar    // If the types mismatch then we have to rewrite the definition.
1339bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    assert(OldFn->isDeclaration() &&
13400558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner           "Shouldn't replace non-declaration");
134134809507232bc4c3c4840c7d092c7440219fddafChris Lattner
134262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // F is the Function* for the one with the wrong type, we must make a new
134362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Function* and update everything that used F (a declaration) with the new
134462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Function* (which will be a definition).
134562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    //
134662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // This happens if there is a prototype for a function
134762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // (e.g. "int f()") and then a definition of a different type
1348f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    // (e.g. "int f(int x)").  Move the old function aside so that it
1349f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    // doesn't interfere with GetAddrOfFunction.
1350f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    OldFn->setName(llvm::StringRef());
1351b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1353bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // If this is an implementation of a function without a prototype, try to
1354bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // replace any existing uses of the function (which may be calls) with uses
1355bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    // of the new function
13569fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner    if (D->getType()->isFunctionNoProtoType()) {
1357bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn);
13589fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner      OldFn->removeDeadConstantUsers();
13599fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner    }
13601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Replace uses of F with the Function we will endow with a body.
1362bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    if (!Entry->use_empty()) {
13631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      llvm::Constant *NewPtrForOldDecl =
13643c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
1365bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      Entry->replaceAllUsesWith(NewPtrForOldDecl);
1366bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    }
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    // Ok, delete the old function now, which is dead.
1369bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    OldFn->eraseFromParent();
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13710558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Entry = NewFn;
1372bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13740558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::Function *Fn = cast<llvm::Function>(Entry);
13758b2423361648c39a7d8a3c5e8129e12006deac32John McCall  setFunctionLinkage(D, Fn);
1376bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
1377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  CodeGenFunction(*this).GenerateCode(D, Fn);
13786379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar
13797c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetFunctionDefinitionAttributes(D, Fn);
13807c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetLLVMFunctionAttributesForDefinition(D, Fn);
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
1383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalCtor(Fn, CA->getPriority());
138440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
1385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar    AddGlobalDtor(Fn, DA->getPriority());
1386bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
1387bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
1388f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCallvoid CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
1389f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
139040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  const AliasAttr *AA = D->getAttr<AliasAttr>();
1391bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  assert(AA && "Not an alias?");
1392bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
13939a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson  llvm::StringRef MangledName = getMangledName(GD);
1394f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall
1395f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  // If there is a definition in the module, then it wins over the alias.
1396f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  // This is dubious, but allow it to be safe.  Just ignore the alias.
1397f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1398f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  if (Entry && !Entry->isDeclaration())
1399f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    return;
14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1401f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
1402bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1403bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Create a reference to the named value.  This ensures that it is emitted
1404bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // if a deferred decl.
1405bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  llvm::Constant *Aliasee;
1406bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (isa<llvm::FunctionType>(DeclTy))
1407f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl());
1408bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  else
1409f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
141096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                    llvm::PointerType::getUnqual(DeclTy), 0);
1411bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1412bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Create the new alias itself, but don't set a name yet.
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalValue *GA =
1414bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    new llvm::GlobalAlias(Aliasee->getType(),
1415bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                          llvm::Function::ExternalLinkage,
1416bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                          "", Aliasee, &getModule());
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1418bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (Entry) {
1419f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    assert(Entry->isDeclaration());
1420f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall
1421bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // If there is a declaration in the module, then we had an extern followed
1422bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // by the alias, as in:
1423bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   extern int test6();
1424bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   ...
1425bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //   int test6() __attribute__((alias("test7")));
1426bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    //
1427bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // Remove it and replace uses of it with the alias.
1428f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall    GA->takeName(Entry);
14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14303c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
1431bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner                                                          Entry->getType()));
1432bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Entry->eraseFromParent();
1433f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall  } else {
14349a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson    GA->setName(MangledName);
1435bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14377c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // Set attributes which are particular to an alias; this is a
14387c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // specialization of the attributes which may be set on a global
14397c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // variable/function.
144040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (D->hasAttr<DLLExportAttr>()) {
14417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
14427c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      // The dllexport attribute is ignored for undefined symbols.
144306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (FD->hasBody())
14447c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        GA->setLinkage(llvm::Function::DLLExportLinkage);
14457c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    } else {
14467c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      GA->setLinkage(llvm::Function::DLLExportLinkage);
14477c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    }
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (D->hasAttr<WeakAttr>() ||
144911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola             D->hasAttr<WeakRefAttr>() ||
145040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis             D->hasAttr<WeakImportAttr>()) {
14517c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    GA->setLinkage(llvm::Function::WeakAnyLinkage);
14527c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  }
14537c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
14547c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  SetCommonAttributes(D, GA);
1455bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner}
1456bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1457b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like
1458b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf".
145934771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbarllvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
146034771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar                                                  unsigned BuiltinID) {
14613e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
14633e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor         "isn't a lib fn");
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14653e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  // Get the name, skip over the __builtin_ prefix (if necessary).
14663e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
14673e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  if (Context.BuiltinInfo.isLibFunction(BuiltinID))
14683e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor    Name += 10;
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::FunctionType *Ty =
1471386ca78d0b03b1fb519e60d1a14cd12a220364a6Eli Friedman    cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
1472bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
147334771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar  return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD));
1474bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner}
1475bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
14767acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
14777acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                            unsigned NumTys) {
14787acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return llvm::Intrinsic::getDeclaration(&getModule(),
14797acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
14807acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner}
1481bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
14823ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang
14833ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemCpyFn(const llvm::Type *DestType,
14843ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                           const llvm::Type *SrcType,
14853ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                           const llvm::Type *SizeType) {
14863ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType };
14873ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  return getIntrinsic(llvm::Intrinsic::memcpy, ArgTypes, 3);
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1489c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
14903ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemMoveFn(const llvm::Type *DestType,
14913ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                            const llvm::Type *SrcType,
14923ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                            const llvm::Type *SizeType) {
14933ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType };
14943ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  return getIntrinsic(llvm::Intrinsic::memmove, ArgTypes, 3);
14950c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman}
14960c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman
14973ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wangllvm::Function *CodeGenModule::getMemSetFn(const llvm::Type *DestType,
14983ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                           const llvm::Type *SizeType) {
14993ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  const llvm::Type *ArgTypes[2] = { DestType, SizeType };
15003ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  return getIntrinsic(llvm::Intrinsic::memset, ArgTypes, 2);
150141ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio}
15027acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner
15031d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarstatic llvm::StringMapEntry<llvm::Constant*> &
15041d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarGetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
15051d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                         const StringLiteral *Literal,
150670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                         bool TargetIsLSB,
15071d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                         bool &IsUTF16,
15081d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                         unsigned &StringLength) {
15092f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  llvm::StringRef String = Literal->getString();
15102f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  unsigned NumBytes = String.size();
15111d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar
1512f015b034159d40e7033309e50036804eb1971787Daniel Dunbar  // Check for simple case.
1513f015b034159d40e7033309e50036804eb1971787Daniel Dunbar  if (!Literal->containsNonAsciiOrNull()) {
1514f015b034159d40e7033309e50036804eb1971787Daniel Dunbar    StringLength = NumBytes;
15152f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer    return Map.GetOrCreateValue(String);
1516f015b034159d40e7033309e50036804eb1971787Daniel Dunbar  }
1517f015b034159d40e7033309e50036804eb1971787Daniel Dunbar
15181d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  // Otherwise, convert the UTF8 literals into a byte string.
15191d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  llvm::SmallVector<UTF16, 128> ToBuf(NumBytes);
15202f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  const UTF8 *FromPtr = (UTF8 *)String.data();
15211d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  UTF16 *ToPtr = &ToBuf[0];
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1523e7ddfb974884ec75ffd66953b79511ea457493baFariborz Jahanian  (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1524e7ddfb974884ec75ffd66953b79511ea457493baFariborz Jahanian                           &ToPtr, ToPtr + NumBytes,
1525e7ddfb974884ec75ffd66953b79511ea457493baFariborz Jahanian                           strictConversion);
15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // ConvertUTF8toUTF16 returns the length in ToPtr.
15281d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  StringLength = ToPtr - &ToBuf[0];
152970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar
153070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // Render the UTF-16 string into a byte array and convert to the target byte
153170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // order.
153270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  //
153370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // FIXME: This isn't something we should need to do here.
153470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  llvm::SmallString<128> AsBytes;
153570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  AsBytes.reserve(StringLength * 2);
153670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  for (unsigned i = 0; i != StringLength; ++i) {
153770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    unsigned short Val = ToBuf[i];
153870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    if (TargetIsLSB) {
153970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      AsBytes.push_back(Val & 0xFF);
154070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      AsBytes.push_back(Val >> 8);
154170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    } else {
154270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      AsBytes.push_back(Val >> 8);
154370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      AsBytes.push_back(Val & 0xFF);
154470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    }
154570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  }
1546434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar  // Append one extra null character, the second is automatically added by our
1547434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar  // caller.
1548434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar  AsBytes.push_back(0);
154970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar
15501d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  IsUTF16 = true;
155170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size()));
15521d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar}
15531d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar
15541d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarllvm::Constant *
15551d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarCodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
15561d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  unsigned StringLength = 0;
15571d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  bool isUTF16 = false;
15581d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  llvm::StringMapEntry<llvm::Constant*> &Entry =
15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    GetConstantCFStringEntry(CFConstantStringMap, Literal,
156070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                             getTargetData().isLittleEndian(),
156170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                             isUTF16, StringLength);
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15631d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  if (llvm::Constant *C = Entry.getValue())
15641d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    return C;
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15660032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Constant *Zero =
15670032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
15683e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  llvm::Constant *Zeros[] = { Zero, Zero };
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15709d4a15fd3b85434c43ea27562793de63a793321aChris Lattner  // If we don't already have it, get __CFConstantStringClassReference.
1571c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (!CFConstantStringClassRef) {
1572c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
157396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    Ty = llvm::ArrayType::get(Ty, 0);
15741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Constant *GV = CreateRuntimeVariable(Ty,
15759d4a15fd3b85434c43ea27562793de63a793321aChris Lattner                                           "__CFConstantStringClassReference");
15763e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    // Decay array -> ptr
15773e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    CFConstantStringClassRef =
15783c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1579c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1581e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson  QualType CFTy = getContext().getCFConstantStringType();
15823e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::StructType *STy =
1584e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson    cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1585e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson
15865add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson  std::vector<llvm::Constant*> Fields(4);
158744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1588c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Class pointer.
15895add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson  Fields[0] = CFConstantStringClassRef;
15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1591c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Flags.
15923e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
15945add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson    llvm::ConstantInt::get(Ty, 0x07C8);
15955add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson
1596c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String pointer.
15970032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
1598a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar
159995b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner  llvm::GlobalValue::LinkageTypes Linkage;
1600278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner  bool isConstant;
1601a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  if (isUTF16) {
1602278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    // FIXME: why do utf strings get "_" labels instead of "L" labels?
160395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    Linkage = llvm::GlobalValue::InternalLinkage;
1604278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    // Note: -fwritable-strings doesn't make unicode CFStrings writable, but
1605278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    // does make plain ascii ones writable.
1606278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    isConstant = true;
1607a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  } else {
160895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    Linkage = llvm::GlobalValue::PrivateLinkage;
1609278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    isConstant = !Features.WritableStrings;
1610a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  }
1611278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::GlobalVariable *GV =
1613278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner    new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
1614278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner                             ".str");
1615a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  if (isUTF16) {
16164da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck    CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
16174da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck    GV->setAlignment(Align.getQuantity());
1618a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  }
16195add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson  Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
16205add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson
1621c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String length.
1622c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().LongTy);
16235add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson  Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1625c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // The struct.
162608e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson  C = llvm::ConstantStruct::get(STy, Fields);
16271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                llvm::GlobalVariable::PrivateLinkage, C,
162995b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                                "_unnamed_cfstring_");
16308e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (const char *Sect = getContext().Target.getCFStringSection())
16318e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    GV->setSection(Sect);
16321d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  Entry.setValue(GV);
16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16340c67829763b98bc670062b553897a851fab17401Anders Carlsson  return GV;
1635c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson}
163645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
163733e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanianllvm::Constant *
16384c73307c74764ba99e1379677fe92af72f676531Fariborz JahanianCodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
16392bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  unsigned StringLength = 0;
16402bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  bool isUTF16 = false;
16412bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::StringMapEntry<llvm::Constant*> &Entry =
16422bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    GetConstantCFStringEntry(CFConstantStringMap, Literal,
16432bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                             getTargetData().isLittleEndian(),
16442bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                             isUTF16, StringLength);
16452bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16462bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (llvm::Constant *C = Entry.getValue())
16472bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    return C;
16482bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16492bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::Constant *Zero =
16502bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
16512bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::Constant *Zeros[] = { Zero, Zero };
16522bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
1653ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian  // If we don't already have it, get _NSConstantStringClassReference.
16544c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian  if (!ConstantStringClassRef) {
16554c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    std::string StringClass(getLangOptions().ObjCConstantStringClass);
16562bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
16572bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    Ty = llvm::ArrayType::get(Ty, 0);
16584c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    llvm::Constant *GV;
16594c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    if (StringClass.empty())
16604c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian      GV = CreateRuntimeVariable(Ty,
16614c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian                                 Features.ObjCNonFragileABI ?
16624c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian                                 "OBJC_CLASS_$_NSConstantString" :
16634c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian                                 "_NSConstantStringClassReference");
16644c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    else {
16654c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian      std::string str;
16664c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian      if (Features.ObjCNonFragileABI)
16674c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian        str = "OBJC_CLASS_$_" + StringClass;
16684c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian      else
16694c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian        str = "_" + StringClass + "ClassReference";
16704c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian      GV = CreateRuntimeVariable(Ty, str);
16714c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    }
16722bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Decay array -> ptr
16734c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    ConstantStringClassRef =
16744c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian    llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
16752bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
16762bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16772bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  QualType NSTy = getContext().getNSConstantStringType();
16782bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16792bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const llvm::StructType *STy =
16802bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  cast<llvm::StructType>(getTypes().ConvertType(NSTy));
16812bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16822bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  std::vector<llvm::Constant*> Fields(3);
16832bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16842bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  // Class pointer.
16854c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian  Fields[0] = ConstantStringClassRef;
16862bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16872bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  // String pointer.
16882bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
16892bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
16902bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::GlobalValue::LinkageTypes Linkage;
16912bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  bool isConstant;
16922bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (isUTF16) {
16932bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // FIXME: why do utf strings get "_" labels instead of "L" labels?
16942bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    Linkage = llvm::GlobalValue::InternalLinkage;
16952bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Note: -fwritable-strings doesn't make unicode NSStrings writable, but
16962bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // does make plain ascii ones writable.
16972bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    isConstant = true;
16982bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  } else {
16992bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    Linkage = llvm::GlobalValue::PrivateLinkage;
17002bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    isConstant = !Features.WritableStrings;
17012bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
17022bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
17032bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  llvm::GlobalVariable *GV =
17042bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
17052bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                           ".str");
17062bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (isUTF16) {
17072bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
17082bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    GV->setAlignment(Align.getQuantity());
17092bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
17102bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
17112bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
17122bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  // String length.
17132bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
17142bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
17152bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
17162bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  // The struct.
17172bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  C = llvm::ConstantStruct::get(STy, Fields);
17182bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
17192bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                llvm::GlobalVariable::PrivateLinkage, C,
17202bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                "_unnamed_nsstring_");
17212bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  // FIXME. Fix section.
1722ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian  if (const char *Sect =
1723ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian        Features.ObjCNonFragileABI
1724ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian          ? getContext().Target.getNSStringNonFragileABISection()
1725ec951e0c2fc0db00c36bc60c900331dde32c1b43Fariborz Jahanian          : getContext().Target.getNSStringSection())
17262bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    GV->setSection(Sect);
17272bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  Entry.setValue(GV);
17282bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
17292bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  return GV;
173033e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanian}
173133e982bf782d851bfe5767acb1336fcf3419ac6bFariborz Jahanian
17326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a
17331e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type.
17346143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
17351e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  const ConstantArrayType *CAT =
17361e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    getContext().getAsConstantArrayType(E->getType());
17371e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  assert(CAT && "String isn't pointer or array!");
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1739dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  // Resize the string to the right size.
17401e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  uint64_t RealLen = CAT->getSize().getZExtValue();
17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1742dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner  if (E->isWide())
1743dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    RealLen *= getContext().Target.getWCharWidth()/8;
17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17452f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  std::string Str = E->getString().str();
17461e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  Str.resize(RealLen, '\0');
17471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17481e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  return Str;
17491e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar}
17501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
17516143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
17526143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal.
17536143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant *
17546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
17556143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // FIXME: This can be more efficient.
17567eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman  // FIXME: We shouldn't need to bitcast the constant in the wide string case.
17577eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman  llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S));
17587eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman  if (S->isWide()) {
17597eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman    llvm::Type *DestTy =
17607eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman        llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType()));
17617eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman    C = llvm::ConstantExpr::getBitCast(C, DestTy);
17627eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman  }
17637eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman  return C;
17646143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
17656143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
1766eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1767eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node.
1768eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant *
1769eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1770eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  std::string Str;
1771eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1772a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman
1773a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman  return GetAddrOfConstantCString(Str);
1774eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner}
1775eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1776eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1777a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal.
17781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic llvm::Constant *GenerateStringLiteral(const std::string &str,
177945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                             bool constant,
17805fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             CodeGenModule &CGM,
17815fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                             const char *GlobalName) {
17826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  // Create Constant for this string literal. Don't add a '\0'.
17830032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Constant *C =
17840032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::ConstantArray::get(CGM.getLLVMContext(), str, false);
17851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
178645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this string
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
178895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                                  llvm::GlobalValue::PrivateLinkage,
17891c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                                  C, GlobalName);
179045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
179145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
17926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array
17936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the
17946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically;
17956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is
17966143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on
17976143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings.
17986143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar///
17996143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type.
18005fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
18015fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                       const char *GlobalName) {
18028e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  bool IsConstant = !Features.WritableStrings;
18038e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar
18048e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  // Get the default prefix if a name wasn't specified.
18058e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (!GlobalName)
180695b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    GlobalName = ".str";
18078e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar
18088e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  // Don't share any string literals if strings aren't constant.
18098e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (!IsConstant)
18105fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    return GenerateStringLiteral(str, false, *this, GlobalName);
18111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::StringMapEntry<llvm::Constant *> &Entry =
181395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner    ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
181445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
181545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Entry.getValue())
1816eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner    return Entry.getValue();
181745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
181845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this.
18195fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
182045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  Entry.setValue(C);
182145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
182245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
18236143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
18246143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character
18256143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-'
18266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type.
18275fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
18285fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                                        const char *GlobalName){
1829c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner  return GetAddrOfConstantString(str + '\0', GlobalName);
18306143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar}
183141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1832af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized
1833af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation.
18341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::EmitObjCPropertyImplementations(const
1835af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                                                    ObjCImplementationDecl *D) {
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCImplementationDecl::propimpl_iterator
183717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1838af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCPropertyImplDecl *PID = *i;
18391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1840af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Dynamic is just for type-checking.
1841af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1842af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      ObjCPropertyDecl *PD = PID->getPropertyDecl();
1843af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1844af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // Determine which methods need to be implemented, some may have
1845af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // been overridden. Note that ::isSynthesized is not the method
1846af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // we want, that just indicates if the decl came from a
1847af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // property. What we want to know is if the method is defined in
1848af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      // this implementation.
184917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      if (!D->getInstanceMethod(PD->getGetterName()))
1850fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCGetter(
1851fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1852af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!PD->isReadOnly() &&
185317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis          !D->getInstanceMethod(PD->getSetterName()))
1854fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian        CodeGenFunction(*this).GenerateObjCSetter(
1855fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                 const_cast<ObjCImplementationDecl *>(D), PID);
1856af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    }
1857af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
1858af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
1859af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1860109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian/// EmitObjCIvarInitializations - Emit information for ivar initialization
1861109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian/// for an implementation.
1862109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
1863109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0)
1864109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    return;
1865109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D));
1866109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  assert(DC && "EmitObjCIvarInitializations - null DeclContext");
1867109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
1868109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
1869109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(),
1870109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                  D->getLocation(),
1871109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                  D->getLocation(), cxxSelector,
1872109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                  getContext().VoidTy, 0,
18733fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                                                  DC, true, false, true, false,
1874109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                  ObjCMethodDecl::Required);
1875109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  D->addInstanceMethod(DTORMethod);
1876109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
1877109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
1878109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  II = &getContext().Idents.get(".cxx_construct");
1879109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  cxxSelector = getContext().Selectors.getSelector(0, &II);
1880109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  // The constructor returns 'self'.
1881109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
1882109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                D->getLocation(),
1883109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                D->getLocation(), cxxSelector,
1884109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                getContext().getObjCIdType(), 0,
18853fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                                                DC, true, false, true, false,
1886109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                ObjCMethodDecl::Required);
1887109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  D->addInstanceMethod(CTORMethod);
1888109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
1889109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
1890109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
1891109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian}
1892109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
189391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace.
1894984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
189517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
1896984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson       I != E; ++I)
1897984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson    EmitTopLevelDecl(*I);
1898984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson}
1899984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson
190091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec.
190191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
1902f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman  if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
1903f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman      LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
190491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    ErrorUnsupported(LSD, "linkage spec");
190591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    return;
190691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  }
190791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
190817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
190991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson       I != E; ++I)
191091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    EmitTopLevelDecl(*I);
191191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson}
191291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
191341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration.
191441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) {
191541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // If an error has occurred, stop code generation, but continue
191641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // parsing and semantic analysis (to ensure all warnings and errors
191741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // are emitted).
191841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  if (Diags.hasErrorOccurred())
191941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
192041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
192116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  // Ignore dependent declarations.
192216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
192316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return;
19241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
192541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  switch (D->getKind()) {
1926293361afd4199c92aabff9267fddea890943c586Anders Carlsson  case Decl::CXXConversion:
19272b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  case Decl::CXXMethod:
192841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Function:
192916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    // Skip function templates
193016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
193116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor      return;
19321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1933555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson    EmitGlobal(cast<FunctionDecl>(D));
1934555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson    break;
1935555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson
193641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Var:
1937555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson    EmitGlobal(cast<VarDecl>(D));
193841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
193941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
194095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  // C++ Decls
194141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::Namespace:
1942984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson    EmitNamespace(cast<NamespaceDecl>(D));
194341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
19449cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    // No code generation needed.
19459d0c6613ec040ad8d952556be909232a7f25dedeJohn McCall  case Decl::UsingShadow:
19469cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  case Decl::Using:
1947dd9967a6374c9a44be4af02aaeee340ffb82848fDouglas Gregor  case Decl::UsingDirective:
1948127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  case Decl::ClassTemplate:
1949127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  case Decl::FunctionTemplate:
1950018837b081bd1a725bae1c020eb1649975b04d67Anders Carlsson  case Decl::NamespaceAlias:
19519cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    break;
195295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  case Decl::CXXConstructor:
19531fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson    // Skip function templates
19541fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson    if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
19551fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson      return;
19561fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson
195795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    EmitCXXConstructors(cast<CXXConstructorDecl>(D));
195895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    break;
195927ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson  case Decl::CXXDestructor:
196027ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson    EmitCXXDestructors(cast<CXXDestructorDecl>(D));
196127ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson    break;
196236674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson
196336674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson  case Decl::StaticAssert:
196436674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson    // Nothing to do.
196536674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson    break;
196636674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson
196795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  // Objective-C Decls
19681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
196938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian  // Forward declarations, no (immediate) code generation.
197041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCClass:
197141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCForwardProtocol:
1972b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian  case Decl::ObjCInterface:
197341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
1974000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian
1975000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian    case Decl::ObjCCategory: {
1976000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian      ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
1977000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian      if (CD->IsClassExtension() && CD->hasSynthBitfield())
1978000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian        Context.ResetObjCLayout(CD->getClassInterface());
1979000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian      break;
1980000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian    }
1981000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian
1982285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner
198341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCProtocol:
1984b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian    Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
198541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
198641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
198741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCCategoryImpl:
1988af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Categories have properties but don't support synthesize so we
1989af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // can ignore them here.
199041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
199141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
199241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1993af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  case Decl::ObjCImplementation: {
1994af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1995000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian    if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield())
1996000835d0b04345c0014c603fe6339b3bc154050eFariborz Jahanian      Context.ResetObjCLayout(OMD->getClassInterface());
1997af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    EmitObjCPropertyImplementations(OMD);
1998109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    EmitObjCIvarInitializations(OMD);
1999af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    Runtime->GenerateClass(OMD);
200041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
20011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
200241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::ObjCMethod: {
200341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
200441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // If this is not a prototype, emit the body.
20056fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    if (OMD->getBody())
200641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      CodeGenFunction(*this).GenerateObjCMethod(OMD);
200741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
200841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
20091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::ObjCCompatibleAlias:
2010305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian    // compatibility-alias is a directive and has no code gen.
201141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
201241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
201391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  case Decl::LinkageSpec:
201491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    EmitLinkageSpec(cast<LinkageSpecDecl>(D));
201541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
201641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
201741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  case Decl::FileScopeAsm: {
201841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
20198d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer    llvm::StringRef AsmString = AD->getAsmString()->getString();
20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
202141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    const std::string &S = getModule().getModuleInlineAsm();
202241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (S.empty())
202341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      getModule().setModuleInlineAsm(AsmString);
202441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    else
20258d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer      getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
202641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    break;
202741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
20281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  default:
2030f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Make sure we handled everything we should, every other kind is a
2031f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
2032f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // function. Need to recode Decl::Kind to do that easily.
203341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    assert(isa<TypeDecl>(D) && "Unsupported decl kind");
203441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  }
203541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar}
2036744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2037744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Turns the given pointer into a constant.
2038744016dde06fcffd50931e94a98c850f8b12cd87John McCallstatic llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
2039744016dde06fcffd50931e94a98c850f8b12cd87John McCall                                          const void *Ptr) {
2040744016dde06fcffd50931e94a98c850f8b12cd87John McCall  uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
2041744016dde06fcffd50931e94a98c850f8b12cd87John McCall  const llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
2042744016dde06fcffd50931e94a98c850f8b12cd87John McCall  return llvm::ConstantInt::get(i64, PtrInt);
2043744016dde06fcffd50931e94a98c850f8b12cd87John McCall}
2044744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2045744016dde06fcffd50931e94a98c850f8b12cd87John McCallstatic void EmitGlobalDeclMetadata(CodeGenModule &CGM,
2046744016dde06fcffd50931e94a98c850f8b12cd87John McCall                                   llvm::NamedMDNode *&GlobalMetadata,
2047744016dde06fcffd50931e94a98c850f8b12cd87John McCall                                   GlobalDecl D,
2048744016dde06fcffd50931e94a98c850f8b12cd87John McCall                                   llvm::GlobalValue *Addr) {
2049744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (!GlobalMetadata)
2050744016dde06fcffd50931e94a98c850f8b12cd87John McCall    GlobalMetadata =
2051744016dde06fcffd50931e94a98c850f8b12cd87John McCall      CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
2052744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2053744016dde06fcffd50931e94a98c850f8b12cd87John McCall  // TODO: should we report variant information for ctors/dtors?
2054744016dde06fcffd50931e94a98c850f8b12cd87John McCall  llvm::Value *Ops[] = {
2055744016dde06fcffd50931e94a98c850f8b12cd87John McCall    Addr,
2056744016dde06fcffd50931e94a98c850f8b12cd87John McCall    GetPointerConstant(CGM.getLLVMContext(), D.getDecl())
2057744016dde06fcffd50931e94a98c850f8b12cd87John McCall  };
2058744016dde06fcffd50931e94a98c850f8b12cd87John McCall  GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops, 2));
2059744016dde06fcffd50931e94a98c850f8b12cd87John McCall}
2060744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2061744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Emits metadata nodes associating all the global values in the
2062744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// current module with the Decls they came from.  This is useful for
2063744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// projects using IR gen as a subroutine.
2064744016dde06fcffd50931e94a98c850f8b12cd87John McCall///
2065744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Since there's currently no way to associate an MDNode directly
2066744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// with an llvm::GlobalValue, we create a global named metadata
2067744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// with the name 'clang.global.decl.ptrs'.
2068744016dde06fcffd50931e94a98c850f8b12cd87John McCallvoid CodeGenModule::EmitDeclMetadata() {
2069744016dde06fcffd50931e94a98c850f8b12cd87John McCall  llvm::NamedMDNode *GlobalMetadata = 0;
2070744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2071744016dde06fcffd50931e94a98c850f8b12cd87John McCall  // StaticLocalDeclMap
2072744016dde06fcffd50931e94a98c850f8b12cd87John McCall  for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator
2073744016dde06fcffd50931e94a98c850f8b12cd87John McCall         I = MangledDeclNames.begin(), E = MangledDeclNames.end();
2074744016dde06fcffd50931e94a98c850f8b12cd87John McCall       I != E; ++I) {
2075744016dde06fcffd50931e94a98c850f8b12cd87John McCall    llvm::GlobalValue *Addr = getModule().getNamedValue(I->second);
2076744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr);
2077744016dde06fcffd50931e94a98c850f8b12cd87John McCall  }
2078744016dde06fcffd50931e94a98c850f8b12cd87John McCall}
2079744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2080744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// Emits metadata nodes for all the local variables in the current
2081744016dde06fcffd50931e94a98c850f8b12cd87John McCall/// function.
2082744016dde06fcffd50931e94a98c850f8b12cd87John McCallvoid CodeGenFunction::EmitDeclMetadata() {
2083744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (LocalDeclMap.empty()) return;
2084744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2085744016dde06fcffd50931e94a98c850f8b12cd87John McCall  llvm::LLVMContext &Context = getLLVMContext();
2086744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2087744016dde06fcffd50931e94a98c850f8b12cd87John McCall  // Find the unique metadata ID for this name.
2088744016dde06fcffd50931e94a98c850f8b12cd87John McCall  unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
2089744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2090744016dde06fcffd50931e94a98c850f8b12cd87John McCall  llvm::NamedMDNode *GlobalMetadata = 0;
2091744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2092744016dde06fcffd50931e94a98c850f8b12cd87John McCall  for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator
2093744016dde06fcffd50931e94a98c850f8b12cd87John McCall         I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) {
2094744016dde06fcffd50931e94a98c850f8b12cd87John McCall    const Decl *D = I->first;
2095744016dde06fcffd50931e94a98c850f8b12cd87John McCall    llvm::Value *Addr = I->second;
2096744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2097744016dde06fcffd50931e94a98c850f8b12cd87John McCall    if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
2098744016dde06fcffd50931e94a98c850f8b12cd87John McCall      llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
2099744016dde06fcffd50931e94a98c850f8b12cd87John McCall      Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, &DAddr, 1));
2100744016dde06fcffd50931e94a98c850f8b12cd87John McCall    } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
2101744016dde06fcffd50931e94a98c850f8b12cd87John McCall      GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
2102744016dde06fcffd50931e94a98c850f8b12cd87John McCall      EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
2103744016dde06fcffd50931e94a98c850f8b12cd87John McCall    }
2104744016dde06fcffd50931e94a98c850f8b12cd87John McCall  }
2105744016dde06fcffd50931e94a98c850f8b12cd87John McCall}
2106673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2107673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar///@name Custom Runtime Function Interfaces
2108673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar///@{
2109673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar//
2110673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar// FIXME: These can be eliminated once we can have clients just get the required
2111673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar// AST nodes from the builtin tables.
2112673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2113673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbarllvm::Constant *CodeGenModule::getBlockObjectDispose() {
2114673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  if (BlockObjectDispose)
2115673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    return BlockObjectDispose;
2116673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2117754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // If we saw an explicit decl, use that.
2118754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  if (BlockObjectDisposeDecl) {
2119754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    return BlockObjectDispose = GetAddrOfFunction(
2120754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      BlockObjectDisposeDecl,
2121754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      getTypes().GetFunctionType(BlockObjectDisposeDecl));
2122754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  }
2123754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2124754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // Otherwise construct the function by hand.
2125673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  const llvm::FunctionType *FTy;
2126673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  std::vector<const llvm::Type*> ArgTys;
2127673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2128673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  ArgTys.push_back(PtrToInt8Ty);
2129673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2130673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2131673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  return BlockObjectDispose =
2132673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    CreateRuntimeFunction(FTy, "_Block_object_dispose");
2133673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar}
2134673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2135673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbarllvm::Constant *CodeGenModule::getBlockObjectAssign() {
2136673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  if (BlockObjectAssign)
2137673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    return BlockObjectAssign;
2138673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2139754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // If we saw an explicit decl, use that.
2140754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  if (BlockObjectAssignDecl) {
2141754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    return BlockObjectAssign = GetAddrOfFunction(
2142754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      BlockObjectAssignDecl,
2143754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      getTypes().GetFunctionType(BlockObjectAssignDecl));
2144754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  }
2145754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2146754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // Otherwise construct the function by hand.
2147673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  const llvm::FunctionType *FTy;
2148673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  std::vector<const llvm::Type*> ArgTys;
2149673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2150673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  ArgTys.push_back(PtrToInt8Ty);
2151673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  ArgTys.push_back(PtrToInt8Ty);
2152673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2153673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2154673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  return BlockObjectAssign =
2155673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    CreateRuntimeFunction(FTy, "_Block_object_assign");
2156673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar}
2157673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2158673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbarllvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
2159673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  if (NSConcreteGlobalBlock)
2160673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    return NSConcreteGlobalBlock;
2161754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2162754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // If we saw an explicit decl, use that.
2163754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  if (NSConcreteGlobalBlockDecl) {
2164754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    return NSConcreteGlobalBlock = GetAddrOfGlobalVar(
2165754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      NSConcreteGlobalBlockDecl,
2166754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType()));
2167754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  }
2168754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2169754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // Otherwise construct the variable by hand.
2170673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  return NSConcreteGlobalBlock = CreateRuntimeVariable(
2171673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    PtrToInt8Ty, "_NSConcreteGlobalBlock");
2172673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar}
2173673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2174673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbarllvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
2175673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  if (NSConcreteStackBlock)
2176673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    return NSConcreteStackBlock;
2177754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2178754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // If we saw an explicit decl, use that.
2179754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  if (NSConcreteStackBlockDecl) {
2180754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar    return NSConcreteStackBlock = GetAddrOfGlobalVar(
2181754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      NSConcreteStackBlockDecl,
2182754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar      getTypes().ConvertType(NSConcreteStackBlockDecl->getType()));
2183754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  }
2184754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar
2185754b9fbaa13749c61393cc613eec7c79efe60ddfDaniel Dunbar  // Otherwise construct the variable by hand.
2186673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar  return NSConcreteStackBlock = CreateRuntimeVariable(
2187673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar    PtrToInt8Ty, "_NSConcreteStackBlock");
2188673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar}
2189673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar
2190673431a2986f750b4d8fadb57abf3f00db27bbbdDaniel Dunbar///@}
2191