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