CodeGenModule.h 35 KB

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