blob: 35d61cfdd41424421adc8800d70e063434f925a0 [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 "llvm/ADT/SmallVector.h"
21#include "clang/Basic/TargetInfo.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000022#include "clang/AST/CharUnits.h"
Mike Stump2a998142009-03-04 18:17:45 +000023#include "clang/AST/Expr.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/ExprObjC.h"
26
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;
36 class TargetData;
37 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
John McCalld16c2cf2011-02-08 08:22:06 +000050enum BlockFlag_t {
51 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
52 BLOCK_HAS_CXX_OBJ = (1 << 26),
53 BLOCK_IS_GLOBAL = (1 << 28),
54 BLOCK_USE_STRET = (1 << 29),
55 BLOCK_HAS_SIGNATURE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000056};
John McCalld16c2cf2011-02-08 08:22:06 +000057class BlockFlags {
58 uint32_t flags;
Mike Stumpd883d842009-03-04 15:35:22 +000059
John McCalld16c2cf2011-02-08 08:22:06 +000060 BlockFlags(uint32_t flags) : flags(flags) {}
Mike Stump2a998142009-03-04 18:17:45 +000061public:
John McCalld16c2cf2011-02-08 08:22:06 +000062 BlockFlags() : flags(0) {}
63 BlockFlags(BlockFlag_t flag) : flags(flag) {}
Mike Stump2a998142009-03-04 18:17:45 +000064
John McCalld16c2cf2011-02-08 08:22:06 +000065 uint32_t getBitMask() const { return flags; }
66 bool empty() const { return flags == 0; }
Mike Stump2a998142009-03-04 18:17:45 +000067
John McCalld16c2cf2011-02-08 08:22:06 +000068 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
69 return BlockFlags(l.flags | r.flags);
70 }
71 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
72 l.flags |= r.flags;
73 return l;
74 }
75 friend bool operator&(BlockFlags l, BlockFlags r) {
76 return (l.flags & r.flags);
Mike Stump2a998142009-03-04 18:17:45 +000077 }
Mike Stumpd883d842009-03-04 15:35:22 +000078};
John McCalld16c2cf2011-02-08 08:22:06 +000079inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) {
80 return BlockFlags(l) | BlockFlags(r);
81}
Mike Stumpd883d842009-03-04 15:35:22 +000082
John McCalld16c2cf2011-02-08 08:22:06 +000083enum BlockFieldFlag_t {
84 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
85 block, ... */
86 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
Mike Stump797b6322009-03-05 01:23:13 +000087
John McCalld16c2cf2011-02-08 08:22:06 +000088 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
89 variable */
90 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
91 helpers */
John McCallf85e1932011-06-15 23:02:42 +000092 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCalld16c2cf2011-02-08 08:22:06 +000093 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +000094 support routines */
John McCalld16c2cf2011-02-08 08:22:06 +000095 BLOCK_BYREF_CURRENT_MAX = 256
96};
97
98class BlockFieldFlags {
99 uint32_t flags;
100
101 BlockFieldFlags(uint32_t flags) : flags(flags) {}
102public:
103 BlockFieldFlags() : flags(0) {}
104 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
105
106 uint32_t getBitMask() const { return flags; }
107 bool empty() const { return flags == 0; }
108
109 /// Answers whether the flags indicate that this field is an object
110 /// or block pointer that requires _Block_object_assign/dispose.
111 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
112
113 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
114 return BlockFieldFlags(l.flags | r.flags);
115 }
116 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
117 l.flags |= r.flags;
118 return l;
119 }
120 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
121 return (l.flags & r.flags);
122 }
123};
124inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
125 return BlockFieldFlags(l) | BlockFieldFlags(r);
126}
127
128/// CGBlockInfo - Information to generate a block literal.
129class CGBlockInfo {
130public:
131 /// Name - The name of the block, kindof.
132 const char *Name;
133
134 /// The field index of 'this' within the block, if there is one.
135 unsigned CXXThisIndex;
136
137 class Capture {
138 uintptr_t Data;
139
140 public:
141 bool isIndex() const { return (Data & 1) != 0; }
142 bool isConstant() const { return !isIndex(); }
143 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
144 llvm::Value *getConstant() const {
145 assert(isConstant());
146 return reinterpret_cast<llvm::Value*>(Data);
147 }
148
149 static Capture makeIndex(unsigned index) {
150 Capture v;
151 v.Data = (index << 1) | 1;
152 return v;
153 }
154
155 static Capture makeConstant(llvm::Value *value) {
156 Capture v;
157 v.Data = reinterpret_cast<uintptr_t>(value);
158 return v;
159 }
Mike Stumpd883d842009-03-04 15:35:22 +0000160 };
Mike Stump3947de52009-03-04 18:57:26 +0000161
John McCalld16c2cf2011-02-08 08:22:06 +0000162 /// The mapping of allocated indexes within the block.
163 llvm::DenseMap<const VarDecl*, Capture> Captures;
Mike Stump3947de52009-03-04 18:57:26 +0000164
John McCalld16c2cf2011-02-08 08:22:06 +0000165 /// CanBeGlobal - True if the block can be global, i.e. it has
166 /// no non-constant captures.
167 bool CanBeGlobal : 1;
Mike Stump08920992009-03-07 02:35:30 +0000168
John McCalld16c2cf2011-02-08 08:22:06 +0000169 /// True if the block needs a custom copy or dispose function.
170 bool NeedsCopyDispose : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000171
John McCalld16c2cf2011-02-08 08:22:06 +0000172 /// HasCXXObject - True if the block's custom copy/dispose functions
173 /// need to be run even in GC mode.
174 bool HasCXXObject : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000175
John McCall64cd2322011-03-09 08:39:33 +0000176 /// UsesStret : True if the block uses an stret return. Mutable
177 /// because it gets set later in the block-creation process.
178 mutable bool UsesStret : 1;
179
John McCalld16c2cf2011-02-08 08:22:06 +0000180 const llvm::StructType *StructureType;
181 const BlockExpr *Block;
182 CharUnits BlockSize;
183 CharUnits BlockAlign;
Mike Stump3947de52009-03-04 18:57:26 +0000184
John McCalld16c2cf2011-02-08 08:22:06 +0000185 const Capture &getCapture(const VarDecl *var) const {
186 llvm::DenseMap<const VarDecl*, Capture>::const_iterator
187 it = Captures.find(var);
188 assert(it != Captures.end() && "no entry for variable!");
189 return it->second;
190 }
191
192 const BlockDecl *getBlockDecl() const { return Block->getBlockDecl(); }
193 const BlockExpr *getBlockExpr() const { return Block; }
194
195 CGBlockInfo(const BlockExpr *blockExpr, const char *Name);
Mike Stumpd883d842009-03-04 15:35:22 +0000196};
197
198} // end namespace CodeGen
199} // end namespace clang
200
201#endif