clang  7.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 
57  BLOCK_IS_NOESCAPE = (1 << 23),
59  BLOCK_HAS_CXX_OBJ = (1 << 26),
60  BLOCK_IS_GLOBAL = (1 << 28),
61  BLOCK_USE_STRET = (1 << 29),
62  BLOCK_HAS_SIGNATURE = (1 << 30),
64 };
65 class BlockFlags {
66  uint32_t flags;
67 
68 public:
69  BlockFlags(uint32_t flags) : flags(flags) {}
70  BlockFlags() : flags(0) {}
71  BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
72  BlockFlags(BlockByrefFlags flag) : flags(flag) {}
73 
74  uint32_t getBitMask() const { return flags; }
75  bool empty() const { return flags == 0; }
76 
78  return BlockFlags(l.flags | r.flags);
79  }
81  l.flags |= r.flags;
82  return l;
83  }
84  friend bool operator&(BlockFlags l, BlockFlags r) {
85  return (l.flags & r.flags);
86  }
88  return (flags == r.flags);
89  }
90 };
92  return BlockFlags(l) | BlockFlags(r);
93 }
94 
96  BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
97  block, ... */
98  BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
99 
100  BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
101  variable */
102  BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
103  helpers */
104  BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
105  BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
106  support routines */
108 };
109 
111  uint32_t flags;
112 
113  BlockFieldFlags(uint32_t flags) : flags(flags) {}
114 public:
115  BlockFieldFlags() : flags(0) {}
116  BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
117 
118  uint32_t getBitMask() const { return flags; }
119  bool empty() const { return flags == 0; }
120 
121  /// Answers whether the flags indicate that this field is an object
122  /// or block pointer that requires _Block_object_assign/dispose.
123  bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
124 
126  return BlockFieldFlags(l.flags | r.flags);
127  }
129  l.flags |= r.flags;
130  return l;
131  }
133  return (l.flags & r.flags);
134  }
135 };
137  return BlockFieldFlags(l) | BlockFieldFlags(r);
138 }
139 
140 /// Information about the layout of a __block variable.
142 public:
143  llvm::StructType *Type;
144  unsigned FieldIndex;
147 };
148 
149 /// CGBlockInfo - Information to generate a block literal.
150 class CGBlockInfo {
151 public:
152  /// Name - The name of the block, kindof.
153  StringRef Name;
154 
155  /// The field index of 'this' within the block, if there is one.
156  unsigned CXXThisIndex;
157 
158  class Capture {
159  uintptr_t Data;
162 
163  /// Type of the capture field. Normally, this is identical to the type of
164  /// the capture's VarDecl, but can be different if there is an enclosing
165  /// lambda.
166  QualType FieldType;
167 
168  public:
169  bool isIndex() const { return (Data & 1) != 0; }
170  bool isConstant() const { return !isIndex(); }
171 
172  unsigned getIndex() const {
173  assert(isIndex());
174  return Data >> 1;
175  }
177  assert(isIndex());
178  return CharUnits::fromQuantity(Offset);
179  }
181  assert(isIndex());
182  return Cleanup;
183  }
185  assert(isIndex());
186  Cleanup = cleanup;
187  }
188 
190  assert(isConstant());
191  return reinterpret_cast<llvm::Value*>(Data);
192  }
193 
194  QualType fieldType() const {
195  return FieldType;
196  }
197 
198  static Capture makeIndex(unsigned index, CharUnits offset,
199  QualType FieldType) {
200  Capture v;
201  v.Data = (index << 1) | 1;
202  v.Offset = offset.getQuantity();
203  v.FieldType = FieldType;
204  return v;
205  }
206 
208  Capture v;
209  v.Data = reinterpret_cast<uintptr_t>(value);
210  return v;
211  }
212  };
213 
214  /// CanBeGlobal - True if the block can be global, i.e. it has
215  /// no non-constant captures.
216  bool CanBeGlobal : 1;
217 
218  /// True if the block has captures that would necessitate custom copy or
219  /// dispose helper functions if the block were escaping.
221 
222  /// HasCXXObject - True if the block's custom copy/dispose functions
223  /// need to be run even in GC mode.
224  bool HasCXXObject : 1;
225 
226  /// UsesStret : True if the block uses an stret return. Mutable
227  /// because it gets set later in the block-creation process.
228  mutable bool UsesStret : 1;
229 
230  /// HasCapturedVariableLayout : True if block has captured variables
231  /// and their layout meta-data has been generated.
233 
234  /// The mapping of allocated indexes within the block.
235  llvm::DenseMap<const VarDecl*, Capture> Captures;
236 
238  llvm::StructType *StructureType;
239  const BlockDecl *Block;
244 
245  // Offset of the gap caused by block header having a smaller
246  // alignment than the alignment of the block descriptor. This
247  // is the gap offset before the first capturued field.
249  // Gap size caused by aligning first field after block header.
250  // This could be zero if no forced alignment is required.
252 
253  /// An instruction which dominates the full-expression that the
254  /// block is inside.
255  llvm::Instruction *DominatingIP;
256 
257  /// The next block in the block-info chain. Invalid if this block
258  /// info is not part of the CGF's block-info chain, which is true
259  /// if it corresponds to a global block or a block whose expression
260  /// has been encountered.
262 
263  const Capture &getCapture(const VarDecl *var) const {
264  return const_cast<CGBlockInfo*>(this)->getCapture(var);
265  }
266  Capture &getCapture(const VarDecl *var) {
267  llvm::DenseMap<const VarDecl*, Capture>::iterator
268  it = Captures.find(var);
269  assert(it != Captures.end() && "no entry for variable!");
270  return it->second;
271  }
272 
273  const BlockDecl *getBlockDecl() const { return Block; }
274  const BlockExpr *getBlockExpr() const {
275  assert(BlockExpression);
276  assert(BlockExpression->getBlockDecl() == Block);
277  return BlockExpression;
278  }
279 
280  CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
281 
282  // Indicates whether the block needs a custom copy or dispose function.
283  bool needsCopyDisposeHelpers() const {
284  return NeedsCopyDispose && !Block->doesNotEscape();
285  }
286 };
287 
288 } // end namespace CodeGen
289 } // end namespace clang
290 
291 #endif
const BlockDecl * getBlockDecl() const
Definition: Expr.h:4949
Information about the layout of a __block variable.
Definition: CGBlocks.h:141
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:263
int64_t QuantityType
Definition: CharUnits.h:40
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:248
A (possibly-)qualified type.
Definition: Type.h:655
Capture & getCapture(const VarDecl *var)
Definition: CGBlocks.h:266
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
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:2049
C Language Family Type Representation.
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:125
bool operator==(BlockFlags r)
Definition: CGBlocks.h:87
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
BlockFlags(uint32_t flags)
Definition: CGBlocks.h:69
Represents a variable declaration or definition.
Definition: Decl.h:812
EHScopeStack::stable_iterator getCleanup() const
Definition: CGBlocks.h:180
Defines the clang::Expr interface and subclasses for C++ expressions.
const BlockDecl * Block
Definition: CGBlocks.h:239
bool doesNotEscape() const
Definition: Decl.h:3991
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:232
BlockFlags(BlockByrefFlags flag)
Definition: CGBlocks.h:72
BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r)
Definition: CGBlocks.h:136
BlockFieldFlags(BlockFieldFlag_t flag)
Definition: CGBlocks.h:116
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:273
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:224
uint32_t getBitMask() const
Definition: CGBlocks.h:118
static Capture makeConstant(llvm::Value *value)
Definition: CGBlocks.h:207
friend BlockFieldFlags & operator|=(BlockFieldFlags &l, BlockFieldFlags r)
Definition: CGBlocks.h:128
StringRef Name
Name - The name of the block, kindof.
Definition: CGBlocks.h:153
bool NeedsCopyDispose
True if the block has captures that would necessitate custom copy or dispose helper functions if the ...
Definition: CGBlocks.h:220
const BlockExpr * BlockExpression
Definition: CGBlocks.h:240
uint32_t getBitMask() const
Definition: CGBlocks.h:74
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:150
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:216
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
BlockFlags(BlockLiteralFlags flag)
Definition: CGBlocks.h:71
CGBlockInfo * NextBlockInfo
The next block in the block-info chain.
Definition: CGBlocks.h:261
DiagnosticLevelMask operator &(DiagnosticLevelMask LHS, DiagnosticLevelMask RHS)
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3849
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4935
friend BlockFlags operator|(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:77
bool needsCopyDisposeHelpers() const
Definition: CGBlocks.h:283
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType)
Definition: CGBlocks.h:198
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:228
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
llvm::StructType * StructureType
Definition: CGBlocks.h:238
llvm::DenseMap< const VarDecl *, Capture > Captures
The mapping of allocated indexes within the block.
Definition: CGBlocks.h:235
An aligned address.
Definition: Address.h:25
llvm::Value * getConstant() const
Definition: CGBlocks.h:189
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:274
unsigned CXXThisIndex
The field index of &#39;this&#39; within the block, if there is one.
Definition: CGBlocks.h:156
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:123
friend BlockFlags & operator|=(BlockFlags &l, BlockFlags r)
Definition: CGBlocks.h:80
llvm::StructType * Type
Definition: CGBlocks.h:143
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:251
Defines the clang::TargetInfo interface.
const internal::VariadicDynCastAllOfMatcher< Decl, BlockDecl > blockDecl
Matches block declarations.
llvm::Instruction * DominatingIP
An instruction which dominates the full-expression that the block is inside.
Definition: CGBlocks.h:255
void setCleanup(EHScopeStack::stable_iterator cleanup)
Definition: CGBlocks.h:184