1 //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the internal state used for llvm translation for block literals.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
17 #include "CGBuilder.h"
20 #include "CodeGenFunction.h"
21 #include "CodeGenTypes.h"
22 #include "clang/AST/CharUnits.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/ExprObjC.h"
26 #include "clang/AST/Type.h"
27 #include "clang/Basic/TargetInfo.h"
45 // Flags stored in __block variables.
46 enum BlockByrefFlags {
47 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
48 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
49 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
50 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
51 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
52 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
53 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
56 enum BlockLiteralFlags {
57 BLOCK_IS_NOESCAPE = (1 << 23),
58 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
59 BLOCK_HAS_CXX_OBJ = (1 << 26),
60 BLOCK_IS_GLOBAL = (1 << 28),
61 BLOCK_USE_STRET = (1 << 29),
62 BLOCK_HAS_SIGNATURE = (1 << 30),
63 BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
69 BlockFlags(uint32_t flags) : flags(flags) {}
70 BlockFlags() : flags(0) {}
71 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
72 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
74 uint32_t getBitMask() const { return flags; }
75 bool empty() const { return flags == 0; }
77 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
78 return BlockFlags(l.flags | r.flags);
80 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
84 friend bool operator&(BlockFlags l, BlockFlags r) {
85 return (l.flags & r.flags);
87 bool operator==(BlockFlags r) {
88 return (flags == r.flags);
91 inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
92 return BlockFlags(l) | BlockFlags(r);
95 enum BlockFieldFlag_t {
96 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
98 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
100 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
102 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
104 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
105 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
107 BLOCK_BYREF_CURRENT_MAX = 256
110 class BlockFieldFlags {
113 BlockFieldFlags(uint32_t flags) : flags(flags) {}
115 BlockFieldFlags() : flags(0) {}
116 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
118 uint32_t getBitMask() const { return flags; }
119 bool empty() const { return flags == 0; }
121 /// Answers whether the flags indicate that this field is an object
122 /// or block pointer that requires _Block_object_assign/dispose.
123 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
125 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
126 return BlockFieldFlags(l.flags | r.flags);
128 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
132 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
133 return (l.flags & r.flags);
135 bool operator==(BlockFieldFlags Other) const {
136 return flags == Other.flags;
139 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
140 return BlockFieldFlags(l) | BlockFieldFlags(r);
143 /// Information about the layout of a __block variable.
144 class BlockByrefInfo {
146 llvm::StructType *Type;
148 CharUnits ByrefAlignment;
149 CharUnits FieldOffset;
152 /// CGBlockInfo - Information to generate a block literal.
155 /// Name - The name of the block, kindof.
158 /// The field index of 'this' within the block, if there is one.
159 unsigned CXXThisIndex;
163 EHScopeStack::stable_iterator Cleanup;
164 CharUnits::QuantityType Offset;
166 /// Type of the capture field. Normally, this is identical to the type of
167 /// the capture's VarDecl, but can be different if there is an enclosing
172 bool isIndex() const { return (Data & 1) != 0; }
173 bool isConstant() const { return !isIndex(); }
175 unsigned getIndex() const {
179 CharUnits getOffset() const {
181 return CharUnits::fromQuantity(Offset);
183 EHScopeStack::stable_iterator getCleanup() const {
187 void setCleanup(EHScopeStack::stable_iterator cleanup) {
192 llvm::Value *getConstant() const {
193 assert(isConstant());
194 return reinterpret_cast<llvm::Value*>(Data);
197 QualType fieldType() const {
201 static Capture makeIndex(unsigned index, CharUnits offset,
202 QualType FieldType) {
204 v.Data = (index << 1) | 1;
205 v.Offset = offset.getQuantity();
206 v.FieldType = FieldType;
210 static Capture makeConstant(llvm::Value *value) {
212 v.Data = reinterpret_cast<uintptr_t>(value);
217 /// CanBeGlobal - True if the block can be global, i.e. it has
218 /// no non-constant captures.
219 bool CanBeGlobal : 1;
221 /// True if the block has captures that would necessitate custom copy or
222 /// dispose helper functions if the block were escaping.
223 bool NeedsCopyDispose : 1;
225 /// HasCXXObject - True if the block's custom copy/dispose functions
226 /// need to be run even in GC mode.
227 bool HasCXXObject : 1;
229 /// UsesStret : True if the block uses an stret return. Mutable
230 /// because it gets set later in the block-creation process.
231 mutable bool UsesStret : 1;
233 /// HasCapturedVariableLayout : True if block has captured variables
234 /// and their layout meta-data has been generated.
235 bool HasCapturedVariableLayout : 1;
237 /// Indicates whether an object of a non-external C++ class is captured. This
238 /// bit is used to determine the linkage of the block copy/destroy helper
240 bool CapturesNonExternalType : 1;
242 /// The mapping of allocated indexes within the block.
243 llvm::DenseMap<const VarDecl*, Capture> Captures;
245 Address LocalAddress;
246 llvm::StructType *StructureType;
247 const BlockDecl *Block;
248 const BlockExpr *BlockExpression;
250 CharUnits BlockAlign;
251 CharUnits CXXThisOffset;
253 // Offset of the gap caused by block header having a smaller
254 // alignment than the alignment of the block descriptor. This
255 // is the gap offset before the first capturued field.
256 CharUnits BlockHeaderForcedGapOffset;
257 // Gap size caused by aligning first field after block header.
258 // This could be zero if no forced alignment is required.
259 CharUnits BlockHeaderForcedGapSize;
261 /// An instruction which dominates the full-expression that the
263 llvm::Instruction *DominatingIP;
265 /// The next block in the block-info chain. Invalid if this block
266 /// info is not part of the CGF's block-info chain, which is true
267 /// if it corresponds to a global block or a block whose expression
268 /// has been encountered.
269 CGBlockInfo *NextBlockInfo;
271 const Capture &getCapture(const VarDecl *var) const {
272 return const_cast<CGBlockInfo*>(this)->getCapture(var);
274 Capture &getCapture(const VarDecl *var) {
275 llvm::DenseMap<const VarDecl*, Capture>::iterator
276 it = Captures.find(var);
277 assert(it != Captures.end() && "no entry for variable!");
281 const BlockDecl *getBlockDecl() const { return Block; }
282 const BlockExpr *getBlockExpr() const {
283 assert(BlockExpression);
284 assert(BlockExpression->getBlockDecl() == Block);
285 return BlockExpression;
288 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
290 // Indicates whether the block needs a custom copy or dispose function.
291 bool needsCopyDisposeHelpers() const {
292 return NeedsCopyDispose && !Block->doesNotEscape();
296 } // end namespace CodeGen
297 } // end namespace clang