clang  14.0.0git
CGBlocks.h
Go to the documentation of this file.
1 //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is the internal state used for llvm translation for block literals.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
14 #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15 
16 #include "CGBuilder.h"
17 #include "CGCall.h"
18 #include "CGValue.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenTypes.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 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
27 
28 namespace llvm {
29 class Constant;
30 class Function;
31 class GlobalValue;
32 class DataLayout;
33 class FunctionType;
34 class PointerType;
35 class Value;
36 class LLVMContext;
37 }
38 
39 namespace clang {
40 namespace CodeGen {
41 
42 class CGBlockInfo;
43 
44 // Flags stored in __block variables.
46  BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
47  BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
53 };
54 
56  BLOCK_IS_NOESCAPE = (1 << 23),
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  bool operator==(BlockFieldFlags Other) const {
135  return flags == Other.flags;
136  }
137 };
139  return BlockFieldFlags(l) | BlockFieldFlags(r);
140 }
141 
142 /// Information about the layout of a __block variable.
144 public:
145  llvm::StructType *Type;
146  unsigned FieldIndex;
149 };
150 
151 /// CGBlockInfo - Information to generate a block literal.
152 class CGBlockInfo {
153 public:
154  /// Name - The name of the block, kindof.
155  StringRef Name;
156 
157  /// The field index of 'this' within the block, if there is one.
158  unsigned CXXThisIndex;
159 
160  class Capture {
161  uintptr_t Data;
164 
165  /// Type of the capture field. Normally, this is identical to the type of
166  /// the capture's VarDecl, but can be different if there is an enclosing
167  /// lambda.
168  QualType FieldType;
169 
170  public:
171  bool isIndex() const { return (Data & 1) != 0; }
172  bool isConstant() const { return !isIndex(); }
173 
174  unsigned getIndex() const {
175  assert(isIndex());
176  return Data >> 1;
177  }
179  assert(isIndex());
181  }
183  assert(isIndex());
184  return Cleanup;
185  }
187  assert(isIndex());
188  Cleanup = cleanup;
189  }
190 
192  assert(isConstant());
193  return reinterpret_cast<llvm::Value*>(Data);
194  }
195 
196  QualType fieldType() const {
197  return FieldType;
198  }
199 
200  static Capture makeIndex(unsigned index, CharUnits offset,
201  QualType FieldType) {
202  Capture v;
203  v.Data = (index << 1) | 1;
204  v.Offset = offset.getQuantity();
205  v.FieldType = FieldType;
206  return v;
207  }
208 
210  Capture v;
211  v.Data = reinterpret_cast<uintptr_t>(value);
212  return v;
213  }
214  };
215 
216  /// CanBeGlobal - True if the block can be global, i.e. it has
217  /// no non-constant captures.
218  bool CanBeGlobal : 1;
219 
220  /// True if the block has captures that would necessitate custom copy or
221  /// dispose helper functions if the block were escaping.
223 
224  /// HasCXXObject - True if the block's custom copy/dispose functions
225  /// need to be run even in GC mode.
226  bool HasCXXObject : 1;
227 
228  /// UsesStret : True if the block uses an stret return. Mutable
229  /// because it gets set later in the block-creation process.
230  mutable bool UsesStret : 1;
231 
232  /// HasCapturedVariableLayout : True if block has captured variables
233  /// and their layout meta-data has been generated.
235 
236  /// Indicates whether an object of a non-external C++ class is captured. This
237  /// bit is used to determine the linkage of the block copy/destroy helper
238  /// functions.
240 
241  /// The mapping of allocated indexes within the block.
242  llvm::DenseMap<const VarDecl*, Capture> Captures;
243 
245  llvm::StructType *StructureType;
246  const BlockDecl *Block;
251 
252  // Offset of the gap caused by block header having a smaller
253  // alignment than the alignment of the block descriptor. This
254  // is the gap offset before the first capturued field.
256  // Gap size caused by aligning first field after block header.
257  // This could be zero if no forced alignment is required.
259 
260  /// The next block in the block-info chain. Invalid if this block
261  /// info is not part of the CGF's block-info chain, which is true
262  /// if it corresponds to a global block or a block whose expression
263  /// has been encountered.
265 
266  const Capture &getCapture(const VarDecl *var) const {
267  return const_cast<CGBlockInfo*>(this)->getCapture(var);
268  }
269  Capture &getCapture(const VarDecl *var) {
270  llvm::DenseMap<const VarDecl*, Capture>::iterator
271  it = Captures.find(var);
272  assert(it != Captures.end() && "no entry for variable!");
273  return it->second;
274  }
275 
276  const BlockDecl *getBlockDecl() const { return Block; }
277  const BlockExpr *getBlockExpr() const {
278  assert(BlockExpression);
279  assert(BlockExpression->getBlockDecl() == Block);
280  return BlockExpression;
281  }
282 
283  CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
284 
285  // Indicates whether the block needs a custom copy or dispose function.
286  bool needsCopyDisposeHelpers() const {
287  return NeedsCopyDispose && !Block->doesNotEscape();
288  }
289 };
290 
291 } // end namespace CodeGen
292 } // end namespace clang
293 
294 #endif
clang::CodeGen::CGBlockInfo::BlockExpression
const BlockExpr * BlockExpression
Definition: CGBlocks.h:247
clang::CodeGen::BlockFlags::operator|=
friend BlockFlags & operator|=(BlockFlags &l, BlockFlags r)
Definition: CGBlocks.h:79
llvm
Definition: Dominators.h:30
clang::CodeGen::CGBlockInfo::CGBlockInfo
CGBlockInfo(const BlockDecl *blockDecl, StringRef Name)
Definition: CGBlocks.cpp:35
clang::CodeGen::CGBlockInfo::CXXThisOffset
CharUnits CXXThisOffset
Definition: CGBlocks.h:250
clang::CodeGen::CGBlockInfo::LocalAddress
Address LocalAddress
Definition: CGBlocks.h:244
clang::CodeGen::BlockByrefInfo::FieldIndex
unsigned FieldIndex
Definition: CGBlocks.h:146
CodeGenFunction.h
clang::CodeGen::BlockByrefInfo
Information about the layout of a __block variable.
Definition: CGBlocks.h:143
clang::CodeGen::BlockByrefInfo::ByrefAlignment
CharUnits ByrefAlignment
Definition: CGBlocks.h:147
clang::CodeGen::CGBlockInfo::UsesStret
bool UsesStret
UsesStret : True if the block uses an stret return.
Definition: CGBlocks.h:230
TargetInfo.h
clang::CodeGen::CGBlockInfo::BlockAlign
CharUnits BlockAlign
Definition: CGBlocks.h:249
clang::CharUnits::QuantityType
int64_t QuantityType
Definition: CharUnits.h:40
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::CodeGen::CGBlockInfo
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:152
clang::CodeGen::BlockFieldFlags::operator|
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:124
clang::CodeGen::BLOCK_BYREF_HAS_COPY_DISPOSE
@ BLOCK_BYREF_HAS_COPY_DISPOSE
Definition: CGBlocks.h:46
clang::CodeGen::BLOCK_HAS_EXTENDED_LAYOUT
@ BLOCK_HAS_EXTENDED_LAYOUT
Definition: CGBlocks.h:62
clang::CodeGen::CGBlockInfo::getCapture
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:266
clang::CodeGen::CGBlockInfo::Capture::isIndex
bool isIndex() const
Definition: CGBlocks.h:171
clang::CodeGen::CGBlockInfo::getCapture
Capture & getCapture(const VarDecl *var)
Definition: CGBlocks.h:269
clang::CodeGen::BlockFlags::operator==
bool operator==(BlockFlags r)
Definition: CGBlocks.h:86
clang::CodeGen::BlockFlags::operator|
friend BlockFlags operator|(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:76
clang::CodeGen::BlockFieldFlags::operator|=
friend BlockFieldFlags & operator|=(BlockFieldFlags &l, BlockFieldFlags r)
Definition: CGBlocks.h:127
clang::CodeGen::BLOCK_BYREF_LAYOUT_UNRETAINED
@ BLOCK_BYREF_LAYOUT_UNRETAINED
Definition: CGBlocks.h:52
clang::CodeGen::BLOCK_USE_STRET
@ BLOCK_USE_STRET
Definition: CGBlocks.h:60
clang::CodeGen::BlockFieldFlags::BlockFieldFlags
BlockFieldFlags()
Definition: CGBlocks.h:114
clang::CodeGen::BlockLiteralFlags
BlockLiteralFlags
Definition: CGBlocks.h:55
clang::CodeGen::CGBlockInfo::needsCopyDisposeHelpers
bool needsCopyDisposeHelpers() const
Definition: CGBlocks.h:286
clang::CodeGen::CGBlockInfo::NeedsCopyDispose
bool NeedsCopyDispose
True if the block has captures that would necessitate custom copy or dispose helper functions if the ...
Definition: CGBlocks.h:222
clang::CodeGen::BlockFieldFlag_t
BlockFieldFlag_t
Definition: CGBlocks.h:94
clang::ast_matchers::blockDecl
const internal::VariadicDynCastAllOfMatcher< Decl, BlockDecl > blockDecl
Matches block declarations.
Definition: ASTMatchersInternal.cpp:835
clang::CodeGen::CGBlockInfo::Capture::setCleanup
void setCleanup(EHScopeStack::stable_iterator cleanup)
Definition: CGBlocks.h:186
clang::CodeGen::CGBlockInfo::Capture
Definition: CGBlocks.h:160
clang::CodeGen::BLOCK_HAS_CXX_OBJ
@ BLOCK_HAS_CXX_OBJ
Definition: CGBlocks.h:58
Offset
unsigned Offset
Definition: Format.cpp:2335
clang::BlockDecl::doesNotEscape
bool doesNotEscape() const
Definition: Decl.h:4304
clang::CodeGen::CGBlockInfo::HasCapturedVariableLayout
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:234
clang::CodeGen::BlockFieldFlags::operator&
friend bool operator&(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:131
clang::CodeGen::BLOCK_BYREF_LAYOUT_EXTENDED
@ BLOCK_BYREF_LAYOUT_EXTENDED
Definition: CGBlocks.h:48
clang::CodeGen::BlockFlags::BlockFlags
BlockFlags(BlockLiteralFlags flag)
Definition: CGBlocks.h:70
clang::CodeGen::BLOCK_BYREF_LAYOUT_NON_OBJECT
@ BLOCK_BYREF_LAYOUT_NON_OBJECT
Definition: CGBlocks.h:49
clang::CodeGen::CGBlockInfo::BlockHeaderForcedGapSize
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:258
CGCall.h
clang::CodeGen::operator|
BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r)
Definition: CGBlocks.h:90
clang::CodeGen::CGBlockInfo::CanBeGlobal
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:218
clang::CodeGen::BlockByrefInfo::Type
llvm::StructType * Type
Definition: CGBlocks.h:145
clang::CodeGen::BLOCK_BYREF_CURRENT_MAX
@ BLOCK_BYREF_CURRENT_MAX
Definition: CGBlocks.h:106
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4153
CGValue.h
clang::CodeGen::CGBlockInfo::Block
const BlockDecl * Block
Definition: CGBlocks.h:246
clang::CodeGen::CGBlockInfo::StructureType
llvm::StructType * StructureType
Definition: CGBlocks.h:245
clang::CodeGen::BLOCK_IS_GLOBAL
@ BLOCK_IS_GLOBAL
Definition: CGBlocks.h:59
clang::CodeGen::BlockFlags::BlockFlags
BlockFlags()
Definition: CGBlocks.h:69
clang::CharUnits::fromQuantity
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
clang::BlockExpr::getBlockDecl
const BlockDecl * getBlockDecl() const
Definition: Expr.h:5977
clang::CodeGen::CGBlockInfo::Capture::getOffset
CharUnits getOffset() const
Definition: CGBlocks.h:178
clang::CodeGen::BLOCK_BYREF_CALLER
@ BLOCK_BYREF_CALLER
Definition: CGBlocks.h:104
clang::CodeGen::BLOCK_HAS_COPY_DISPOSE
@ BLOCK_HAS_COPY_DISPOSE
Definition: CGBlocks.h:57
uintptr_t
__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-base.h:124
Type.h
clang::CodeGen::CGBlockInfo::Name
StringRef Name
Name - The name of the block, kindof.
Definition: CGBlocks.h:155
Expr.h
clang::CodeGen::BlockFieldFlags::BlockFieldFlags
BlockFieldFlags(BlockFieldFlag_t flag)
Definition: CGBlocks.h:115
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::CodeGen::CGBlockInfo::CXXThisIndex
unsigned CXXThisIndex
The field index of 'this' within the block, if there is one.
Definition: CGBlocks.h:158
clang::CodeGen::BlockFieldFlags::getBitMask
uint32_t getBitMask() const
Definition: CGBlocks.h:117
clang::CodeGen::BlockFlags
Definition: CGBlocks.h:64
clang::CodeGen::BlockFieldFlags
Definition: CGBlocks.h:109
ExprObjC.h
ExprCXX.h
clang::CodeGen::Address
An aligned address.
Definition: Address.h:24
clang::CodeGen::CGBlockInfo::HasCXXObject
bool HasCXXObject
HasCXXObject - True if the block's custom copy/dispose functions need to be run even in GC mode.
Definition: CGBlocks.h:226
clang::CodeGen::BLOCK_FIELD_IS_BLOCK
@ BLOCK_FIELD_IS_BLOCK
Definition: CGBlocks.h:97
clang::CodeGen::BlockFlags::operator&
friend bool operator&(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:83
CharUnits.h
clang::CodeGen::BLOCK_BYREF_LAYOUT_STRONG
@ BLOCK_BYREF_LAYOUT_STRONG
Definition: CGBlocks.h:50
clang::CodeGen::CGBlockInfo::Capture::isConstant
bool isConstant() const
Definition: CGBlocks.h:172
clang::CodeGen::BLOCK_FIELD_IS_BYREF
@ BLOCK_FIELD_IS_BYREF
Definition: CGBlocks.h:99
clang::CodeGen::BLOCK_FIELD_IS_ARC
@ BLOCK_FIELD_IS_ARC
Definition: CGBlocks.h:103
clang::CodeGen::BlockFlags::BlockFlags
BlockFlags(uint32_t flags)
Definition: CGBlocks.h:68
clang::CodeGen::CGBlockInfo::Capture::getCleanup
EHScopeStack::stable_iterator getCleanup() const
Definition: CGBlocks.h:182
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5965
clang::CodeGen::CGBlockInfo::getBlockDecl
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:276
Value
Value
Definition: UninitializedValues.cpp:102
CGBuilder.h
clang::CodeGen::BlockFieldFlags::isSpecialPointer
bool isSpecialPointer() const
Answers whether the flags indicate that this field is an object or block pointer that requires _Block...
Definition: CGBlocks.h:122
CodeGenTypes.h
clang::CodeGen::BLOCK_IS_NOESCAPE
@ BLOCK_IS_NOESCAPE
Definition: CGBlocks.h:56
clang::CodeGen::CGBlockInfo::Capture::getIndex
unsigned getIndex() const
Definition: CGBlocks.h:174
clang::CodeGen::CGBlockInfo::Capture::fieldType
QualType fieldType() const
Definition: CGBlocks.h:196
clang::CodeGen::CGBlockInfo::BlockHeaderForcedGapOffset
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:255
clang::CodeGen::BlockFieldFlags::operator==
bool operator==(BlockFieldFlags Other) const
Definition: CGBlocks.h:134
clang::CodeGen::BLOCK_FIELD_IS_WEAK
@ BLOCK_FIELD_IS_WEAK
Definition: CGBlocks.h:101
clang::CodeGen::BlockFlags::BlockFlags
BlockFlags(BlockByrefFlags flag)
Definition: CGBlocks.h:71
clang::CodeGen::BLOCK_BYREF_LAYOUT_WEAK
@ BLOCK_BYREF_LAYOUT_WEAK
Definition: CGBlocks.h:51
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::BLOCK_FIELD_IS_OBJECT
@ BLOCK_FIELD_IS_OBJECT
Definition: CGBlocks.h:95
clang::CodeGen::CGBlockInfo::NextBlockInfo
CGBlockInfo * NextBlockInfo
The next block in the block-info chain.
Definition: CGBlocks.h:264
clang::CodeGen::BLOCK_BYREF_LAYOUT_MASK
@ BLOCK_BYREF_LAYOUT_MASK
Definition: CGBlocks.h:47
clang::CodeGen::BLOCK_HAS_SIGNATURE
@ BLOCK_HAS_SIGNATURE
Definition: CGBlocks.h:61
clang::CodeGen::CGBlockInfo::getBlockExpr
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:277
clang::CodeGen::BlockFlags::getBitMask
uint32_t getBitMask() const
Definition: CGBlocks.h:73
v
do v
Definition: arm_acle.h:76
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::index::SymbolKind::Function
@ Function
clang::CodeGen::CGBlockInfo::Capture::getConstant
llvm::Value * getConstant() const
Definition: CGBlocks.h:191
clang::CodeGen::BlockByrefInfo::FieldOffset
CharUnits FieldOffset
Definition: CGBlocks.h:148
clang::CodeGen::BlockFlags::empty
bool empty() const
Definition: CGBlocks.h:74
clang::CodeGen::BlockByrefFlags
BlockByrefFlags
Definition: CGBlocks.h:45
clang::CodeGen::CGBlockInfo::BlockSize
CharUnits BlockSize
Definition: CGBlocks.h:248
clang::CodeGen::CGBlockInfo::Captures
llvm::DenseMap< const VarDecl *, Capture > Captures
The mapping of allocated indexes within the block.
Definition: CGBlocks.h:242
clang::format::cleanup
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:3039
clang::CodeGen::BlockFieldFlags::empty
bool empty() const
Definition: CGBlocks.h:118
clang::CodeGen::CGBlockInfo::Capture::makeIndex
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType)
Definition: CGBlocks.h:200
clang::CodeGen::CGBlockInfo::CapturesNonExternalType
bool CapturesNonExternalType
Indicates whether an object of a non-external C++ class is captured.
Definition: CGBlocks.h:239
clang::CharUnits::getQuantity
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
clang::CodeGen::EHScopeStack::stable_iterator
A saved depth on the scope stack.
Definition: EHScopeStack.h:101
clang::CodeGen::CGBlockInfo::Capture::makeConstant
static Capture makeConstant(llvm::Value *value)
Definition: CGBlocks.h:209