clang  3.9.0
CGBlocks.h
Go to the documentation of this file.
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 LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
16 
17 #include "CGBuilder.h"
18 #include "CGCall.h"
19 #include "CGValue.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"
29 
30 namespace llvm {
31 class Module;
32 class Constant;
33 class Function;
34 class GlobalValue;
35 class DataLayout;
36 class FunctionType;
37 class PointerType;
38 class Value;
39 class LLVMContext;
40 }
41 
42 namespace clang {
43 
44 namespace CodeGen {
45 
46 class CodeGenModule;
47 class CGBlockInfo;
48 
49 // Flags stored in __block variables.
51  BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
52  BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
58 };
59 
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),
67 };
68 class BlockFlags {
69  uint32_t flags;
70 
71 public:
72  BlockFlags(uint32_t flags) : flags(flags) {}
73  BlockFlags() : flags(0) {}
74  BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
75  BlockFlags(BlockByrefFlags flag) : flags(flag) {}
76 
77  uint32_t getBitMask() const { return flags; }
78  bool empty() const { return flags == 0; }
79 
81  return BlockFlags(l.flags | r.flags);
82  }
84  l.flags |= r.flags;
85  return l;
86  }
87  friend bool operator&(BlockFlags l, BlockFlags r) {
88  return (l.flags & r.flags);
89  }
91  return (flags == r.flags);
92  }
93 };
95  return BlockFlags(l) | BlockFlags(r);
96 }
97 
99  BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
100  block, ... */
101  BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
102 
103  BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
104  variable */
105  BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
106  helpers */
107  BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
108  BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
109  support routines */
111 };
112 
114  uint32_t flags;
115 
116  BlockFieldFlags(uint32_t flags) : flags(flags) {}
117 public:
118  BlockFieldFlags() : flags(0) {}
119  BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
120 
121  uint32_t getBitMask() const { return flags; }
122  bool empty() const { return flags == 0; }
123 
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; }
127 
129  return BlockFieldFlags(l.flags | r.flags);
130  }
132  l.flags |= r.flags;
133  return l;
134  }
136  return (l.flags & r.flags);
137  }
138 };
140  return BlockFieldFlags(l) | BlockFieldFlags(r);
141 }
142 
143 /// Information about the layout of a __block variable.
145 public:
146  llvm::StructType *Type;
147  unsigned FieldIndex;
150 };
151 
152 /// CGBlockInfo - Information to generate a block literal.
153 class CGBlockInfo {
154 public:
155  /// Name - The name of the block, kindof.
156  StringRef Name;
157 
158  /// The field index of 'this' within the block, if there is one.
159  unsigned CXXThisIndex;
160 
161  class Capture {
162  uintptr_t Data;
165 
166  public:
167  bool isIndex() const { return (Data & 1) != 0; }
168  bool isConstant() const { return !isIndex(); }
169 
170  unsigned getIndex() const {
171  assert(isIndex());
172  return Data >> 1;
173  }
175  assert(isIndex());
177  }
179  assert(isIndex());
180  return Cleanup;
181  }
183  assert(isIndex());
184  Cleanup = cleanup;
185  }
186 
188  assert(isConstant());
189  return reinterpret_cast<llvm::Value*>(Data);
190  }
191 
192  static Capture makeIndex(unsigned index, CharUnits offset) {
193  Capture v;
194  v.Data = (index << 1) | 1;
195  v.Offset = offset.getQuantity();
196  return v;
197  }
198 
200  Capture v;
201  v.Data = reinterpret_cast<uintptr_t>(value);
202  return v;
203  }
204  };
205 
206  /// CanBeGlobal - True if the block can be global, i.e. it has
207  /// no non-constant captures.
208  bool CanBeGlobal : 1;
209 
210  /// True if the block needs a custom copy or dispose function.
212 
213  /// HasCXXObject - True if the block's custom copy/dispose functions
214  /// need to be run even in GC mode.
215  bool HasCXXObject : 1;
216 
217  /// UsesStret : True if the block uses an stret return. Mutable
218  /// because it gets set later in the block-creation process.
219  mutable bool UsesStret : 1;
220 
221  /// HasCapturedVariableLayout : True if block has captured variables
222  /// and their layout meta-data has been generated.
224 
225  /// The mapping of allocated indexes within the block.
226  llvm::DenseMap<const VarDecl*, Capture> Captures;
227 
229  llvm::StructType *StructureType;
230  const BlockDecl *Block;
235 
236  // Offset of the gap caused by block header having a smaller
237  // alignment than the alignment of the block descriptor. This
238  // is the gap offset before the first capturued field.
240  // Gap size caused by aligning first field after block header.
241  // This could be zero if no forced alignment is required.
243 
244  /// An instruction which dominates the full-expression that the
245  /// block is inside.
246  llvm::Instruction *DominatingIP;
247 
248  /// The next block in the block-info chain. Invalid if this block
249  /// info is not part of the CGF's block-info chain, which is true
250  /// if it corresponds to a global block or a block whose expression
251  /// has been encountered.
253 
254  const Capture &getCapture(const VarDecl *var) const {
255  return const_cast<CGBlockInfo*>(this)->getCapture(var);
256  }
257  Capture &getCapture(const VarDecl *var) {
259  it = Captures.find(var);
260  assert(it != Captures.end() && "no entry for variable!");
261  return it->second;
262  }
263 
264  const BlockDecl *getBlockDecl() const { return Block; }
265  const BlockExpr *getBlockExpr() const {
266  assert(BlockExpression);
267  assert(BlockExpression->getBlockDecl() == Block);
268  return BlockExpression;
269  }
270 
271  CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
272 };
273 
274 } // end namespace CodeGen
275 } // end namespace clang
276 
277 #endif
Information about the layout of a __block variable.
Definition: CGBlocks.h:144
int64_t QuantityType
Definition: CharUnits.h:40
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:239
tooling::Replacements cleanup(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, ArrayRef< CharSourceRange > Ranges)
Clean up any erroneous/redundant code in the given Ranges in the file ID.
Definition: Format.cpp:1639
Capture & getCapture(const VarDecl *var)
Definition: CGBlocks.h:257
C Language Family Type Representation.
BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r)
Definition: CGBlocks.h:94
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:128
bool operator==(BlockFlags r)
Definition: CGBlocks.h:90
BlockFlags(uint32_t flags)
Definition: CGBlocks.h:72
uint32_t getBitMask() const
Definition: CGBlocks.h:121
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:768
CGBlockInfo(const BlockDecl *blockDecl, StringRef Name)
Definition: CGBlocks.cpp:30
Defines the clang::Expr interface and subclasses for C++ expressions.
const BlockDecl * Block
Definition: CGBlocks.h:230
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:223
BlockFlags(BlockByrefFlags flag)
Definition: CGBlocks.h:75
friend bool operator&(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:135
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:254
BlockFieldFlags(BlockFieldFlag_t flag)
Definition: CGBlocks.h:119
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
uint32_t Offset
Definition: CacheTokens.cpp:44
static Capture makeIndex(unsigned index, CharUnits offset)
Definition: CGBlocks.h:192
bool HasCXXObject
HasCXXObject - True if the block's custom copy/dispose functions need to be run even in GC mode...
Definition: CGBlocks.h:215
static Capture makeConstant(llvm::Value *value)
Definition: CGBlocks.h:199
friend BlockFieldFlags & operator|=(BlockFieldFlags &l, BlockFieldFlags r)
Definition: CGBlocks.h:131
StringRef Name
Name - The name of the block, kindof.
Definition: CGBlocks.h:156
bool NeedsCopyDispose
True if the block needs a custom copy or dispose function.
Definition: CGBlocks.h:211
const BlockExpr * BlockExpression
Definition: CGBlocks.h:231
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:153
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:208
BlockFlags(BlockLiteralFlags flag)
Definition: CGBlocks.h:74
CGBlockInfo * NextBlockInfo
The next block in the block-info chain.
Definition: CGBlocks.h:252
bool isSpecialPointer() const
Answers whether the flags indicate that this field is an object or block pointer that requires _Block...
Definition: CGBlocks.h:126
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3456
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4567
friend BlockFlags operator|(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:80
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
do v
Definition: arm_acle.h:78
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:75
EHScopeStack::stable_iterator getCleanup() const
Definition: CGBlocks.h:178
bool UsesStret
UsesStret : True if the block uses an stret return.
Definition: CGBlocks.h:219
const TemplateArgument * iterator
Definition: Type.h:4233
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
llvm::StructType * StructureType
Definition: CGBlocks.h:229
bool empty() const
Definition: CGBlocks.h:78
llvm::DenseMap< const VarDecl *, Capture > Captures
The mapping of allocated indexes within the block.
Definition: CGBlocks.h:226
An aligned address.
Definition: Address.h:25
unsigned CXXThisIndex
The field index of 'this' within the block, if there is one.
Definition: CGBlocks.h:159
const BlockDecl * getBlockDecl() const
Definition: Expr.h:4581
llvm::Value * getConstant() const
Definition: CGBlocks.h:187
friend BlockFlags & operator|=(BlockFlags &l, BlockFlags r)
Definition: CGBlocks.h:83
llvm::StructType * Type
Definition: CGBlocks.h:146
friend bool operator&(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:87
uint32_t getBitMask() const
Definition: CGBlocks.h:77
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:264
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:242
Defines the clang::TargetInfo interface.
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:265
llvm::Instruction * DominatingIP
An instruction which dominates the full-expression that the block is inside.
Definition: CGBlocks.h:246
void setCleanup(EHScopeStack::stable_iterator cleanup)
Definition: CGBlocks.h:182