]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGBlocks.h
Replace our implementation of the vis(3) and unvis(3) APIs with
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGBlocks.h
1 //===-- 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
17 #include "CodeGenTypes.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/Module.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/AST/CharUnits.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25
26 #include "CodeGenFunction.h"
27 #include "CGBuilder.h"
28 #include "CGCall.h"
29 #include "CGValue.h"
30
31 namespace llvm {
32   class Module;
33   class Constant;
34   class Function;
35   class GlobalValue;
36   class DataLayout;
37   class FunctionType;
38   class PointerType;
39   class Value;
40   class LLVMContext;
41 }
42
43 namespace clang {
44
45 namespace CodeGen {
46
47 class CodeGenModule;
48 class CGBlockInfo;
49
50 // Flags stored in __block variables.
51 enum 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),
57   BLOCK_BYREF_LAYOUT_WEAK              = (4   << 28),
58   BLOCK_BYREF_LAYOUT_UNRETAINED        = (5   << 28)
59 };
60
61 enum BlockLiteralFlags {
62   BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
63   BLOCK_HAS_CXX_OBJ =       (1 << 26),
64   BLOCK_IS_GLOBAL =         (1 << 28),
65   BLOCK_USE_STRET =         (1 << 29),
66   BLOCK_HAS_SIGNATURE  =    (1 << 30),
67   BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31)
68 };
69 class BlockFlags {
70   uint32_t flags;
71
72   BlockFlags(uint32_t flags) : flags(flags) {}
73 public:
74   BlockFlags() : flags(0) {}
75   BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
76
77   uint32_t getBitMask() const { return flags; }
78   bool empty() const { return flags == 0; }
79
80   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);
89   }
90 };
91 inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
92   return BlockFlags(l) | BlockFlags(r);
93 }
94
95 enum BlockFieldFlag_t {
96   BLOCK_FIELD_IS_OBJECT   = 0x03,  /* id, NSObject, __attribute__((NSObject)),
97                                     block, ... */
98   BLOCK_FIELD_IS_BLOCK    = 0x07,  /* a block variable */
99
100   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 */
104   BLOCK_FIELD_IS_ARC      = 0x40,  /* field has ARC-specific semantics */
105   BLOCK_BYREF_CALLER      = 128,   /* called from __block (byref) copy/dispose
106                                       support routines */
107   BLOCK_BYREF_CURRENT_MAX = 256
108 };
109
110 class BlockFieldFlags {
111   uint32_t flags;
112
113   BlockFieldFlags(uint32_t flags) : flags(flags) {}
114 public:
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 };
136 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
137   return BlockFieldFlags(l) | BlockFieldFlags(r);
138 }
139
140 /// CGBlockInfo - Information to generate a block literal.
141 class CGBlockInfo {
142 public:
143   /// Name - The name of the block, kindof.
144   llvm::StringRef Name;
145
146   /// The field index of 'this' within the block, if there is one.
147   unsigned CXXThisIndex;
148
149   class Capture {
150     uintptr_t Data;
151     EHScopeStack::stable_iterator Cleanup;
152
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     }
161     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     }
169
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     }    
181   };
182
183   /// CanBeGlobal - True if the block can be global, i.e. it has
184   /// no non-constant captures.
185   bool CanBeGlobal : 1;
186
187   /// True if the block needs a custom copy or dispose function.
188   bool NeedsCopyDispose : 1;
189
190   /// HasCXXObject - True if the block's custom copy/dispose functions
191   /// need to be run even in GC mode.
192   bool HasCXXObject : 1;
193
194   /// 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;
197   
198   /// HasCapturedVariableLayout : True if block has captured variables
199   /// and their layout meta-data has been generated.
200   bool HasCapturedVariableLayout : 1;
201
202   /// The mapping of allocated indexes within the block.
203   llvm::DenseMap<const VarDecl*, Capture> Captures;  
204
205   llvm::AllocaInst *Address;
206   llvm::StructType *StructureType;
207   const BlockDecl *Block;
208   const BlockExpr *BlockExpression;
209   CharUnits BlockSize;
210   CharUnits BlockAlign;
211
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.
220   CGBlockInfo *NextBlockInfo;
221
222   const Capture &getCapture(const VarDecl *var) const {
223     return const_cast<CGBlockInfo*>(this)->getCapture(var);
224   }
225   Capture &getCapture(const VarDecl *var) {
226     llvm::DenseMap<const VarDecl*, Capture>::iterator
227       it = Captures.find(var);
228     assert(it != Captures.end() && "no entry for variable!");
229     return it->second;
230   }
231
232   const BlockDecl *getBlockDecl() const { return Block; }
233   const BlockExpr *getBlockExpr() const {
234     assert(BlockExpression);
235     assert(BlockExpression->getBlockDecl() == Block);
236     return BlockExpression;
237   }
238
239   CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name);
240 };
241
242 }  // end namespace CodeGen
243 }  // end namespace clang
244
245 #endif