clang 22.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"
27
28namespace llvm {
29class Value;
30}
31
32namespace clang {
33namespace CodeGen {
34
35class CGBlockInfo;
36
37// Flags stored in __block variables.
39 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
40 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
46};
47
49 BLOCK_IS_NOESCAPE = (1 << 23),
51 BLOCK_HAS_CXX_OBJ = (1 << 26),
52 BLOCK_IS_GLOBAL = (1 << 28),
53 BLOCK_USE_STRET = (1 << 29),
55 BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
56};
58 uint32_t flags;
59
60public:
61 BlockFlags(uint32_t flags) : flags(flags) {}
62 BlockFlags() : flags(0) {}
63 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
64 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
65
66 uint32_t getBitMask() const { return flags; }
67 bool empty() const { return flags == 0; }
68
70 return BlockFlags(l.flags | r.flags);
71 }
73 l.flags |= r.flags;
74 return l;
75 }
76 friend bool operator&(BlockFlags l, BlockFlags r) {
77 return (l.flags & r.flags);
78 }
80 return (flags == r.flags);
81 }
82};
84 return BlockFlags(l) | BlockFlags(r);
85}
86
88 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
89 block, ... */
90 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
91
92 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
93 variable */
94 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
95 helpers */
96 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
97 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
98 support routines */
101
103 uint32_t flags;
104
105 BlockFieldFlags(uint32_t flags) : flags(flags) {}
106public:
107 BlockFieldFlags() : flags(0) {}
108 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
109
110 uint32_t getBitMask() const { return flags; }
111 bool empty() const { return flags == 0; }
112
113 /// Answers whether the flags indicate that this field is an object
114 /// or block pointer that requires _Block_object_assign/dispose.
115 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
116
118 return BlockFieldFlags(l.flags | r.flags);
119 }
121 l.flags |= r.flags;
122 return l;
123 }
125 return (l.flags & r.flags);
126 }
128 return flags == Other.flags;
129 }
130};
132 return BlockFieldFlags(l) | BlockFieldFlags(r);
133}
134
135/// Information about the layout of a __block variable.
137public:
138 llvm::StructType *Type;
139 unsigned FieldIndex;
142};
143
144/// Represents a type of copy/destroy operation that should be performed for an
145/// entity that's captured by a block.
147 None,
148 CXXRecord, // Copy or destroy
150 ARCWeak,
151 ARCStrong,
153 BlockObject, // Assign or release
154};
155
156/// CGBlockInfo - Information to generate a block literal.
158public:
159 /// Name - The name of the block, kindof.
160 StringRef Name;
161
162 /// The field index of 'this' within the block, if there is one.
163 unsigned CXXThisIndex;
164
165 class Capture {
166 uintptr_t Data;
169
170 /// Type of the capture field. Normally, this is identical to the type of
171 /// the capture's VarDecl, but can be different if there is an enclosing
172 /// lambda.
173 QualType FieldType;
174
175 public:
176 bool isIndex() const { return (Data & 1) != 0; }
177 bool isConstant() const { return !isIndex(); }
178
179 unsigned getIndex() const {
180 assert(isIndex());
181 return Data >> 1;
182 }
184 assert(isIndex());
185 return CharUnits::fromQuantity(Offset);
186 }
188 assert(isIndex());
189 return Cleanup;
190 }
192 assert(isIndex());
193 Cleanup = cleanup;
194 }
195
196 llvm::Value *getConstant() const {
197 assert(isConstant());
198 return reinterpret_cast<llvm::Value*>(Data);
199 }
200
202 return FieldType;
203 }
204
205 static Capture
206 makeIndex(unsigned index, CharUnits offset, QualType FieldType,
209 const BlockDecl::Capture *Cap) {
210 Capture v;
211 v.Data = (index << 1) | 1;
212 v.Offset = offset.getQuantity();
213 v.FieldType = FieldType;
214 v.CopyKind = CopyKind;
218 v.Cap = Cap;
219 return v;
220 }
221
222 static Capture makeConstant(llvm::Value *value,
223 const BlockDecl::Capture *Cap) {
224 Capture v;
225 v.Data = reinterpret_cast<uintptr_t>(value);
226 v.Cap = Cap;
227 return v;
228 }
229
230 bool isConstantOrTrivial() const {
233 }
234
239 };
240
241 /// CanBeGlobal - True if the block can be global, i.e. it has
242 /// no non-constant captures.
243 bool CanBeGlobal : 1;
244
245 /// True if the block has captures that would necessitate custom copy or
246 /// dispose helper functions if the block were escaping.
248
249 /// Indicates whether the block is non-escaping.
250 bool NoEscape : 1;
251
252 /// HasCXXObject - True if the block's custom copy/dispose functions
253 /// need to be run even in GC mode.
254 bool HasCXXObject : 1;
255
256 /// UsesStret : True if the block uses an stret return. Mutable
257 /// because it gets set later in the block-creation process.
258 mutable bool UsesStret : 1;
259
260 /// HasCapturedVariableLayout : True if block has captured variables
261 /// and their layout meta-data has been generated.
263
264 /// Indicates whether an object of a non-external C++ class is captured. This
265 /// bit is used to determine the linkage of the block copy/destroy helper
266 /// functions.
268
269 /// Mapping from variables to pointers to captures in SortedCaptures.
270 llvm::DenseMap<const VarDecl *, Capture *> Captures;
271
272 /// The block's captures. Non-constant captures are sorted by their offsets.
274
275 // Currently we assume that block-pointer types are never signed.
277 llvm::StructType *StructureType;
283
284 // Offset of the gap caused by block header having a smaller
285 // alignment than the alignment of the block descriptor. This
286 // is the gap offset before the first capturued field.
288 // Gap size caused by aligning first field after block header.
289 // This could be zero if no forced alignment is required.
291
293 for (auto &C : SortedCaptures)
294 Captures[C.Cap->getVariable()] = &C;
295 }
296
297 const Capture &getCapture(const VarDecl *var) const {
298 return const_cast<CGBlockInfo*>(this)->getCapture(var);
299 }
301 auto it = Captures.find(var);
302 assert(it != Captures.end() && "no entry for variable!");
303 return *it->second;
304 }
305
306 const BlockDecl *getBlockDecl() const { return Block; }
307 const BlockExpr *getBlockExpr() const {
308 assert(BlockExpression);
309 assert(BlockExpression->getBlockDecl() == Block);
310 return BlockExpression;
311 }
312
313 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
314};
315
316} // end namespace CodeGen
317} // end namespace clang
318
319#endif
Defines the clang::Expr interface and subclasses for C++ expressions.
const char * Data
C Language Family Type Representation.
A class which contains all the information about a particular captured value.
Definition: Decl.h:4640
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4634
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6560
const BlockDecl * getBlockDecl() const
Definition: Expr.h:6572
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
int64_t QuantityType
Definition: CharUnits.h:40
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
Information about the layout of a __block variable.
Definition: CGBlocks.h:136
llvm::StructType * Type
Definition: CGBlocks.h:138
friend BlockFieldFlags & operator|=(BlockFieldFlags &l, BlockFieldFlags r)
Definition: CGBlocks.h:120
uint32_t getBitMask() const
Definition: CGBlocks.h:110
bool operator==(BlockFieldFlags Other) const
Definition: CGBlocks.h:127
BlockFieldFlags(BlockFieldFlag_t flag)
Definition: CGBlocks.h:108
friend bool operator&(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:124
bool isSpecialPointer() const
Answers whether the flags indicate that this field is an object or block pointer that requires _Block...
Definition: CGBlocks.h:115
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r)
Definition: CGBlocks.h:117
friend BlockFlags & operator|=(BlockFlags &l, BlockFlags r)
Definition: CGBlocks.h:72
friend BlockFlags operator|(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:69
friend bool operator&(BlockFlags l, BlockFlags r)
Definition: CGBlocks.h:76
BlockFlags(BlockLiteralFlags flag)
Definition: CGBlocks.h:63
BlockFlags(BlockByrefFlags flag)
Definition: CGBlocks.h:64
bool operator==(BlockFlags r)
Definition: CGBlocks.h:79
BlockFlags(uint32_t flags)
Definition: CGBlocks.h:61
uint32_t getBitMask() const
Definition: CGBlocks.h:66
const BlockDecl::Capture * Cap
Definition: CGBlocks.h:238
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType, BlockCaptureEntityKind CopyKind, BlockFieldFlags CopyFlags, BlockCaptureEntityKind DisposeKind, BlockFieldFlags DisposeFlags, const BlockDecl::Capture *Cap)
Definition: CGBlocks.h:206
BlockCaptureEntityKind CopyKind
Definition: CGBlocks.h:235
EHScopeStack::stable_iterator getCleanup() const
Definition: CGBlocks.h:187
BlockCaptureEntityKind DisposeKind
Definition: CGBlocks.h:236
llvm::Value * getConstant() const
Definition: CGBlocks.h:196
void setCleanup(EHScopeStack::stable_iterator cleanup)
Definition: CGBlocks.h:191
static Capture makeConstant(llvm::Value *value, const BlockDecl::Capture *Cap)
Definition: CGBlocks.h:222
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:157
StringRef Name
Name - The name of the block, kindof.
Definition: CGBlocks.h:160
llvm::DenseMap< const VarDecl *, Capture * > Captures
Mapping from variables to pointers to captures in SortedCaptures.
Definition: CGBlocks.h:270
unsigned CXXThisIndex
The field index of 'this' within the block, if there is one.
Definition: CGBlocks.h:163
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:306
llvm::StructType * StructureType
Definition: CGBlocks.h:277
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:287
bool UsesStret
UsesStret : True if the block uses an stret return.
Definition: CGBlocks.h:258
const BlockExpr * BlockExpression
Definition: CGBlocks.h:279
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:307
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:262
const BlockDecl * Block
Definition: CGBlocks.h:278
bool CapturesNonExternalType
Indicates whether an object of a non-external C++ class is captured.
Definition: CGBlocks.h:267
bool NeedsCopyDispose
True if the block has captures that would necessitate custom copy or dispose helper functions if the ...
Definition: CGBlocks.h:247
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:243
bool HasCXXObject
HasCXXObject - True if the block's custom copy/dispose functions need to be run even in GC mode.
Definition: CGBlocks.h:254
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:290
Capture & getCapture(const VarDecl *var)
Definition: CGBlocks.h:300
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:297
llvm::SmallVector< Capture, 4 > SortedCaptures
The block's captures. Non-constant captures are sorted by their offsets.
Definition: CGBlocks.h:273
bool NoEscape
Indicates whether the block is non-escaping.
Definition: CGBlocks.h:250
A saved depth on the scope stack.
Definition: EHScopeStack.h:106
An abstract representation of an aligned address.
Definition: Address.h:42
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a variable declaration or definition.
Definition: Decl.h:925
Defines the clang::TargetInfo interface.
@ BLOCK_HAS_SIGNATURE
Definition: CGBlocks.h:54
@ BLOCK_IS_NOESCAPE
Definition: CGBlocks.h:49
@ BLOCK_HAS_CXX_OBJ
Definition: CGBlocks.h:51
@ BLOCK_HAS_EXTENDED_LAYOUT
Definition: CGBlocks.h:55
@ BLOCK_HAS_COPY_DISPOSE
Definition: CGBlocks.h:50
@ BLOCK_FIELD_IS_BYREF
Definition: CGBlocks.h:92
@ BLOCK_FIELD_IS_WEAK
Definition: CGBlocks.h:94
@ BLOCK_FIELD_IS_ARC
Definition: CGBlocks.h:96
@ BLOCK_BYREF_CALLER
Definition: CGBlocks.h:97
@ BLOCK_FIELD_IS_BLOCK
Definition: CGBlocks.h:90
@ BLOCK_BYREF_CURRENT_MAX
Definition: CGBlocks.h:99
@ BLOCK_FIELD_IS_OBJECT
Definition: CGBlocks.h:88
@ BLOCK_BYREF_LAYOUT_MASK
Definition: CGBlocks.h:40
@ BLOCK_BYREF_LAYOUT_WEAK
Definition: CGBlocks.h:44
@ BLOCK_BYREF_LAYOUT_STRONG
Definition: CGBlocks.h:43
@ BLOCK_BYREF_LAYOUT_EXTENDED
Definition: CGBlocks.h:41
@ BLOCK_BYREF_LAYOUT_NON_OBJECT
Definition: CGBlocks.h:42
@ BLOCK_BYREF_HAS_COPY_DISPOSE
Definition: CGBlocks.h:39
@ BLOCK_BYREF_LAYOUT_UNRETAINED
Definition: CGBlocks.h:45
BlockCaptureEntityKind
Represents a type of copy/destroy operation that should be performed for an entity that's captured by...
Definition: CGBlocks.h:146
BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r)
Definition: CGBlocks.h:83
const internal::VariadicDynCastAllOfMatcher< Decl, BlockDecl > blockDecl
Matches block declarations.
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...