1 //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the internal state used for llvm translation for block literals.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
14 #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
16 #include "CGBuilder.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenTypes.h"
21 #include "clang/AST/CharUnits.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
44 // Flags stored in __block variables.
45 enum BlockByrefFlags {
46 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
47 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
48 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
49 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
50 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
51 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
52 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
55 enum BlockLiteralFlags {
56 BLOCK_IS_NOESCAPE = (1 << 23),
57 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
58 BLOCK_HAS_CXX_OBJ = (1 << 26),
59 BLOCK_IS_GLOBAL = (1 << 28),
60 BLOCK_USE_STRET = (1 << 29),
61 BLOCK_HAS_SIGNATURE = (1 << 30),
62 BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
68 BlockFlags(uint32_t flags) : flags(flags) {}
69 BlockFlags() : flags(0) {}
70 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
71 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
73 uint32_t getBitMask() const { return flags; }
74 bool empty() const { return flags == 0; }
76 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
77 return BlockFlags(l.flags | r.flags);
79 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
83 friend bool operator&(BlockFlags l, BlockFlags r) {
84 return (l.flags & r.flags);
86 bool operator==(BlockFlags r) {
87 return (flags == r.flags);
90 inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
91 return BlockFlags(l) | BlockFlags(r);
94 enum BlockFieldFlag_t {
95 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
97 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
99 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
101 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
103 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
104 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
106 BLOCK_BYREF_CURRENT_MAX = 256
109 class BlockFieldFlags {
112 BlockFieldFlags(uint32_t flags) : flags(flags) {}
114 BlockFieldFlags() : flags(0) {}
115 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
117 uint32_t getBitMask() const { return flags; }
118 bool empty() const { return flags == 0; }
120 /// Answers whether the flags indicate that this field is an object
121 /// or block pointer that requires _Block_object_assign/dispose.
122 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
124 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
125 return BlockFieldFlags(l.flags | r.flags);
127 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
131 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
132 return (l.flags & r.flags);
134 bool operator==(BlockFieldFlags Other) const {
135 return flags == Other.flags;
138 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
139 return BlockFieldFlags(l) | BlockFieldFlags(r);
142 /// Information about the layout of a __block variable.
143 class BlockByrefInfo {
145 llvm::StructType *Type;
147 CharUnits ByrefAlignment;
148 CharUnits FieldOffset;
151 /// CGBlockInfo - Information to generate a block literal.
154 /// Name - The name of the block, kindof.
157 /// The field index of 'this' within the block, if there is one.
158 unsigned CXXThisIndex;
162 EHScopeStack::stable_iterator Cleanup;
163 CharUnits::QuantityType Offset;
165 /// Type of the capture field. Normally, this is identical to the type of
166 /// the capture's VarDecl, but can be different if there is an enclosing
171 bool isIndex() const { return (Data & 1) != 0; }
172 bool isConstant() const { return !isIndex(); }
174 unsigned getIndex() const {
178 CharUnits getOffset() const {
180 return CharUnits::fromQuantity(Offset);
182 EHScopeStack::stable_iterator getCleanup() const {
186 void setCleanup(EHScopeStack::stable_iterator cleanup) {
191 llvm::Value *getConstant() const {
192 assert(isConstant());
193 return reinterpret_cast<llvm::Value*>(Data);
196 QualType fieldType() const {
200 static Capture makeIndex(unsigned index, CharUnits offset,
201 QualType FieldType) {
203 v.Data = (index << 1) | 1;
204 v.Offset = offset.getQuantity();
205 v.FieldType = FieldType;
209 static Capture makeConstant(llvm::Value *value) {
211 v.Data = reinterpret_cast<uintptr_t>(value);
216 /// CanBeGlobal - True if the block can be global, i.e. it has
217 /// no non-constant captures.
218 bool CanBeGlobal : 1;
220 /// True if the block has captures that would necessitate custom copy or
221 /// dispose helper functions if the block were escaping.
222 bool NeedsCopyDispose : 1;
224 /// HasCXXObject - True if the block's custom copy/dispose functions
225 /// need to be run even in GC mode.
226 bool HasCXXObject : 1;
228 /// UsesStret : True if the block uses an stret return. Mutable
229 /// because it gets set later in the block-creation process.
230 mutable bool UsesStret : 1;
232 /// HasCapturedVariableLayout : True if block has captured variables
233 /// and their layout meta-data has been generated.
234 bool HasCapturedVariableLayout : 1;
236 /// Indicates whether an object of a non-external C++ class is captured. This
237 /// bit is used to determine the linkage of the block copy/destroy helper
239 bool CapturesNonExternalType : 1;
241 /// The mapping of allocated indexes within the block.
242 llvm::DenseMap<const VarDecl*, Capture> Captures;
244 Address LocalAddress;
245 llvm::StructType *StructureType;
246 const BlockDecl *Block;
247 const BlockExpr *BlockExpression;
249 CharUnits BlockAlign;
250 CharUnits CXXThisOffset;
252 // Offset of the gap caused by block header having a smaller
253 // alignment than the alignment of the block descriptor. This
254 // is the gap offset before the first capturued field.
255 CharUnits BlockHeaderForcedGapOffset;
256 // Gap size caused by aligning first field after block header.
257 // This could be zero if no forced alignment is required.
258 CharUnits BlockHeaderForcedGapSize;
260 /// An instruction which dominates the full-expression that the
262 llvm::Instruction *DominatingIP;
264 /// The next block in the block-info chain. Invalid if this block
265 /// info is not part of the CGF's block-info chain, which is true
266 /// if it corresponds to a global block or a block whose expression
267 /// has been encountered.
268 CGBlockInfo *NextBlockInfo;
270 const Capture &getCapture(const VarDecl *var) const {
271 return const_cast<CGBlockInfo*>(this)->getCapture(var);
273 Capture &getCapture(const VarDecl *var) {
274 llvm::DenseMap<const VarDecl*, Capture>::iterator
275 it = Captures.find(var);
276 assert(it != Captures.end() && "no entry for variable!");
280 const BlockDecl *getBlockDecl() const { return Block; }
281 const BlockExpr *getBlockExpr() const {
282 assert(BlockExpression);
283 assert(BlockExpression->getBlockDecl() == Block);
284 return BlockExpression;
287 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
289 // Indicates whether the block needs a custom copy or dispose function.
290 bool needsCopyDisposeHelpers() const {
291 return NeedsCopyDispose && !Block->doesNotEscape();
295 } // end namespace CodeGen
296 } // end namespace clang