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