blob: 4d8dead2be8c0d6e0180d20d292107753e708b81 [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
Mike Stump2a998142009-03-04 18:17:45 +000026#include "CGBuilder.h"
27#include "CGCall.h"
28#include "CGValue.h"
29
30namespace llvm {
31 class Module;
32 class Constant;
33 class Function;
34 class GlobalValue;
35 class TargetData;
36 class FunctionType;
John McCall3d3ec1c2010-04-21 10:05:39 +000037 class PointerType;
Mike Stump2a998142009-03-04 18:17:45 +000038 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000039 class LLVMContext;
Mike Stump2a998142009-03-04 18:17:45 +000040}
41
Mike Stumpd883d842009-03-04 15:35:22 +000042namespace clang {
43
44namespace CodeGen {
John McCalld16c2cf2011-02-08 08:22:06 +000045
Mike Stump90a90432009-03-04 18:47:42 +000046class CodeGenModule;
John McCall6b5a61b2011-02-07 10:33:21 +000047class CGBlockInfo;
Mike Stumpd883d842009-03-04 15:35:22 +000048
John McCalld16c2cf2011-02-08 08:22:06 +000049enum BlockFlag_t {
50 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
51 BLOCK_HAS_CXX_OBJ = (1 << 26),
52 BLOCK_IS_GLOBAL = (1 << 28),
53 BLOCK_USE_STRET = (1 << 29),
54 BLOCK_HAS_SIGNATURE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000055};
John McCalld16c2cf2011-02-08 08:22:06 +000056class BlockFlags {
57 uint32_t flags;
Mike Stumpd883d842009-03-04 15:35:22 +000058
John McCalld16c2cf2011-02-08 08:22:06 +000059 BlockFlags(uint32_t flags) : flags(flags) {}
Mike Stump2a998142009-03-04 18:17:45 +000060public:
John McCalld16c2cf2011-02-08 08:22:06 +000061 BlockFlags() : flags(0) {}
62 BlockFlags(BlockFlag_t flag) : flags(flag) {}
Mike Stump2a998142009-03-04 18:17:45 +000063
John McCalld16c2cf2011-02-08 08:22:06 +000064 uint32_t getBitMask() const { return flags; }
65 bool empty() const { return flags == 0; }
Mike Stump2a998142009-03-04 18:17:45 +000066
John McCalld16c2cf2011-02-08 08:22:06 +000067 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
68 return BlockFlags(l.flags | r.flags);
69 }
70 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
71 l.flags |= r.flags;
72 return l;
73 }
74 friend bool operator&(BlockFlags l, BlockFlags r) {
75 return (l.flags & r.flags);
Mike Stump2a998142009-03-04 18:17:45 +000076 }
Mike Stumpd883d842009-03-04 15:35:22 +000077};
John McCalld16c2cf2011-02-08 08:22:06 +000078inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) {
79 return BlockFlags(l) | BlockFlags(r);
80}
Mike Stumpd883d842009-03-04 15:35:22 +000081
John McCalld16c2cf2011-02-08 08:22:06 +000082enum BlockFieldFlag_t {
83 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
84 block, ... */
85 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
Mike Stump797b6322009-03-05 01:23:13 +000086
John McCalld16c2cf2011-02-08 08:22:06 +000087 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
88 variable */
89 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
90 helpers */
John McCallf85e1932011-06-15 23:02:42 +000091 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCalld16c2cf2011-02-08 08:22:06 +000092 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +000093 support routines */
John McCalld16c2cf2011-02-08 08:22:06 +000094 BLOCK_BYREF_CURRENT_MAX = 256
95};
96
97class BlockFieldFlags {
98 uint32_t flags;
99
100 BlockFieldFlags(uint32_t flags) : flags(flags) {}
101public:
102 BlockFieldFlags() : flags(0) {}
103 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
104
105 uint32_t getBitMask() const { return flags; }
106 bool empty() const { return flags == 0; }
107
108 /// Answers whether the flags indicate that this field is an object
109 /// or block pointer that requires _Block_object_assign/dispose.
110 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
111
112 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
113 return BlockFieldFlags(l.flags | r.flags);
114 }
115 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
116 l.flags |= r.flags;
117 return l;
118 }
119 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
120 return (l.flags & r.flags);
121 }
122};
123inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
124 return BlockFieldFlags(l) | BlockFieldFlags(r);
125}
126
127/// CGBlockInfo - Information to generate a block literal.
128class CGBlockInfo {
129public:
130 /// Name - The name of the block, kindof.
131 const char *Name;
132
133 /// The field index of 'this' within the block, if there is one.
134 unsigned CXXThisIndex;
135
136 class Capture {
137 uintptr_t Data;
138
139 public:
140 bool isIndex() const { return (Data & 1) != 0; }
141 bool isConstant() const { return !isIndex(); }
142 unsigned getIndex() const { assert(isIndex()); return Data >> 1; }
143 llvm::Value *getConstant() const {
144 assert(isConstant());
145 return reinterpret_cast<llvm::Value*>(Data);
146 }
147
148 static Capture makeIndex(unsigned index) {
149 Capture v;
150 v.Data = (index << 1) | 1;
151 return v;
152 }
153
154 static Capture makeConstant(llvm::Value *value) {
155 Capture v;
156 v.Data = reinterpret_cast<uintptr_t>(value);
157 return v;
158 }
Mike Stumpd883d842009-03-04 15:35:22 +0000159 };
Mike Stump3947de52009-03-04 18:57:26 +0000160
John McCalld16c2cf2011-02-08 08:22:06 +0000161 /// The mapping of allocated indexes within the block.
162 llvm::DenseMap<const VarDecl*, Capture> Captures;
Mike Stump3947de52009-03-04 18:57:26 +0000163
John McCalld16c2cf2011-02-08 08:22:06 +0000164 /// CanBeGlobal - True if the block can be global, i.e. it has
165 /// no non-constant captures.
166 bool CanBeGlobal : 1;
Mike Stump08920992009-03-07 02:35:30 +0000167
John McCalld16c2cf2011-02-08 08:22:06 +0000168 /// True if the block needs a custom copy or dispose function.
169 bool NeedsCopyDispose : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000170
John McCalld16c2cf2011-02-08 08:22:06 +0000171 /// HasCXXObject - True if the block's custom copy/dispose functions
172 /// need to be run even in GC mode.
173 bool HasCXXObject : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000174
John McCall64cd2322011-03-09 08:39:33 +0000175 /// UsesStret : True if the block uses an stret return. Mutable
176 /// because it gets set later in the block-creation process.
177 mutable bool UsesStret : 1;
178
John McCalld16c2cf2011-02-08 08:22:06 +0000179 const llvm::StructType *StructureType;
180 const BlockExpr *Block;
181 CharUnits BlockSize;
182 CharUnits BlockAlign;
Mike Stump3947de52009-03-04 18:57:26 +0000183
John McCalld16c2cf2011-02-08 08:22:06 +0000184 const Capture &getCapture(const VarDecl *var) const {
185 llvm::DenseMap<const VarDecl*, Capture>::const_iterator
186 it = Captures.find(var);
187 assert(it != Captures.end() && "no entry for variable!");
188 return it->second;
189 }
190
191 const BlockDecl *getBlockDecl() const { return Block->getBlockDecl(); }
192 const BlockExpr *getBlockExpr() const { return Block; }
193
194 CGBlockInfo(const BlockExpr *blockExpr, const char *Name);
Mike Stumpd883d842009-03-04 15:35:22 +0000195};
196
197} // end namespace CodeGen
198} // end namespace clang
199
200#endif