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"
51 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
52 BLOCK_HAS_CXX_OBJ = (1 << 26),
53 BLOCK_IS_GLOBAL = (1 << 28),
54 BLOCK_USE_STRET = (1 << 29),
55 BLOCK_HAS_SIGNATURE = (1 << 30)
60 BlockFlags(uint32_t flags) : flags(flags) {}
62 BlockFlags() : flags(0) {}
63 BlockFlags(BlockFlag_t flag) : flags(flag) {}
65 uint32_t getBitMask() const { return flags; }
66 bool empty() const { return flags == 0; }
68 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
69 return BlockFlags(l.flags | r.flags);
71 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
75 friend bool operator&(BlockFlags l, BlockFlags r) {
76 return (l.flags & r.flags);
79 inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) {
80 return BlockFlags(l) | BlockFlags(r);
83 enum BlockFieldFlag_t {
84 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
86 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
88 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
90 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
92 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
93 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
95 BLOCK_BYREF_CURRENT_MAX = 256
98 class BlockFieldFlags {
101 BlockFieldFlags(uint32_t flags) : flags(flags) {}
103 BlockFieldFlags() : flags(0) {}
104 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
106 uint32_t getBitMask() const { return flags; }
107 bool empty() const { return flags == 0; }
109 /// Answers whether the flags indicate that this field is an object
110 /// or block pointer that requires _Block_object_assign/dispose.
111 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
113 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
114 return BlockFieldFlags(l.flags | r.flags);
116 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
120 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
121 return (l.flags & r.flags);
124 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
125 return BlockFieldFlags(l) | BlockFieldFlags(r);
128 /// CGBlockInfo - Information to generate a block literal.
131 /// Name - The name of the block, kindof.
132 llvm::StringRef Name;
134 /// The field index of 'this' within the block, if there is one.
135 unsigned CXXThisIndex;
139 EHScopeStack::stable_iterator Cleanup;
142 bool isIndex() const { return (Data & 1) != 0; }
143 bool isConstant() const { return !isIndex(); }
144 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
145 llvm::Value *getConstant() const {
146 assert(isConstant());
147 return reinterpret_cast<llvm::Value*>(Data);
149 EHScopeStack::stable_iterator getCleanup() const {
153 void setCleanup(EHScopeStack::stable_iterator cleanup) {
158 static Capture makeIndex(unsigned index) {
160 v.Data = (index << 1) | 1;
164 static Capture makeConstant(llvm::Value *value) {
166 v.Data = reinterpret_cast<uintptr_t>(value);
171 /// CanBeGlobal - True if the block can be global, i.e. it has
172 /// no non-constant captures.
173 bool CanBeGlobal : 1;
175 /// True if the block needs a custom copy or dispose function.
176 bool NeedsCopyDispose : 1;
178 /// HasCXXObject - True if the block's custom copy/dispose functions
179 /// need to be run even in GC mode.
180 bool HasCXXObject : 1;
182 /// UsesStret : True if the block uses an stret return. Mutable
183 /// because it gets set later in the block-creation process.
184 mutable bool UsesStret : 1;
186 /// The mapping of allocated indexes within the block.
187 llvm::DenseMap<const VarDecl*, Capture> Captures;
189 llvm::AllocaInst *Address;
190 llvm::StructType *StructureType;
191 const BlockDecl *Block;
192 const BlockExpr *BlockExpression;
194 CharUnits BlockAlign;
196 /// An instruction which dominates the full-expression that the
198 llvm::Instruction *DominatingIP;
200 /// The next block in the block-info chain. Invalid if this block
201 /// info is not part of the CGF's block-info chain, which is true
202 /// if it corresponds to a global block or a block whose expression
203 /// has been encountered.
204 CGBlockInfo *NextBlockInfo;
206 const Capture &getCapture(const VarDecl *var) const {
207 return const_cast<CGBlockInfo*>(this)->getCapture(var);
209 Capture &getCapture(const VarDecl *var) {
210 llvm::DenseMap<const VarDecl*, Capture>::iterator
211 it = Captures.find(var);
212 assert(it != Captures.end() && "no entry for variable!");
216 const BlockDecl *getBlockDecl() const { return Block; }
217 const BlockExpr *getBlockExpr() const {
218 assert(BlockExpression);
219 assert(BlockExpression->getBlockDecl() == Block);
220 return BlockExpression;
223 CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
226 } // end namespace CodeGen
227 } // end namespace clang