CGBlocks.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- 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 state used for llvm translation for block literals.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef CLANG_CODEGEN_CGBLOCKS_H
  14. #define CLANG_CODEGEN_CGBLOCKS_H
  15. #include "CodeGenTypes.h"
  16. #include "clang/AST/Type.h"
  17. #include "llvm/Module.h"
  18. #include "clang/Basic/TargetInfo.h"
  19. #include "clang/AST/CharUnits.h"
  20. #include "clang/AST/Expr.h"
  21. #include "clang/AST/ExprCXX.h"
  22. #include "clang/AST/ExprObjC.h"
  23. #include "CodeGenFunction.h"
  24. #include "CGBuilder.h"
  25. #include "CGCall.h"
  26. #include "CGValue.h"
  27. namespace llvm {
  28. class Module;
  29. class Constant;
  30. class Function;
  31. class GlobalValue;
  32. class TargetData;
  33. class FunctionType;
  34. class PointerType;
  35. class Value;
  36. class LLVMContext;
  37. }
  38. namespace clang {
  39. namespace CodeGen {
  40. class CodeGenModule;
  41. class CGBlockInfo;
  42. enum BlockFlag_t {
  43. BLOCK_HAS_COPY_DISPOSE = (1 << 25),
  44. BLOCK_HAS_CXX_OBJ = (1 << 26),
  45. BLOCK_IS_GLOBAL = (1 << 28),
  46. BLOCK_USE_STRET = (1 << 29),
  47. BLOCK_HAS_SIGNATURE = (1 << 30)
  48. };
  49. class BlockFlags {
  50. uint32_t flags;
  51. BlockFlags(uint32_t flags) : flags(flags) {}
  52. public:
  53. BlockFlags() : flags(0) {}
  54. BlockFlags(BlockFlag_t flag) : flags(flag) {}
  55. uint32_t getBitMask() const { return flags; }
  56. bool empty() const { return flags == 0; }
  57. friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
  58. return BlockFlags(l.flags | r.flags);
  59. }
  60. friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
  61. l.flags |= r.flags;
  62. return l;
  63. }
  64. friend bool operator&(BlockFlags l, BlockFlags r) {
  65. return (l.flags & r.flags);
  66. }
  67. };
  68. inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) {
  69. return BlockFlags(l) | BlockFlags(r);
  70. }
  71. enum BlockFieldFlag_t {
  72. BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
  73. block, ... */
  74. BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
  75. BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
  76. variable */
  77. BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
  78. helpers */
  79. BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
  80. BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
  81. support routines */
  82. BLOCK_BYREF_CURRENT_MAX = 256
  83. };
  84. class BlockFieldFlags {
  85. uint32_t flags;
  86. BlockFieldFlags(uint32_t flags) : flags(flags) {}
  87. public:
  88. BlockFieldFlags() : flags(0) {}
  89. BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
  90. uint32_t getBitMask() const { return flags; }
  91. bool empty() const { return flags == 0; }
  92. /// Answers whether the flags indicate that this field is an object
  93. /// or block pointer that requires _Block_object_assign/dispose.
  94. bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
  95. friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
  96. return BlockFieldFlags(l.flags | r.flags);
  97. }
  98. friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
  99. l.flags |= r.flags;
  100. return l;
  101. }
  102. friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
  103. return (l.flags & r.flags);
  104. }
  105. };
  106. inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
  107. return BlockFieldFlags(l) | BlockFieldFlags(r);
  108. }
  109. /// CGBlockInfo - Information to generate a block literal.
  110. class CGBlockInfo {
  111. public:
  112. /// Name - The name of the block, kindof.
  113. llvm::StringRef Name;
  114. /// The field index of 'this' within the block, if there is one.
  115. unsigned CXXThisIndex;
  116. class Capture {
  117. uintptr_t Data;
  118. EHScopeStack::stable_iterator Cleanup;
  119. public:
  120. bool isIndex() const { return (Data & 1) != 0; }
  121. bool isConstant() const { return !isIndex(); }
  122. unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
  123. llvm::Value *getConstant() const {
  124. assert(isConstant());
  125. return reinterpret_cast<llvm::Value*>(Data);
  126. }
  127. EHScopeStack::stable_iterator getCleanup() const {
  128. assert(isIndex());
  129. return Cleanup;
  130. }
  131. void setCleanup(EHScopeStack::stable_iterator cleanup) {
  132. assert(isIndex());
  133. Cleanup = cleanup;
  134. }
  135. static Capture makeIndex(unsigned index) {
  136. Capture v;
  137. v.Data = (index << 1) | 1;
  138. return v;
  139. }
  140. static Capture makeConstant(llvm::Value *value) {
  141. Capture v;
  142. v.Data = reinterpret_cast<uintptr_t>(value);
  143. return v;
  144. }
  145. };
  146. /// CanBeGlobal - True if the block can be global, i.e. it has
  147. /// no non-constant captures.
  148. bool CanBeGlobal : 1;
  149. /// True if the block needs a custom copy or dispose function.
  150. bool NeedsCopyDispose : 1;
  151. /// HasCXXObject - True if the block's custom copy/dispose functions
  152. /// need to be run even in GC mode.
  153. bool HasCXXObject : 1;
  154. /// UsesStret : True if the block uses an stret return. Mutable
  155. /// because it gets set later in the block-creation process.
  156. mutable bool UsesStret : 1;
  157. /// The mapping of allocated indexes within the block.
  158. llvm::DenseMap<const VarDecl*, Capture> Captures;
  159. llvm::AllocaInst *Address;
  160. llvm::StructType *StructureType;
  161. const BlockDecl *Block;
  162. const BlockExpr *BlockExpression;
  163. CharUnits BlockSize;
  164. CharUnits BlockAlign;
  165. /// An instruction which dominates the full-expression that the
  166. /// block is inside.
  167. llvm::Instruction *DominatingIP;
  168. /// The next block in the block-info chain. Invalid if this block
  169. /// info is not part of the CGF's block-info chain, which is true
  170. /// if it corresponds to a global block or a block whose expression
  171. /// has been encountered.
  172. CGBlockInfo *NextBlockInfo;
  173. const Capture &getCapture(const VarDecl *var) const {
  174. return const_cast<CGBlockInfo*>(this)->getCapture(var);
  175. }
  176. Capture &getCapture(const VarDecl *var) {
  177. llvm::DenseMap<const VarDecl*, Capture>::iterator
  178. it = Captures.find(var);
  179. assert(it != Captures.end() && "no entry for variable!");
  180. return it->second;
  181. }
  182. const BlockDecl *getBlockDecl() const { return Block; }
  183. const BlockExpr *getBlockExpr() const {
  184. assert(BlockExpression);
  185. assert(BlockExpression->getBlockDecl() == Block);
  186. return BlockExpression;
  187. }
  188. CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
  189. };
  190. } // end namespace CodeGen
  191. } // end namespace clang
  192. #endif