CGBlocks.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 Value;
  27. }
  28. namespace clang {
  29. namespace CodeGen {
  30. class CGBlockInfo;
  31. // Flags stored in __block variables.
  32. enum BlockByrefFlags {
  33. BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
  34. BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
  35. BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
  36. BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
  37. BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
  38. BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
  39. BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
  40. };
  41. enum BlockLiteralFlags {
  42. BLOCK_IS_NOESCAPE = (1 << 23),
  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. BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
  49. };
  50. class BlockFlags {
  51. uint32_t flags;
  52. public:
  53. BlockFlags(uint32_t flags) : flags(flags) {}
  54. BlockFlags() : flags(0) {}
  55. BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
  56. BlockFlags(BlockByrefFlags flag) : flags(flag) {}
  57. uint32_t getBitMask() const { return flags; }
  58. bool empty() const { return flags == 0; }
  59. friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
  60. return BlockFlags(l.flags | r.flags);
  61. }
  62. friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
  63. l.flags |= r.flags;
  64. return l;
  65. }
  66. friend bool operator&(BlockFlags l, BlockFlags r) {
  67. return (l.flags & r.flags);
  68. }
  69. bool operator==(BlockFlags r) {
  70. return (flags == r.flags);
  71. }
  72. };
  73. inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
  74. return BlockFlags(l) | BlockFlags(r);
  75. }
  76. enum BlockFieldFlag_t {
  77. BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
  78. block, ... */
  79. BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
  80. BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
  81. variable */
  82. BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
  83. helpers */
  84. BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
  85. BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
  86. support routines */
  87. BLOCK_BYREF_CURRENT_MAX = 256
  88. };
  89. class BlockFieldFlags {
  90. uint32_t flags;
  91. BlockFieldFlags(uint32_t flags) : flags(flags) {}
  92. public:
  93. BlockFieldFlags() : flags(0) {}
  94. BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
  95. uint32_t getBitMask() const { return flags; }
  96. bool empty() const { return flags == 0; }
  97. /// Answers whether the flags indicate that this field is an object
  98. /// or block pointer that requires _Block_object_assign/dispose.
  99. bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
  100. friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
  101. return BlockFieldFlags(l.flags | r.flags);
  102. }
  103. friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
  104. l.flags |= r.flags;
  105. return l;
  106. }
  107. friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
  108. return (l.flags & r.flags);
  109. }
  110. bool operator==(BlockFieldFlags Other) const {
  111. return flags == Other.flags;
  112. }
  113. };
  114. inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
  115. return BlockFieldFlags(l) | BlockFieldFlags(r);
  116. }
  117. /// Information about the layout of a __block variable.
  118. class BlockByrefInfo {
  119. public:
  120. llvm::StructType *Type;
  121. unsigned FieldIndex;
  122. CharUnits ByrefAlignment;
  123. CharUnits FieldOffset;
  124. };
  125. /// Represents a type of copy/destroy operation that should be performed for an
  126. /// entity that's captured by a block.
  127. enum class BlockCaptureEntityKind {
  128. None,
  129. CXXRecord, // Copy or destroy
  130. ARCWeak,
  131. ARCStrong,
  132. NonTrivialCStruct,
  133. BlockObject, // Assign or release
  134. };
  135. /// CGBlockInfo - Information to generate a block literal.
  136. class CGBlockInfo {
  137. public:
  138. /// Name - The name of the block, kindof.
  139. StringRef Name;
  140. /// The field index of 'this' within the block, if there is one.
  141. unsigned CXXThisIndex;
  142. class Capture {
  143. uintptr_t Data;
  144. EHScopeStack::stable_iterator Cleanup;
  145. CharUnits::QuantityType Offset;
  146. /// Type of the capture field. Normally, this is identical to the type of
  147. /// the capture's VarDecl, but can be different if there is an enclosing
  148. /// lambda.
  149. QualType FieldType;
  150. public:
  151. bool isIndex() const { return (Data & 1) != 0; }
  152. bool isConstant() const { return !isIndex(); }
  153. unsigned getIndex() const {
  154. assert(isIndex());
  155. return Data >> 1;
  156. }
  157. CharUnits getOffset() const {
  158. assert(isIndex());
  159. return CharUnits::fromQuantity(Offset);
  160. }
  161. EHScopeStack::stable_iterator getCleanup() const {
  162. assert(isIndex());
  163. return Cleanup;
  164. }
  165. void setCleanup(EHScopeStack::stable_iterator cleanup) {
  166. assert(isIndex());
  167. Cleanup = cleanup;
  168. }
  169. llvm::Value *getConstant() const {
  170. assert(isConstant());
  171. return reinterpret_cast<llvm::Value*>(Data);
  172. }
  173. QualType fieldType() const {
  174. return FieldType;
  175. }
  176. static Capture
  177. makeIndex(unsigned index, CharUnits offset, QualType FieldType,
  178. BlockCaptureEntityKind CopyKind, BlockFieldFlags CopyFlags,
  179. BlockCaptureEntityKind DisposeKind, BlockFieldFlags DisposeFlags,
  180. const BlockDecl::Capture *Cap) {
  181. Capture v;
  182. v.Data = (index << 1) | 1;
  183. v.Offset = offset.getQuantity();
  184. v.FieldType = FieldType;
  185. v.CopyKind = CopyKind;
  186. v.CopyFlags = CopyFlags;
  187. v.DisposeKind = DisposeKind;
  188. v.DisposeFlags = DisposeFlags;
  189. v.Cap = Cap;
  190. return v;
  191. }
  192. static Capture makeConstant(llvm::Value *value,
  193. const BlockDecl::Capture *Cap) {
  194. Capture v;
  195. v.Data = reinterpret_cast<uintptr_t>(value);
  196. v.Cap = Cap;
  197. return v;
  198. }
  199. bool isConstantOrTrivial() const {
  200. return CopyKind == BlockCaptureEntityKind::None &&
  201. DisposeKind == BlockCaptureEntityKind::None;
  202. }
  203. BlockCaptureEntityKind CopyKind = BlockCaptureEntityKind::None,
  204. DisposeKind = BlockCaptureEntityKind::None;
  205. BlockFieldFlags CopyFlags, DisposeFlags;
  206. const BlockDecl::Capture *Cap;
  207. };
  208. /// CanBeGlobal - True if the block can be global, i.e. it has
  209. /// no non-constant captures.
  210. bool CanBeGlobal : 1;
  211. /// True if the block has captures that would necessitate custom copy or
  212. /// dispose helper functions if the block were escaping.
  213. bool NeedsCopyDispose : 1;
  214. /// Indicates whether the block is non-escaping.
  215. bool NoEscape : 1;
  216. /// HasCXXObject - True if the block's custom copy/dispose functions
  217. /// need to be run even in GC mode.
  218. bool HasCXXObject : 1;
  219. /// UsesStret : True if the block uses an stret return. Mutable
  220. /// because it gets set later in the block-creation process.
  221. mutable bool UsesStret : 1;
  222. /// HasCapturedVariableLayout : True if block has captured variables
  223. /// and their layout meta-data has been generated.
  224. bool HasCapturedVariableLayout : 1;
  225. /// Indicates whether an object of a non-external C++ class is captured. This
  226. /// bit is used to determine the linkage of the block copy/destroy helper
  227. /// functions.
  228. bool CapturesNonExternalType : 1;
  229. /// Mapping from variables to pointers to captures in SortedCaptures.
  230. llvm::DenseMap<const VarDecl *, Capture *> Captures;
  231. /// The block's captures. Non-constant captures are sorted by their offsets.
  232. llvm::SmallVector<Capture, 4> SortedCaptures;
  233. Address LocalAddress;
  234. llvm::StructType *StructureType;
  235. const BlockDecl *Block;
  236. const BlockExpr *BlockExpression;
  237. CharUnits BlockSize;
  238. CharUnits BlockAlign;
  239. CharUnits CXXThisOffset;
  240. // Offset of the gap caused by block header having a smaller
  241. // alignment than the alignment of the block descriptor. This
  242. // is the gap offset before the first capturued field.
  243. CharUnits BlockHeaderForcedGapOffset;
  244. // Gap size caused by aligning first field after block header.
  245. // This could be zero if no forced alignment is required.
  246. CharUnits BlockHeaderForcedGapSize;
  247. /// The next block in the block-info chain. Invalid if this block
  248. /// info is not part of the CGF's block-info chain, which is true
  249. /// if it corresponds to a global block or a block whose expression
  250. /// has been encountered.
  251. CGBlockInfo *NextBlockInfo;
  252. void buildCaptureMap() {
  253. for (auto &C : SortedCaptures)
  254. Captures[C.Cap->getVariable()] = &C;
  255. }
  256. const Capture &getCapture(const VarDecl *var) const {
  257. return const_cast<CGBlockInfo*>(this)->getCapture(var);
  258. }
  259. Capture &getCapture(const VarDecl *var) {
  260. auto it = Captures.find(var);
  261. assert(it != Captures.end() && "no entry for variable!");
  262. return *it->second;
  263. }
  264. const BlockDecl *getBlockDecl() const { return Block; }
  265. const BlockExpr *getBlockExpr() const {
  266. assert(BlockExpression);
  267. assert(BlockExpression->getBlockDecl() == Block);
  268. return BlockExpression;
  269. }
  270. CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
  271. };
  272. } // end namespace CodeGen
  273. } // end namespace clang
  274. #endif