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 "CodeGenTypes.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/Module.h"
20 #include "clang/Basic/TargetInfo.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"
26 #include "CodeGenFunction.h"
27 #include "CGBuilder.h"
50 // Flags stored in __block variables.
51 enum BlockByrefFlags {
52 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
53 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
54 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
55 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
56 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
57 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
58 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
61 enum BlockLiteralFlags {
62 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
63 BLOCK_HAS_CXX_OBJ = (1 << 26),
64 BLOCK_IS_GLOBAL = (1 << 28),
65 BLOCK_USE_STRET = (1 << 29),
66 BLOCK_HAS_SIGNATURE = (1 << 30),
67 BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31)
72 BlockFlags(uint32_t flags) : flags(flags) {}
74 BlockFlags() : flags(0) {}
75 BlockFlags(BlockLiteralFlags 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);
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);
136 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
137 return BlockFieldFlags(l) | BlockFieldFlags(r);
140 /// CGBlockInfo - Information to generate a block literal.
143 /// Name - The name of the block, kindof.
144 llvm::StringRef Name;
146 /// The field index of 'this' within the block, if there is one.
147 unsigned CXXThisIndex;
151 EHScopeStack::stable_iterator Cleanup;
154 bool isIndex() const { return (Data & 1) != 0; }
155 bool isConstant() const { return !isIndex(); }
156 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
157 llvm::Value *getConstant() const {
158 assert(isConstant());
159 return reinterpret_cast<llvm::Value*>(Data);
161 EHScopeStack::stable_iterator getCleanup() const {
165 void setCleanup(EHScopeStack::stable_iterator cleanup) {
170 static Capture makeIndex(unsigned index) {
172 v.Data = (index << 1) | 1;
176 static Capture makeConstant(llvm::Value *value) {
178 v.Data = reinterpret_cast<uintptr_t>(value);
183 /// CanBeGlobal - True if the block can be global, i.e. it has
184 /// no non-constant captures.
185 bool CanBeGlobal : 1;
187 /// True if the block needs a custom copy or dispose function.
188 bool NeedsCopyDispose : 1;
190 /// HasCXXObject - True if the block's custom copy/dispose functions
191 /// need to be run even in GC mode.
192 bool HasCXXObject : 1;
194 /// UsesStret : True if the block uses an stret return. Mutable
195 /// because it gets set later in the block-creation process.
196 mutable bool UsesStret : 1;
198 /// HasCapturedVariableLayout : True if block has captured variables
199 /// and their layout meta-data has been generated.
200 bool HasCapturedVariableLayout : 1;
202 /// The mapping of allocated indexes within the block.
203 llvm::DenseMap<const VarDecl*, Capture> Captures;
205 llvm::AllocaInst *Address;
206 llvm::StructType *StructureType;
207 const BlockDecl *Block;
208 const BlockExpr *BlockExpression;
210 CharUnits BlockAlign;
212 /// An instruction which dominates the full-expression that the
214 llvm::Instruction *DominatingIP;
216 /// The next block in the block-info chain. Invalid if this block
217 /// info is not part of the CGF's block-info chain, which is true
218 /// if it corresponds to a global block or a block whose expression
219 /// has been encountered.
220 CGBlockInfo *NextBlockInfo;
222 const Capture &getCapture(const VarDecl *var) const {
223 return const_cast<CGBlockInfo*>(this)->getCapture(var);
225 Capture &getCapture(const VarDecl *var) {
226 llvm::DenseMap<const VarDecl*, Capture>::iterator
227 it = Captures.find(var);
228 assert(it != Captures.end() && "no entry for variable!");
232 const BlockDecl *getBlockDecl() const { return Block; }
233 const BlockExpr *getBlockExpr() const {
234 assert(BlockExpression);
235 assert(BlockExpression->getBlockDecl() == Block);
236 return BlockExpression;
239 CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
242 } // end namespace CodeGen
243 } // end namespace clang