blob: 698ecd3d926a65251e75a7a87dd1b6c05ea901c9 [file] [log] [blame]
Mike Stump376e3c02009-03-04 15:35:22 +00001//===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mike Stump376e3c02009-03-04 15:35:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This is the internal state used for llvm translation for block literals.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
14#define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
Mike Stump376e3c02009-03-04 15:35:22 +000015
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "CGBuilder.h"
17#include "CGCall.h"
18#include "CGValue.h"
19#include "CodeGenFunction.h"
Mike Stump95435672009-03-04 18:17:45 +000020#include "CodeGenTypes.h"
Ken Dyck40775002010-01-11 17:06:35 +000021#include "clang/AST/CharUnits.h"
Mike Stump95435672009-03-04 18:17:45 +000022#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/AST/Type.h"
26#include "clang/Basic/TargetInfo.h"
Mike Stump95435672009-03-04 18:17:45 +000027
28namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000029class Constant;
30class Function;
31class GlobalValue;
32class DataLayout;
33class FunctionType;
34class PointerType;
35class Value;
36class LLVMContext;
Alexander Kornienkoab9db512015-06-22 23:07:51 +000037}
Mike Stump95435672009-03-04 18:17:45 +000038
Mike Stump376e3c02009-03-04 15:35:22 +000039namespace clang {
Mike Stump376e3c02009-03-04 15:35:22 +000040namespace CodeGen {
John McCallad7c5c12011-02-08 08:22:06 +000041
John McCall351762c2011-02-07 10:33:21 +000042class CGBlockInfo;
Mike Stump376e3c02009-03-04 15:35:22 +000043
Fariborz Jahanian77599ce2012-10-26 01:13:38 +000044// Flags stored in __block variables.
45enum BlockByrefFlags {
46 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
47 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
48 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
49 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
50 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
Fariborz Jahanian5f8d3242012-10-26 20:33:59 +000051 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
52 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
Fariborz Jahanian77599ce2012-10-26 01:13:38 +000053};
54
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000055enum BlockLiteralFlags {
Akira Hatanakadbfa4532018-07-20 17:10:32 +000056 BLOCK_IS_NOESCAPE = (1 << 23),
John McCallad7c5c12011-02-08 08:22:06 +000057 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
58 BLOCK_HAS_CXX_OBJ = (1 << 26),
59 BLOCK_IS_GLOBAL = (1 << 28),
60 BLOCK_USE_STRET = (1 << 29),
Fariborz Jahaniane14f98e2012-11-10 18:30:40 +000061 BLOCK_HAS_SIGNATURE = (1 << 30),
Benjamin Kramer0181e7a2018-09-24 17:51:15 +000062 BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
Mike Stump376e3c02009-03-04 15:35:22 +000063};
John McCallad7c5c12011-02-08 08:22:06 +000064class BlockFlags {
65 uint32_t flags;
Mike Stump376e3c02009-03-04 15:35:22 +000066
Mike Stump95435672009-03-04 18:17:45 +000067public:
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000068 BlockFlags(uint32_t flags) : flags(flags) {}
John McCallad7c5c12011-02-08 08:22:06 +000069 BlockFlags() : flags(0) {}
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000070 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000071 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
Fangrui Song6907ce22018-07-30 19:24:48 +000072
John McCallad7c5c12011-02-08 08:22:06 +000073 uint32_t getBitMask() const { return flags; }
74 bool empty() const { return flags == 0; }
Mike Stump95435672009-03-04 18:17:45 +000075
John McCallad7c5c12011-02-08 08:22:06 +000076 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
77 return BlockFlags(l.flags | r.flags);
78 }
79 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
80 l.flags |= r.flags;
81 return l;
82 }
83 friend bool operator&(BlockFlags l, BlockFlags r) {
84 return (l.flags & r.flags);
Mike Stump95435672009-03-04 18:17:45 +000085 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000086 bool operator==(BlockFlags r) {
87 return (flags == r.flags);
88 }
Mike Stump376e3c02009-03-04 15:35:22 +000089};
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000090inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
John McCallad7c5c12011-02-08 08:22:06 +000091 return BlockFlags(l) | BlockFlags(r);
92}
Mike Stump376e3c02009-03-04 15:35:22 +000093
John McCallad7c5c12011-02-08 08:22:06 +000094enum BlockFieldFlag_t {
95 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
96 block, ... */
97 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
Mike Stump626aecc2009-03-05 01:23:13 +000098
John McCallad7c5c12011-02-08 08:22:06 +000099 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 */
John McCall31168b02011-06-15 23:02:42 +0000103 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCallad7c5c12011-02-08 08:22:06 +0000104 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stump376e3c02009-03-04 15:35:22 +0000105 support routines */
John McCallad7c5c12011-02-08 08:22:06 +0000106 BLOCK_BYREF_CURRENT_MAX = 256
107};
108
109class BlockFieldFlags {
110 uint32_t flags;
111
112 BlockFieldFlags(uint32_t flags) : flags(flags) {}
113public:
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
124 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
125 return BlockFieldFlags(l.flags | r.flags);
126 }
127 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
128 l.flags |= r.flags;
129 return l;
130 }
131 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
132 return (l.flags & r.flags);
133 }
Akira Hatanaka2ec36f02018-08-17 15:46:07 +0000134 bool operator==(BlockFieldFlags Other) const {
135 return flags == Other.flags;
136 }
John McCallad7c5c12011-02-08 08:22:06 +0000137};
138inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
139 return BlockFieldFlags(l) | BlockFieldFlags(r);
140}
141
John McCall7f416cc2015-09-08 08:05:57 +0000142/// Information about the layout of a __block variable.
143class BlockByrefInfo {
144public:
145 llvm::StructType *Type;
146 unsigned FieldIndex;
147 CharUnits ByrefAlignment;
148 CharUnits FieldOffset;
149};
150
John McCallad7c5c12011-02-08 08:22:06 +0000151/// CGBlockInfo - Information to generate a block literal.
152class CGBlockInfo {
153public:
154 /// Name - The name of the block, kindof.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000155 StringRef Name;
John McCallad7c5c12011-02-08 08:22:06 +0000156
157 /// The field index of 'this' within the block, if there is one.
158 unsigned CXXThisIndex;
159
160 class Capture {
161 uintptr_t Data;
John McCall08ef4662011-11-10 08:15:53 +0000162 EHScopeStack::stable_iterator Cleanup;
John McCall7f416cc2015-09-08 08:05:57 +0000163 CharUnits::QuantityType Offset;
John McCallad7c5c12011-02-08 08:22:06 +0000164
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000165 /// 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
John McCallad7c5c12011-02-08 08:22:06 +0000170 public:
171 bool isIndex() const { return (Data & 1) != 0; }
172 bool isConstant() const { return !isIndex(); }
John McCall7f416cc2015-09-08 08:05:57 +0000173
174 unsigned getIndex() const {
175 assert(isIndex());
176 return Data >> 1;
177 }
178 CharUnits getOffset() const {
179 assert(isIndex());
180 return CharUnits::fromQuantity(Offset);
John McCallad7c5c12011-02-08 08:22:06 +0000181 }
John McCall08ef4662011-11-10 08:15:53 +0000182 EHScopeStack::stable_iterator getCleanup() const {
183 assert(isIndex());
184 return Cleanup;
185 }
186 void setCleanup(EHScopeStack::stable_iterator cleanup) {
187 assert(isIndex());
188 Cleanup = cleanup;
189 }
John McCallad7c5c12011-02-08 08:22:06 +0000190
John McCall7f416cc2015-09-08 08:05:57 +0000191 llvm::Value *getConstant() const {
192 assert(isConstant());
193 return reinterpret_cast<llvm::Value*>(Data);
194 }
195
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000196 QualType fieldType() const {
197 return FieldType;
198 }
199
200 static Capture makeIndex(unsigned index, CharUnits offset,
201 QualType FieldType) {
John McCallad7c5c12011-02-08 08:22:06 +0000202 Capture v;
203 v.Data = (index << 1) | 1;
John McCall7f416cc2015-09-08 08:05:57 +0000204 v.Offset = offset.getQuantity();
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000205 v.FieldType = FieldType;
John McCallad7c5c12011-02-08 08:22:06 +0000206 return v;
207 }
208
209 static Capture makeConstant(llvm::Value *value) {
210 Capture v;
211 v.Data = reinterpret_cast<uintptr_t>(value);
212 return v;
Fangrui Song6907ce22018-07-30 19:24:48 +0000213 }
Mike Stump376e3c02009-03-04 15:35:22 +0000214 };
Mike Stump06acea8a2009-03-04 18:57:26 +0000215
John McCallad7c5c12011-02-08 08:22:06 +0000216 /// CanBeGlobal - True if the block can be global, i.e. it has
217 /// no non-constant captures.
218 bool CanBeGlobal : 1;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000219
Akira Hatanakadbfa4532018-07-20 17:10:32 +0000220 /// True if the block has captures that would necessitate custom copy or
221 /// dispose helper functions if the block were escaping.
John McCallad7c5c12011-02-08 08:22:06 +0000222 bool NeedsCopyDispose : 1;
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000223
John McCallad7c5c12011-02-08 08:22:06 +0000224 /// HasCXXObject - True if the block's custom copy/dispose functions
225 /// need to be run even in GC mode.
226 bool HasCXXObject : 1;
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000227
John McCall85915252011-03-09 08:39:33 +0000228 /// 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;
Fangrui Song6907ce22018-07-30 19:24:48 +0000231
Fariborz Jahanian23290b02012-11-01 18:32:55 +0000232 /// HasCapturedVariableLayout : True if block has captured variables
233 /// and their layout meta-data has been generated.
234 bool HasCapturedVariableLayout : 1;
John McCall85915252011-03-09 08:39:33 +0000235
Akira Hatanaka9978da32018-08-10 15:09:24 +0000236 /// 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.
239 bool CapturesNonExternalType : 1;
240
John McCall08ef4662011-11-10 08:15:53 +0000241 /// The mapping of allocated indexes within the block.
Fangrui Song6907ce22018-07-30 19:24:48 +0000242 llvm::DenseMap<const VarDecl*, Capture> Captures;
John McCall08ef4662011-11-10 08:15:53 +0000243
John McCall7f416cc2015-09-08 08:05:57 +0000244 Address LocalAddress;
Chris Lattner2192fe52011-07-18 04:24:23 +0000245 llvm::StructType *StructureType;
John McCall08ef4662011-11-10 08:15:53 +0000246 const BlockDecl *Block;
247 const BlockExpr *BlockExpression;
John McCallad7c5c12011-02-08 08:22:06 +0000248 CharUnits BlockSize;
249 CharUnits BlockAlign;
John McCall7f416cc2015-09-08 08:05:57 +0000250 CharUnits CXXThisOffset;
Fangrui Song6907ce22018-07-30 19:24:48 +0000251
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +0000252 // 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.
255 CharUnits BlockHeaderForcedGapOffset;
256 // Gap size caused by aligning first field after block header.
257 // This could be zero if no forced alignment is required.
258 CharUnits BlockHeaderForcedGapSize;
John McCallf4beacd2011-11-10 10:43:54 +0000259
John McCallf4beacd2011-11-10 10:43:54 +0000260 /// 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.
John McCall08ef4662011-11-10 08:15:53 +0000264 CGBlockInfo *NextBlockInfo;
Mike Stump06acea8a2009-03-04 18:57:26 +0000265
John McCallad7c5c12011-02-08 08:22:06 +0000266 const Capture &getCapture(const VarDecl *var) const {
John McCall08ef4662011-11-10 08:15:53 +0000267 return const_cast<CGBlockInfo*>(this)->getCapture(var);
268 }
269 Capture &getCapture(const VarDecl *var) {
270 llvm::DenseMap<const VarDecl*, Capture>::iterator
John McCallad7c5c12011-02-08 08:22:06 +0000271 it = Captures.find(var);
272 assert(it != Captures.end() && "no entry for variable!");
273 return it->second;
274 }
275
John McCall08ef4662011-11-10 08:15:53 +0000276 const BlockDecl *getBlockDecl() const { return Block; }
277 const BlockExpr *getBlockExpr() const {
278 assert(BlockExpression);
279 assert(BlockExpression->getBlockDecl() == Block);
280 return BlockExpression;
281 }
John McCallad7c5c12011-02-08 08:22:06 +0000282
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000283 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
Akira Hatanakadbfa4532018-07-20 17:10:32 +0000284
285 // Indicates whether the block needs a custom copy or dispose function.
286 bool needsCopyDisposeHelpers() const {
287 return NeedsCopyDispose && !Block->doesNotEscape();
288 }
Mike Stump376e3c02009-03-04 15:35:22 +0000289};
290
291} // end namespace CodeGen
292} // end namespace clang
293
294#endif