CGBlocks.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is the internal state used for llvm translation for block literals.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
  14. #include "CGBuilder.h"
  15. #include "CGCall.h"
  16. #include "CGValue.h"
  17. #include "CodeGenFunction.h"
  18. #include "CodeGenTypes.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 "clang/AST/Type.h"
  24. #include "clang/Basic/TargetInfo.h"
  25. namespace llvm {
  26. class Constant;
  27. class Function;
  28. class GlobalValue;
  29. class DataLayout;
  30. class FunctionType;
  31. class PointerType;
  32. class Value;
  33. class LLVMContext;
  34. }
  35. namespace clang {
  36. namespace CodeGen {
  37. class CGBlockInfo;
  38. // Flags stored in __block variables.
  39. enum BlockByrefFlags {
  40. BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
  41. BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
  42. BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
  43. BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
  44. BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
  45. BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
  46. BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
  47. };
  48. enum BlockLiteralFlags {
  49. BLOCK_IS_NOESCAPE = (1 << 23),
  50. BLOCK_HAS_COPY_DISPOSE = (1 << 25),
  51. BLOCK_HAS_CXX_OBJ = (1 << 26),
  52. BLOCK_IS_GLOBAL = (1 << 28),
  53. BLOCK_USE_STRET = (1 << 29),
  54. BLOCK_HAS_SIGNATURE = (1 << 30),
  55. BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
  56. };
  57. class BlockFlags {
  58. uint32_t flags;
  59. public:
  60. BlockFlags(uint32_t flags) : flags(flags) {}
  61. BlockFlags() : flags(0) {}
  62. BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
  63. BlockFlags(BlockByrefFlags flag) : flags(flag) {}
  64. uint32_t getBitMask() const { return flags; }
  65. bool empty() const { return flags == 0; }
  66. friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
  67. return BlockFlags(l.flags | r.flags);
  68. }
  69. friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
  70. l.flags |= r.flags;
  71. return l;
  72. }
  73. friend bool operator&(BlockFlags l, BlockFlags r) {
  74. return (l.flags & r.flags);
  75. }
  76. bool operator==(BlockFlags r) {
  77. return (flags == r.flags);
  78. }
  79. };
  80. inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
  81. return BlockFlags(l) | BlockFlags(r);
  82. }
  83. enum BlockFieldFlag_t {
  84. BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
  85. block, ... */
  86. BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
  87. BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
  88. variable */
  89. BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
  90. helpers */
  91. BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
  92. BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
  93. support routines */
  94. BLOCK_BYREF_CURRENT_MAX = 256
  95. };
  96. class BlockFieldFlags {
  97. uint32_t flags;
  98. BlockFieldFlags(uint32_t flags) : flags(flags) {}
  99. public:
  100. BlockFieldFlags() : flags(0) {}
  101. BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
  102. uint32_t getBitMask() const { return flags; }
  103. bool empty() const { return flags == 0; }
  104. /// Answers whether the flags indicate that this field is an object
  105. /// or block pointer that requires _Block_object_assign/dispose.
  106. bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
  107. friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
  108. return BlockFieldFlags(l.flags | r.flags);
  109. }
  110. friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
  111. l.flags |= r.flags;
  112. return l;
  113. }
  114. friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
  115. return (l.flags & r.flags);
  116. }
  117. bool operator==(BlockFieldFlags Other) const {
  118. return flags == Other.flags;
  119. }
  120. };
  121. inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
  122. return BlockFieldFlags(l) | BlockFieldFlags(r);
  123. }
  124. /// Information about the layout of a __block variable.
  125. class BlockByrefInfo {
  126. public:
  127. llvm::StructType *Type;
  128. unsigned FieldIndex;
  129. CharUnits ByrefAlignment;
  130. CharUnits FieldOffset;
  131. };
  132. /// CGBlockInfo - Information to generate a block literal.
  133. class CGBlockInfo {
  134. public:
  135. /// Name - The name of the block, kindof.
  136. StringRef Name;
  137. /// The field index of 'this' within the block, if there is one.
  138. unsigned CXXThisIndex;
  139. class Capture {
  140. uintptr_t Data;
  141. EHScopeStack::stable_iterator Cleanup;
  142. CharUnits::QuantityType Offset;
  143. /// Type of the capture field. Normally, this is identical to the type of
  144. /// the capture's VarDecl, but can be different if there is an enclosing
  145. /// lambda.
  146. QualType FieldType;
  147. public:
  148. bool isIndex() const { return (Data & 1) != 0; }
  149. bool isConstant() const { return !isIndex(); }
  150. unsigned getIndex() const {
  151. assert(isIndex());
  152. return Data >> 1;
  153. }
  154. CharUnits getOffset() const {
  155. assert(isIndex());
  156. return CharUnits::fromQuantity(Offset);
  157. }
  158. EHScopeStack::stable_iterator getCleanup() const {
  159. assert(isIndex());
  160. return Cleanup;
  161. }
  162. void setCleanup(EHScopeStack::stable_iterator cleanup) {
  163. assert(isIndex());
  164. Cleanup = cleanup;
  165. }
  166. llvm::Value *getConstant() const {
  167. assert(isConstant());
  168. return reinterpret_cast<llvm::Value*>(Data);
  169. }
  170. QualType fieldType() const {
  171. return FieldType;
  172. }
  173. static Capture makeIndex(unsigned index, CharUnits offset,
  174. QualType FieldType) {
  175. Capture v;
  176. v.Data = (index << 1) | 1;
  177. v.Offset = offset.getQuantity();
  178. v.FieldType = FieldType;
  179. return v;
  180. }
  181. static Capture makeConstant(llvm::Value *value) {
  182. Capture v;
  183. v.Data = reinterpret_cast<uintptr_t>(value);
  184. return v;
  185. }
  186. };
  187. /// CanBeGlobal - True if the block can be global, i.e. it has
  188. /// no non-constant captures.
  189. bool CanBeGlobal : 1;
  190. /// True if the block has captures that would necessitate custom copy or
  191. /// dispose helper functions if the block were escaping.
  192. bool NeedsCopyDispose : 1;
  193. /// HasCXXObject - True if the block's custom copy/dispose functions
  194. /// need to be run even in GC mode.
  195. bool HasCXXObject : 1;
  196. /// UsesStret : True if the block uses an stret return. Mutable
  197. /// because it gets set later in the block-creation process.
  198. mutable bool UsesStret : 1;
  199. /// HasCapturedVariableLayout : True if block has captured variables
  200. /// and their layout meta-data has been generated.
  201. bool HasCapturedVariableLayout : 1;
  202. /// Indicates whether an object of a non-external C++ class is captured. This
  203. /// bit is used to determine the linkage of the block copy/destroy helper
  204. /// functions.
  205. bool CapturesNonExternalType : 1;
  206. /// The mapping of allocated indexes within the block.
  207. llvm::DenseMap<const VarDecl*, Capture> Captures;
  208. Address LocalAddress;
  209. llvm::StructType *StructureType;
  210. const BlockDecl *Block;
  211. const BlockExpr *BlockExpression;
  212. CharUnits BlockSize;
  213. CharUnits BlockAlign;
  214. CharUnits CXXThisOffset;
  215. // Offset of the gap caused by block header having a smaller
  216. // alignment than the alignment of the block descriptor. This
  217. // is the gap offset before the first capturued field.
  218. CharUnits BlockHeaderForcedGapOffset;
  219. // Gap size caused by aligning first field after block header.
  220. // This could be zero if no forced alignment is required.
  221. CharUnits BlockHeaderForcedGapSize;
  222. /// An instruction which dominates the full-expression that the
  223. /// block is inside.
  224. llvm::Instruction *DominatingIP;
  225. /// The next block in the block-info chain. Invalid if this block
  226. /// info is not part of the CGF's block-info chain, which is true
  227. /// if it corresponds to a global block or a block whose expression
  228. /// has been encountered.
  229. CGBlockInfo *NextBlockInfo;
  230. const Capture &getCapture(const VarDecl *var) const {
  231. return const_cast<CGBlockInfo*>(this)->getCapture(var);
  232. }
  233. Capture &getCapture(const VarDecl *var) {
  234. llvm::DenseMap<const VarDecl*, Capture>::iterator
  235. it = Captures.find(var);
  236. assert(it != Captures.end() && "no entry for variable!");
  237. return it->second;
  238. }
  239. const BlockDecl *getBlockDecl() const { return Block; }
  240. const BlockExpr *getBlockExpr() const {
  241. assert(BlockExpression);
  242. assert(BlockExpression->getBlockDecl() == Block);
  243. return BlockExpression;
  244. }
  245. CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
  246. // Indicates whether the block needs a custom copy or dispose function.
  247. bool needsCopyDisposeHelpers() const {
  248. return NeedsCopyDispose && !Block->doesNotEscape();
  249. }
  250. };
  251. } // end namespace CodeGen
  252. } // end namespace clang
  253. #endif