1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
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 is the internal per-translation-unit state used for llvm translation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef CLANG_CODEGEN_CODEGENMODULE_H
15 #define CLANG_CODEGEN_CODEGENMODULE_H
16 
17 #include "clang/Basic/ABI.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/Mangle.h"
23 #include "CGCall.h"
24 #include "CGVTables.h"
25 #include "CodeGenTypes.h"
26 #include "GlobalDecl.h"
27 #include "llvm/Module.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringSet.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/Support/ValueHandle.h"
33 
34 namespace llvm {
35   class Module;
36   class Constant;
37   class Function;
38   class GlobalValue;
39   class TargetData;
40   class FunctionType;
41   class LLVMContext;
42 }
43 
44 namespace clang {
45   class TargetCodeGenInfo;
46   class ASTContext;
47   class FunctionDecl;
48   class IdentifierInfo;
49   class ObjCMethodDecl;
50   class ObjCImplementationDecl;
51   class ObjCCategoryImplDecl;
52   class ObjCProtocolDecl;
53   class ObjCEncodeExpr;
54   class BlockExpr;
55   class CharUnits;
56   class Decl;
57   class Expr;
58   class Stmt;
59   class StringLiteral;
60   class NamedDecl;
61   class ValueDecl;
62   class VarDecl;
63   class LangOptions;
64   class CodeGenOptions;
65   class Diagnostic;
66   class AnnotateAttr;
67   class CXXDestructorDecl;
68   class MangleBuffer;
69 
70 namespace CodeGen {
71 
72   class CodeGenFunction;
73   class CodeGenTBAA;
74   class CGCXXABI;
75   class CGDebugInfo;
76   class CGObjCRuntime;
77   class BlockFieldFlags;
78 
79   struct OrderGlobalInits {
80     unsigned int priority;
81     unsigned int lex_order;
82     OrderGlobalInits(unsigned int p, unsigned int l)
83       : priority(p), lex_order(l) {}
84 
85     bool operator==(const OrderGlobalInits &RHS) const {
86       return priority == RHS.priority &&
87              lex_order == RHS.lex_order;
88     }
89 
90     bool operator<(const OrderGlobalInits &RHS) const {
91       if (priority < RHS.priority)
92         return true;
93 
94       return priority == RHS.priority && lex_order < RHS.lex_order;
95     }
96   };
97 
98   struct CodeGenTypeCache {
99     /// i8, i32, and i64
100     const llvm::IntegerType *Int8Ty, *Int32Ty, *Int64Ty;
101 
102     /// int
103     const llvm::IntegerType *IntTy;
104 
105     /// intptr_t and size_t, which we assume are the same
106     union {
107       const llvm::IntegerType *IntPtrTy;
108       const llvm::IntegerType *SizeTy;
109     };
110 
111     /// void* in address space 0
112     union {
113       const llvm::PointerType *VoidPtrTy;
114       const llvm::PointerType *Int8PtrTy;
115     };
116 
117     /// void** in address space 0
118     union {
119       const llvm::PointerType *VoidPtrPtrTy;
120       const llvm::PointerType *Int8PtrPtrTy;
121     };
122 
123     /// The width of an address-zero pointer.
124     unsigned char PointerWidthInBits;
125   };
126 
127 /// CodeGenModule - This class organizes the cross-function state that is used
128 /// while generating LLVM code.
129 class CodeGenModule : public CodeGenTypeCache {
130   CodeGenModule(const CodeGenModule&);  // DO NOT IMPLEMENT
131   void operator=(const CodeGenModule&); // DO NOT IMPLEMENT
132 
133   typedef std::vector<std::pair<llvm::Constant*, int> > CtorList;
134 
135   ASTContext &Context;
136   const LangOptions &Features;
137   const CodeGenOptions &CodeGenOpts;
138   llvm::Module &TheModule;
139   const llvm::TargetData &TheTargetData;
140   mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
141   Diagnostic &Diags;
142   CGCXXABI &ABI;
143   CodeGenTypes Types;
144   CodeGenTBAA *TBAA;
145 
146   /// VTables - Holds information about C++ vtables.
147   CodeGenVTables VTables;
148   friend class CodeGenVTables;
149 
150   CGObjCRuntime* Runtime;
151   CGDebugInfo* DebugInfo;
152 
153   // WeakRefReferences - A set of references that have only been seen via
154   // a weakref so far. This is used to remove the weak of the reference if we ever
155   // see a direct reference or a definition.
156   llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
157 
158   /// DeferredDecls - This contains all the decls which have definitions but
159   /// which are deferred for emission and therefore should only be output if
160   /// they are actually used.  If a decl is in this, then it is known to have
161   /// not been referenced yet.
162   llvm::StringMap<GlobalDecl> DeferredDecls;
163 
164   /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen
165   /// that *are* actually referenced.  These get code generated when the module
166   /// is done.
167   std::vector<GlobalDecl> DeferredDeclsToEmit;
168 
169   /// LLVMUsed - List of global values which are required to be
170   /// present in the object file; bitcast to i8*. This is used for
171   /// forcing visibility of symbols which may otherwise be optimized
172   /// out.
173   std::vector<llvm::WeakVH> LLVMUsed;
174 
175   /// GlobalCtors - Store the list of global constructors and their respective
176   /// priorities to be emitted when the translation unit is complete.
177   CtorList GlobalCtors;
178 
179   /// GlobalDtors - Store the list of global destructors and their respective
180   /// priorities to be emitted when the translation unit is complete.
181   CtorList GlobalDtors;
182 
183   /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names.
184   llvm::DenseMap<GlobalDecl, llvm::StringRef> MangledDeclNames;
185   llvm::BumpPtrAllocator MangledNamesAllocator;
186 
187   std::vector<llvm::Constant*> Annotations;
188 
189   llvm::StringMap<llvm::Constant*> CFConstantStringMap;
190   llvm::StringMap<llvm::Constant*> ConstantStringMap;
191   llvm::DenseMap<const Decl*, llvm::Value*> StaticLocalDeclMap;
192 
193   /// CXXGlobalInits - Global variables with initializers that need to run
194   /// before main.
195   std::vector<llvm::Constant*> CXXGlobalInits;
196 
197   /// When a C++ decl with an initializer is deferred, null is
198   /// appended to CXXGlobalInits, and the index of that null is placed
199   /// here so that the initializer will be performed in the correct
200   /// order.
201   llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
202 
203   /// - Global variables with initializers whose order of initialization
204   /// is set by init_priority attribute.
205 
206   llvm::SmallVector<std::pair<OrderGlobalInits, llvm::Function*>, 8>
207     PrioritizedCXXGlobalInits;
208 
209   /// CXXGlobalDtors - Global destructor functions and arguments that need to
210   /// run on termination.
211   std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
212 
213   /// CFConstantStringClassRef - Cached reference to the class for constant
214   /// strings. This value has type int * but is actually an Obj-C class pointer.
215   llvm::Constant *CFConstantStringClassRef;
216 
217   /// ConstantStringClassRef - Cached reference to the class for constant
218   /// strings. This value has type int * but is actually an Obj-C class pointer.
219   llvm::Constant *ConstantStringClassRef;
220 
221   /// Lazily create the Objective-C runtime
222   void createObjCRuntime();
223 
224   llvm::LLVMContext &VMContext;
225 
226   /// @name Cache for Blocks Runtime Globals
227   /// @{
228 
229   const VarDecl *NSConcreteGlobalBlockDecl;
230   const VarDecl *NSConcreteStackBlockDecl;
231   llvm::Constant *NSConcreteGlobalBlock;
232   llvm::Constant *NSConcreteStackBlock;
233 
234   const FunctionDecl *BlockObjectAssignDecl;
235   const FunctionDecl *BlockObjectDisposeDecl;
236   llvm::Constant *BlockObjectAssign;
237   llvm::Constant *BlockObjectDispose;
238 
239   const llvm::Type *BlockDescriptorType;
240   const llvm::Type *GenericBlockLiteralType;
241 
242   struct {
243     int GlobalUniqueCount;
244   } Block;
245 
246   llvm::DenseMap<uint64_t, llvm::Constant *> AssignCache;
247   llvm::DenseMap<uint64_t, llvm::Constant *> DestroyCache;
248 
249   /// @}
250 public:
251   CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
252                 llvm::Module &M, const llvm::TargetData &TD, Diagnostic &Diags);
253 
254   ~CodeGenModule();
255 
256   /// Release - Finalize LLVM code generation.
257   void Release();
258 
259   /// getObjCRuntime() - Return a reference to the configured
260   /// Objective-C runtime.
261   CGObjCRuntime &getObjCRuntime() {
262     if (!Runtime) createObjCRuntime();
263     return *Runtime;
264   }
265 
266   /// hasObjCRuntime() - Return true iff an Objective-C runtime has
267   /// been configured.
268   bool hasObjCRuntime() { return !!Runtime; }
269 
270   /// getCXXABI() - Return a reference to the configured C++ ABI.
271   CGCXXABI &getCXXABI() { return ABI; }
272 
273   llvm::Value *getStaticLocalDeclAddress(const VarDecl *VD) {
274     return StaticLocalDeclMap[VD];
275   }
276   void setStaticLocalDeclAddress(const VarDecl *D,
277                              llvm::GlobalVariable *GV) {
278     StaticLocalDeclMap[D] = GV;
279   }
280 
281   CGDebugInfo *getDebugInfo() { return DebugInfo; }
282   ASTContext &getContext() const { return Context; }
283   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
284   const LangOptions &getLangOptions() const { return Features; }
285   llvm::Module &getModule() const { return TheModule; }
286   CodeGenTypes &getTypes() { return Types; }
287   CodeGenVTables &getVTables() { return VTables; }
288   Diagnostic &getDiags() const { return Diags; }
289   const llvm::TargetData &getTargetData() const { return TheTargetData; }
290   const TargetInfo &getTarget() const { return Context.Target; }
291   llvm::LLVMContext &getLLVMContext() { return VMContext; }
292   const TargetCodeGenInfo &getTargetCodeGenInfo();
293   bool isTargetDarwin() const;
294 
295   llvm::MDNode *getTBAAInfo(QualType QTy);
296 
297   static void DecorateInstruction(llvm::Instruction *Inst,
298                                   llvm::MDNode *TBAAInfo);
299 
300   /// setGlobalVisibility - Set the visibility for the given LLVM
301   /// GlobalValue.
302   void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
303 
304   /// TypeVisibilityKind - The kind of global variable that is passed to
305   /// setTypeVisibility
306   enum TypeVisibilityKind {
307     TVK_ForVTT,
308     TVK_ForVTable,
309     TVK_ForRTTI,
310     TVK_ForRTTIName
311   };
312 
313   /// setTypeVisibility - Set the visibility for the given global
314   /// value which holds information about a type.
315   void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D,
316                          TypeVisibilityKind TVK) const;
317 
318   static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
319     switch (V) {
320     case DefaultVisibility:   return llvm::GlobalValue::DefaultVisibility;
321     case HiddenVisibility:    return llvm::GlobalValue::HiddenVisibility;
322     case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
323     }
324     llvm_unreachable("unknown visibility!");
325     return llvm::GlobalValue::DefaultVisibility;
326   }
327 
328   llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
329     if (isa<CXXConstructorDecl>(GD.getDecl()))
330       return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
331                                      GD.getCtorType());
332     else if (isa<CXXDestructorDecl>(GD.getDecl()))
333       return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
334                                      GD.getDtorType());
335     else if (isa<FunctionDecl>(GD.getDecl()))
336       return GetAddrOfFunction(GD);
337     else
338       return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
339   }
340 
341   /// CreateOrReplaceCXXRuntimeVariable - Will return a global variable of the given
342   /// type. If a variable with a different type already exists then a new
343   /// variable with the right type will be created and all uses of the old
344   /// variable will be replaced with a bitcast to the new variable.
345   llvm::GlobalVariable *
346   CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, const llvm::Type *Ty,
347                                     llvm::GlobalValue::LinkageTypes Linkage);
348 
349   /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
350   /// given global variable.  If Ty is non-null and if the global doesn't exist,
351   /// then it will be greated with the specified type instead of whatever the
352   /// normal requested type would be.
353   llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
354                                      const llvm::Type *Ty = 0);
355 
356   /// GetAddrOfFunction - Return the address of the given function.  If Ty is
357   /// non-null, then this function will use the specified type if it has to
358   /// create it.
359   llvm::Constant *GetAddrOfFunction(GlobalDecl GD,
360                                     const llvm::Type *Ty = 0,
361                                     bool ForVTable = false);
362 
363   /// GetAddrOfRTTIDescriptor - Get the address of the RTTI descriptor
364   /// for the given type.
365   llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
366 
367   /// GetAddrOfThunk - Get the address of the thunk for the given global decl.
368   llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
369 
370   /// GetWeakRefReference - Get a reference to the target of VD.
371   llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
372 
373   /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to
374   /// a class. Returns null if the offset is 0.
375   llvm::Constant *
376   GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
377                                CastExpr::path_const_iterator PathBegin,
378                                CastExpr::path_const_iterator PathEnd);
379 
380   llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T,
381                                        BlockFieldFlags flags,
382                                        unsigned Align,
383                                        const VarDecl *variable);
384   llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T,
385                                           BlockFieldFlags flags,
386                                           unsigned Align,
387                                           const VarDecl *variable);
388 
389   /// getGlobalUniqueCount - Fetches the global unique block count.
390   int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; }
391 
392   /// getBlockDescriptorType - Fetches the type of a generic block
393   /// descriptor.
394   const llvm::Type *getBlockDescriptorType();
395 
396   /// getGenericBlockLiteralType - The type of a generic block literal.
397   const llvm::Type *getGenericBlockLiteralType();
398 
399   /// GetAddrOfGlobalBlock - Gets the address of a block which
400   /// requires no captures.
401   llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
402 
403   /// GetStringForStringLiteral - Return the appropriate bytes for a string
404   /// literal, properly padded to match the literal type. If only the address of
405   /// a constant is needed consider using GetAddrOfConstantStringLiteral.
406   std::string GetStringForStringLiteral(const StringLiteral *E);
407 
408   /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object
409   /// for the given string.
410   llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
411 
412   /// GetAddrOfConstantString - Return a pointer to a constant NSString object
413   /// for the given string. Or a user defined String object as defined via
414   /// -fconstant-string-class=class_name option.
415   llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
416 
417   /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array
418   /// for the given string literal.
419   llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
420 
421   /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
422   /// array for the given ObjCEncodeExpr node.
423   llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
424 
425   /// GetAddrOfConstantString - Returns a pointer to a character array
426   /// containing the literal. This contents are exactly that of the given
427   /// string, i.e. it will not be null terminated automatically; see
428   /// GetAddrOfConstantCString. Note that whether the result is actually a
429   /// pointer to an LLVM constant depends on Feature.WriteableStrings.
430   ///
431   /// The result has pointer to array type.
432   ///
433   /// \param GlobalName If provided, the name to use for the global
434   /// (if one is created).
435   llvm::Constant *GetAddrOfConstantString(const std::string& str,
436                                           const char *GlobalName=0);
437 
438   /// GetAddrOfConstantCString - Returns a pointer to a character array
439   /// containing the literal and a terminating '\0' character. The result has
440   /// pointer to array type.
441   ///
442   /// \param GlobalName If provided, the name to use for the global (if one is
443   /// created).
444   llvm::Constant *GetAddrOfConstantCString(const std::string &str,
445                                            const char *GlobalName=0);
446 
447   /// GetAddrOfCXXConstructor - Return the address of the constructor of the
448   /// given type.
449   llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
450                                              CXXCtorType Type);
451 
452   /// GetAddrOfCXXDestructor - Return the address of the constructor of the
453   /// given type.
454   llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
455                                             CXXDtorType Type);
456 
457   /// getBuiltinLibFunction - Given a builtin id for a function like
458   /// "__builtin_fabsf", return a Function* for "fabsf".
459   llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
460                                      unsigned BuiltinID);
461 
462   llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
463                                unsigned NumTys = 0);
464 
465   /// EmitTopLevelDecl - Emit code for a single top level declaration.
466   void EmitTopLevelDecl(Decl *D);
467 
468   /// AddUsedGlobal - Add a global which should be forced to be
469   /// present in the object file; these are emitted to the llvm.used
470   /// metadata global.
471   void AddUsedGlobal(llvm::GlobalValue *GV);
472 
473   void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
474 
475   /// AddCXXDtorEntry - Add a destructor and object to add to the C++ global
476   /// destructor function.
477   void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
478     CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
479   }
480 
481   /// CreateRuntimeFunction - Create a new runtime function with the specified
482   /// type and name.
483   llvm::Constant *CreateRuntimeFunction(const llvm::FunctionType *Ty,
484                                         llvm::StringRef Name);
485   /// CreateRuntimeVariable - Create a new runtime global variable with the
486   /// specified type and name.
487   llvm::Constant *CreateRuntimeVariable(const llvm::Type *Ty,
488                                         llvm::StringRef Name);
489 
490   ///@name Custom Blocks Runtime Interfaces
491   ///@{
492 
493   llvm::Constant *getNSConcreteGlobalBlock();
494   llvm::Constant *getNSConcreteStackBlock();
495   llvm::Constant *getBlockObjectAssign();
496   llvm::Constant *getBlockObjectDispose();
497 
498   ///@}
499 
500   void UpdateCompletedType(const TagDecl *TD) {
501     // Make sure that this type is translated.
502     Types.UpdateCompletedType(TD);
503   }
504 
505   llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
506 
507   /// EmitConstantExpr - Try to emit the given expression as a
508   /// constant; returns 0 if the expression cannot be emitted as a
509   /// constant.
510   llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
511                                    CodeGenFunction *CGF = 0);
512 
513   /// EmitNullConstant - Return the result of value-initializing the given
514   /// type, i.e. a null expression of the given type.  This is usually,
515   /// but not always, an LLVM null constant.
516   llvm::Constant *EmitNullConstant(QualType T);
517 
518   llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
519                                    const AnnotateAttr *AA, unsigned LineNo);
520 
521   /// ErrorUnsupported - Print out an error that codegen doesn't support the
522   /// specified stmt yet.
523   /// \param OmitOnError - If true, then this error should only be emitted if no
524   /// other errors have been reported.
525   void ErrorUnsupported(const Stmt *S, const char *Type,
526                         bool OmitOnError=false);
527 
528   /// ErrorUnsupported - Print out an error that codegen doesn't support the
529   /// specified decl yet.
530   /// \param OmitOnError - If true, then this error should only be emitted if no
531   /// other errors have been reported.
532   void ErrorUnsupported(const Decl *D, const char *Type,
533                         bool OmitOnError=false);
534 
535   /// SetInternalFunctionAttributes - Set the attributes on the LLVM
536   /// function for the given decl and function info. This applies
537   /// attributes necessary for handling the ABI as well as user
538   /// specified attributes like section.
539   void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
540                                      const CGFunctionInfo &FI);
541 
542   /// SetLLVMFunctionAttributes - Set the LLVM function attributes
543   /// (sext, zext, etc).
544   void SetLLVMFunctionAttributes(const Decl *D,
545                                  const CGFunctionInfo &Info,
546                                  llvm::Function *F);
547 
548   /// SetLLVMFunctionAttributesForDefinition - Set the LLVM function attributes
549   /// which only apply to a function definintion.
550   void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
551 
552   /// ReturnTypeUsesSRet - Return true iff the given type uses 'sret' when used
553   /// as a return type.
554   bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
555 
556   /// ReturnTypeUsesSret - Return true iff the given type uses 'fpret' when used
557   /// as a return type.
558   bool ReturnTypeUsesFPRet(QualType ResultType);
559 
560   /// ConstructAttributeList - Get the LLVM attributes and calling convention to
561   /// use for a particular function type.
562   ///
563   /// \param Info - The function type information.
564   /// \param TargetDecl - The decl these attributes are being constructed
565   /// for. If supplied the attributes applied to this decl may contribute to the
566   /// function attributes and calling convention.
567   /// \param PAL [out] - On return, the attribute list to use.
568   /// \param CallingConv [out] - On return, the LLVM calling convention to use.
569   void ConstructAttributeList(const CGFunctionInfo &Info,
570                               const Decl *TargetDecl,
571                               AttributeListType &PAL,
572                               unsigned &CallingConv);
573 
574   llvm::StringRef getMangledName(GlobalDecl GD);
575   void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
576                            const BlockDecl *BD);
577 
578   void EmitTentativeDefinition(const VarDecl *D);
579 
580   void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
581 
582   llvm::GlobalVariable::LinkageTypes
583   getFunctionLinkage(const FunctionDecl *FD);
584 
585   void setFunctionLinkage(const FunctionDecl *FD, llvm::GlobalValue *V) {
586     V->setLinkage(getFunctionLinkage(FD));
587   }
588 
589   /// getVTableLinkage - Return the appropriate linkage for the vtable, VTT,
590   /// and type information of the given class.
591   llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
592 
593   /// GetTargetTypeStoreSize - Return the store size, in character units, of
594   /// the given LLVM type.
595   CharUnits GetTargetTypeStoreSize(const llvm::Type *Ty) const;
596 
597   /// GetLLVMLinkageVarDefinition - Returns LLVM linkage for a global
598   /// variable.
599   llvm::GlobalValue::LinkageTypes
600   GetLLVMLinkageVarDefinition(const VarDecl *D,
601                               llvm::GlobalVariable *GV);
602 
603   std::vector<const CXXRecordDecl*> DeferredVTables;
604 
605 private:
606   llvm::GlobalValue *GetGlobalValue(llvm::StringRef Ref);
607 
608   llvm::Constant *GetOrCreateLLVMFunction(llvm::StringRef MangledName,
609                                           const llvm::Type *Ty,
610                                           GlobalDecl D,
611                                           bool ForVTable);
612   llvm::Constant *GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
613                                         const llvm::PointerType *PTy,
614                                         const VarDecl *D,
615                                         bool UnnamedAddr = false);
616 
617   /// SetCommonAttributes - Set attributes which are common to any
618   /// form of a global definition (alias, Objective-C method,
619   /// function, global variable).
620   ///
621   /// NOTE: This should only be called for definitions.
622   void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
623 
624   /// SetFunctionDefinitionAttributes - Set attributes for a global definition.
625   void SetFunctionDefinitionAttributes(const FunctionDecl *D,
626                                        llvm::GlobalValue *GV);
627 
628   /// SetFunctionAttributes - Set function attributes for a function
629   /// declaration.
630   void SetFunctionAttributes(GlobalDecl GD,
631                              llvm::Function *F,
632                              bool IsIncompleteFunction);
633 
634   /// EmitGlobal - Emit code for a singal global function or var decl. Forward
635   /// declarations are emitted lazily.
636   void EmitGlobal(GlobalDecl D);
637 
638   void EmitGlobalDefinition(GlobalDecl D);
639 
640   void EmitGlobalFunctionDefinition(GlobalDecl GD);
641   void EmitGlobalVarDefinition(const VarDecl *D);
642   void EmitAliasDefinition(GlobalDecl GD);
643   void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
644   void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
645 
646   // C++ related functions.
647 
648   bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target);
649   bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
650 
651   void EmitNamespace(const NamespaceDecl *D);
652   void EmitLinkageSpec(const LinkageSpecDecl *D);
653 
654   /// EmitCXXConstructors - Emit constructors (base, complete) from a
655   /// C++ constructor Decl.
656   void EmitCXXConstructors(const CXXConstructorDecl *D);
657 
658   /// EmitCXXConstructor - Emit a single constructor with the given type from
659   /// a C++ constructor Decl.
660   void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
661 
662   /// EmitCXXDestructors - Emit destructors (base, complete) from a
663   /// C++ destructor Decl.
664   void EmitCXXDestructors(const CXXDestructorDecl *D);
665 
666   /// EmitCXXDestructor - Emit a single destructor with the given type from
667   /// a C++ destructor Decl.
668   void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
669 
670   /// EmitCXXGlobalInitFunc - Emit the function that initializes C++ globals.
671   void EmitCXXGlobalInitFunc();
672 
673   /// EmitCXXGlobalDtorFunc - Emit the function that destroys C++ globals.
674   void EmitCXXGlobalDtorFunc();
675 
676   void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
677                                     llvm::GlobalVariable *Addr);
678 
679   // FIXME: Hardcoding priority here is gross.
680   void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
681   void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
682 
683   /// EmitCtorList - Generates a global array of functions and priorities using
684   /// the given list and name. This array will have appending linkage and is
685   /// suitable for use as a LLVM constructor or destructor array.
686   void EmitCtorList(const CtorList &Fns, const char *GlobalName);
687 
688   void EmitAnnotations(void);
689 
690   /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the
691   /// given type.
692   void EmitFundamentalRTTIDescriptor(QualType Type);
693 
694   /// EmitFundamentalRTTIDescriptors - Emit the RTTI descriptors for the
695   /// builtin types.
696   void EmitFundamentalRTTIDescriptors();
697 
698   /// EmitDeferred - Emit any needed decls for which code generation
699   /// was deferred.
700   void EmitDeferred(void);
701 
702   /// EmitLLVMUsed - Emit the llvm.used metadata used to force
703   /// references to global which may otherwise be optimized out.
704   void EmitLLVMUsed(void);
705 
706   void EmitDeclMetadata();
707 
708   /// MayDeferGeneration - Determine if the given decl can be emitted
709   /// lazily; this is only relevant for definitions. The given decl
710   /// must be either a function or var decl.
711   bool MayDeferGeneration(const ValueDecl *D);
712 
713   /// SimplifyPersonality - Check whether we can use a "simpler", more
714   /// core exceptions personality function.
715   void SimplifyPersonality();
716 };
717 }  // end namespace CodeGen
718 }  // end namespace clang
719 
720 #endif
721