CodeGenModule.cpp revision c532b502858032f377056dc8cba2fe43cba8702b
1//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-module state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CGDebugInfo.h"
16#include "CodeGenFunction.h"
17#include "CodeGenTBAA.h"
18#include "CGCall.h"
19#include "CGCXXABI.h"
20#include "CGObjCRuntime.h"
21#include "TargetInfo.h"
22#include "clang/Frontend/CodeGenOptions.h"
23#include "clang/AST/ASTContext.h"
24#include "clang/AST/CharUnits.h"
25#include "clang/AST/DeclObjC.h"
26#include "clang/AST/DeclCXX.h"
27#include "clang/AST/DeclTemplate.h"
28#include "clang/AST/Mangle.h"
29#include "clang/AST/RecordLayout.h"
30#include "clang/Basic/Builtins.h"
31#include "clang/Basic/Diagnostic.h"
32#include "clang/Basic/SourceManager.h"
33#include "clang/Basic/TargetInfo.h"
34#include "clang/Basic/ConvertUTF.h"
35#include "llvm/CallingConv.h"
36#include "llvm/Module.h"
37#include "llvm/Intrinsics.h"
38#include "llvm/LLVMContext.h"
39#include "llvm/ADT/Triple.h"
40#include "llvm/Target/TargetData.h"
41#include "llvm/Support/CallSite.h"
42#include "llvm/Support/ErrorHandling.h"
43using namespace clang;
44using namespace CodeGen;
45
46static CGCXXABI &createCXXABI(CodeGenModule &CGM) {
47  switch (CGM.getContext().Target.getCXXABI()) {
48  case CXXABI_ARM: return *CreateARMCXXABI(CGM);
49  case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM);
50  case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM);
51  }
52
53  llvm_unreachable("invalid C++ ABI kind");
54  return *CreateItaniumCXXABI(CGM);
55}
56
57
58CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
59                             llvm::Module &M, const llvm::TargetData &TD,
60                             Diagnostic &diags)
61  : BlockModule(C, M, TD, Types, *this), Context(C),
62    Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M),
63    TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
64    ABI(createCXXABI(*this)),
65    Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI),
66    TBAA(0),
67    VTables(*this), Runtime(0),
68    CFConstantStringClassRef(0), ConstantStringClassRef(0),
69    VMContext(M.getContext()),
70    NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0),
71    NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
72    BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0),
73    BlockObjectAssign(0), BlockObjectDispose(0){
74
75  if (!Features.ObjC1)
76    Runtime = 0;
77  else if (!Features.NeXTRuntime)
78    Runtime = CreateGNUObjCRuntime(*this);
79  else if (Features.ObjCNonFragileABI)
80    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
81  else
82    Runtime = CreateMacObjCRuntime(*this);
83
84  // Enable TBAA unless it's suppressed.
85  if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)
86    TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(),
87                           ABI.getMangleContext());
88
89  // If debug info generation is enabled, create the CGDebugInfo object.
90  DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0;
91}
92
93CodeGenModule::~CodeGenModule() {
94  delete Runtime;
95  delete &ABI;
96  delete TBAA;
97  delete DebugInfo;
98}
99
100void CodeGenModule::createObjCRuntime() {
101  if (!Features.NeXTRuntime)
102    Runtime = CreateGNUObjCRuntime(*this);
103  else if (Features.ObjCNonFragileABI)
104    Runtime = CreateMacNonFragileABIObjCRuntime(*this);
105  else
106    Runtime = CreateMacObjCRuntime(*this);
107}
108
109void CodeGenModule::Release() {
110  EmitDeferred();
111  EmitCXXGlobalInitFunc();
112  EmitCXXGlobalDtorFunc();
113  if (Runtime)
114    if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
115      AddGlobalCtor(ObjCInitFunction);
116  EmitCtorList(GlobalCtors, "llvm.global_ctors");
117  EmitCtorList(GlobalDtors, "llvm.global_dtors");
118  EmitAnnotations();
119  EmitLLVMUsed();
120
121  SimplifyPersonality();
122
123  if (getCodeGenOpts().EmitDeclMetadata)
124    EmitDeclMetadata();
125}
126
127llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
128  if (!TBAA)
129    return 0;
130  return TBAA->getTBAAInfo(QTy);
131}
132
133void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
134                                        llvm::MDNode *TBAAInfo) {
135  Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
136}
137
138bool CodeGenModule::isTargetDarwin() const {
139  return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin;
140}
141
142/// ErrorUnsupported - Print out an error that codegen doesn't support the
143/// specified stmt yet.
144void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
145                                     bool OmitOnError) {
146  if (OmitOnError && getDiags().hasErrorOccurred())
147    return;
148  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
149                                               "cannot compile this %0 yet");
150  std::string Msg = Type;
151  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
152    << Msg << S->getSourceRange();
153}
154
155/// ErrorUnsupported - Print out an error that codegen doesn't support the
156/// specified decl yet.
157void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
158                                     bool OmitOnError) {
159  if (OmitOnError && getDiags().hasErrorOccurred())
160    return;
161  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
162                                               "cannot compile this %0 yet");
163  std::string Msg = Type;
164  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
165}
166
167static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
168  switch (V) {
169  case DefaultVisibility:   return llvm::GlobalValue::DefaultVisibility;
170  case HiddenVisibility:    return llvm::GlobalValue::HiddenVisibility;
171  case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
172  }
173  llvm_unreachable("unknown visibility!");
174  return llvm::GlobalValue::DefaultVisibility;
175}
176
177
178void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
179                                        const NamedDecl *D,
180                                        bool IsForDefinition) const {
181  // Internal definitions always have default visibility.
182  if (GV->hasLocalLinkage()) {
183    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
184    return;
185  }
186
187  // Set visibility for definitions.
188  NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility();
189  if (LV.visibilityExplicit() ||
190      (IsForDefinition && !GV->hasAvailableExternallyLinkage()))
191    GV->setVisibility(GetLLVMVisibility(LV.visibility()));
192}
193
194/// Set the symbol visibility of type information (vtable and RTTI)
195/// associated with the given type.
196void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV,
197                                      const CXXRecordDecl *RD,
198                                      bool IsForRTTI,
199                                      bool IsForDefinition) const {
200  setGlobalVisibility(GV, RD, IsForDefinition);
201
202  if (!CodeGenOpts.HiddenWeakVTables)
203    return;
204
205  // We want to drop the visibility to hidden for weak type symbols.
206  // This isn't possible if there might be unresolved references
207  // elsewhere that rely on this symbol being visible.
208
209  // This should be kept roughly in sync with setThunkVisibility
210  // in CGVTables.cpp.
211
212  // Preconditions.
213  if (GV->getLinkage() != llvm::GlobalVariable::WeakODRLinkage ||
214      GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
215    return;
216
217  // Don't override an explicit visibility attribute.
218  if (RD->hasAttr<VisibilityAttr>())
219    return;
220
221  switch (RD->getTemplateSpecializationKind()) {
222  // We have to disable the optimization if this is an EI definition
223  // because there might be EI declarations in other shared objects.
224  case TSK_ExplicitInstantiationDefinition:
225  case TSK_ExplicitInstantiationDeclaration:
226    return;
227
228  // Every use of a non-template class's type information has to emit it.
229  case TSK_Undeclared:
230    break;
231
232  // In theory, implicit instantiations can ignore the possibility of
233  // an explicit instantiation declaration because there necessarily
234  // must be an EI definition somewhere with default visibility.  In
235  // practice, it's possible to have an explicit instantiation for
236  // an arbitrary template class, and linkers aren't necessarily able
237  // to deal with mixed-visibility symbols.
238  case TSK_ExplicitSpecialization:
239  case TSK_ImplicitInstantiation:
240    if (!CodeGenOpts.HiddenWeakTemplateVTables)
241      return;
242    break;
243  }
244
245  // If there's a key function, there may be translation units
246  // that don't have the key function's definition.  But ignore
247  // this if we're emitting RTTI under -fno-rtti.
248  if (!IsForRTTI || Features.RTTI)
249    if (Context.getKeyFunction(RD))
250      return;
251
252  // Otherwise, drop the visibility to hidden.
253  GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
254  GV->setUnnamedAddr(true);
255}
256
257llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
258  const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
259
260  llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
261  if (!Str.empty())
262    return Str;
263
264  if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
265    IdentifierInfo *II = ND->getIdentifier();
266    assert(II && "Attempt to mangle unnamed decl.");
267
268    Str = II->getName();
269    return Str;
270  }
271
272  llvm::SmallString<256> Buffer;
273  if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
274    getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Buffer);
275  else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
276    getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer);
277  else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND))
278    getCXXABI().getMangleContext().mangleBlock(BD, Buffer);
279  else
280    getCXXABI().getMangleContext().mangleName(ND, Buffer);
281
282  // Allocate space for the mangled name.
283  size_t Length = Buffer.size();
284  char *Name = MangledNamesAllocator.Allocate<char>(Length);
285  std::copy(Buffer.begin(), Buffer.end(), Name);
286
287  Str = llvm::StringRef(Name, Length);
288
289  return Str;
290}
291
292void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
293                                        const BlockDecl *BD) {
294  MangleContext &MangleCtx = getCXXABI().getMangleContext();
295  const Decl *D = GD.getDecl();
296  if (D == 0)
297    MangleCtx.mangleGlobalBlock(BD, Buffer.getBuffer());
298  else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
299    MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Buffer.getBuffer());
300  else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D))
301    MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Buffer.getBuffer());
302  else
303    MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Buffer.getBuffer());
304}
305
306llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) {
307  return getModule().getNamedValue(Name);
308}
309
310/// AddGlobalCtor - Add a function to the list that will be called before
311/// main() runs.
312void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
313  // FIXME: Type coercion of void()* types.
314  GlobalCtors.push_back(std::make_pair(Ctor, Priority));
315}
316
317/// AddGlobalDtor - Add a function to the list that will be called
318/// when the module is unloaded.
319void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
320  // FIXME: Type coercion of void()* types.
321  GlobalDtors.push_back(std::make_pair(Dtor, Priority));
322}
323
324void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
325  // Ctor function type is void()*.
326  llvm::FunctionType* CtorFTy =
327    llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
328                            std::vector<const llvm::Type*>(),
329                            false);
330  llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
331
332  // Get the type of a ctor entry, { i32, void ()* }.
333  llvm::StructType* CtorStructTy =
334    llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext),
335                          llvm::PointerType::getUnqual(CtorFTy), NULL);
336
337  // Construct the constructor and destructor arrays.
338  std::vector<llvm::Constant*> Ctors;
339  for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
340    std::vector<llvm::Constant*> S;
341    S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
342                I->second, false));
343    S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
344    Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
345  }
346
347  if (!Ctors.empty()) {
348    llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
349    new llvm::GlobalVariable(TheModule, AT, false,
350                             llvm::GlobalValue::AppendingLinkage,
351                             llvm::ConstantArray::get(AT, Ctors),
352                             GlobalName);
353  }
354}
355
356void CodeGenModule::EmitAnnotations() {
357  if (Annotations.empty())
358    return;
359
360  // Create a new global variable for the ConstantStruct in the Module.
361  llvm::Constant *Array =
362  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
363                                                Annotations.size()),
364                           Annotations);
365  llvm::GlobalValue *gv =
366  new llvm::GlobalVariable(TheModule, Array->getType(), false,
367                           llvm::GlobalValue::AppendingLinkage, Array,
368                           "llvm.global.annotations");
369  gv->setSection("llvm.metadata");
370}
371
372llvm::GlobalValue::LinkageTypes
373CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
374  GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
375
376  if (Linkage == GVA_Internal)
377    return llvm::Function::InternalLinkage;
378
379  if (D->hasAttr<DLLExportAttr>())
380    return llvm::Function::DLLExportLinkage;
381
382  if (D->hasAttr<WeakAttr>())
383    return llvm::Function::WeakAnyLinkage;
384
385  // In C99 mode, 'inline' functions are guaranteed to have a strong
386  // definition somewhere else, so we can use available_externally linkage.
387  if (Linkage == GVA_C99Inline)
388    return llvm::Function::AvailableExternallyLinkage;
389
390  // In C++, the compiler has to emit a definition in every translation unit
391  // that references the function.  We should use linkonce_odr because
392  // a) if all references in this translation unit are optimized away, we
393  // don't need to codegen it.  b) if the function persists, it needs to be
394  // merged with other definitions. c) C++ has the ODR, so we know the
395  // definition is dependable.
396  if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
397    return llvm::Function::LinkOnceODRLinkage;
398
399  // An explicit instantiation of a template has weak linkage, since
400  // explicit instantiations can occur in multiple translation units
401  // and must all be equivalent. However, we are not allowed to
402  // throw away these explicit instantiations.
403  if (Linkage == GVA_ExplicitTemplateInstantiation)
404    return llvm::Function::WeakODRLinkage;
405
406  // Otherwise, we have strong external linkage.
407  assert(Linkage == GVA_StrongExternal);
408  return llvm::Function::ExternalLinkage;
409}
410
411
412/// SetFunctionDefinitionAttributes - Set attributes for a global.
413///
414/// FIXME: This is currently only done for aliases and functions, but not for
415/// variables (these details are set in EmitGlobalVarDefinition for variables).
416void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
417                                                    llvm::GlobalValue *GV) {
418  SetCommonAttributes(D, GV);
419}
420
421void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
422                                              const CGFunctionInfo &Info,
423                                              llvm::Function *F) {
424  unsigned CallingConv;
425  AttributeListType AttributeList;
426  ConstructAttributeList(Info, D, AttributeList, CallingConv);
427  F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
428                                          AttributeList.size()));
429  F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
430}
431
432void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
433                                                           llvm::Function *F) {
434  if (!Features.Exceptions && !Features.ObjCNonFragileABI)
435    F->addFnAttr(llvm::Attribute::NoUnwind);
436
437  if (D->hasAttr<AlwaysInlineAttr>())
438    F->addFnAttr(llvm::Attribute::AlwaysInline);
439
440  if (D->hasAttr<NakedAttr>())
441    F->addFnAttr(llvm::Attribute::Naked);
442
443  if (D->hasAttr<NoInlineAttr>())
444    F->addFnAttr(llvm::Attribute::NoInline);
445
446  if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
447    F->setUnnamedAddr(true);
448
449  if (Features.getStackProtectorMode() == LangOptions::SSPOn)
450    F->addFnAttr(llvm::Attribute::StackProtect);
451  else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
452    F->addFnAttr(llvm::Attribute::StackProtectReq);
453
454  unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
455  if (alignment)
456    F->setAlignment(alignment);
457
458  // C++ ABI requires 2-byte alignment for member functions.
459  if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
460    F->setAlignment(2);
461}
462
463void CodeGenModule::SetCommonAttributes(const Decl *D,
464                                        llvm::GlobalValue *GV) {
465  if (isa<NamedDecl>(D))
466    setGlobalVisibility(GV, cast<NamedDecl>(D), /*ForDef*/ true);
467  else
468    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
469
470  if (D->hasAttr<UsedAttr>())
471    AddUsedGlobal(GV);
472
473  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
474    GV->setSection(SA->getName());
475
476  getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this);
477}
478
479void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
480                                                  llvm::Function *F,
481                                                  const CGFunctionInfo &FI) {
482  SetLLVMFunctionAttributes(D, FI, F);
483  SetLLVMFunctionAttributesForDefinition(D, F);
484
485  F->setLinkage(llvm::Function::InternalLinkage);
486
487  SetCommonAttributes(D, F);
488}
489
490void CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
491                                          llvm::Function *F,
492                                          bool IsIncompleteFunction) {
493  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
494
495  if (!IsIncompleteFunction)
496    SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F);
497
498  // Only a few attributes are set on declarations; these may later be
499  // overridden by a definition.
500
501  if (FD->hasAttr<DLLImportAttr>()) {
502    F->setLinkage(llvm::Function::DLLImportLinkage);
503  } else if (FD->hasAttr<WeakAttr>() ||
504             FD->hasAttr<WeakImportAttr>()) {
505    // "extern_weak" is overloaded in LLVM; we probably should have
506    // separate linkage types for this.
507    F->setLinkage(llvm::Function::ExternalWeakLinkage);
508  } else {
509    F->setLinkage(llvm::Function::ExternalLinkage);
510
511    NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility();
512    if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) {
513      F->setVisibility(GetLLVMVisibility(LV.visibility()));
514    }
515  }
516
517  if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
518    F->setSection(SA->getName());
519}
520
521void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
522  assert(!GV->isDeclaration() &&
523         "Only globals with definition can force usage.");
524  LLVMUsed.push_back(GV);
525}
526
527void CodeGenModule::EmitLLVMUsed() {
528  // Don't create llvm.used if there is no need.
529  if (LLVMUsed.empty())
530    return;
531
532  const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
533
534  // Convert LLVMUsed to what ConstantArray needs.
535  std::vector<llvm::Constant*> UsedArray;
536  UsedArray.resize(LLVMUsed.size());
537  for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
538    UsedArray[i] =
539     llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
540                                      i8PTy);
541  }
542
543  if (UsedArray.empty())
544    return;
545  llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size());
546
547  llvm::GlobalVariable *GV =
548    new llvm::GlobalVariable(getModule(), ATy, false,
549                             llvm::GlobalValue::AppendingLinkage,
550                             llvm::ConstantArray::get(ATy, UsedArray),
551                             "llvm.used");
552
553  GV->setSection("llvm.metadata");
554}
555
556void CodeGenModule::EmitDeferred() {
557  // Emit code for any potentially referenced deferred decls.  Since a
558  // previously unused static decl may become used during the generation of code
559  // for a static function, iterate until no  changes are made.
560
561  while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) {
562    if (!DeferredVTables.empty()) {
563      const CXXRecordDecl *RD = DeferredVTables.back();
564      DeferredVTables.pop_back();
565      getVTables().GenerateClassData(getVTableLinkage(RD), RD);
566      continue;
567    }
568
569    GlobalDecl D = DeferredDeclsToEmit.back();
570    DeferredDeclsToEmit.pop_back();
571
572    // Check to see if we've already emitted this.  This is necessary
573    // for a couple of reasons: first, decls can end up in the
574    // deferred-decls queue multiple times, and second, decls can end
575    // up with definitions in unusual ways (e.g. by an extern inline
576    // function acquiring a strong function redefinition).  Just
577    // ignore these cases.
578    //
579    // TODO: That said, looking this up multiple times is very wasteful.
580    llvm::StringRef Name = getMangledName(D);
581    llvm::GlobalValue *CGRef = GetGlobalValue(Name);
582    assert(CGRef && "Deferred decl wasn't referenced?");
583
584    if (!CGRef->isDeclaration())
585      continue;
586
587    // GlobalAlias::isDeclaration() defers to the aliasee, but for our
588    // purposes an alias counts as a definition.
589    if (isa<llvm::GlobalAlias>(CGRef))
590      continue;
591
592    // Otherwise, emit the definition and move on to the next one.
593    EmitGlobalDefinition(D);
594  }
595}
596
597/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
598/// annotation information for a given GlobalValue.  The annotation struct is
599/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
600/// GlobalValue being annotated.  The second field is the constant string
601/// created from the AnnotateAttr's annotation.  The third field is a constant
602/// string containing the name of the translation unit.  The fourth field is
603/// the line number in the file of the annotated value declaration.
604///
605/// FIXME: this does not unique the annotation string constants, as llvm-gcc
606///        appears to.
607///
608llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
609                                                const AnnotateAttr *AA,
610                                                unsigned LineNo) {
611  llvm::Module *M = &getModule();
612
613  // get [N x i8] constants for the annotation string, and the filename string
614  // which are the 2nd and 3rd elements of the global annotation structure.
615  const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext);
616  llvm::Constant *anno = llvm::ConstantArray::get(VMContext,
617                                                  AA->getAnnotation(), true);
618  llvm::Constant *unit = llvm::ConstantArray::get(VMContext,
619                                                  M->getModuleIdentifier(),
620                                                  true);
621
622  // Get the two global values corresponding to the ConstantArrays we just
623  // created to hold the bytes of the strings.
624  llvm::GlobalValue *annoGV =
625    new llvm::GlobalVariable(*M, anno->getType(), false,
626                             llvm::GlobalValue::PrivateLinkage, anno,
627                             GV->getName());
628  // translation unit name string, emitted into the llvm.metadata section.
629  llvm::GlobalValue *unitGV =
630    new llvm::GlobalVariable(*M, unit->getType(), false,
631                             llvm::GlobalValue::PrivateLinkage, unit,
632                             ".str");
633  unitGV->setUnnamedAddr(true);
634
635  // Create the ConstantStruct for the global annotation.
636  llvm::Constant *Fields[4] = {
637    llvm::ConstantExpr::getBitCast(GV, SBP),
638    llvm::ConstantExpr::getBitCast(annoGV, SBP),
639    llvm::ConstantExpr::getBitCast(unitGV, SBP),
640    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo)
641  };
642  return llvm::ConstantStruct::get(VMContext, Fields, 4, false);
643}
644
645bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
646  // Never defer when EmitAllDecls is specified.
647  if (Features.EmitAllDecls)
648    return false;
649
650  return !getContext().DeclMustBeEmitted(Global);
651}
652
653llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
654  const AliasAttr *AA = VD->getAttr<AliasAttr>();
655  assert(AA && "No alias?");
656
657  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
658
659  // See if there is already something with the target's name in the module.
660  llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
661
662  llvm::Constant *Aliasee;
663  if (isa<llvm::FunctionType>(DeclTy))
664    Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl());
665  else
666    Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
667                                    llvm::PointerType::getUnqual(DeclTy), 0);
668  if (!Entry) {
669    llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
670    F->setLinkage(llvm::Function::ExternalWeakLinkage);
671    WeakRefReferences.insert(F);
672  }
673
674  return Aliasee;
675}
676
677void CodeGenModule::EmitGlobal(GlobalDecl GD) {
678  const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
679
680  // Weak references don't produce any output by themselves.
681  if (Global->hasAttr<WeakRefAttr>())
682    return;
683
684  // If this is an alias definition (which otherwise looks like a declaration)
685  // emit it now.
686  if (Global->hasAttr<AliasAttr>())
687    return EmitAliasDefinition(GD);
688
689  // Ignore declarations, they will be emitted on their first use.
690  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
691    if (FD->getIdentifier()) {
692      llvm::StringRef Name = FD->getName();
693      if (Name == "_Block_object_assign") {
694        BlockObjectAssignDecl = FD;
695      } else if (Name == "_Block_object_dispose") {
696        BlockObjectDisposeDecl = FD;
697      }
698    }
699
700    // Forward declarations are emitted lazily on first use.
701    if (!FD->isThisDeclarationADefinition())
702      return;
703  } else {
704    const VarDecl *VD = cast<VarDecl>(Global);
705    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
706
707    if (VD->getIdentifier()) {
708      llvm::StringRef Name = VD->getName();
709      if (Name == "_NSConcreteGlobalBlock") {
710        NSConcreteGlobalBlockDecl = VD;
711      } else if (Name == "_NSConcreteStackBlock") {
712        NSConcreteStackBlockDecl = VD;
713      }
714    }
715
716
717    if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
718      return;
719  }
720
721  // Defer code generation when possible if this is a static definition, inline
722  // function etc.  These we only want to emit if they are used.
723  if (!MayDeferGeneration(Global)) {
724    // Emit the definition if it can't be deferred.
725    EmitGlobalDefinition(GD);
726    return;
727  }
728
729  // If we're deferring emission of a C++ variable with an
730  // initializer, remember the order in which it appeared in the file.
731  if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) &&
732      cast<VarDecl>(Global)->hasInit()) {
733    DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
734    CXXGlobalInits.push_back(0);
735  }
736
737  // If the value has already been used, add it directly to the
738  // DeferredDeclsToEmit list.
739  llvm::StringRef MangledName = getMangledName(GD);
740  if (GetGlobalValue(MangledName))
741    DeferredDeclsToEmit.push_back(GD);
742  else {
743    // Otherwise, remember that we saw a deferred decl with this name.  The
744    // first use of the mangled name will cause it to move into
745    // DeferredDeclsToEmit.
746    DeferredDecls[MangledName] = GD;
747  }
748}
749
750void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) {
751  const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
752
753  PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
754                                 Context.getSourceManager(),
755                                 "Generating code for declaration");
756
757  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
758    // At -O0, don't generate IR for functions with available_externally
759    // linkage.
760    if (CodeGenOpts.OptimizationLevel == 0 &&
761        !Function->hasAttr<AlwaysInlineAttr>() &&
762        getFunctionLinkage(Function)
763                                  == llvm::Function::AvailableExternallyLinkage)
764      return;
765
766    if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
767      if (Method->isVirtual())
768        getVTables().EmitThunks(GD);
769
770      if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method))
771        return EmitCXXConstructor(CD, GD.getCtorType());
772
773      if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Method))
774        return EmitCXXDestructor(DD, GD.getDtorType());
775    }
776
777    return EmitGlobalFunctionDefinition(GD);
778  }
779
780  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
781    return EmitGlobalVarDefinition(VD);
782
783  assert(0 && "Invalid argument to EmitGlobalDefinition()");
784}
785
786/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
787/// module, create and return an llvm Function with the specified type. If there
788/// is something in the module with the specified name, return it potentially
789/// bitcasted to the right type.
790///
791/// If D is non-null, it specifies a decl that correspond to this.  This is used
792/// to set the attributes on the function when it is first created.
793llvm::Constant *
794CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName,
795                                       const llvm::Type *Ty,
796                                       GlobalDecl D) {
797  // Lookup the entry, lazily creating it if necessary.
798  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
799  if (Entry) {
800    if (WeakRefReferences.count(Entry)) {
801      const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl());
802      if (FD && !FD->hasAttr<WeakAttr>())
803        Entry->setLinkage(llvm::Function::ExternalLinkage);
804
805      WeakRefReferences.erase(Entry);
806    }
807
808    if (Entry->getType()->getElementType() == Ty)
809      return Entry;
810
811    // Make sure the result is of the correct type.
812    const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
813    return llvm::ConstantExpr::getBitCast(Entry, PTy);
814  }
815
816  // This function doesn't have a complete type (for example, the return
817  // type is an incomplete struct). Use a fake type instead, and make
818  // sure not to try to set attributes.
819  bool IsIncompleteFunction = false;
820
821  const llvm::FunctionType *FTy;
822  if (isa<llvm::FunctionType>(Ty)) {
823    FTy = cast<llvm::FunctionType>(Ty);
824  } else {
825    FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
826                                  std::vector<const llvm::Type*>(), false);
827    IsIncompleteFunction = true;
828  }
829
830  llvm::Function *F = llvm::Function::Create(FTy,
831                                             llvm::Function::ExternalLinkage,
832                                             MangledName, &getModule());
833  assert(F->getName() == MangledName && "name was uniqued!");
834  if (D.getDecl())
835    SetFunctionAttributes(D, F, IsIncompleteFunction);
836
837  // This is the first use or definition of a mangled name.  If there is a
838  // deferred decl with this name, remember that we need to emit it at the end
839  // of the file.
840  llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
841  if (DDI != DeferredDecls.end()) {
842    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
843    // list, and remove it from DeferredDecls (since we don't need it anymore).
844    DeferredDeclsToEmit.push_back(DDI->second);
845    DeferredDecls.erase(DDI);
846
847  // Otherwise, there are cases we have to worry about where we're
848  // using a declaration for which we must emit a definition but where
849  // we might not find a top-level definition:
850  //   - member functions defined inline in their classes
851  //   - friend functions defined inline in some class
852  //   - special member functions with implicit definitions
853  // If we ever change our AST traversal to walk into class methods,
854  // this will be unnecessary.
855  } else if (getLangOptions().CPlusPlus && D.getDecl()) {
856    // Look for a declaration that's lexically in a record.
857    const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl());
858    do {
859      if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
860        if (FD->isImplicit()) {
861          assert(FD->isUsed() && "Sema didn't mark implicit function as used!");
862          DeferredDeclsToEmit.push_back(D);
863          break;
864        } else if (FD->isThisDeclarationADefinition()) {
865          DeferredDeclsToEmit.push_back(D);
866          break;
867        }
868      }
869      FD = FD->getPreviousDeclaration();
870    } while (FD);
871  }
872
873  // Make sure the result is of the requested type.
874  if (!IsIncompleteFunction) {
875    assert(F->getType()->getElementType() == Ty);
876    return F;
877  }
878
879  const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
880  return llvm::ConstantExpr::getBitCast(F, PTy);
881}
882
883/// GetAddrOfFunction - Return the address of the given function.  If Ty is
884/// non-null, then this function will use the specified type if it has to
885/// create it (this occurs when we see a definition of the function).
886llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
887                                                 const llvm::Type *Ty) {
888  // If there was no specific requested type, just convert it now.
889  if (!Ty)
890    Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
891
892  llvm::StringRef MangledName = getMangledName(GD);
893  return GetOrCreateLLVMFunction(MangledName, Ty, GD);
894}
895
896/// CreateRuntimeFunction - Create a new runtime function with the specified
897/// type and name.
898llvm::Constant *
899CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
900                                     llvm::StringRef Name) {
901  return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl());
902}
903
904static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) {
905  if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType())
906    return false;
907  if (Context.getLangOptions().CPlusPlus &&
908      Context.getBaseElementType(D->getType())->getAs<RecordType>()) {
909    // FIXME: We should do something fancier here!
910    return false;
911  }
912  return true;
913}
914
915/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
916/// create and return an llvm GlobalVariable with the specified type.  If there
917/// is something in the module with the specified name, return it potentially
918/// bitcasted to the right type.
919///
920/// If D is non-null, it specifies a decl that correspond to this.  This is used
921/// to set the attributes on the global when it is first created.
922llvm::Constant *
923CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
924                                     const llvm::PointerType *Ty,
925                                     const VarDecl *D,
926                                     bool UnnamedAddr) {
927  // Lookup the entry, lazily creating it if necessary.
928  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
929  if (Entry) {
930    if (WeakRefReferences.count(Entry)) {
931      if (D && !D->hasAttr<WeakAttr>())
932        Entry->setLinkage(llvm::Function::ExternalLinkage);
933
934      WeakRefReferences.erase(Entry);
935    }
936
937    if (UnnamedAddr)
938      Entry->setUnnamedAddr(true);
939
940    if (Entry->getType() == Ty)
941      return Entry;
942
943    // Make sure the result is of the correct type.
944    return llvm::ConstantExpr::getBitCast(Entry, Ty);
945  }
946
947  // This is the first use or definition of a mangled name.  If there is a
948  // deferred decl with this name, remember that we need to emit it at the end
949  // of the file.
950  llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
951  if (DDI != DeferredDecls.end()) {
952    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
953    // list, and remove it from DeferredDecls (since we don't need it anymore).
954    DeferredDeclsToEmit.push_back(DDI->second);
955    DeferredDecls.erase(DDI);
956  }
957
958  llvm::GlobalVariable *GV =
959    new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
960                             llvm::GlobalValue::ExternalLinkage,
961                             0, MangledName, 0,
962                             false, Ty->getAddressSpace());
963
964  // Handle things which are present even on external declarations.
965  if (D) {
966    // FIXME: This code is overly simple and should be merged with other global
967    // handling.
968    GV->setConstant(DeclIsConstantGlobal(Context, D));
969
970    // Set linkage and visibility in case we never see a definition.
971    NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility();
972    if (LV.linkage() != ExternalLinkage) {
973      GV->setLinkage(llvm::GlobalValue::InternalLinkage);
974    } else {
975      if (D->hasAttr<DLLImportAttr>())
976        GV->setLinkage(llvm::GlobalValue::DLLImportLinkage);
977      else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>())
978        GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
979
980      // Set visibility on a declaration only if it's explicit.
981      if (LV.visibilityExplicit())
982        GV->setVisibility(GetLLVMVisibility(LV.visibility()));
983    }
984
985    GV->setThreadLocal(D->isThreadSpecified());
986  }
987
988  return GV;
989}
990
991
992/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
993/// given global variable.  If Ty is non-null and if the global doesn't exist,
994/// then it will be greated with the specified type instead of whatever the
995/// normal requested type would be.
996llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
997                                                  const llvm::Type *Ty) {
998  assert(D->hasGlobalStorage() && "Not a global variable");
999  QualType ASTTy = D->getType();
1000  if (Ty == 0)
1001    Ty = getTypes().ConvertTypeForMem(ASTTy);
1002
1003  const llvm::PointerType *PTy =
1004    llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
1005
1006  llvm::StringRef MangledName = getMangledName(D);
1007  return GetOrCreateLLVMGlobal(MangledName, PTy, D);
1008}
1009
1010/// CreateRuntimeVariable - Create a new runtime global variable with the
1011/// specified type and name.
1012llvm::Constant *
1013CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
1014                                     llvm::StringRef Name) {
1015  return GetOrCreateLLVMGlobal(Name,  llvm::PointerType::getUnqual(Ty), 0,
1016                               true);
1017}
1018
1019void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
1020  assert(!D->getInit() && "Cannot emit definite definitions here!");
1021
1022  if (MayDeferGeneration(D)) {
1023    // If we have not seen a reference to this variable yet, place it
1024    // into the deferred declarations table to be emitted if needed
1025    // later.
1026    llvm::StringRef MangledName = getMangledName(D);
1027    if (!GetGlobalValue(MangledName)) {
1028      DeferredDecls[MangledName] = D;
1029      return;
1030    }
1031  }
1032
1033  // The tentative definition is the only definition.
1034  EmitGlobalVarDefinition(D);
1035}
1036
1037void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) {
1038  if (DefinitionRequired)
1039    getVTables().GenerateClassData(getVTableLinkage(Class), Class);
1040}
1041
1042llvm::GlobalVariable::LinkageTypes
1043CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1044  if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
1045    return llvm::GlobalVariable::InternalLinkage;
1046
1047  if (const CXXMethodDecl *KeyFunction
1048                                    = RD->getASTContext().getKeyFunction(RD)) {
1049    // If this class has a key function, use that to determine the linkage of
1050    // the vtable.
1051    const FunctionDecl *Def = 0;
1052    if (KeyFunction->hasBody(Def))
1053      KeyFunction = cast<CXXMethodDecl>(Def);
1054
1055    switch (KeyFunction->getTemplateSpecializationKind()) {
1056      case TSK_Undeclared:
1057      case TSK_ExplicitSpecialization:
1058        if (KeyFunction->isInlined())
1059          return llvm::GlobalVariable::WeakODRLinkage;
1060
1061        return llvm::GlobalVariable::ExternalLinkage;
1062
1063      case TSK_ImplicitInstantiation:
1064      case TSK_ExplicitInstantiationDefinition:
1065        return llvm::GlobalVariable::WeakODRLinkage;
1066
1067      case TSK_ExplicitInstantiationDeclaration:
1068        // FIXME: Use available_externally linkage. However, this currently
1069        // breaks LLVM's build due to undefined symbols.
1070        //      return llvm::GlobalVariable::AvailableExternallyLinkage;
1071        return llvm::GlobalVariable::WeakODRLinkage;
1072    }
1073  }
1074
1075  switch (RD->getTemplateSpecializationKind()) {
1076  case TSK_Undeclared:
1077  case TSK_ExplicitSpecialization:
1078  case TSK_ImplicitInstantiation:
1079  case TSK_ExplicitInstantiationDefinition:
1080    return llvm::GlobalVariable::WeakODRLinkage;
1081
1082  case TSK_ExplicitInstantiationDeclaration:
1083    // FIXME: Use available_externally linkage. However, this currently
1084    // breaks LLVM's build due to undefined symbols.
1085    //   return llvm::GlobalVariable::AvailableExternallyLinkage;
1086    return llvm::GlobalVariable::WeakODRLinkage;
1087  }
1088
1089  // Silence GCC warning.
1090  return llvm::GlobalVariable::WeakODRLinkage;
1091}
1092
1093CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const {
1094    return Context.toCharUnitsFromBits(
1095      TheTargetData.getTypeStoreSizeInBits(Ty));
1096}
1097
1098void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
1099  llvm::Constant *Init = 0;
1100  QualType ASTTy = D->getType();
1101  bool NonConstInit = false;
1102
1103  const Expr *InitExpr = D->getAnyInitializer();
1104
1105  if (!InitExpr) {
1106    // This is a tentative definition; tentative definitions are
1107    // implicitly initialized with { 0 }.
1108    //
1109    // Note that tentative definitions are only emitted at the end of
1110    // a translation unit, so they should never have incomplete
1111    // type. In addition, EmitTentativeDefinition makes sure that we
1112    // never attempt to emit a tentative definition if a real one
1113    // exists. A use may still exists, however, so we still may need
1114    // to do a RAUW.
1115    assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
1116    Init = EmitNullConstant(D->getType());
1117  } else {
1118    Init = EmitConstantExpr(InitExpr, D->getType());
1119    if (!Init) {
1120      QualType T = InitExpr->getType();
1121      if (D->getType()->isReferenceType())
1122        T = D->getType();
1123
1124      if (getLangOptions().CPlusPlus) {
1125        Init = EmitNullConstant(T);
1126        NonConstInit = true;
1127      } else {
1128        ErrorUnsupported(D, "static initializer");
1129        Init = llvm::UndefValue::get(getTypes().ConvertType(T));
1130      }
1131    } else {
1132      // We don't need an initializer, so remove the entry for the delayed
1133      // initializer position (just in case this entry was delayed).
1134      if (getLangOptions().CPlusPlus)
1135        DelayedCXXInitPosition.erase(D);
1136    }
1137  }
1138
1139  const llvm::Type* InitType = Init->getType();
1140  llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
1141
1142  // Strip off a bitcast if we got one back.
1143  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
1144    assert(CE->getOpcode() == llvm::Instruction::BitCast ||
1145           // all zero index gep.
1146           CE->getOpcode() == llvm::Instruction::GetElementPtr);
1147    Entry = CE->getOperand(0);
1148  }
1149
1150  // Entry is now either a Function or GlobalVariable.
1151  llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry);
1152
1153  // We have a definition after a declaration with the wrong type.
1154  // We must make a new GlobalVariable* and update everything that used OldGV
1155  // (a declaration or tentative definition) with the new GlobalVariable*
1156  // (which will be a definition).
1157  //
1158  // This happens if there is a prototype for a global (e.g.
1159  // "extern int x[];") and then a definition of a different type (e.g.
1160  // "int x[10];"). This also happens when an initializer has a different type
1161  // from the type of the global (this happens with unions).
1162  if (GV == 0 ||
1163      GV->getType()->getElementType() != InitType ||
1164      GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) {
1165
1166    // Move the old entry aside so that we'll create a new one.
1167    Entry->setName(llvm::StringRef());
1168
1169    // Make a new global with the correct type, this is now guaranteed to work.
1170    GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
1171
1172    // Replace all uses of the old global with the new global
1173    llvm::Constant *NewPtrForOldDecl =
1174        llvm::ConstantExpr::getBitCast(GV, Entry->getType());
1175    Entry->replaceAllUsesWith(NewPtrForOldDecl);
1176
1177    // Erase the old global, since it is no longer used.
1178    cast<llvm::GlobalValue>(Entry)->eraseFromParent();
1179  }
1180
1181  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
1182    SourceManager &SM = Context.getSourceManager();
1183    AddAnnotation(EmitAnnotateAttr(GV, AA,
1184                              SM.getInstantiationLineNumber(D->getLocation())));
1185  }
1186
1187  GV->setInitializer(Init);
1188
1189  // If it is safe to mark the global 'constant', do so now.
1190  GV->setConstant(false);
1191  if (!NonConstInit && DeclIsConstantGlobal(Context, D))
1192    GV->setConstant(true);
1193
1194  GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
1195
1196  // Set the llvm linkage type as appropriate.
1197  llvm::GlobalValue::LinkageTypes Linkage =
1198    GetLLVMLinkageVarDefinition(D, GV);
1199  GV->setLinkage(Linkage);
1200  if (Linkage == llvm::GlobalVariable::CommonLinkage)
1201    // common vars aren't constant even if declared const.
1202    GV->setConstant(false);
1203
1204  SetCommonAttributes(D, GV);
1205
1206  // Emit the initializer function if necessary.
1207  if (NonConstInit)
1208    EmitCXXGlobalVarDeclInitFunc(D, GV);
1209
1210  // Emit global variable debug information.
1211  if (CGDebugInfo *DI = getDebugInfo()) {
1212    DI->setLocation(D->getLocation());
1213    DI->EmitGlobalVariable(GV, D);
1214  }
1215}
1216
1217llvm::GlobalValue::LinkageTypes
1218CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D,
1219                                           llvm::GlobalVariable *GV) {
1220  GVALinkage Linkage = getContext().GetGVALinkageForVariable(D);
1221  if (Linkage == GVA_Internal)
1222    return llvm::Function::InternalLinkage;
1223  else if (D->hasAttr<DLLImportAttr>())
1224    return llvm::Function::DLLImportLinkage;
1225  else if (D->hasAttr<DLLExportAttr>())
1226    return llvm::Function::DLLExportLinkage;
1227  else if (D->hasAttr<WeakAttr>()) {
1228    if (GV->isConstant())
1229      return llvm::GlobalVariable::WeakODRLinkage;
1230    else
1231      return llvm::GlobalVariable::WeakAnyLinkage;
1232  } else if (Linkage == GVA_TemplateInstantiation ||
1233             Linkage == GVA_ExplicitTemplateInstantiation)
1234    // FIXME: It seems like we can provide more specific linkage here
1235    // (LinkOnceODR, WeakODR).
1236    return llvm::GlobalVariable::WeakAnyLinkage;
1237  else if (!getLangOptions().CPlusPlus &&
1238           ((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) ||
1239             D->getAttr<CommonAttr>()) &&
1240           !D->hasExternalStorage() && !D->getInit() &&
1241           !D->getAttr<SectionAttr>() && !D->isThreadSpecified()) {
1242    // Thread local vars aren't considered common linkage.
1243    return llvm::GlobalVariable::CommonLinkage;
1244  }
1245  return llvm::GlobalVariable::ExternalLinkage;
1246}
1247
1248/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
1249/// implement a function with no prototype, e.g. "int foo() {}".  If there are
1250/// existing call uses of the old function in the module, this adjusts them to
1251/// call the new function directly.
1252///
1253/// This is not just a cleanup: the always_inline pass requires direct calls to
1254/// functions to be able to inline them.  If there is a bitcast in the way, it
1255/// won't inline them.  Instcombine normally deletes these calls, but it isn't
1256/// run at -O0.
1257static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
1258                                                      llvm::Function *NewFn) {
1259  // If we're redefining a global as a function, don't transform it.
1260  llvm::Function *OldFn = dyn_cast<llvm::Function>(Old);
1261  if (OldFn == 0) return;
1262
1263  const llvm::Type *NewRetTy = NewFn->getReturnType();
1264  llvm::SmallVector<llvm::Value*, 4> ArgList;
1265
1266  for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end();
1267       UI != E; ) {
1268    // TODO: Do invokes ever occur in C code?  If so, we should handle them too.
1269    llvm::Value::use_iterator I = UI++; // Increment before the CI is erased.
1270    llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I);
1271    if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I)
1272    llvm::CallSite CS(CI);
1273    if (!CI || !CS.isCallee(I)) continue;
1274
1275    // If the return types don't match exactly, and if the call isn't dead, then
1276    // we can't transform this call.
1277    if (CI->getType() != NewRetTy && !CI->use_empty())
1278      continue;
1279
1280    // If the function was passed too few arguments, don't transform.  If extra
1281    // arguments were passed, we silently drop them.  If any of the types
1282    // mismatch, we don't transform.
1283    unsigned ArgNo = 0;
1284    bool DontTransform = false;
1285    for (llvm::Function::arg_iterator AI = NewFn->arg_begin(),
1286         E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) {
1287      if (CS.arg_size() == ArgNo ||
1288          CS.getArgument(ArgNo)->getType() != AI->getType()) {
1289        DontTransform = true;
1290        break;
1291      }
1292    }
1293    if (DontTransform)
1294      continue;
1295
1296    // Okay, we can transform this.  Create the new call instruction and copy
1297    // over the required information.
1298    ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo);
1299    llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(),
1300                                                     ArgList.end(), "", CI);
1301    ArgList.clear();
1302    if (!NewCall->getType()->isVoidTy())
1303      NewCall->takeName(CI);
1304    NewCall->setAttributes(CI->getAttributes());
1305    NewCall->setCallingConv(CI->getCallingConv());
1306
1307    // Finally, remove the old call, replacing any uses with the new one.
1308    if (!CI->use_empty())
1309      CI->replaceAllUsesWith(NewCall);
1310
1311    // Copy debug location attached to CI.
1312    if (!CI->getDebugLoc().isUnknown())
1313      NewCall->setDebugLoc(CI->getDebugLoc());
1314    CI->eraseFromParent();
1315  }
1316}
1317
1318
1319void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) {
1320  const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
1321  const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD);
1322  // Get or create the prototype for the function.
1323  llvm::Constant *Entry = GetAddrOfFunction(GD, Ty);
1324
1325  // Strip off a bitcast if we got one back.
1326  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
1327    assert(CE->getOpcode() == llvm::Instruction::BitCast);
1328    Entry = CE->getOperand(0);
1329  }
1330
1331
1332  if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) {
1333    llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry);
1334
1335    // If the types mismatch then we have to rewrite the definition.
1336    assert(OldFn->isDeclaration() &&
1337           "Shouldn't replace non-declaration");
1338
1339    // F is the Function* for the one with the wrong type, we must make a new
1340    // Function* and update everything that used F (a declaration) with the new
1341    // Function* (which will be a definition).
1342    //
1343    // This happens if there is a prototype for a function
1344    // (e.g. "int f()") and then a definition of a different type
1345    // (e.g. "int f(int x)").  Move the old function aside so that it
1346    // doesn't interfere with GetAddrOfFunction.
1347    OldFn->setName(llvm::StringRef());
1348    llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
1349
1350    // If this is an implementation of a function without a prototype, try to
1351    // replace any existing uses of the function (which may be calls) with uses
1352    // of the new function
1353    if (D->getType()->isFunctionNoProtoType()) {
1354      ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn);
1355      OldFn->removeDeadConstantUsers();
1356    }
1357
1358    // Replace uses of F with the Function we will endow with a body.
1359    if (!Entry->use_empty()) {
1360      llvm::Constant *NewPtrForOldDecl =
1361        llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
1362      Entry->replaceAllUsesWith(NewPtrForOldDecl);
1363    }
1364
1365    // Ok, delete the old function now, which is dead.
1366    OldFn->eraseFromParent();
1367
1368    Entry = NewFn;
1369  }
1370
1371  // We need to set linkage and visibility on the function before
1372  // generating code for it because various parts of IR generation
1373  // want to propagate this information down (e.g. to local static
1374  // declarations).
1375  llvm::Function *Fn = cast<llvm::Function>(Entry);
1376  setFunctionLinkage(D, Fn);
1377
1378  // FIXME: this is redundant with part of SetFunctionDefinitionAttributes
1379  setGlobalVisibility(Fn, D, /*ForDef*/ true);
1380
1381  CodeGenFunction(*this).GenerateCode(D, Fn);
1382
1383  SetFunctionDefinitionAttributes(D, Fn);
1384  SetLLVMFunctionAttributesForDefinition(D, Fn);
1385
1386  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
1387    AddGlobalCtor(Fn, CA->getPriority());
1388  if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
1389    AddGlobalDtor(Fn, DA->getPriority());
1390}
1391
1392void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
1393  const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
1394  const AliasAttr *AA = D->getAttr<AliasAttr>();
1395  assert(AA && "Not an alias?");
1396
1397  llvm::StringRef MangledName = getMangledName(GD);
1398
1399  // If there is a definition in the module, then it wins over the alias.
1400  // This is dubious, but allow it to be safe.  Just ignore the alias.
1401  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1402  if (Entry && !Entry->isDeclaration())
1403    return;
1404
1405  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
1406
1407  // Create a reference to the named value.  This ensures that it is emitted
1408  // if a deferred decl.
1409  llvm::Constant *Aliasee;
1410  if (isa<llvm::FunctionType>(DeclTy))
1411    Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl());
1412  else
1413    Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
1414                                    llvm::PointerType::getUnqual(DeclTy), 0);
1415
1416  // Create the new alias itself, but don't set a name yet.
1417  llvm::GlobalValue *GA =
1418    new llvm::GlobalAlias(Aliasee->getType(),
1419                          llvm::Function::ExternalLinkage,
1420                          "", Aliasee, &getModule());
1421
1422  if (Entry) {
1423    assert(Entry->isDeclaration());
1424
1425    // If there is a declaration in the module, then we had an extern followed
1426    // by the alias, as in:
1427    //   extern int test6();
1428    //   ...
1429    //   int test6() __attribute__((alias("test7")));
1430    //
1431    // Remove it and replace uses of it with the alias.
1432    GA->takeName(Entry);
1433
1434    Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
1435                                                          Entry->getType()));
1436    Entry->eraseFromParent();
1437  } else {
1438    GA->setName(MangledName);
1439  }
1440
1441  // Set attributes which are particular to an alias; this is a
1442  // specialization of the attributes which may be set on a global
1443  // variable/function.
1444  if (D->hasAttr<DLLExportAttr>()) {
1445    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1446      // The dllexport attribute is ignored for undefined symbols.
1447      if (FD->hasBody())
1448        GA->setLinkage(llvm::Function::DLLExportLinkage);
1449    } else {
1450      GA->setLinkage(llvm::Function::DLLExportLinkage);
1451    }
1452  } else if (D->hasAttr<WeakAttr>() ||
1453             D->hasAttr<WeakRefAttr>() ||
1454             D->hasAttr<WeakImportAttr>()) {
1455    GA->setLinkage(llvm::Function::WeakAnyLinkage);
1456  }
1457
1458  SetCommonAttributes(D, GA);
1459}
1460
1461/// getBuiltinLibFunction - Given a builtin id for a function like
1462/// "__builtin_fabsf", return a Function* for "fabsf".
1463llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
1464                                                  unsigned BuiltinID) {
1465  assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
1466          Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
1467         "isn't a lib fn");
1468
1469  // Get the name, skip over the __builtin_ prefix (if necessary).
1470  const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
1471  if (Context.BuiltinInfo.isLibFunction(BuiltinID))
1472    Name += 10;
1473
1474  const llvm::FunctionType *Ty =
1475    cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
1476
1477  return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD));
1478}
1479
1480llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
1481                                            unsigned NumTys) {
1482  return llvm::Intrinsic::getDeclaration(&getModule(),
1483                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
1484}
1485
1486static llvm::StringMapEntry<llvm::Constant*> &
1487GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
1488                         const StringLiteral *Literal,
1489                         bool TargetIsLSB,
1490                         bool &IsUTF16,
1491                         unsigned &StringLength) {
1492  llvm::StringRef String = Literal->getString();
1493  unsigned NumBytes = String.size();
1494
1495  // Check for simple case.
1496  if (!Literal->containsNonAsciiOrNull()) {
1497    StringLength = NumBytes;
1498    return Map.GetOrCreateValue(String);
1499  }
1500
1501  // Otherwise, convert the UTF8 literals into a byte string.
1502  llvm::SmallVector<UTF16, 128> ToBuf(NumBytes);
1503  const UTF8 *FromPtr = (UTF8 *)String.data();
1504  UTF16 *ToPtr = &ToBuf[0];
1505
1506  (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1507                           &ToPtr, ToPtr + NumBytes,
1508                           strictConversion);
1509
1510  // ConvertUTF8toUTF16 returns the length in ToPtr.
1511  StringLength = ToPtr - &ToBuf[0];
1512
1513  // Render the UTF-16 string into a byte array and convert to the target byte
1514  // order.
1515  //
1516  // FIXME: This isn't something we should need to do here.
1517  llvm::SmallString<128> AsBytes;
1518  AsBytes.reserve(StringLength * 2);
1519  for (unsigned i = 0; i != StringLength; ++i) {
1520    unsigned short Val = ToBuf[i];
1521    if (TargetIsLSB) {
1522      AsBytes.push_back(Val & 0xFF);
1523      AsBytes.push_back(Val >> 8);
1524    } else {
1525      AsBytes.push_back(Val >> 8);
1526      AsBytes.push_back(Val & 0xFF);
1527    }
1528  }
1529  // Append one extra null character, the second is automatically added by our
1530  // caller.
1531  AsBytes.push_back(0);
1532
1533  IsUTF16 = true;
1534  return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size()));
1535}
1536
1537llvm::Constant *
1538CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
1539  unsigned StringLength = 0;
1540  bool isUTF16 = false;
1541  llvm::StringMapEntry<llvm::Constant*> &Entry =
1542    GetConstantCFStringEntry(CFConstantStringMap, Literal,
1543                             getTargetData().isLittleEndian(),
1544                             isUTF16, StringLength);
1545
1546  if (llvm::Constant *C = Entry.getValue())
1547    return C;
1548
1549  llvm::Constant *Zero =
1550      llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
1551  llvm::Constant *Zeros[] = { Zero, Zero };
1552
1553  // If we don't already have it, get __CFConstantStringClassReference.
1554  if (!CFConstantStringClassRef) {
1555    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
1556    Ty = llvm::ArrayType::get(Ty, 0);
1557    llvm::Constant *GV = CreateRuntimeVariable(Ty,
1558                                           "__CFConstantStringClassReference");
1559    // Decay array -> ptr
1560    CFConstantStringClassRef =
1561      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1562  }
1563
1564  QualType CFTy = getContext().getCFConstantStringType();
1565
1566  const llvm::StructType *STy =
1567    cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1568
1569  std::vector<llvm::Constant*> Fields(4);
1570
1571  // Class pointer.
1572  Fields[0] = CFConstantStringClassRef;
1573
1574  // Flags.
1575  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
1576  Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
1577    llvm::ConstantInt::get(Ty, 0x07C8);
1578
1579  // String pointer.
1580  llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
1581
1582  llvm::GlobalValue::LinkageTypes Linkage;
1583  bool isConstant;
1584  if (isUTF16) {
1585    // FIXME: why do utf strings get "_" labels instead of "L" labels?
1586    Linkage = llvm::GlobalValue::InternalLinkage;
1587    // Note: -fwritable-strings doesn't make unicode CFStrings writable, but
1588    // does make plain ascii ones writable.
1589    isConstant = true;
1590  } else {
1591    Linkage = llvm::GlobalValue::PrivateLinkage;
1592    isConstant = !Features.WritableStrings;
1593  }
1594
1595  llvm::GlobalVariable *GV =
1596    new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
1597                             ".str");
1598  GV->setUnnamedAddr(true);
1599  if (isUTF16) {
1600    CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
1601    GV->setAlignment(Align.getQuantity());
1602  }
1603  Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1604
1605  // String length.
1606  Ty = getTypes().ConvertType(getContext().LongTy);
1607  Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
1608
1609  // The struct.
1610  C = llvm::ConstantStruct::get(STy, Fields);
1611  GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
1612                                llvm::GlobalVariable::PrivateLinkage, C,
1613                                "_unnamed_cfstring_");
1614  if (const char *Sect = getContext().Target.getCFStringSection())
1615    GV->setSection(Sect);
1616  Entry.setValue(GV);
1617
1618  return GV;
1619}
1620
1621llvm::Constant *
1622CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
1623  unsigned StringLength = 0;
1624  bool isUTF16 = false;
1625  llvm::StringMapEntry<llvm::Constant*> &Entry =
1626    GetConstantCFStringEntry(CFConstantStringMap, Literal,
1627                             getTargetData().isLittleEndian(),
1628                             isUTF16, StringLength);
1629
1630  if (llvm::Constant *C = Entry.getValue())
1631    return C;
1632
1633  llvm::Constant *Zero =
1634  llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
1635  llvm::Constant *Zeros[] = { Zero, Zero };
1636
1637  // If we don't already have it, get _NSConstantStringClassReference.
1638  if (!ConstantStringClassRef) {
1639    std::string StringClass(getLangOptions().ObjCConstantStringClass);
1640    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
1641    Ty = llvm::ArrayType::get(Ty, 0);
1642    llvm::Constant *GV;
1643    if (StringClass.empty())
1644      GV = CreateRuntimeVariable(Ty,
1645                                 Features.ObjCNonFragileABI ?
1646                                 "OBJC_CLASS_$_NSConstantString" :
1647                                 "_NSConstantStringClassReference");
1648    else {
1649      std::string str;
1650      if (Features.ObjCNonFragileABI)
1651        str = "OBJC_CLASS_$_" + StringClass;
1652      else
1653        str = "_" + StringClass + "ClassReference";
1654      GV = CreateRuntimeVariable(Ty, str);
1655    }
1656    // Decay array -> ptr
1657    ConstantStringClassRef =
1658    llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1659  }
1660
1661  QualType NSTy = getContext().getNSConstantStringType();
1662
1663  const llvm::StructType *STy =
1664  cast<llvm::StructType>(getTypes().ConvertType(NSTy));
1665
1666  std::vector<llvm::Constant*> Fields(3);
1667
1668  // Class pointer.
1669  Fields[0] = ConstantStringClassRef;
1670
1671  // String pointer.
1672  llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
1673
1674  llvm::GlobalValue::LinkageTypes Linkage;
1675  bool isConstant;
1676  if (isUTF16) {
1677    // FIXME: why do utf strings get "_" labels instead of "L" labels?
1678    Linkage = llvm::GlobalValue::InternalLinkage;
1679    // Note: -fwritable-strings doesn't make unicode NSStrings writable, but
1680    // does make plain ascii ones writable.
1681    isConstant = true;
1682  } else {
1683    Linkage = llvm::GlobalValue::PrivateLinkage;
1684    isConstant = !Features.WritableStrings;
1685  }
1686
1687  llvm::GlobalVariable *GV =
1688  new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
1689                           ".str");
1690  GV->setUnnamedAddr(true);
1691  if (isUTF16) {
1692    CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
1693    GV->setAlignment(Align.getQuantity());
1694  }
1695  Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
1696
1697  // String length.
1698  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
1699  Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
1700
1701  // The struct.
1702  C = llvm::ConstantStruct::get(STy, Fields);
1703  GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
1704                                llvm::GlobalVariable::PrivateLinkage, C,
1705                                "_unnamed_nsstring_");
1706  // FIXME. Fix section.
1707  if (const char *Sect =
1708        Features.ObjCNonFragileABI
1709          ? getContext().Target.getNSStringNonFragileABISection()
1710          : getContext().Target.getNSStringSection())
1711    GV->setSection(Sect);
1712  Entry.setValue(GV);
1713
1714  return GV;
1715}
1716
1717/// GetStringForStringLiteral - Return the appropriate bytes for a
1718/// string literal, properly padded to match the literal type.
1719std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
1720  const ConstantArrayType *CAT =
1721    getContext().getAsConstantArrayType(E->getType());
1722  assert(CAT && "String isn't pointer or array!");
1723
1724  // Resize the string to the right size.
1725  uint64_t RealLen = CAT->getSize().getZExtValue();
1726
1727  if (E->isWide())
1728    RealLen *= getContext().Target.getWCharWidth()/8;
1729
1730  std::string Str = E->getString().str();
1731  Str.resize(RealLen, '\0');
1732
1733  return Str;
1734}
1735
1736/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
1737/// constant array for the given string literal.
1738llvm::Constant *
1739CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
1740  // FIXME: This can be more efficient.
1741  // FIXME: We shouldn't need to bitcast the constant in the wide string case.
1742  llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S));
1743  if (S->isWide()) {
1744    llvm::Type *DestTy =
1745        llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType()));
1746    C = llvm::ConstantExpr::getBitCast(C, DestTy);
1747  }
1748  return C;
1749}
1750
1751/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1752/// array for the given ObjCEncodeExpr node.
1753llvm::Constant *
1754CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1755  std::string Str;
1756  getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1757
1758  return GetAddrOfConstantCString(Str);
1759}
1760
1761
1762/// GenerateWritableString -- Creates storage for a string literal.
1763static llvm::Constant *GenerateStringLiteral(const std::string &str,
1764                                             bool constant,
1765                                             CodeGenModule &CGM,
1766                                             const char *GlobalName) {
1767  // Create Constant for this string literal. Don't add a '\0'.
1768  llvm::Constant *C =
1769      llvm::ConstantArray::get(CGM.getLLVMContext(), str, false);
1770
1771  // Create a global variable for this string
1772  llvm::GlobalVariable *GV =
1773    new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
1774                             llvm::GlobalValue::PrivateLinkage,
1775                             C, GlobalName);
1776  GV->setUnnamedAddr(true);
1777  return GV;
1778}
1779
1780/// GetAddrOfConstantString - Returns a pointer to a character array
1781/// containing the literal. This contents are exactly that of the
1782/// given string, i.e. it will not be null terminated automatically;
1783/// see GetAddrOfConstantCString. Note that whether the result is
1784/// actually a pointer to an LLVM constant depends on
1785/// Feature.WriteableStrings.
1786///
1787/// The result has pointer to array type.
1788llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
1789                                                       const char *GlobalName) {
1790  bool IsConstant = !Features.WritableStrings;
1791
1792  // Get the default prefix if a name wasn't specified.
1793  if (!GlobalName)
1794    GlobalName = ".str";
1795
1796  // Don't share any string literals if strings aren't constant.
1797  if (!IsConstant)
1798    return GenerateStringLiteral(str, false, *this, GlobalName);
1799
1800  llvm::StringMapEntry<llvm::Constant *> &Entry =
1801    ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
1802
1803  if (Entry.getValue())
1804    return Entry.getValue();
1805
1806  // Create a global variable for this.
1807  llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
1808  Entry.setValue(C);
1809  return C;
1810}
1811
1812/// GetAddrOfConstantCString - Returns a pointer to a character
1813/// array containing the literal and a terminating '\-'
1814/// character. The result has pointer to array type.
1815llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
1816                                                        const char *GlobalName){
1817  return GetAddrOfConstantString(str + '\0', GlobalName);
1818}
1819
1820/// EmitObjCPropertyImplementations - Emit information for synthesized
1821/// properties for an implementation.
1822void CodeGenModule::EmitObjCPropertyImplementations(const
1823                                                    ObjCImplementationDecl *D) {
1824  for (ObjCImplementationDecl::propimpl_iterator
1825         i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1826    ObjCPropertyImplDecl *PID = *i;
1827
1828    // Dynamic is just for type-checking.
1829    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1830      ObjCPropertyDecl *PD = PID->getPropertyDecl();
1831
1832      // Determine which methods need to be implemented, some may have
1833      // been overridden. Note that ::isSynthesized is not the method
1834      // we want, that just indicates if the decl came from a
1835      // property. What we want to know is if the method is defined in
1836      // this implementation.
1837      if (!D->getInstanceMethod(PD->getGetterName()))
1838        CodeGenFunction(*this).GenerateObjCGetter(
1839                                 const_cast<ObjCImplementationDecl *>(D), PID);
1840      if (!PD->isReadOnly() &&
1841          !D->getInstanceMethod(PD->getSetterName()))
1842        CodeGenFunction(*this).GenerateObjCSetter(
1843                                 const_cast<ObjCImplementationDecl *>(D), PID);
1844    }
1845  }
1846}
1847
1848/// EmitObjCIvarInitializations - Emit information for ivar initialization
1849/// for an implementation.
1850void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
1851  if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0)
1852    return;
1853  DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D));
1854  assert(DC && "EmitObjCIvarInitializations - null DeclContext");
1855  IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
1856  Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
1857  ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(),
1858                                                  D->getLocation(),
1859                                                  D->getLocation(), cxxSelector,
1860                                                  getContext().VoidTy, 0,
1861                                                  DC, true, false, true, false,
1862                                                  ObjCMethodDecl::Required);
1863  D->addInstanceMethod(DTORMethod);
1864  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
1865
1866  II = &getContext().Idents.get(".cxx_construct");
1867  cxxSelector = getContext().Selectors.getSelector(0, &II);
1868  // The constructor returns 'self'.
1869  ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
1870                                                D->getLocation(),
1871                                                D->getLocation(), cxxSelector,
1872                                                getContext().getObjCIdType(), 0,
1873                                                DC, true, false, true, false,
1874                                                ObjCMethodDecl::Required);
1875  D->addInstanceMethod(CTORMethod);
1876  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
1877
1878
1879}
1880
1881/// EmitNamespace - Emit all declarations in a namespace.
1882void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
1883  for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
1884       I != E; ++I)
1885    EmitTopLevelDecl(*I);
1886}
1887
1888// EmitLinkageSpec - Emit all declarations in a linkage spec.
1889void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
1890  if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
1891      LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
1892    ErrorUnsupported(LSD, "linkage spec");
1893    return;
1894  }
1895
1896  for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
1897       I != E; ++I)
1898    EmitTopLevelDecl(*I);
1899}
1900
1901/// EmitTopLevelDecl - Emit code for a single top level declaration.
1902void CodeGenModule::EmitTopLevelDecl(Decl *D) {
1903  // If an error has occurred, stop code generation, but continue
1904  // parsing and semantic analysis (to ensure all warnings and errors
1905  // are emitted).
1906  if (Diags.hasErrorOccurred())
1907    return;
1908
1909  // Ignore dependent declarations.
1910  if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
1911    return;
1912
1913  switch (D->getKind()) {
1914  case Decl::CXXConversion:
1915  case Decl::CXXMethod:
1916  case Decl::Function:
1917    // Skip function templates
1918    if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
1919      return;
1920
1921    EmitGlobal(cast<FunctionDecl>(D));
1922    break;
1923
1924  case Decl::Var:
1925    EmitGlobal(cast<VarDecl>(D));
1926    break;
1927
1928  // C++ Decls
1929  case Decl::Namespace:
1930    EmitNamespace(cast<NamespaceDecl>(D));
1931    break;
1932    // No code generation needed.
1933  case Decl::UsingShadow:
1934  case Decl::Using:
1935  case Decl::UsingDirective:
1936  case Decl::ClassTemplate:
1937  case Decl::FunctionTemplate:
1938  case Decl::NamespaceAlias:
1939    break;
1940  case Decl::CXXConstructor:
1941    // Skip function templates
1942    if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
1943      return;
1944
1945    EmitCXXConstructors(cast<CXXConstructorDecl>(D));
1946    break;
1947  case Decl::CXXDestructor:
1948    EmitCXXDestructors(cast<CXXDestructorDecl>(D));
1949    break;
1950
1951  case Decl::StaticAssert:
1952    // Nothing to do.
1953    break;
1954
1955  // Objective-C Decls
1956
1957  // Forward declarations, no (immediate) code generation.
1958  case Decl::ObjCClass:
1959  case Decl::ObjCForwardProtocol:
1960  case Decl::ObjCInterface:
1961    break;
1962
1963    case Decl::ObjCCategory: {
1964      ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
1965      if (CD->IsClassExtension() && CD->hasSynthBitfield())
1966        Context.ResetObjCLayout(CD->getClassInterface());
1967      break;
1968    }
1969
1970
1971  case Decl::ObjCProtocol:
1972    Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
1973    break;
1974
1975  case Decl::ObjCCategoryImpl:
1976    // Categories have properties but don't support synthesize so we
1977    // can ignore them here.
1978    Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
1979    break;
1980
1981  case Decl::ObjCImplementation: {
1982    ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1983    if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield())
1984      Context.ResetObjCLayout(OMD->getClassInterface());
1985    EmitObjCPropertyImplementations(OMD);
1986    EmitObjCIvarInitializations(OMD);
1987    Runtime->GenerateClass(OMD);
1988    break;
1989  }
1990  case Decl::ObjCMethod: {
1991    ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
1992    // If this is not a prototype, emit the body.
1993    if (OMD->getBody())
1994      CodeGenFunction(*this).GenerateObjCMethod(OMD);
1995    break;
1996  }
1997  case Decl::ObjCCompatibleAlias:
1998    // compatibility-alias is a directive and has no code gen.
1999    break;
2000
2001  case Decl::LinkageSpec:
2002    EmitLinkageSpec(cast<LinkageSpecDecl>(D));
2003    break;
2004
2005  case Decl::FileScopeAsm: {
2006    FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
2007    llvm::StringRef AsmString = AD->getAsmString()->getString();
2008
2009    const std::string &S = getModule().getModuleInlineAsm();
2010    if (S.empty())
2011      getModule().setModuleInlineAsm(AsmString);
2012    else
2013      getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
2014    break;
2015  }
2016
2017  default:
2018    // Make sure we handled everything we should, every other kind is a
2019    // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
2020    // function. Need to recode Decl::Kind to do that easily.
2021    assert(isa<TypeDecl>(D) && "Unsupported decl kind");
2022  }
2023}
2024
2025/// Turns the given pointer into a constant.
2026static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
2027                                          const void *Ptr) {
2028  uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
2029  const llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
2030  return llvm::ConstantInt::get(i64, PtrInt);
2031}
2032
2033static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
2034                                   llvm::NamedMDNode *&GlobalMetadata,
2035                                   GlobalDecl D,
2036                                   llvm::GlobalValue *Addr) {
2037  if (!GlobalMetadata)
2038    GlobalMetadata =
2039      CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
2040
2041  // TODO: should we report variant information for ctors/dtors?
2042  llvm::Value *Ops[] = {
2043    Addr,
2044    GetPointerConstant(CGM.getLLVMContext(), D.getDecl())
2045  };
2046  GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops, 2));
2047}
2048
2049/// Emits metadata nodes associating all the global values in the
2050/// current module with the Decls they came from.  This is useful for
2051/// projects using IR gen as a subroutine.
2052///
2053/// Since there's currently no way to associate an MDNode directly
2054/// with an llvm::GlobalValue, we create a global named metadata
2055/// with the name 'clang.global.decl.ptrs'.
2056void CodeGenModule::EmitDeclMetadata() {
2057  llvm::NamedMDNode *GlobalMetadata = 0;
2058
2059  // StaticLocalDeclMap
2060  for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator
2061         I = MangledDeclNames.begin(), E = MangledDeclNames.end();
2062       I != E; ++I) {
2063    llvm::GlobalValue *Addr = getModule().getNamedValue(I->second);
2064    EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr);
2065  }
2066}
2067
2068/// Emits metadata nodes for all the local variables in the current
2069/// function.
2070void CodeGenFunction::EmitDeclMetadata() {
2071  if (LocalDeclMap.empty()) return;
2072
2073  llvm::LLVMContext &Context = getLLVMContext();
2074
2075  // Find the unique metadata ID for this name.
2076  unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
2077
2078  llvm::NamedMDNode *GlobalMetadata = 0;
2079
2080  for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator
2081         I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) {
2082    const Decl *D = I->first;
2083    llvm::Value *Addr = I->second;
2084
2085    if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
2086      llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
2087      Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, &DAddr, 1));
2088    } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
2089      GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
2090      EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
2091    }
2092  }
2093}
2094
2095///@name Custom Runtime Function Interfaces
2096///@{
2097//
2098// FIXME: These can be eliminated once we can have clients just get the required
2099// AST nodes from the builtin tables.
2100
2101llvm::Constant *CodeGenModule::getBlockObjectDispose() {
2102  if (BlockObjectDispose)
2103    return BlockObjectDispose;
2104
2105  // If we saw an explicit decl, use that.
2106  if (BlockObjectDisposeDecl) {
2107    return BlockObjectDispose = GetAddrOfFunction(
2108      BlockObjectDisposeDecl,
2109      getTypes().GetFunctionType(BlockObjectDisposeDecl));
2110  }
2111
2112  // Otherwise construct the function by hand.
2113  const llvm::FunctionType *FTy;
2114  std::vector<const llvm::Type*> ArgTys;
2115  const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2116  ArgTys.push_back(PtrToInt8Ty);
2117  ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2118  FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2119  return BlockObjectDispose =
2120    CreateRuntimeFunction(FTy, "_Block_object_dispose");
2121}
2122
2123llvm::Constant *CodeGenModule::getBlockObjectAssign() {
2124  if (BlockObjectAssign)
2125    return BlockObjectAssign;
2126
2127  // If we saw an explicit decl, use that.
2128  if (BlockObjectAssignDecl) {
2129    return BlockObjectAssign = GetAddrOfFunction(
2130      BlockObjectAssignDecl,
2131      getTypes().GetFunctionType(BlockObjectAssignDecl));
2132  }
2133
2134  // Otherwise construct the function by hand.
2135  const llvm::FunctionType *FTy;
2136  std::vector<const llvm::Type*> ArgTys;
2137  const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2138  ArgTys.push_back(PtrToInt8Ty);
2139  ArgTys.push_back(PtrToInt8Ty);
2140  ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2141  FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2142  return BlockObjectAssign =
2143    CreateRuntimeFunction(FTy, "_Block_object_assign");
2144}
2145
2146llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
2147  if (NSConcreteGlobalBlock)
2148    return NSConcreteGlobalBlock;
2149
2150  // If we saw an explicit decl, use that.
2151  if (NSConcreteGlobalBlockDecl) {
2152    return NSConcreteGlobalBlock = GetAddrOfGlobalVar(
2153      NSConcreteGlobalBlockDecl,
2154      getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType()));
2155  }
2156
2157  // Otherwise construct the variable by hand.
2158  return NSConcreteGlobalBlock = CreateRuntimeVariable(
2159    PtrToInt8Ty, "_NSConcreteGlobalBlock");
2160}
2161
2162llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
2163  if (NSConcreteStackBlock)
2164    return NSConcreteStackBlock;
2165
2166  // If we saw an explicit decl, use that.
2167  if (NSConcreteStackBlockDecl) {
2168    return NSConcreteStackBlock = GetAddrOfGlobalVar(
2169      NSConcreteStackBlockDecl,
2170      getTypes().ConvertType(NSConcreteStackBlockDecl->getType()));
2171  }
2172
2173  // Otherwise construct the variable by hand.
2174  return NSConcreteStackBlock = CreateRuntimeVariable(
2175    PtrToInt8Ty, "_NSConcreteStackBlock");
2176}
2177
2178///@}
2179