xref: /llvm-project/clang/lib/CodeGen/CodeGenModule.cpp (revision a45a70cf735cc0639d8ea3063db92645a782c959)
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"
43 using namespace clang;
44 using namespace CodeGen;
45 
46 static 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 
58 CodeGenModule::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 
93 CodeGenModule::~CodeGenModule() {
94   delete Runtime;
95   delete &ABI;
96   delete TBAA;
97   delete DebugInfo;
98 }
99 
100 void 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 
109 void 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 
127 llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
128   if (!TBAA)
129     return 0;
130   return TBAA->getTBAAInfo(QTy);
131 }
132 
133 void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
134                                         llvm::MDNode *TBAAInfo) {
135   Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
136 }
137 
138 bool 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.
144 void 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.
157 void 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 
167 static 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 
178 void 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.
196 void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV,
197                                       const CXXRecordDecl *RD,
198                                       bool IsForRTTI) const {
199   setGlobalVisibility(GV, RD, /*IsForDefinition=*/true);
200 
201   if (!CodeGenOpts.HiddenWeakVTables)
202     return;
203 
204   // We want to drop the visibility to hidden for weak type symbols.
205   // This isn't possible if there might be unresolved references
206   // elsewhere that rely on this symbol being visible.
207 
208   // This should be kept roughly in sync with setThunkVisibility
209   // in CGVTables.cpp.
210 
211   // Preconditions.
212   if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage ||
213       GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
214     return;
215 
216   // Don't override an explicit visibility attribute.
217   if (RD->hasAttr<VisibilityAttr>())
218     return;
219 
220   switch (RD->getTemplateSpecializationKind()) {
221   // We have to disable the optimization if this is an EI definition
222   // because there might be EI declarations in other shared objects.
223   case TSK_ExplicitInstantiationDefinition:
224   case TSK_ExplicitInstantiationDeclaration:
225     return;
226 
227   // Every use of a non-template class's type information has to emit it.
228   case TSK_Undeclared:
229     break;
230 
231   // In theory, implicit instantiations can ignore the possibility of
232   // an explicit instantiation declaration because there necessarily
233   // must be an EI definition somewhere with default visibility.  In
234   // practice, it's possible to have an explicit instantiation for
235   // an arbitrary template class, and linkers aren't necessarily able
236   // to deal with mixed-visibility symbols.
237   case TSK_ExplicitSpecialization:
238   case TSK_ImplicitInstantiation:
239     if (!CodeGenOpts.HiddenWeakTemplateVTables)
240       return;
241     break;
242   }
243 
244   // If there's a key function, there may be translation units
245   // that don't have the key function's definition.  But ignore
246   // this if we're emitting RTTI under -fno-rtti.
247   if (!IsForRTTI || Features.RTTI)
248     if (Context.getKeyFunction(RD))
249       return;
250 
251   // Otherwise, drop the visibility to hidden.
252   GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
253   GV->setUnnamedAddr(true);
254 }
255 
256 llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
257   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
258 
259   llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
260   if (!Str.empty())
261     return Str;
262 
263   if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
264     IdentifierInfo *II = ND->getIdentifier();
265     assert(II && "Attempt to mangle unnamed decl.");
266 
267     Str = II->getName();
268     return Str;
269   }
270 
271   llvm::SmallString<256> Buffer;
272   if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
273     getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Buffer);
274   else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
275     getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer);
276   else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND))
277     getCXXABI().getMangleContext().mangleBlock(BD, Buffer);
278   else
279     getCXXABI().getMangleContext().mangleName(ND, Buffer);
280 
281   // Allocate space for the mangled name.
282   size_t Length = Buffer.size();
283   char *Name = MangledNamesAllocator.Allocate<char>(Length);
284   std::copy(Buffer.begin(), Buffer.end(), Name);
285 
286   Str = llvm::StringRef(Name, Length);
287 
288   return Str;
289 }
290 
291 void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
292                                         const BlockDecl *BD) {
293   MangleContext &MangleCtx = getCXXABI().getMangleContext();
294   const Decl *D = GD.getDecl();
295   if (D == 0)
296     MangleCtx.mangleGlobalBlock(BD, Buffer.getBuffer());
297   else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
298     MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Buffer.getBuffer());
299   else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D))
300     MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Buffer.getBuffer());
301   else
302     MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Buffer.getBuffer());
303 }
304 
305 llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) {
306   return getModule().getNamedValue(Name);
307 }
308 
309 /// AddGlobalCtor - Add a function to the list that will be called before
310 /// main() runs.
311 void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
312   // FIXME: Type coercion of void()* types.
313   GlobalCtors.push_back(std::make_pair(Ctor, Priority));
314 }
315 
316 /// AddGlobalDtor - Add a function to the list that will be called
317 /// when the module is unloaded.
318 void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
319   // FIXME: Type coercion of void()* types.
320   GlobalDtors.push_back(std::make_pair(Dtor, Priority));
321 }
322 
323 void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
324   // Ctor function type is void()*.
325   llvm::FunctionType* CtorFTy =
326     llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false);
327   llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
328 
329   // Get the type of a ctor entry, { i32, void ()* }.
330   llvm::StructType* CtorStructTy =
331     llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext),
332                           llvm::PointerType::getUnqual(CtorFTy), NULL);
333 
334   // Construct the constructor and destructor arrays.
335   std::vector<llvm::Constant*> Ctors;
336   for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
337     std::vector<llvm::Constant*> S;
338     S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
339                 I->second, false));
340     S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
341     Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
342   }
343 
344   if (!Ctors.empty()) {
345     llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
346     new llvm::GlobalVariable(TheModule, AT, false,
347                              llvm::GlobalValue::AppendingLinkage,
348                              llvm::ConstantArray::get(AT, Ctors),
349                              GlobalName);
350   }
351 }
352 
353 void CodeGenModule::EmitAnnotations() {
354   if (Annotations.empty())
355     return;
356 
357   // Create a new global variable for the ConstantStruct in the Module.
358   llvm::Constant *Array =
359   llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
360                                                 Annotations.size()),
361                            Annotations);
362   llvm::GlobalValue *gv =
363   new llvm::GlobalVariable(TheModule, Array->getType(), false,
364                            llvm::GlobalValue::AppendingLinkage, Array,
365                            "llvm.global.annotations");
366   gv->setSection("llvm.metadata");
367 }
368 
369 llvm::GlobalValue::LinkageTypes
370 CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
371   GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
372 
373   if (Linkage == GVA_Internal)
374     return llvm::Function::InternalLinkage;
375 
376   if (D->hasAttr<DLLExportAttr>())
377     return llvm::Function::DLLExportLinkage;
378 
379   if (D->hasAttr<WeakAttr>())
380     return llvm::Function::WeakAnyLinkage;
381 
382   // In C99 mode, 'inline' functions are guaranteed to have a strong
383   // definition somewhere else, so we can use available_externally linkage.
384   if (Linkage == GVA_C99Inline)
385     return llvm::Function::AvailableExternallyLinkage;
386 
387   // In C++, the compiler has to emit a definition in every translation unit
388   // that references the function.  We should use linkonce_odr because
389   // a) if all references in this translation unit are optimized away, we
390   // don't need to codegen it.  b) if the function persists, it needs to be
391   // merged with other definitions. c) C++ has the ODR, so we know the
392   // definition is dependable.
393   if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
394     return llvm::Function::LinkOnceODRLinkage;
395 
396   // An explicit instantiation of a template has weak linkage, since
397   // explicit instantiations can occur in multiple translation units
398   // and must all be equivalent. However, we are not allowed to
399   // throw away these explicit instantiations.
400   if (Linkage == GVA_ExplicitTemplateInstantiation)
401     return llvm::Function::WeakODRLinkage;
402 
403   // Otherwise, we have strong external linkage.
404   assert(Linkage == GVA_StrongExternal);
405   return llvm::Function::ExternalLinkage;
406 }
407 
408 
409 /// SetFunctionDefinitionAttributes - Set attributes for a global.
410 ///
411 /// FIXME: This is currently only done for aliases and functions, but not for
412 /// variables (these details are set in EmitGlobalVarDefinition for variables).
413 void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
414                                                     llvm::GlobalValue *GV) {
415   SetCommonAttributes(D, GV);
416 }
417 
418 void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
419                                               const CGFunctionInfo &Info,
420                                               llvm::Function *F) {
421   unsigned CallingConv;
422   AttributeListType AttributeList;
423   ConstructAttributeList(Info, D, AttributeList, CallingConv);
424   F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
425                                           AttributeList.size()));
426   F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
427 }
428 
429 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
430                                                            llvm::Function *F) {
431   if (!Features.Exceptions && !Features.ObjCNonFragileABI)
432     F->addFnAttr(llvm::Attribute::NoUnwind);
433 
434   if (D->hasAttr<AlwaysInlineAttr>())
435     F->addFnAttr(llvm::Attribute::AlwaysInline);
436 
437   if (D->hasAttr<NakedAttr>())
438     F->addFnAttr(llvm::Attribute::Naked);
439 
440   if (D->hasAttr<NoInlineAttr>())
441     F->addFnAttr(llvm::Attribute::NoInline);
442 
443   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
444     F->setUnnamedAddr(true);
445 
446   if (Features.getStackProtectorMode() == LangOptions::SSPOn)
447     F->addFnAttr(llvm::Attribute::StackProtect);
448   else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
449     F->addFnAttr(llvm::Attribute::StackProtectReq);
450 
451   unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
452   if (alignment)
453     F->setAlignment(alignment);
454 
455   // C++ ABI requires 2-byte alignment for member functions.
456   if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
457     F->setAlignment(2);
458 }
459 
460 void CodeGenModule::SetCommonAttributes(const Decl *D,
461                                         llvm::GlobalValue *GV) {
462   if (isa<NamedDecl>(D))
463     setGlobalVisibility(GV, cast<NamedDecl>(D), /*ForDef*/ true);
464   else
465     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
466 
467   if (D->hasAttr<UsedAttr>())
468     AddUsedGlobal(GV);
469 
470   if (const SectionAttr *SA = D->getAttr<SectionAttr>())
471     GV->setSection(SA->getName());
472 
473   getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this);
474 }
475 
476 void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
477                                                   llvm::Function *F,
478                                                   const CGFunctionInfo &FI) {
479   SetLLVMFunctionAttributes(D, FI, F);
480   SetLLVMFunctionAttributesForDefinition(D, F);
481 
482   F->setLinkage(llvm::Function::InternalLinkage);
483 
484   SetCommonAttributes(D, F);
485 }
486 
487 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
488                                           llvm::Function *F,
489                                           bool IsIncompleteFunction) {
490   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
491 
492   if (!IsIncompleteFunction)
493     SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F);
494 
495   // Only a few attributes are set on declarations; these may later be
496   // overridden by a definition.
497 
498   if (FD->hasAttr<DLLImportAttr>()) {
499     F->setLinkage(llvm::Function::DLLImportLinkage);
500   } else if (FD->hasAttr<WeakAttr>() ||
501              FD->hasAttr<WeakImportAttr>()) {
502     // "extern_weak" is overloaded in LLVM; we probably should have
503     // separate linkage types for this.
504     F->setLinkage(llvm::Function::ExternalWeakLinkage);
505   } else {
506     F->setLinkage(llvm::Function::ExternalLinkage);
507 
508     NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility();
509     if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) {
510       F->setVisibility(GetLLVMVisibility(LV.visibility()));
511     }
512   }
513 
514   if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
515     F->setSection(SA->getName());
516 }
517 
518 void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
519   assert(!GV->isDeclaration() &&
520          "Only globals with definition can force usage.");
521   LLVMUsed.push_back(GV);
522 }
523 
524 void CodeGenModule::EmitLLVMUsed() {
525   // Don't create llvm.used if there is no need.
526   if (LLVMUsed.empty())
527     return;
528 
529   const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
530 
531   // Convert LLVMUsed to what ConstantArray needs.
532   std::vector<llvm::Constant*> UsedArray;
533   UsedArray.resize(LLVMUsed.size());
534   for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
535     UsedArray[i] =
536      llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
537                                       i8PTy);
538   }
539 
540   if (UsedArray.empty())
541     return;
542   llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size());
543 
544   llvm::GlobalVariable *GV =
545     new llvm::GlobalVariable(getModule(), ATy, false,
546                              llvm::GlobalValue::AppendingLinkage,
547                              llvm::ConstantArray::get(ATy, UsedArray),
548                              "llvm.used");
549 
550   GV->setSection("llvm.metadata");
551 }
552 
553 void CodeGenModule::EmitDeferred() {
554   // Emit code for any potentially referenced deferred decls.  Since a
555   // previously unused static decl may become used during the generation of code
556   // for a static function, iterate until no  changes are made.
557 
558   while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) {
559     if (!DeferredVTables.empty()) {
560       const CXXRecordDecl *RD = DeferredVTables.back();
561       DeferredVTables.pop_back();
562       getVTables().GenerateClassData(getVTableLinkage(RD), RD);
563       continue;
564     }
565 
566     GlobalDecl D = DeferredDeclsToEmit.back();
567     DeferredDeclsToEmit.pop_back();
568 
569     // Check to see if we've already emitted this.  This is necessary
570     // for a couple of reasons: first, decls can end up in the
571     // deferred-decls queue multiple times, and second, decls can end
572     // up with definitions in unusual ways (e.g. by an extern inline
573     // function acquiring a strong function redefinition).  Just
574     // ignore these cases.
575     //
576     // TODO: That said, looking this up multiple times is very wasteful.
577     llvm::StringRef Name = getMangledName(D);
578     llvm::GlobalValue *CGRef = GetGlobalValue(Name);
579     assert(CGRef && "Deferred decl wasn't referenced?");
580 
581     if (!CGRef->isDeclaration())
582       continue;
583 
584     // GlobalAlias::isDeclaration() defers to the aliasee, but for our
585     // purposes an alias counts as a definition.
586     if (isa<llvm::GlobalAlias>(CGRef))
587       continue;
588 
589     // Otherwise, emit the definition and move on to the next one.
590     EmitGlobalDefinition(D);
591   }
592 }
593 
594 /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
595 /// annotation information for a given GlobalValue.  The annotation struct is
596 /// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
597 /// GlobalValue being annotated.  The second field is the constant string
598 /// created from the AnnotateAttr's annotation.  The third field is a constant
599 /// string containing the name of the translation unit.  The fourth field is
600 /// the line number in the file of the annotated value declaration.
601 ///
602 /// FIXME: this does not unique the annotation string constants, as llvm-gcc
603 ///        appears to.
604 ///
605 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
606                                                 const AnnotateAttr *AA,
607                                                 unsigned LineNo) {
608   llvm::Module *M = &getModule();
609 
610   // get [N x i8] constants for the annotation string, and the filename string
611   // which are the 2nd and 3rd elements of the global annotation structure.
612   const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext);
613   llvm::Constant *anno = llvm::ConstantArray::get(VMContext,
614                                                   AA->getAnnotation(), true);
615   llvm::Constant *unit = llvm::ConstantArray::get(VMContext,
616                                                   M->getModuleIdentifier(),
617                                                   true);
618 
619   // Get the two global values corresponding to the ConstantArrays we just
620   // created to hold the bytes of the strings.
621   llvm::GlobalValue *annoGV =
622     new llvm::GlobalVariable(*M, anno->getType(), false,
623                              llvm::GlobalValue::PrivateLinkage, anno,
624                              GV->getName());
625   // translation unit name string, emitted into the llvm.metadata section.
626   llvm::GlobalValue *unitGV =
627     new llvm::GlobalVariable(*M, unit->getType(), false,
628                              llvm::GlobalValue::PrivateLinkage, unit,
629                              ".str");
630   unitGV->setUnnamedAddr(true);
631 
632   // Create the ConstantStruct for the global annotation.
633   llvm::Constant *Fields[4] = {
634     llvm::ConstantExpr::getBitCast(GV, SBP),
635     llvm::ConstantExpr::getBitCast(annoGV, SBP),
636     llvm::ConstantExpr::getBitCast(unitGV, SBP),
637     llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo)
638   };
639   return llvm::ConstantStruct::get(VMContext, Fields, 4, false);
640 }
641 
642 bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
643   // Never defer when EmitAllDecls is specified.
644   if (Features.EmitAllDecls)
645     return false;
646 
647   return !getContext().DeclMustBeEmitted(Global);
648 }
649 
650 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
651   const AliasAttr *AA = VD->getAttr<AliasAttr>();
652   assert(AA && "No alias?");
653 
654   const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
655 
656   // See if there is already something with the target's name in the module.
657   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
658 
659   llvm::Constant *Aliasee;
660   if (isa<llvm::FunctionType>(DeclTy))
661     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl());
662   else
663     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
664                                     llvm::PointerType::getUnqual(DeclTy), 0);
665   if (!Entry) {
666     llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
667     F->setLinkage(llvm::Function::ExternalWeakLinkage);
668     WeakRefReferences.insert(F);
669   }
670 
671   return Aliasee;
672 }
673 
674 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
675   const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
676 
677   // Weak references don't produce any output by themselves.
678   if (Global->hasAttr<WeakRefAttr>())
679     return;
680 
681   // If this is an alias definition (which otherwise looks like a declaration)
682   // emit it now.
683   if (Global->hasAttr<AliasAttr>())
684     return EmitAliasDefinition(GD);
685 
686   // Ignore declarations, they will be emitted on their first use.
687   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
688     if (FD->getIdentifier()) {
689       llvm::StringRef Name = FD->getName();
690       if (Name == "_Block_object_assign") {
691         BlockObjectAssignDecl = FD;
692       } else if (Name == "_Block_object_dispose") {
693         BlockObjectDisposeDecl = FD;
694       }
695     }
696 
697     // Forward declarations are emitted lazily on first use.
698     if (!FD->isThisDeclarationADefinition())
699       return;
700   } else {
701     const VarDecl *VD = cast<VarDecl>(Global);
702     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
703 
704     if (VD->getIdentifier()) {
705       llvm::StringRef Name = VD->getName();
706       if (Name == "_NSConcreteGlobalBlock") {
707         NSConcreteGlobalBlockDecl = VD;
708       } else if (Name == "_NSConcreteStackBlock") {
709         NSConcreteStackBlockDecl = VD;
710       }
711     }
712 
713 
714     if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
715       return;
716   }
717 
718   // Defer code generation when possible if this is a static definition, inline
719   // function etc.  These we only want to emit if they are used.
720   if (!MayDeferGeneration(Global)) {
721     // Emit the definition if it can't be deferred.
722     EmitGlobalDefinition(GD);
723     return;
724   }
725 
726   // If we're deferring emission of a C++ variable with an
727   // initializer, remember the order in which it appeared in the file.
728   if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) &&
729       cast<VarDecl>(Global)->hasInit()) {
730     DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
731     CXXGlobalInits.push_back(0);
732   }
733 
734   // If the value has already been used, add it directly to the
735   // DeferredDeclsToEmit list.
736   llvm::StringRef MangledName = getMangledName(GD);
737   if (GetGlobalValue(MangledName))
738     DeferredDeclsToEmit.push_back(GD);
739   else {
740     // Otherwise, remember that we saw a deferred decl with this name.  The
741     // first use of the mangled name will cause it to move into
742     // DeferredDeclsToEmit.
743     DeferredDecls[MangledName] = GD;
744   }
745 }
746 
747 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) {
748   const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
749 
750   PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
751                                  Context.getSourceManager(),
752                                  "Generating code for declaration");
753 
754   if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
755     // At -O0, don't generate IR for functions with available_externally
756     // linkage.
757     if (CodeGenOpts.OptimizationLevel == 0 &&
758         !Function->hasAttr<AlwaysInlineAttr>() &&
759         getFunctionLinkage(Function)
760                                   == llvm::Function::AvailableExternallyLinkage)
761       return;
762 
763     if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
764       if (Method->isVirtual())
765         getVTables().EmitThunks(GD);
766 
767       if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method))
768         return EmitCXXConstructor(CD, GD.getCtorType());
769 
770       if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Method))
771         return EmitCXXDestructor(DD, GD.getDtorType());
772     }
773 
774     return EmitGlobalFunctionDefinition(GD);
775   }
776 
777   if (const VarDecl *VD = dyn_cast<VarDecl>(D))
778     return EmitGlobalVarDefinition(VD);
779 
780   assert(0 && "Invalid argument to EmitGlobalDefinition()");
781 }
782 
783 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
784 /// module, create and return an llvm Function with the specified type. If there
785 /// is something in the module with the specified name, return it potentially
786 /// bitcasted to the right type.
787 ///
788 /// If D is non-null, it specifies a decl that correspond to this.  This is used
789 /// to set the attributes on the function when it is first created.
790 llvm::Constant *
791 CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName,
792                                        const llvm::Type *Ty,
793                                        GlobalDecl D) {
794   // Lookup the entry, lazily creating it if necessary.
795   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
796   if (Entry) {
797     if (WeakRefReferences.count(Entry)) {
798       const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl());
799       if (FD && !FD->hasAttr<WeakAttr>())
800         Entry->setLinkage(llvm::Function::ExternalLinkage);
801 
802       WeakRefReferences.erase(Entry);
803     }
804 
805     if (Entry->getType()->getElementType() == Ty)
806       return Entry;
807 
808     // Make sure the result is of the correct type.
809     const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
810     return llvm::ConstantExpr::getBitCast(Entry, PTy);
811   }
812 
813   // This function doesn't have a complete type (for example, the return
814   // type is an incomplete struct). Use a fake type instead, and make
815   // sure not to try to set attributes.
816   bool IsIncompleteFunction = false;
817 
818   const llvm::FunctionType *FTy;
819   if (isa<llvm::FunctionType>(Ty)) {
820     FTy = cast<llvm::FunctionType>(Ty);
821   } else {
822     FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false);
823     IsIncompleteFunction = true;
824   }
825 
826   llvm::Function *F = llvm::Function::Create(FTy,
827                                              llvm::Function::ExternalLinkage,
828                                              MangledName, &getModule());
829   assert(F->getName() == MangledName && "name was uniqued!");
830   if (D.getDecl())
831     SetFunctionAttributes(D, F, IsIncompleteFunction);
832 
833   // This is the first use or definition of a mangled name.  If there is a
834   // deferred decl with this name, remember that we need to emit it at the end
835   // of the file.
836   llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
837   if (DDI != DeferredDecls.end()) {
838     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
839     // list, and remove it from DeferredDecls (since we don't need it anymore).
840     DeferredDeclsToEmit.push_back(DDI->second);
841     DeferredDecls.erase(DDI);
842 
843   // Otherwise, there are cases we have to worry about where we're
844   // using a declaration for which we must emit a definition but where
845   // we might not find a top-level definition:
846   //   - member functions defined inline in their classes
847   //   - friend functions defined inline in some class
848   //   - special member functions with implicit definitions
849   // If we ever change our AST traversal to walk into class methods,
850   // this will be unnecessary.
851   } else if (getLangOptions().CPlusPlus && D.getDecl()) {
852     // Look for a declaration that's lexically in a record.
853     const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl());
854     do {
855       if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
856         if (FD->isImplicit()) {
857           assert(FD->isUsed() && "Sema didn't mark implicit function as used!");
858           DeferredDeclsToEmit.push_back(D);
859           break;
860         } else if (FD->isThisDeclarationADefinition()) {
861           DeferredDeclsToEmit.push_back(D);
862           break;
863         }
864       }
865       FD = FD->getPreviousDeclaration();
866     } while (FD);
867   }
868 
869   // Make sure the result is of the requested type.
870   if (!IsIncompleteFunction) {
871     assert(F->getType()->getElementType() == Ty);
872     return F;
873   }
874 
875   const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
876   return llvm::ConstantExpr::getBitCast(F, PTy);
877 }
878 
879 /// GetAddrOfFunction - Return the address of the given function.  If Ty is
880 /// non-null, then this function will use the specified type if it has to
881 /// create it (this occurs when we see a definition of the function).
882 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
883                                                  const llvm::Type *Ty) {
884   // If there was no specific requested type, just convert it now.
885   if (!Ty)
886     Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
887 
888   llvm::StringRef MangledName = getMangledName(GD);
889   return GetOrCreateLLVMFunction(MangledName, Ty, GD);
890 }
891 
892 /// CreateRuntimeFunction - Create a new runtime function with the specified
893 /// type and name.
894 llvm::Constant *
895 CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
896                                      llvm::StringRef Name) {
897   return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl());
898 }
899 
900 static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) {
901   if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType())
902     return false;
903   if (Context.getLangOptions().CPlusPlus &&
904       Context.getBaseElementType(D->getType())->getAs<RecordType>()) {
905     // FIXME: We should do something fancier here!
906     return false;
907   }
908   return true;
909 }
910 
911 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
912 /// create and return an llvm GlobalVariable with the specified type.  If there
913 /// is something in the module with the specified name, return it potentially
914 /// bitcasted to the right type.
915 ///
916 /// If D is non-null, it specifies a decl that correspond to this.  This is used
917 /// to set the attributes on the global when it is first created.
918 llvm::Constant *
919 CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
920                                      const llvm::PointerType *Ty,
921                                      const VarDecl *D,
922                                      bool UnnamedAddr) {
923   // Lookup the entry, lazily creating it if necessary.
924   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
925   if (Entry) {
926     if (WeakRefReferences.count(Entry)) {
927       if (D && !D->hasAttr<WeakAttr>())
928         Entry->setLinkage(llvm::Function::ExternalLinkage);
929 
930       WeakRefReferences.erase(Entry);
931     }
932 
933     if (UnnamedAddr)
934       Entry->setUnnamedAddr(true);
935 
936     if (Entry->getType() == Ty)
937       return Entry;
938 
939     // Make sure the result is of the correct type.
940     return llvm::ConstantExpr::getBitCast(Entry, Ty);
941   }
942 
943   // This is the first use or definition of a mangled name.  If there is a
944   // deferred decl with this name, remember that we need to emit it at the end
945   // of the file.
946   llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
947   if (DDI != DeferredDecls.end()) {
948     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
949     // list, and remove it from DeferredDecls (since we don't need it anymore).
950     DeferredDeclsToEmit.push_back(DDI->second);
951     DeferredDecls.erase(DDI);
952   }
953 
954   llvm::GlobalVariable *GV =
955     new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
956                              llvm::GlobalValue::ExternalLinkage,
957                              0, MangledName, 0,
958                              false, Ty->getAddressSpace());
959 
960   // Handle things which are present even on external declarations.
961   if (D) {
962     // FIXME: This code is overly simple and should be merged with other global
963     // handling.
964     GV->setConstant(DeclIsConstantGlobal(Context, D));
965 
966     // Set linkage and visibility in case we never see a definition.
967     NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility();
968     if (LV.linkage() != ExternalLinkage) {
969       GV->setLinkage(llvm::GlobalValue::InternalLinkage);
970     } else {
971       if (D->hasAttr<DLLImportAttr>())
972         GV->setLinkage(llvm::GlobalValue::DLLImportLinkage);
973       else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>())
974         GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
975 
976       // Set visibility on a declaration only if it's explicit.
977       if (LV.visibilityExplicit())
978         GV->setVisibility(GetLLVMVisibility(LV.visibility()));
979     }
980 
981     GV->setThreadLocal(D->isThreadSpecified());
982   }
983 
984   return GV;
985 }
986 
987 
988 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
989 /// given global variable.  If Ty is non-null and if the global doesn't exist,
990 /// then it will be greated with the specified type instead of whatever the
991 /// normal requested type would be.
992 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
993                                                   const llvm::Type *Ty) {
994   assert(D->hasGlobalStorage() && "Not a global variable");
995   QualType ASTTy = D->getType();
996   if (Ty == 0)
997     Ty = getTypes().ConvertTypeForMem(ASTTy);
998 
999   const llvm::PointerType *PTy =
1000     llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
1001 
1002   llvm::StringRef MangledName = getMangledName(D);
1003   return GetOrCreateLLVMGlobal(MangledName, PTy, D);
1004 }
1005 
1006 /// CreateRuntimeVariable - Create a new runtime global variable with the
1007 /// specified type and name.
1008 llvm::Constant *
1009 CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
1010                                      llvm::StringRef Name) {
1011   return GetOrCreateLLVMGlobal(Name,  llvm::PointerType::getUnqual(Ty), 0,
1012                                true);
1013 }
1014 
1015 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
1016   assert(!D->getInit() && "Cannot emit definite definitions here!");
1017 
1018   if (MayDeferGeneration(D)) {
1019     // If we have not seen a reference to this variable yet, place it
1020     // into the deferred declarations table to be emitted if needed
1021     // later.
1022     llvm::StringRef MangledName = getMangledName(D);
1023     if (!GetGlobalValue(MangledName)) {
1024       DeferredDecls[MangledName] = D;
1025       return;
1026     }
1027   }
1028 
1029   // The tentative definition is the only definition.
1030   EmitGlobalVarDefinition(D);
1031 }
1032 
1033 void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) {
1034   if (DefinitionRequired)
1035     getVTables().GenerateClassData(getVTableLinkage(Class), Class);
1036 }
1037 
1038 llvm::GlobalVariable::LinkageTypes
1039 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1040   if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
1041     return llvm::GlobalVariable::InternalLinkage;
1042 
1043   if (const CXXMethodDecl *KeyFunction
1044                                     = RD->getASTContext().getKeyFunction(RD)) {
1045     // If this class has a key function, use that to determine the linkage of
1046     // the vtable.
1047     const FunctionDecl *Def = 0;
1048     if (KeyFunction->hasBody(Def))
1049       KeyFunction = cast<CXXMethodDecl>(Def);
1050 
1051     switch (KeyFunction->getTemplateSpecializationKind()) {
1052       case TSK_Undeclared:
1053       case TSK_ExplicitSpecialization:
1054         if (KeyFunction->isInlined())
1055           return llvm::GlobalVariable::LinkOnceODRLinkage;
1056 
1057         return llvm::GlobalVariable::ExternalLinkage;
1058 
1059       case TSK_ImplicitInstantiation:
1060         return llvm::GlobalVariable::LinkOnceODRLinkage;
1061 
1062       case TSK_ExplicitInstantiationDefinition:
1063         return llvm::GlobalVariable::WeakODRLinkage;
1064 
1065       case TSK_ExplicitInstantiationDeclaration:
1066         // FIXME: Use available_externally linkage. However, this currently
1067         // breaks LLVM's build due to undefined symbols.
1068         //      return llvm::GlobalVariable::AvailableExternallyLinkage;
1069         return llvm::GlobalVariable::LinkOnceODRLinkage;
1070     }
1071   }
1072 
1073   switch (RD->getTemplateSpecializationKind()) {
1074   case TSK_Undeclared:
1075   case TSK_ExplicitSpecialization:
1076   case TSK_ImplicitInstantiation:
1077     return llvm::GlobalVariable::LinkOnceODRLinkage;
1078 
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::LinkOnceODRLinkage;
1087   }
1088 
1089   // Silence GCC warning.
1090   return llvm::GlobalVariable::LinkOnceODRLinkage;
1091 }
1092 
1093 CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const {
1094     return Context.toCharUnitsFromBits(
1095       TheTargetData.getTypeStoreSizeInBits(Ty));
1096 }
1097 
1098 void 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 
1217 llvm::GlobalValue::LinkageTypes
1218 CodeGenModule::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.
1257 static 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 
1319 void 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 
1392 void 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".
1463 llvm::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 
1480 llvm::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 
1486 static llvm::StringMapEntry<llvm::Constant*> &
1487 GetConstantCFStringEntry(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 
1537 llvm::Constant *
1538 CodeGenModule::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 
1621 llvm::Constant *
1622 CodeGenModule::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.
1719 std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
1720   const ASTContext &Context = getContext();
1721   const ConstantArrayType *CAT =
1722     Context.getAsConstantArrayType(E->getType());
1723   assert(CAT && "String isn't pointer or array!");
1724 
1725   // Resize the string to the right size.
1726   uint64_t RealLen = CAT->getSize().getZExtValue();
1727 
1728   if (E->isWide())
1729     RealLen *= Context.Target.getWCharWidth() / Context.getCharWidth();
1730 
1731   std::string Str = E->getString().str();
1732   Str.resize(RealLen, '\0');
1733 
1734   return Str;
1735 }
1736 
1737 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
1738 /// constant array for the given string literal.
1739 llvm::Constant *
1740 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
1741   // FIXME: This can be more efficient.
1742   // FIXME: We shouldn't need to bitcast the constant in the wide string case.
1743   llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S));
1744   if (S->isWide()) {
1745     llvm::Type *DestTy =
1746         llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType()));
1747     C = llvm::ConstantExpr::getBitCast(C, DestTy);
1748   }
1749   return C;
1750 }
1751 
1752 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
1753 /// array for the given ObjCEncodeExpr node.
1754 llvm::Constant *
1755 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
1756   std::string Str;
1757   getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1758 
1759   return GetAddrOfConstantCString(Str);
1760 }
1761 
1762 
1763 /// GenerateWritableString -- Creates storage for a string literal.
1764 static llvm::Constant *GenerateStringLiteral(const std::string &str,
1765                                              bool constant,
1766                                              CodeGenModule &CGM,
1767                                              const char *GlobalName) {
1768   // Create Constant for this string literal. Don't add a '\0'.
1769   llvm::Constant *C =
1770       llvm::ConstantArray::get(CGM.getLLVMContext(), str, false);
1771 
1772   // Create a global variable for this string
1773   llvm::GlobalVariable *GV =
1774     new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
1775                              llvm::GlobalValue::PrivateLinkage,
1776                              C, GlobalName);
1777   GV->setUnnamedAddr(true);
1778   return GV;
1779 }
1780 
1781 /// GetAddrOfConstantString - Returns a pointer to a character array
1782 /// containing the literal. This contents are exactly that of the
1783 /// given string, i.e. it will not be null terminated automatically;
1784 /// see GetAddrOfConstantCString. Note that whether the result is
1785 /// actually a pointer to an LLVM constant depends on
1786 /// Feature.WriteableStrings.
1787 ///
1788 /// The result has pointer to array type.
1789 llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
1790                                                        const char *GlobalName) {
1791   bool IsConstant = !Features.WritableStrings;
1792 
1793   // Get the default prefix if a name wasn't specified.
1794   if (!GlobalName)
1795     GlobalName = ".str";
1796 
1797   // Don't share any string literals if strings aren't constant.
1798   if (!IsConstant)
1799     return GenerateStringLiteral(str, false, *this, GlobalName);
1800 
1801   llvm::StringMapEntry<llvm::Constant *> &Entry =
1802     ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
1803 
1804   if (Entry.getValue())
1805     return Entry.getValue();
1806 
1807   // Create a global variable for this.
1808   llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
1809   Entry.setValue(C);
1810   return C;
1811 }
1812 
1813 /// GetAddrOfConstantCString - Returns a pointer to a character
1814 /// array containing the literal and a terminating '\-'
1815 /// character. The result has pointer to array type.
1816 llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
1817                                                         const char *GlobalName){
1818   return GetAddrOfConstantString(str + '\0', GlobalName);
1819 }
1820 
1821 /// EmitObjCPropertyImplementations - Emit information for synthesized
1822 /// properties for an implementation.
1823 void CodeGenModule::EmitObjCPropertyImplementations(const
1824                                                     ObjCImplementationDecl *D) {
1825   for (ObjCImplementationDecl::propimpl_iterator
1826          i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1827     ObjCPropertyImplDecl *PID = *i;
1828 
1829     // Dynamic is just for type-checking.
1830     if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1831       ObjCPropertyDecl *PD = PID->getPropertyDecl();
1832 
1833       // Determine which methods need to be implemented, some may have
1834       // been overridden. Note that ::isSynthesized is not the method
1835       // we want, that just indicates if the decl came from a
1836       // property. What we want to know is if the method is defined in
1837       // this implementation.
1838       if (!D->getInstanceMethod(PD->getGetterName()))
1839         CodeGenFunction(*this).GenerateObjCGetter(
1840                                  const_cast<ObjCImplementationDecl *>(D), PID);
1841       if (!PD->isReadOnly() &&
1842           !D->getInstanceMethod(PD->getSetterName()))
1843         CodeGenFunction(*this).GenerateObjCSetter(
1844                                  const_cast<ObjCImplementationDecl *>(D), PID);
1845     }
1846   }
1847 }
1848 
1849 /// EmitObjCIvarInitializations - Emit information for ivar initialization
1850 /// for an implementation.
1851 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
1852   if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0)
1853     return;
1854   DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D));
1855   assert(DC && "EmitObjCIvarInitializations - null DeclContext");
1856   IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
1857   Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
1858   ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(),
1859                                                   D->getLocation(),
1860                                                   D->getLocation(), cxxSelector,
1861                                                   getContext().VoidTy, 0,
1862                                                   DC, true, false, true, false,
1863                                                   ObjCMethodDecl::Required);
1864   D->addInstanceMethod(DTORMethod);
1865   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
1866 
1867   II = &getContext().Idents.get(".cxx_construct");
1868   cxxSelector = getContext().Selectors.getSelector(0, &II);
1869   // The constructor returns 'self'.
1870   ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
1871                                                 D->getLocation(),
1872                                                 D->getLocation(), cxxSelector,
1873                                                 getContext().getObjCIdType(), 0,
1874                                                 DC, true, false, true, false,
1875                                                 ObjCMethodDecl::Required);
1876   D->addInstanceMethod(CTORMethod);
1877   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
1878 
1879 
1880 }
1881 
1882 /// EmitNamespace - Emit all declarations in a namespace.
1883 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
1884   for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
1885        I != E; ++I)
1886     EmitTopLevelDecl(*I);
1887 }
1888 
1889 // EmitLinkageSpec - Emit all declarations in a linkage spec.
1890 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
1891   if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
1892       LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
1893     ErrorUnsupported(LSD, "linkage spec");
1894     return;
1895   }
1896 
1897   for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
1898        I != E; ++I)
1899     EmitTopLevelDecl(*I);
1900 }
1901 
1902 /// EmitTopLevelDecl - Emit code for a single top level declaration.
1903 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
1904   // If an error has occurred, stop code generation, but continue
1905   // parsing and semantic analysis (to ensure all warnings and errors
1906   // are emitted).
1907   if (Diags.hasErrorOccurred())
1908     return;
1909 
1910   // Ignore dependent declarations.
1911   if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
1912     return;
1913 
1914   switch (D->getKind()) {
1915   case Decl::CXXConversion:
1916   case Decl::CXXMethod:
1917   case Decl::Function:
1918     // Skip function templates
1919     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
1920       return;
1921 
1922     EmitGlobal(cast<FunctionDecl>(D));
1923     break;
1924 
1925   case Decl::Var:
1926     EmitGlobal(cast<VarDecl>(D));
1927     break;
1928 
1929   // C++ Decls
1930   case Decl::Namespace:
1931     EmitNamespace(cast<NamespaceDecl>(D));
1932     break;
1933     // No code generation needed.
1934   case Decl::UsingShadow:
1935   case Decl::Using:
1936   case Decl::UsingDirective:
1937   case Decl::ClassTemplate:
1938   case Decl::FunctionTemplate:
1939   case Decl::NamespaceAlias:
1940     break;
1941   case Decl::CXXConstructor:
1942     // Skip function templates
1943     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
1944       return;
1945 
1946     EmitCXXConstructors(cast<CXXConstructorDecl>(D));
1947     break;
1948   case Decl::CXXDestructor:
1949     EmitCXXDestructors(cast<CXXDestructorDecl>(D));
1950     break;
1951 
1952   case Decl::StaticAssert:
1953     // Nothing to do.
1954     break;
1955 
1956   // Objective-C Decls
1957 
1958   // Forward declarations, no (immediate) code generation.
1959   case Decl::ObjCClass:
1960   case Decl::ObjCForwardProtocol:
1961   case Decl::ObjCInterface:
1962     break;
1963 
1964     case Decl::ObjCCategory: {
1965       ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
1966       if (CD->IsClassExtension() && CD->hasSynthBitfield())
1967         Context.ResetObjCLayout(CD->getClassInterface());
1968       break;
1969     }
1970 
1971 
1972   case Decl::ObjCProtocol:
1973     Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
1974     break;
1975 
1976   case Decl::ObjCCategoryImpl:
1977     // Categories have properties but don't support synthesize so we
1978     // can ignore them here.
1979     Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
1980     break;
1981 
1982   case Decl::ObjCImplementation: {
1983     ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1984     if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield())
1985       Context.ResetObjCLayout(OMD->getClassInterface());
1986     EmitObjCPropertyImplementations(OMD);
1987     EmitObjCIvarInitializations(OMD);
1988     Runtime->GenerateClass(OMD);
1989     break;
1990   }
1991   case Decl::ObjCMethod: {
1992     ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
1993     // If this is not a prototype, emit the body.
1994     if (OMD->getBody())
1995       CodeGenFunction(*this).GenerateObjCMethod(OMD);
1996     break;
1997   }
1998   case Decl::ObjCCompatibleAlias:
1999     // compatibility-alias is a directive and has no code gen.
2000     break;
2001 
2002   case Decl::LinkageSpec:
2003     EmitLinkageSpec(cast<LinkageSpecDecl>(D));
2004     break;
2005 
2006   case Decl::FileScopeAsm: {
2007     FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
2008     llvm::StringRef AsmString = AD->getAsmString()->getString();
2009 
2010     const std::string &S = getModule().getModuleInlineAsm();
2011     if (S.empty())
2012       getModule().setModuleInlineAsm(AsmString);
2013     else
2014       getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
2015     break;
2016   }
2017 
2018   default:
2019     // Make sure we handled everything we should, every other kind is a
2020     // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
2021     // function. Need to recode Decl::Kind to do that easily.
2022     assert(isa<TypeDecl>(D) && "Unsupported decl kind");
2023   }
2024 }
2025 
2026 /// Turns the given pointer into a constant.
2027 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
2028                                           const void *Ptr) {
2029   uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
2030   const llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
2031   return llvm::ConstantInt::get(i64, PtrInt);
2032 }
2033 
2034 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
2035                                    llvm::NamedMDNode *&GlobalMetadata,
2036                                    GlobalDecl D,
2037                                    llvm::GlobalValue *Addr) {
2038   if (!GlobalMetadata)
2039     GlobalMetadata =
2040       CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
2041 
2042   // TODO: should we report variant information for ctors/dtors?
2043   llvm::Value *Ops[] = {
2044     Addr,
2045     GetPointerConstant(CGM.getLLVMContext(), D.getDecl())
2046   };
2047   GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops, 2));
2048 }
2049 
2050 /// Emits metadata nodes associating all the global values in the
2051 /// current module with the Decls they came from.  This is useful for
2052 /// projects using IR gen as a subroutine.
2053 ///
2054 /// Since there's currently no way to associate an MDNode directly
2055 /// with an llvm::GlobalValue, we create a global named metadata
2056 /// with the name 'clang.global.decl.ptrs'.
2057 void CodeGenModule::EmitDeclMetadata() {
2058   llvm::NamedMDNode *GlobalMetadata = 0;
2059 
2060   // StaticLocalDeclMap
2061   for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator
2062          I = MangledDeclNames.begin(), E = MangledDeclNames.end();
2063        I != E; ++I) {
2064     llvm::GlobalValue *Addr = getModule().getNamedValue(I->second);
2065     EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr);
2066   }
2067 }
2068 
2069 /// Emits metadata nodes for all the local variables in the current
2070 /// function.
2071 void CodeGenFunction::EmitDeclMetadata() {
2072   if (LocalDeclMap.empty()) return;
2073 
2074   llvm::LLVMContext &Context = getLLVMContext();
2075 
2076   // Find the unique metadata ID for this name.
2077   unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
2078 
2079   llvm::NamedMDNode *GlobalMetadata = 0;
2080 
2081   for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator
2082          I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) {
2083     const Decl *D = I->first;
2084     llvm::Value *Addr = I->second;
2085 
2086     if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
2087       llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
2088       Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, &DAddr, 1));
2089     } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
2090       GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
2091       EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
2092     }
2093   }
2094 }
2095 
2096 ///@name Custom Runtime Function Interfaces
2097 ///@{
2098 //
2099 // FIXME: These can be eliminated once we can have clients just get the required
2100 // AST nodes from the builtin tables.
2101 
2102 llvm::Constant *CodeGenModule::getBlockObjectDispose() {
2103   if (BlockObjectDispose)
2104     return BlockObjectDispose;
2105 
2106   // If we saw an explicit decl, use that.
2107   if (BlockObjectDisposeDecl) {
2108     return BlockObjectDispose = GetAddrOfFunction(
2109       BlockObjectDisposeDecl,
2110       getTypes().GetFunctionType(BlockObjectDisposeDecl));
2111   }
2112 
2113   // Otherwise construct the function by hand.
2114   const llvm::FunctionType *FTy;
2115   std::vector<const llvm::Type*> ArgTys;
2116   const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2117   ArgTys.push_back(PtrToInt8Ty);
2118   ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2119   FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2120   return BlockObjectDispose =
2121     CreateRuntimeFunction(FTy, "_Block_object_dispose");
2122 }
2123 
2124 llvm::Constant *CodeGenModule::getBlockObjectAssign() {
2125   if (BlockObjectAssign)
2126     return BlockObjectAssign;
2127 
2128   // If we saw an explicit decl, use that.
2129   if (BlockObjectAssignDecl) {
2130     return BlockObjectAssign = GetAddrOfFunction(
2131       BlockObjectAssignDecl,
2132       getTypes().GetFunctionType(BlockObjectAssignDecl));
2133   }
2134 
2135   // Otherwise construct the function by hand.
2136   const llvm::FunctionType *FTy;
2137   std::vector<const llvm::Type*> ArgTys;
2138   const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2139   ArgTys.push_back(PtrToInt8Ty);
2140   ArgTys.push_back(PtrToInt8Ty);
2141   ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2142   FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
2143   return BlockObjectAssign =
2144     CreateRuntimeFunction(FTy, "_Block_object_assign");
2145 }
2146 
2147 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
2148   if (NSConcreteGlobalBlock)
2149     return NSConcreteGlobalBlock;
2150 
2151   // If we saw an explicit decl, use that.
2152   if (NSConcreteGlobalBlockDecl) {
2153     return NSConcreteGlobalBlock = GetAddrOfGlobalVar(
2154       NSConcreteGlobalBlockDecl,
2155       getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType()));
2156   }
2157 
2158   // Otherwise construct the variable by hand.
2159   return NSConcreteGlobalBlock = CreateRuntimeVariable(
2160     PtrToInt8Ty, "_NSConcreteGlobalBlock");
2161 }
2162 
2163 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
2164   if (NSConcreteStackBlock)
2165     return NSConcreteStackBlock;
2166 
2167   // If we saw an explicit decl, use that.
2168   if (NSConcreteStackBlockDecl) {
2169     return NSConcreteStackBlock = GetAddrOfGlobalVar(
2170       NSConcreteStackBlockDecl,
2171       getTypes().ConvertType(NSConcreteStackBlockDecl->getType()));
2172   }
2173 
2174   // Otherwise construct the variable by hand.
2175   return NSConcreteStackBlock = CreateRuntimeVariable(
2176     PtrToInt8Ty, "_NSConcreteStackBlock");
2177 }
2178 
2179 ///@}
2180