blob: 3f9fc16d9b101ec54d046bf46a6b07b8d45d705b [file] [log] [blame]
Mike Stump376e3c02009-03-04 15:35:22 +00001//===-- 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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15#define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
Mike Stump376e3c02009-03-04 15:35:22 +000016
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGBuilder.h"
18#include "CGCall.h"
19#include "CGValue.h"
20#include "CodeGenFunction.h"
Mike Stump95435672009-03-04 18:17:45 +000021#include "CodeGenTypes.h"
Ken Dyck40775002010-01-11 17:06:35 +000022#include "clang/AST/CharUnits.h"
Mike Stump95435672009-03-04 18:17:45 +000023#include "clang/AST/Expr.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/ExprObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/Type.h"
27#include "clang/Basic/TargetInfo.h"
Mike Stump95435672009-03-04 18:17:45 +000028
29namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000030class Constant;
31class Function;
32class GlobalValue;
33class DataLayout;
34class FunctionType;
35class PointerType;
36class Value;
37class LLVMContext;
Alexander Kornienkoab9db512015-06-22 23:07:51 +000038}
Mike Stump95435672009-03-04 18:17:45 +000039
Mike Stump376e3c02009-03-04 15:35:22 +000040namespace clang {
Mike Stump376e3c02009-03-04 15:35:22 +000041namespace CodeGen {
John McCallad7c5c12011-02-08 08:22:06 +000042
John McCall351762c2011-02-07 10:33:21 +000043class CGBlockInfo;
Mike Stump376e3c02009-03-04 15:35:22 +000044
Fariborz Jahanian77599ce2012-10-26 01:13:38 +000045// Flags stored in __block variables.
46enum BlockByrefFlags {
47 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
48 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
49 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
50 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
51 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
Fariborz Jahanian5f8d3242012-10-26 20:33:59 +000052 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
53 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
Fariborz Jahanian77599ce2012-10-26 01:13:38 +000054};
55
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000056enum BlockLiteralFlags {
Akira Hatanakadbfa4532018-07-20 17:10:32 +000057 BLOCK_IS_NOESCAPE = (1 << 23),
John McCallad7c5c12011-02-08 08:22:06 +000058 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
59 BLOCK_HAS_CXX_OBJ = (1 << 26),
60 BLOCK_IS_GLOBAL = (1 << 28),
61 BLOCK_USE_STRET = (1 << 29),
Fariborz Jahaniane14f98e2012-11-10 18:30:40 +000062 BLOCK_HAS_SIGNATURE = (1 << 30),
Benjamin Kramer0181e7a2018-09-24 17:51:15 +000063 BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
Mike Stump376e3c02009-03-04 15:35:22 +000064};
John McCallad7c5c12011-02-08 08:22:06 +000065class BlockFlags {
66 uint32_t flags;
Mike Stump376e3c02009-03-04 15:35:22 +000067
Mike Stump95435672009-03-04 18:17:45 +000068public:
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000069 BlockFlags(uint32_t flags) : flags(flags) {}
John McCallad7c5c12011-02-08 08:22:06 +000070 BlockFlags() : flags(0) {}
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000071 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000072 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
Fangrui Song6907ce22018-07-30 19:24:48 +000073
John McCallad7c5c12011-02-08 08:22:06 +000074 uint32_t getBitMask() const { return flags; }
75 bool empty() const { return flags == 0; }
Mike Stump95435672009-03-04 18:17:45 +000076
John McCallad7c5c12011-02-08 08:22:06 +000077 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
78 return BlockFlags(l.flags | r.flags);
79 }
80 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
81 l.flags |= r.flags;
82 return l;
83 }
84 friend bool operator&(BlockFlags l, BlockFlags r) {
85 return (l.flags & r.flags);
Mike Stump95435672009-03-04 18:17:45 +000086 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +000087 bool operator==(BlockFlags r) {
88 return (flags == r.flags);
89 }
Mike Stump376e3c02009-03-04 15:35:22 +000090};
Fariborz Jahaniana3926ec2012-10-25 22:55:52 +000091inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
John McCallad7c5c12011-02-08 08:22:06 +000092 return BlockFlags(l) | BlockFlags(r);
93}
Mike Stump376e3c02009-03-04 15:35:22 +000094
John McCallad7c5c12011-02-08 08:22:06 +000095enum BlockFieldFlag_t {
96 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
97 block, ... */
98 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
Mike Stump626aecc2009-03-05 01:23:13 +000099
John McCallad7c5c12011-02-08 08:22:06 +0000100 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 */
John McCall31168b02011-06-15 23:02:42 +0000104 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCallad7c5c12011-02-08 08:22:06 +0000105 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stump376e3c02009-03-04 15:35:22 +0000106 support routines */
John McCallad7c5c12011-02-08 08:22:06 +0000107 BLOCK_BYREF_CURRENT_MAX = 256
108};
109
110class BlockFieldFlags {
111 uint32_t flags;
112
113 BlockFieldFlags(uint32_t flags) : flags(flags) {}
114public:
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
125 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
126 return BlockFieldFlags(l.flags | r.flags);
127 }
128 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
129 l.flags |= r.flags;
130 return l;
131 }
132 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
133 return (l.flags & r.flags);
134 }
Akira Hatanaka2ec36f02018-08-17 15:46:07 +0000135 bool operator==(BlockFieldFlags Other) const {
136 return flags == Other.flags;
137 }
John McCallad7c5c12011-02-08 08:22:06 +0000138};
139inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
140 return BlockFieldFlags(l) | BlockFieldFlags(r);
141}
142
John McCall7f416cc2015-09-08 08:05:57 +0000143/// Information about the layout of a __block variable.
144class BlockByrefInfo {
145public:
146 llvm::StructType *Type;
147 unsigned FieldIndex;
148 CharUnits ByrefAlignment;
149 CharUnits FieldOffset;
150};
151
John McCallad7c5c12011-02-08 08:22:06 +0000152/// CGBlockInfo - Information to generate a block literal.
153class CGBlockInfo {
154public:
155 /// Name - The name of the block, kindof.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000156 StringRef Name;
John McCallad7c5c12011-02-08 08:22:06 +0000157
158 /// The field index of 'this' within the block, if there is one.
159 unsigned CXXThisIndex;
160
161 class Capture {
162 uintptr_t Data;
John McCall08ef4662011-11-10 08:15:53 +0000163 EHScopeStack::stable_iterator Cleanup;
John McCall7f416cc2015-09-08 08:05:57 +0000164 CharUnits::QuantityType Offset;
John McCallad7c5c12011-02-08 08:22:06 +0000165
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000166 /// Type of the capture field. Normally, this is identical to the type of
167 /// the capture's VarDecl, but can be different if there is an enclosing
168 /// lambda.
169 QualType FieldType;
170
John McCallad7c5c12011-02-08 08:22:06 +0000171 public:
172 bool isIndex() const { return (Data & 1) != 0; }
173 bool isConstant() const { return !isIndex(); }
John McCall7f416cc2015-09-08 08:05:57 +0000174
175 unsigned getIndex() const {
176 assert(isIndex());
177 return Data >> 1;
178 }
179 CharUnits getOffset() const {
180 assert(isIndex());
181 return CharUnits::fromQuantity(Offset);
John McCallad7c5c12011-02-08 08:22:06 +0000182 }
John McCall08ef4662011-11-10 08:15:53 +0000183 EHScopeStack::stable_iterator getCleanup() const {
184 assert(isIndex());
185 return Cleanup;
186 }
187 void setCleanup(EHScopeStack::stable_iterator cleanup) {
188 assert(isIndex());
189 Cleanup = cleanup;
190 }
John McCallad7c5c12011-02-08 08:22:06 +0000191
John McCall7f416cc2015-09-08 08:05:57 +0000192 llvm::Value *getConstant() const {
193 assert(isConstant());
194 return reinterpret_cast<llvm::Value*>(Data);
195 }
196
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000197 QualType fieldType() const {
198 return FieldType;
199 }
200
201 static Capture makeIndex(unsigned index, CharUnits offset,
202 QualType FieldType) {
John McCallad7c5c12011-02-08 08:22:06 +0000203 Capture v;
204 v.Data = (index << 1) | 1;
John McCall7f416cc2015-09-08 08:05:57 +0000205 v.Offset = offset.getQuantity();
Akira Hatanakad542ccf2016-09-16 00:02:06 +0000206 v.FieldType = FieldType;
John McCallad7c5c12011-02-08 08:22:06 +0000207 return v;
208 }
209
210 static Capture makeConstant(llvm::Value *value) {
211 Capture v;
212 v.Data = reinterpret_cast<uintptr_t>(value);
213 return v;
Fangrui Song6907ce22018-07-30 19:24:48 +0000214 }
Mike Stump376e3c02009-03-04 15:35:22 +0000215 };
Mike Stump06acea8a2009-03-04 18:57:26 +0000216
John McCallad7c5c12011-02-08 08:22:06 +0000217 /// CanBeGlobal - True if the block can be global, i.e. it has
218 /// no non-constant captures.
219 bool CanBeGlobal : 1;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000220
Akira Hatanakadbfa4532018-07-20 17:10:32 +0000221 /// True if the block has captures that would necessitate custom copy or
222 /// dispose helper functions if the block were escaping.
John McCallad7c5c12011-02-08 08:22:06 +0000223 bool NeedsCopyDispose : 1;
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000224
John McCallad7c5c12011-02-08 08:22:06 +0000225 /// HasCXXObject - True if the block's custom copy/dispose functions
226 /// need to be run even in GC mode.
227 bool HasCXXObject : 1;
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000228
John McCall85915252011-03-09 08:39:33 +0000229 /// UsesStret : True if the block uses an stret return. Mutable
230 /// because it gets set later in the block-creation process.
231 mutable bool UsesStret : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000232
Fariborz Jahanian23290b02012-11-01 18:32:55 +0000233 /// HasCapturedVariableLayout : True if block has captured variables
234 /// and their layout meta-data has been generated.
235 bool HasCapturedVariableLayout : 1;
John McCall85915252011-03-09 08:39:33 +0000236
Akira Hatanaka9978da32018-08-10 15:09:24 +0000237 /// Indicates whether an object of a non-external C++ class is captured. This
238 /// bit is used to determine the linkage of the block copy/destroy helper
239 /// functions.
240 bool CapturesNonExternalType : 1;
241
John McCall08ef4662011-11-10 08:15:53 +0000242 /// The mapping of allocated indexes within the block.
Fangrui Song6907ce22018-07-30 19:24:48 +0000243 llvm::DenseMap<const VarDecl*, Capture> Captures;
John McCall08ef4662011-11-10 08:15:53 +0000244
John McCall7f416cc2015-09-08 08:05:57 +0000245 Address LocalAddress;
Chris Lattner2192fe52011-07-18 04:24:23 +0000246 llvm::StructType *StructureType;
John McCall08ef4662011-11-10 08:15:53 +0000247 const BlockDecl *Block;
248 const BlockExpr *BlockExpression;
John McCallad7c5c12011-02-08 08:22:06 +0000249 CharUnits BlockSize;
250 CharUnits BlockAlign;
John McCall7f416cc2015-09-08 08:05:57 +0000251 CharUnits CXXThisOffset;
Fangrui Song6907ce22018-07-30 19:24:48 +0000252
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +0000253 // Offset of the gap caused by block header having a smaller
254 // alignment than the alignment of the block descriptor. This
255 // is the gap offset before the first capturued field.
256 CharUnits BlockHeaderForcedGapOffset;
257 // Gap size caused by aligning first field after block header.
258 // This could be zero if no forced alignment is required.
259 CharUnits BlockHeaderForcedGapSize;
John McCallf4beacd2011-11-10 10:43:54 +0000260
261 /// An instruction which dominates the full-expression that the
262 /// block is inside.
263 llvm::Instruction *DominatingIP;
264
265 /// The next block in the block-info chain. Invalid if this block
266 /// info is not part of the CGF's block-info chain, which is true
267 /// if it corresponds to a global block or a block whose expression
268 /// has been encountered.
John McCall08ef4662011-11-10 08:15:53 +0000269 CGBlockInfo *NextBlockInfo;
Mike Stump06acea8a2009-03-04 18:57:26 +0000270
John McCallad7c5c12011-02-08 08:22:06 +0000271 const Capture &getCapture(const VarDecl *var) const {
John McCall08ef4662011-11-10 08:15:53 +0000272 return const_cast<CGBlockInfo*>(this)->getCapture(var);
273 }
274 Capture &getCapture(const VarDecl *var) {
275 llvm::DenseMap<const VarDecl*, Capture>::iterator
John McCallad7c5c12011-02-08 08:22:06 +0000276 it = Captures.find(var);
277 assert(it != Captures.end() && "no entry for variable!");
278 return it->second;
279 }
280
John McCall08ef4662011-11-10 08:15:53 +0000281 const BlockDecl *getBlockDecl() const { return Block; }
282 const BlockExpr *getBlockExpr() const {
283 assert(BlockExpression);
284 assert(BlockExpression->getBlockDecl() == Block);
285 return BlockExpression;
286 }
John McCallad7c5c12011-02-08 08:22:06 +0000287
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000288 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
Akira Hatanakadbfa4532018-07-20 17:10:32 +0000289
290 // Indicates whether the block needs a custom copy or dispose function.
291 bool needsCopyDisposeHelpers() const {
292 return NeedsCopyDispose && !Block->doesNotEscape();
293 }
Mike Stump376e3c02009-03-04 15:35:22 +0000294};
295
296} // end namespace CodeGen
297} // end namespace clang
298
299#endif