CGBlocks.h revision 7b5209cdcbaedf5775d59d46cb3118c8215ba628
1//===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal state used for llvm translation for block literals.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGBLOCKS_H
15#define CLANG_CODEGEN_CGBLOCKS_H
16
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"
25
26#include "CodeGenFunction.h"
27#include "CGBuilder.h"
28#include "CGCall.h"
29#include "CGValue.h"
30
31namespace llvm {
32  class Module;
33  class Constant;
34  class Function;
35  class GlobalValue;
36  class DataLayout;
37  class FunctionType;
38  class PointerType;
39  class Value;
40  class LLVMContext;
41}
42
43namespace clang {
44
45namespace CodeGen {
46
47class CodeGenModule;
48class CGBlockInfo;
49
50// Flags stored in __block variables.
51enum 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)
59};
60
61enum 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};
68class BlockFlags {
69  uint32_t flags;
70
71  BlockFlags(uint32_t flags) : flags(flags) {}
72public:
73  BlockFlags() : flags(0) {}
74  BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
75
76  uint32_t getBitMask() const { return flags; }
77  bool empty() const { return flags == 0; }
78
79  friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
80    return BlockFlags(l.flags | r.flags);
81  }
82  friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
83    l.flags |= r.flags;
84    return l;
85  }
86  friend bool operator&(BlockFlags l, BlockFlags r) {
87    return (l.flags & r.flags);
88  }
89};
90inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
91  return BlockFlags(l) | BlockFlags(r);
92}
93
94enum BlockFieldFlag_t {
95  BLOCK_FIELD_IS_OBJECT   = 0x03,  /* id, NSObject, __attribute__((NSObject)),
96                                    block, ... */
97  BLOCK_FIELD_IS_BLOCK    = 0x07,  /* a block variable */
98
99  BLOCK_FIELD_IS_BYREF    = 0x08,  /* the on stack structure holding the __block
100                                    variable */
101  BLOCK_FIELD_IS_WEAK     = 0x10,  /* declared __weak, only used in byref copy
102                                    helpers */
103  BLOCK_FIELD_IS_ARC      = 0x40,  /* field has ARC-specific semantics */
104  BLOCK_BYREF_CALLER      = 128,   /* called from __block (byref) copy/dispose
105                                      support routines */
106  BLOCK_BYREF_CURRENT_MAX = 256
107};
108
109class BlockFieldFlags {
110  uint32_t flags;
111
112  BlockFieldFlags(uint32_t flags) : flags(flags) {}
113public:
114  BlockFieldFlags() : flags(0) {}
115  BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
116
117  uint32_t getBitMask() const { return flags; }
118  bool empty() const { return flags == 0; }
119
120  /// Answers whether the flags indicate that this field is an object
121  /// or block pointer that requires _Block_object_assign/dispose.
122  bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
123
124  friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
125    return BlockFieldFlags(l.flags | r.flags);
126  }
127  friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
128    l.flags |= r.flags;
129    return l;
130  }
131  friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
132    return (l.flags & r.flags);
133  }
134};
135inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
136  return BlockFieldFlags(l) | BlockFieldFlags(r);
137}
138
139/// CGBlockInfo - Information to generate a block literal.
140class CGBlockInfo {
141public:
142  /// Name - The name of the block, kindof.
143  llvm::StringRef Name;
144
145  /// The field index of 'this' within the block, if there is one.
146  unsigned CXXThisIndex;
147
148  class Capture {
149    uintptr_t Data;
150    EHScopeStack::stable_iterator Cleanup;
151
152  public:
153    bool isIndex() const { return (Data & 1) != 0; }
154    bool isConstant() const { return !isIndex(); }
155    unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
156    llvm::Value *getConstant() const {
157      assert(isConstant());
158      return reinterpret_cast<llvm::Value*>(Data);
159    }
160    EHScopeStack::stable_iterator getCleanup() const {
161      assert(isIndex());
162      return Cleanup;
163    }
164    void setCleanup(EHScopeStack::stable_iterator cleanup) {
165      assert(isIndex());
166      Cleanup = cleanup;
167    }
168
169    static Capture makeIndex(unsigned index) {
170      Capture v;
171      v.Data = (index << 1) | 1;
172      return v;
173    }
174
175    static Capture makeConstant(llvm::Value *value) {
176      Capture v;
177      v.Data = reinterpret_cast<uintptr_t>(value);
178      return v;
179    }
180  };
181
182  /// CanBeGlobal - True if the block can be global, i.e. it has
183  /// no non-constant captures.
184  bool CanBeGlobal : 1;
185
186  /// True if the block needs a custom copy or dispose function.
187  bool NeedsCopyDispose : 1;
188
189  /// HasCXXObject - True if the block's custom copy/dispose functions
190  /// need to be run even in GC mode.
191  bool HasCXXObject : 1;
192
193  /// UsesStret : True if the block uses an stret return.  Mutable
194  /// because it gets set later in the block-creation process.
195  mutable bool UsesStret : 1;
196
197  /// The mapping of allocated indexes within the block.
198  llvm::DenseMap<const VarDecl*, Capture> Captures;
199
200  llvm::AllocaInst *Address;
201  llvm::StructType *StructureType;
202  const BlockDecl *Block;
203  const BlockExpr *BlockExpression;
204  CharUnits BlockSize;
205  CharUnits BlockAlign;
206
207  /// An instruction which dominates the full-expression that the
208  /// block is inside.
209  llvm::Instruction *DominatingIP;
210
211  /// The next block in the block-info chain.  Invalid if this block
212  /// info is not part of the CGF's block-info chain, which is true
213  /// if it corresponds to a global block or a block whose expression
214  /// has been encountered.
215  CGBlockInfo *NextBlockInfo;
216
217  const Capture &getCapture(const VarDecl *var) const {
218    return const_cast<CGBlockInfo*>(this)->getCapture(var);
219  }
220  Capture &getCapture(const VarDecl *var) {
221    llvm::DenseMap<const VarDecl*, Capture>::iterator
222      it = Captures.find(var);
223    assert(it != Captures.end() && "no entry for variable!");
224    return it->second;
225  }
226
227  const BlockDecl *getBlockDecl() const { return Block; }
228  const BlockExpr *getBlockExpr() const {
229    assert(BlockExpression);
230    assert(BlockExpression->getBlockDecl() == Block);
231    return BlockExpression;
232  }
233
234  CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
235};
236
237}  // end namespace CodeGen
238}  // end namespace clang
239
240#endif
241