blob: 62635623857114fdcabe21af9b139db16f0f455b [file] [log] [blame]
Mike Stumpd883d842009-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
14#ifndef CLANG_CODEGEN_CGBLOCKS_H
15#define CLANG_CODEGEN_CGBLOCKS_H
16
Mike Stump2a998142009-03-04 18:17:45 +000017#include "CodeGenTypes.h"
18#include "clang/AST/Type.h"
Owen Andersona1cf15f2009-07-14 23:10:40 +000019#include "llvm/Module.h"
Mike Stump2a998142009-03-04 18:17:45 +000020#include "clang/Basic/TargetInfo.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000021#include "clang/AST/CharUnits.h"
Mike Stump2a998142009-03-04 18:17:45 +000022#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
25
John McCall1a343eb2011-11-10 08:15:53 +000026#include "CodeGenFunction.h"
Mike Stump2a998142009-03-04 18:17:45 +000027#include "CGBuilder.h"
28#include "CGCall.h"
29#include "CGValue.h"
30
31namespace llvm {
32 class Module;
33 class Constant;
34 class Function;
35 class GlobalValue;
Micah Villmow25a6a842012-10-08 16:25:52 +000036 class DataLayout;
Mike Stump2a998142009-03-04 18:17:45 +000037 class FunctionType;
John McCall3d3ec1c2010-04-21 10:05:39 +000038 class PointerType;
Mike Stump2a998142009-03-04 18:17:45 +000039 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000040 class LLVMContext;
Mike Stump2a998142009-03-04 18:17:45 +000041}
42
Mike Stumpd883d842009-03-04 15:35:22 +000043namespace clang {
44
45namespace CodeGen {
John McCalld16c2cf2011-02-08 08:22:06 +000046
Mike Stump90a90432009-03-04 18:47:42 +000047class CodeGenModule;
John McCall6b5a61b2011-02-07 10:33:21 +000048class CGBlockInfo;
Mike Stumpd883d842009-03-04 15:35:22 +000049
Fariborz Jahanian3dac20a2012-10-26 01:13:38 +000050// Flags stored in __block variables.
51enum BlockByrefFlags {
52 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
53 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
54 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
55 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
56 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
Fariborz Jahanian7b5209c2012-10-26 20:33:59 +000057 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
58 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
Fariborz Jahanian3dac20a2012-10-26 01:13:38 +000059};
60
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000061enum BlockLiteralFlags {
Fariborz Jahanianf22ae652012-11-01 18:32:55 +000062 BLOCK_HAS_EXTENDED_LAYOUT = (1 << 23),
John McCalld16c2cf2011-02-08 08:22:06 +000063 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
64 BLOCK_HAS_CXX_OBJ = (1 << 26),
65 BLOCK_IS_GLOBAL = (1 << 28),
66 BLOCK_USE_STRET = (1 << 29),
67 BLOCK_HAS_SIGNATURE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000068};
John McCalld16c2cf2011-02-08 08:22:06 +000069class BlockFlags {
70 uint32_t flags;
Mike Stumpd883d842009-03-04 15:35:22 +000071
John McCalld16c2cf2011-02-08 08:22:06 +000072 BlockFlags(uint32_t flags) : flags(flags) {}
Mike Stump2a998142009-03-04 18:17:45 +000073public:
John McCalld16c2cf2011-02-08 08:22:06 +000074 BlockFlags() : flags(0) {}
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000075 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
Mike Stump2a998142009-03-04 18:17:45 +000076
John McCalld16c2cf2011-02-08 08:22:06 +000077 uint32_t getBitMask() const { return flags; }
78 bool empty() const { return flags == 0; }
Mike Stump2a998142009-03-04 18:17:45 +000079
John McCalld16c2cf2011-02-08 08:22:06 +000080 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
81 return BlockFlags(l.flags | r.flags);
82 }
83 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
84 l.flags |= r.flags;
85 return l;
86 }
87 friend bool operator&(BlockFlags l, BlockFlags r) {
88 return (l.flags & r.flags);
Mike Stump2a998142009-03-04 18:17:45 +000089 }
Mike Stumpd883d842009-03-04 15:35:22 +000090};
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000091inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
John McCalld16c2cf2011-02-08 08:22:06 +000092 return BlockFlags(l) | BlockFlags(r);
93}
Mike Stumpd883d842009-03-04 15:35:22 +000094
John McCalld16c2cf2011-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 Stump797b6322009-03-05 01:23:13 +000099
John McCalld16c2cf2011-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 McCallf85e1932011-06-15 23:02:42 +0000104 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCalld16c2cf2011-02-08 08:22:06 +0000105 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +0000106 support routines */
John McCalld16c2cf2011-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 }
135};
136inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
137 return BlockFieldFlags(l) | BlockFieldFlags(r);
138}
139
140/// CGBlockInfo - Information to generate a block literal.
141class CGBlockInfo {
142public:
143 /// Name - The name of the block, kindof.
John McCall1a343eb2011-11-10 08:15:53 +0000144 llvm::StringRef Name;
John McCalld16c2cf2011-02-08 08:22:06 +0000145
146 /// The field index of 'this' within the block, if there is one.
147 unsigned CXXThisIndex;
148
149 class Capture {
150 uintptr_t Data;
John McCall1a343eb2011-11-10 08:15:53 +0000151 EHScopeStack::stable_iterator Cleanup;
John McCalld16c2cf2011-02-08 08:22:06 +0000152
153 public:
154 bool isIndex() const { return (Data & 1) != 0; }
155 bool isConstant() const { return !isIndex(); }
156 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
157 llvm::Value *getConstant() const {
158 assert(isConstant());
159 return reinterpret_cast<llvm::Value*>(Data);
160 }
John McCall1a343eb2011-11-10 08:15:53 +0000161 EHScopeStack::stable_iterator getCleanup() const {
162 assert(isIndex());
163 return Cleanup;
164 }
165 void setCleanup(EHScopeStack::stable_iterator cleanup) {
166 assert(isIndex());
167 Cleanup = cleanup;
168 }
John McCalld16c2cf2011-02-08 08:22:06 +0000169
170 static Capture makeIndex(unsigned index) {
171 Capture v;
172 v.Data = (index << 1) | 1;
173 return v;
174 }
175
176 static Capture makeConstant(llvm::Value *value) {
177 Capture v;
178 v.Data = reinterpret_cast<uintptr_t>(value);
179 return v;
180 }
Mike Stumpd883d842009-03-04 15:35:22 +0000181 };
Mike Stump3947de52009-03-04 18:57:26 +0000182
John McCalld16c2cf2011-02-08 08:22:06 +0000183 /// CanBeGlobal - True if the block can be global, i.e. it has
184 /// no non-constant captures.
185 bool CanBeGlobal : 1;
Mike Stump08920992009-03-07 02:35:30 +0000186
John McCalld16c2cf2011-02-08 08:22:06 +0000187 /// True if the block needs a custom copy or dispose function.
188 bool NeedsCopyDispose : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000189
John McCalld16c2cf2011-02-08 08:22:06 +0000190 /// HasCXXObject - True if the block's custom copy/dispose functions
191 /// need to be run even in GC mode.
192 bool HasCXXObject : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000193
John McCall64cd2322011-03-09 08:39:33 +0000194 /// UsesStret : True if the block uses an stret return. Mutable
195 /// because it gets set later in the block-creation process.
196 mutable bool UsesStret : 1;
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000197
198 /// HasCapturedVariableLayout : True if block has captured variables
199 /// and their layout meta-data has been generated.
200 bool HasCapturedVariableLayout : 1;
John McCall64cd2322011-03-09 08:39:33 +0000201
John McCall1a343eb2011-11-10 08:15:53 +0000202 /// The mapping of allocated indexes within the block.
203 llvm::DenseMap<const VarDecl*, Capture> Captures;
204
205 llvm::AllocaInst *Address;
Chris Lattner2acc6e32011-07-18 04:24:23 +0000206 llvm::StructType *StructureType;
John McCall1a343eb2011-11-10 08:15:53 +0000207 const BlockDecl *Block;
208 const BlockExpr *BlockExpression;
John McCalld16c2cf2011-02-08 08:22:06 +0000209 CharUnits BlockSize;
210 CharUnits BlockAlign;
John McCall6f103ba2011-11-10 10:43:54 +0000211
212 /// An instruction which dominates the full-expression that the
213 /// block is inside.
214 llvm::Instruction *DominatingIP;
215
216 /// The next block in the block-info chain. Invalid if this block
217 /// info is not part of the CGF's block-info chain, which is true
218 /// if it corresponds to a global block or a block whose expression
219 /// has been encountered.
John McCall1a343eb2011-11-10 08:15:53 +0000220 CGBlockInfo *NextBlockInfo;
Mike Stump3947de52009-03-04 18:57:26 +0000221
John McCalld16c2cf2011-02-08 08:22:06 +0000222 const Capture &getCapture(const VarDecl *var) const {
John McCall1a343eb2011-11-10 08:15:53 +0000223 return const_cast<CGBlockInfo*>(this)->getCapture(var);
224 }
225 Capture &getCapture(const VarDecl *var) {
226 llvm::DenseMap<const VarDecl*, Capture>::iterator
John McCalld16c2cf2011-02-08 08:22:06 +0000227 it = Captures.find(var);
228 assert(it != Captures.end() && "no entry for variable!");
229 return it->second;
230 }
231
John McCall1a343eb2011-11-10 08:15:53 +0000232 const BlockDecl *getBlockDecl() const { return Block; }
233 const BlockExpr *getBlockExpr() const {
234 assert(BlockExpression);
235 assert(BlockExpression->getBlockDecl() == Block);
236 return BlockExpression;
237 }
John McCalld16c2cf2011-02-08 08:22:06 +0000238
John McCall1a343eb2011-11-10 08:15:53 +0000239 CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
Mike Stumpd883d842009-03-04 15:35:22 +0000240};
241
242} // end namespace CodeGen
243} // end namespace clang
244
245#endif