clang  6.0.0svn
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 
29 namespace llvm {
30 class Constant;
31 class Function;
32 class GlobalValue;
33 class DataLayout;
34 class FunctionType;
35 class PointerType;
36 class Value;
37 class LLVMContext;
38 }
39 
40 namespace clang {
41 namespace CodeGen {
42 
43 class CGBlockInfo;
44 
45 // Flags stored in __block variables.
47  BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
48  BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
54 };
55 
58  BLOCK_HAS_CXX_OBJ = (1 << 26),
59  BLOCK_IS_GLOBAL = (1 << 28),
60  BLOCK_USE_STRET = (1 << 29),
61  BLOCK_HAS_SIGNATURE = (1 << 30),
63 };
64 class BlockFlags {
65  uint32_t flags;
66 
67 public:
68  BlockFlags(uint32_t flags) : flags(flags) {}
69  BlockFlags() : flags(0) {}
70  BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
71  BlockFlags(BlockByrefFlags flag) : flags(flag) {}
72 
73  uint32_t getBitMask() const { return flags; }
74  bool empty() const { return flags == 0; }
75 
77  return BlockFlags(l.flags | r.flags);
78  }
80  l.flags |= r.flags;
81  return l;
82  }
83  friend bool operator&(BlockFlags l, BlockFlags r) {
84  return (l.flags & r.flags);
85  }
87  return (flags == r.flags);
88  }
89 };
91  return BlockFlags(l) | BlockFlags(r);
92 }
93 
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 */
107 };
108 
110  uint32_t flags;
111 
112  BlockFieldFlags(uint32_t flags) : flags(flags) {}
113 public:
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 
125  return BlockFieldFlags(l.flags | r.flags);
126  }
128  l.flags |= r.flags;
129  return l;
130  }
132  return (l.flags & r.flags);
133  }
134 };
136  return BlockFieldFlags(l) | BlockFieldFlags(r);
137 }
138 
139 /// Information about the layout of a __block variable.
141 public:
142  llvm::StructType *Type;
143  unsigned FieldIndex;
146 };
147 
148 /// CGBlockInfo - Information to generate a block literal.
149 class CGBlockInfo {
150 public:
151  /// Name - The name of the block, kindof.
152  StringRef Name;
153 
154  /// The field index of 'this' within the block, if there is one.
155  unsigned CXXThisIndex;
156 
157  class Capture {
158  uintptr_t Data;
161 
162  /// Type of the capture field. Normally, this is identical to the type of
163  /// the capture's VarDecl, but can be different if there is an enclosing
164  /// lambda.
165  QualType FieldType;
166 
167  public:
168  bool isIndex() const { return (Data & 1) != 0; }
169  bool isConstant() const { return !isIndex(); }
170 
171  unsigned getIndex() const {
172  assert(isIndex());
173  return Data >> 1;
174  }
176  assert(isIndex());
177  return CharUnits::fromQuantity(Offset);
178  }
180  assert(isIndex());
181  return Cleanup;
182  }
184  assert(isIndex());
185  Cleanup = cleanup;
186  }
187 
189  assert(isConstant());
190  return reinterpret_cast<llvm::Value*>(Data);
191  }
192 
193  QualType fieldType() const {
194  return FieldType;
195  }
196 
197  static Capture makeIndex(unsigned index, CharUnits offset,
198  QualType FieldType) {
199  Capture v;
200  v.Data = (index << 1) | 1;
201  v.Offset = offset.getQuantity();
202  v.FieldType = FieldType;
203  return v;
204  }
205 
207  Capture v;
208  v.Data = reinterpret_cast<uintptr_t>(value);
209  return v;
210  }
211  };
212 
213  /// CanBeGlobal - True if the block can be global, i.e. it has
214  /// no non-constant captures.
215  bool CanBeGlobal : 1;
216 
217  /// True if the block needs a custom copy or dispose function.
219 
220  /// HasCXXObject - True if the block's custom copy/dispose functions
221  /// need to be run even in GC mode.
222  bool HasCXXObject : 1;
223 
224  /// UsesStret : True if the block uses an stret return. Mutable
225  /// because it gets set later in the block-creation process.
226  mutable bool UsesStret : 1;
227 
228  /// HasCapturedVariableLayout : True if block has captured variables
229  /// and their layout meta-data has been generated.
231 
232  /// The mapping of allocated indexes within the block.
233  llvm::DenseMap<const VarDecl*, Capture> Captures;
234 
236  llvm::StructType *StructureType;
237  const BlockDecl *Block;
242 
243  // Offset of the gap caused by block header having a smaller
244  // alignment than the alignment of the block descriptor. This
245  // is the gap offset before the first capturued field.
247  // Gap size caused by aligning first field after block header.
248  // This could be zero if no forced alignment is required.
250 
251  /// An instruction which dominates the full-expression that the
252  /// block is inside.
253  llvm::Instruction *DominatingIP;
254 
255  /// The next block in the block-info chain. Invalid if this block
256  /// info is not part of the CGF's block-info chain, which is true
257  /// if it corresponds to a global block or a block whose expression
258  /// has been encountered.
260 
261  const Capture &getCapture(const VarDecl *var) const {
262  return const_cast<CGBlockInfo*>(this)->getCapture(var);
263  }
264  Capture &getCapture(const VarDecl *var) {
265  llvm::DenseMap<const VarDecl*, Capture>::iterator
266  it = Captures.find(var);
267  assert(it != Captures.end() && "no entry for variable!");
268  return it->second;
269  }
270 
271  const BlockDecl *getBlockDecl() const { return Block; }
272  const BlockExpr *getBlockExpr() const {
273  assert(BlockExpression);
274  assert(BlockExpression->getBlockDecl() == Block);
275  return BlockExpression;
276  }
277 
278  CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
279 };
280 
281 } // end namespace CodeGen
282 } // end namespace clang
283 
284 #endif
const BlockDecl * getBlockDecl() const
Definition: Expr.h:4866
Information about the layout of a __block variable.
Definition: CGBlocks.h:140
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:261
int64_t QuantityType
Definition: CharUnits.h:40
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:246
A (possibly-)qualified type.
Definition: Type.h:614
Capture & getCapture(const VarDecl *var)
Definition: CGBlocks.h:264
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
Definition: Format.cpp:1969
C Language Family Type Representation.
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:124
bool operator==(BlockFlags r)
Definition: CGBlocks.h:86
BlockFlags(uint32_t flags)
Definition: CGBlocks.h:68
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:771
EHScopeStack::stable_iterator getCleanup() const
Definition: CGBlocks.h:179
Defines the clang::Expr interface and subclasses for C++ expressions.
const BlockDecl * Block
Definition: CGBlocks.h:237
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:230
BlockFlags(BlockByrefFlags flag)
Definition: CGBlocks.h:71
BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r)
Definition: CGBlocks.h:135
BlockFieldFlags(BlockFieldFlag_t flag)
Definition: CGBlocks.h:115
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:271
uint32_t Offset
Definition: CacheTokens.cpp:43
bool HasCXXObject
HasCXXObject - True if the block&#39;s custom copy/dispose functions need to be run even in GC mode...
Definition: CGBlocks.h:222
uint32_t getBitMask() const
Definition: CGBlocks.h:117
static Capture makeConstant(llvm::Value *value)
Definition: CGBlocks.h:206
friend BlockFieldFlags & operator|=(BlockFieldFlags &l, BlockFieldFlags r)
Definition: CGBlocks.h:127
StringRef Name
Name - The name of the block, kindof.
Definition: CGBlocks.h:152
bool NeedsCopyDispose
True if the block needs a custom copy or dispose function.
Definition: CGBlocks.h:218
const BlockExpr * BlockExpression
Definition: CGBlocks.h:238
uint32_t getBitMask() const
Definition: CGBlocks.h:73
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:149
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:215
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
BlockFlags(BlockLiteralFlags flag)
Definition: CGBlocks.h:70
CGBlockInfo * NextBlockInfo
The next block in the block-info chain.
Definition: CGBlocks.h:259
DiagnosticLevelMask operator &(DiagnosticLevelMask LHS, DiagnosticLevelMask RHS)
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3595
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4852
friend BlockFlags operator|(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:76
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType)
Definition: CGBlocks.h:197
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:82
bool UsesStret
UsesStret : True if the block uses an stret return.
Definition: CGBlocks.h:226
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
llvm::StructType * StructureType
Definition: CGBlocks.h:236
llvm::DenseMap< const VarDecl *, Capture > Captures
The mapping of allocated indexes within the block.
Definition: CGBlocks.h:233
An aligned address.
Definition: Address.h:25
llvm::Value * getConstant() const
Definition: CGBlocks.h:188
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:272
unsigned CXXThisIndex
The field index of &#39;this&#39; within the block, if there is one.
Definition: CGBlocks.h:155
Dataflow Directional Tag Classes.
bool isSpecialPointer() const
Answers whether the flags indicate that this field is an object or block pointer that requires _Block...
Definition: CGBlocks.h:122
friend BlockFlags & operator|=(BlockFlags &l, BlockFlags r)
Definition: CGBlocks.h:79
llvm::StructType * Type
Definition: CGBlocks.h:142
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:249
Defines the clang::TargetInfo interface.
llvm::Instruction * DominatingIP
An instruction which dominates the full-expression that the block is inside.
Definition: CGBlocks.h:253
void setCleanup(EHScopeStack::stable_iterator cleanup)
Definition: CGBlocks.h:183