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 CLANG_CODEGEN_CGBLOCKS_H
15 #define CLANG_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"
28 #include "llvm/IR/Module.h"
49 // Flags stored in __block variables.
50 enum BlockByrefFlags {
51 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
52 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
53 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
54 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
55 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
56 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
57 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
60 enum BlockLiteralFlags {
61 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
62 BLOCK_HAS_CXX_OBJ = (1 << 26),
63 BLOCK_IS_GLOBAL = (1 << 28),
64 BLOCK_USE_STRET = (1 << 29),
65 BLOCK_HAS_SIGNATURE = (1 << 30),
66 BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31)
72 BlockFlags(uint32_t flags) : flags(flags) {}
73 BlockFlags() : flags(0) {}
74 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
75 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
77 uint32_t getBitMask() const { return flags; }
78 bool empty() const { return flags == 0; }
80 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
81 return BlockFlags(l.flags | r.flags);
83 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
87 friend bool operator&(BlockFlags l, BlockFlags r) {
88 return (l.flags & r.flags);
90 bool operator==(BlockFlags r) {
91 return (flags == r.flags);
94 inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
95 return BlockFlags(l) | BlockFlags(r);
98 enum BlockFieldFlag_t {
99 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
101 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
103 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
105 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
107 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
108 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
110 BLOCK_BYREF_CURRENT_MAX = 256
113 class BlockFieldFlags {
116 BlockFieldFlags(uint32_t flags) : flags(flags) {}
118 BlockFieldFlags() : flags(0) {}
119 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
121 uint32_t getBitMask() const { return flags; }
122 bool empty() const { return flags == 0; }
124 /// Answers whether the flags indicate that this field is an object
125 /// or block pointer that requires _Block_object_assign/dispose.
126 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
128 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
129 return BlockFieldFlags(l.flags | r.flags);
131 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
135 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
136 return (l.flags & r.flags);
139 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
140 return BlockFieldFlags(l) | BlockFieldFlags(r);
143 /// CGBlockInfo - Information to generate a block literal.
146 /// Name - The name of the block, kindof.
149 /// The field index of 'this' within the block, if there is one.
150 unsigned CXXThisIndex;
154 EHScopeStack::stable_iterator Cleanup;
157 bool isIndex() const { return (Data & 1) != 0; }
158 bool isConstant() const { return !isIndex(); }
159 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
160 llvm::Value *getConstant() const {
161 assert(isConstant());
162 return reinterpret_cast<llvm::Value*>(Data);
164 EHScopeStack::stable_iterator getCleanup() const {
168 void setCleanup(EHScopeStack::stable_iterator cleanup) {
173 static Capture makeIndex(unsigned index) {
175 v.Data = (index << 1) | 1;
179 static Capture makeConstant(llvm::Value *value) {
181 v.Data = reinterpret_cast<uintptr_t>(value);
186 /// CanBeGlobal - True if the block can be global, i.e. it has
187 /// no non-constant captures.
188 bool CanBeGlobal : 1;
190 /// True if the block needs a custom copy or dispose function.
191 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;
197 /// UsesStret : True if the block uses an stret return. Mutable
198 /// because it gets set later in the block-creation process.
199 mutable bool UsesStret : 1;
201 /// HasCapturedVariableLayout : True if block has captured variables
202 /// and their layout meta-data has been generated.
203 bool HasCapturedVariableLayout : 1;
205 /// The mapping of allocated indexes within the block.
206 llvm::DenseMap<const VarDecl*, Capture> Captures;
208 llvm::AllocaInst *Address;
209 llvm::StructType *StructureType;
210 const BlockDecl *Block;
211 const BlockExpr *BlockExpression;
213 CharUnits BlockAlign;
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;
223 /// An instruction which dominates the full-expression that the
225 llvm::Instruction *DominatingIP;
227 /// The next block in the block-info chain. Invalid if this block
228 /// info is not part of the CGF's block-info chain, which is true
229 /// if it corresponds to a global block or a block whose expression
230 /// has been encountered.
231 CGBlockInfo *NextBlockInfo;
233 const Capture &getCapture(const VarDecl *var) const {
234 return const_cast<CGBlockInfo*>(this)->getCapture(var);
236 Capture &getCapture(const VarDecl *var) {
237 llvm::DenseMap<const VarDecl*, Capture>::iterator
238 it = Captures.find(var);
239 assert(it != Captures.end() && "no entry for variable!");
243 const BlockDecl *getBlockDecl() const { return Block; }
244 const BlockExpr *getBlockExpr() const {
245 assert(BlockExpression);
246 assert(BlockExpression->getBlockDecl() == Block);
247 return BlockExpression;
250 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
253 } // end namespace CodeGen
254 } // end namespace clang