CodeGenModule.h 31 KB

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