clang 20.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
149 ARCWeak,
150 ARCStrong,
152 BlockObject, // Assign or release
153};
154
155/// CGBlockInfo - Information to generate a block literal.
157public:
158 /// Name - The name of the block, kindof.
159 StringRef Name;
160
161 /// The field index of 'this' within the block, if there is one.
162 unsigned CXXThisIndex;
163
164 class Capture {
165 uintptr_t Data;
168
169 /// Type of the capture field. Normally, this is identical to the type of
170 /// the capture's VarDecl, but can be different if there is an enclosing
171 /// lambda.
172 QualType FieldType;
173
174 public:
175 bool isIndex() const { return (Data & 1) != 0; }
176 bool isConstant() const { return !isIndex(); }
177
178 unsigned getIndex() const {
179 assert(isIndex());
180 return Data >> 1;
181 }
183 assert(isIndex());
184 return CharUnits::fromQuantity(Offset);
185 }
187 assert(isIndex());
188 return Cleanup;
189 }
191 assert(isIndex());
192 Cleanup = cleanup;
193 }
194
195 llvm::Value *getConstant() const {
196 assert(isConstant());
197 return reinterpret_cast<llvm::Value*>(Data);
198 }
199
201 return FieldType;
202 }
203
204 static Capture
205 makeIndex(unsigned index, CharUnits offset, QualType FieldType,
208 const BlockDecl::Capture *Cap) {
209 Capture v;
210 v.Data = (index << 1) | 1;
211 v.Offset = offset.getQuantity();
212 v.FieldType = FieldType;
213 v.CopyKind = CopyKind;
214 v.CopyFlags = CopyFlags;
215 v.DisposeKind = DisposeKind;
216 v.DisposeFlags = DisposeFlags;
217 v.Cap = Cap;
218 return v;
219 }
220
221 static Capture makeConstant(llvm::Value *value,
222 const BlockDecl::Capture *Cap) {
223 Capture v;
224 v.Data = reinterpret_cast<uintptr_t>(value);
225 v.Cap = Cap;
226 return v;
227 }
228
229 bool isConstantOrTrivial() const {
232 }
233
238 };
239
240 /// CanBeGlobal - True if the block can be global, i.e. it has
241 /// no non-constant captures.
242 bool CanBeGlobal : 1;
243
244 /// True if the block has captures that would necessitate custom copy or
245 /// dispose helper functions if the block were escaping.
247
248 /// Indicates whether the block is non-escaping.
249 bool NoEscape : 1;
250
251 /// HasCXXObject - True if the block's custom copy/dispose functions
252 /// need to be run even in GC mode.
253 bool HasCXXObject : 1;
254
255 /// UsesStret : True if the block uses an stret return. Mutable
256 /// because it gets set later in the block-creation process.
257 mutable bool UsesStret : 1;
258
259 /// HasCapturedVariableLayout : True if block has captured variables
260 /// and their layout meta-data has been generated.
262
263 /// Indicates whether an object of a non-external C++ class is captured. This
264 /// bit is used to determine the linkage of the block copy/destroy helper
265 /// functions.
267
268 /// Mapping from variables to pointers to captures in SortedCaptures.
269 llvm::DenseMap<const VarDecl *, Capture *> Captures;
270
271 /// The block's captures. Non-constant captures are sorted by their offsets.
273
274 // Currently we assume that block-pointer types are never signed.
276 llvm::StructType *StructureType;
278 const