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 "CGBuilder.h"
50 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
51 BLOCK_HAS_CXX_OBJ = (1 << 26),
52 BLOCK_IS_GLOBAL = (1 << 28),
53 BLOCK_USE_STRET = (1 << 29),
54 BLOCK_HAS_SIGNATURE = (1 << 30)
59 BlockFlags(uint32_t flags) : flags(flags) {}
61 BlockFlags() : flags(0) {}
62 BlockFlags(BlockFlag_t flag) : flags(flag) {}
64 uint32_t getBitMask() const { return flags; }
65 bool empty() const { return flags == 0; }
67 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
68 return BlockFlags(l.flags | r.flags);
70 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
74 friend bool operator&(BlockFlags l, BlockFlags r) {
75 return (l.flags & r.flags);
78 inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) {
79 return BlockFlags(l) | BlockFlags(r);
82 enum BlockFieldFlag_t {
83 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
85 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
87 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
89 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
91 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
92 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
94 BLOCK_BYREF_CURRENT_MAX = 256
97 class BlockFieldFlags {
100 BlockFieldFlags(uint32_t flags) : flags(flags) {}
102 BlockFieldFlags() : flags(0) {}
103 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
105 uint32_t getBitMask() const { return flags; }
106 bool empty() const { return flags == 0; }
108 /// Answers whether the flags indicate that this field is an object
109 /// or block pointer that requires _Block_object_assign/dispose.
110 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
112 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
113 return BlockFieldFlags(l.flags | r.flags);
115 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
119 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
120 return (l.flags & r.flags);
123 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
124 return BlockFieldFlags(l) | BlockFieldFlags(r);
127 /// CGBlockInfo - Information to generate a block literal.
130 /// Name - The name of the block, kindof.
133 /// The field index of 'this' within the block, if there is one.
134 unsigned CXXThisIndex;
140 bool isIndex() const { return (Data & 1) != 0; }
141 bool isConstant() const { return !isIndex(); }
142 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
143 llvm::Value *getConstant() const {
144 assert(isConstant());
145 return reinterpret_cast<llvm::Value*>(Data);
148 static Capture makeIndex(unsigned index) {
150 v.Data = (index << 1) | 1;
154 static Capture makeConstant(llvm::Value *value) {
156 v.Data = reinterpret_cast<uintptr_t>(value);
161 /// The mapping of allocated indexes within the block.
162 llvm::DenseMap<const VarDecl*, Capture> Captures;
164 /// CanBeGlobal - True if the block can be global, i.e. it has
165 /// no non-constant captures.
166 bool CanBeGlobal : 1;
168 /// True if the block needs a custom copy or dispose function.
169 bool NeedsCopyDispose : 1;
171 /// HasCXXObject - True if the block's custom copy/dispose functions
172 /// need to be run even in GC mode.
173 bool HasCXXObject : 1;
175 /// UsesStret : True if the block uses an stret return. Mutable
176 /// because it gets set later in the block-creation process.
177 mutable bool UsesStret : 1;
179 const llvm::StructType *StructureType;
180 const BlockExpr *Block;
182 CharUnits BlockAlign;
184 const Capture &getCapture(const VarDecl *var) const {
185 llvm::DenseMap<const VarDecl*, Capture>::const_iterator
186 it = Captures.find(var);
187 assert(it != Captures.end() && "no entry for variable!");
191 const BlockDecl *getBlockDecl() const { return Block->getBlockDecl(); }
192 const BlockExpr *getBlockExpr() const { return Block; }
194 CGBlockInfo(const BlockExpr *blockExpr, const char *Name);
197 } // end namespace CodeGen
198 } // end namespace clang