blob: 84ad190d6ff8e3ab69e2d2a476bd0ed6611a6e94 [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"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
25
26#include <vector>
27#include <map>
28
29#include "CGBuilder.h"
30#include "CGCall.h"
31#include "CGValue.h"
32
33namespace llvm {
34 class Module;
35 class Constant;
36 class Function;
37 class GlobalValue;
38 class TargetData;
39 class FunctionType;
40 class Value;
41}
42
Mike Stumpd883d842009-03-04 15:35:22 +000043namespace clang {
44
45namespace CodeGen {
Mike Stump90a90432009-03-04 18:47:42 +000046class CodeGenModule;
Mike Stumpd883d842009-03-04 15:35:22 +000047
48class BlockBase {
49public:
50 enum {
51 BLOCK_NEEDS_FREE = (1 << 24),
52 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
53 BLOCK_HAS_CXX_OBJ = (1 << 26),
54 BLOCK_IS_GC = (1 << 27),
55 BLOCK_IS_GLOBAL = (1 << 28),
56 BLOCK_HAS_DESCRIPTOR = (1 << 29)
57 };
58};
59
60class BlockModule : public BlockBase {
Mike Stump2a998142009-03-04 18:17:45 +000061 ASTContext &Context;
62 llvm::Module &TheModule;
Mike Stump90a90432009-03-04 18:47:42 +000063 const llvm::TargetData &TheTargetData;
Mike Stump2a998142009-03-04 18:17:45 +000064 CodeGenTypes &Types;
Mike Stump90a90432009-03-04 18:47:42 +000065 CodeGenModule &CGM;
Mike Stump2a998142009-03-04 18:17:45 +000066
67 ASTContext &getContext() const { return Context; }
68 llvm::Module &getModule() const { return TheModule; }
69 CodeGenTypes &getTypes() { return Types; }
Mike Stump90a90432009-03-04 18:47:42 +000070 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Mike Stump2a998142009-03-04 18:17:45 +000071public:
72 llvm::Constant *getNSConcreteGlobalBlock();
73 llvm::Constant *getNSConcreteStackBlock();
74 int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; }
75 const llvm::Type *getBlockDescriptorType();
76
77 const llvm::Type *getGenericBlockLiteralType();
78 const llvm::Type *getGenericExtendedBlockLiteralType();
79
Mike Stump90a90432009-03-04 18:47:42 +000080 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
81
Mike Stump2a998142009-03-04 18:17:45 +000082 /// NSConcreteGlobalBlock - Cached reference to the class pointer for global
83 /// blocks.
84 llvm::Constant *NSConcreteGlobalBlock;
85
86 /// NSConcreteStackBlock - Cached reference to the class poinnter for stack
87 /// blocks.
88 llvm::Constant *NSConcreteStackBlock;
89
90 const llvm::Type *BlockDescriptorType;
91 const llvm::Type *GenericBlockLiteralType;
92 const llvm::Type *GenericExtendedBlockLiteralType;
93 struct {
94 int GlobalUniqueCount;
95 } Block;
96
Mike Stump797b6322009-03-05 01:23:13 +000097 llvm::Value *BlockObjectDispose;
98 const llvm::Type *PtrToInt8Ty;
99
Mike Stump90a90432009-03-04 18:47:42 +0000100 BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD,
101 CodeGenTypes &T, CodeGenModule &CodeGen)
102 : Context(C), TheModule(M), TheTargetData(TD), Types(T),
103 CGM(CodeGen),
104 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
Mike Stump797b6322009-03-05 01:23:13 +0000105 GenericBlockLiteralType(0), BlockObjectDispose(0) {
Mike Stump2a998142009-03-04 18:17:45 +0000106 Block.GlobalUniqueCount = 0;
Mike Stump797b6322009-03-05 01:23:13 +0000107 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump2a998142009-03-04 18:17:45 +0000108 }
Mike Stumpd883d842009-03-04 15:35:22 +0000109};
110
111class BlockFunction : public BlockBase {
Mike Stump797b6322009-03-05 01:23:13 +0000112 CodeGenModule &CGM;
Mike Stumpa4f668f2009-03-06 01:33:24 +0000113 CodeGenFunction &CGF;
Mike Stump00470a12009-03-05 08:32:30 +0000114 ASTContext &getContext() const;
Mike Stump797b6322009-03-05 01:23:13 +0000115
Mike Stumpd883d842009-03-04 15:35:22 +0000116public:
Mike Stump797b6322009-03-05 01:23:13 +0000117 const llvm::Type *PtrToInt8Ty;
118 enum {
Mike Stumpd883d842009-03-04 15:35:22 +0000119 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
120 block, ... */
121 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
122 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block
123 variable */
124 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
125 helpers */
126 BLOCK_BYREF_CALLER = 128 /* called from __block (byref) copy/dispose
127 support routines */
128 };
Mike Stump3947de52009-03-04 18:57:26 +0000129
Mike Stumpad75ab42009-03-04 19:03:44 +0000130 /// BlockInfo - Information to generate a block literal.
131 struct BlockInfo {
132 /// BlockLiteralTy - The type of the block literal.
133 const llvm::Type *BlockLiteralTy;
134
135 /// Name - the name of the function this block was created for, if any
136 const char *Name;
137
138 /// ByCopyDeclRefs - Variables from parent scopes that have been imported
139 /// into this block.
140 llvm::SmallVector<const BlockDeclRefExpr *, 8> ByCopyDeclRefs;
141
142 // ByRefDeclRefs - __block variables from parent scopes that have been
143 // imported into this block.
144 llvm::SmallVector<const BlockDeclRefExpr *, 8> ByRefDeclRefs;
145
146 BlockInfo(const llvm::Type *blt, const char *n)
147 : BlockLiteralTy(blt), Name(n) {}
148 };
149
Mike Stump3947de52009-03-04 18:57:26 +0000150 CGBuilderTy &Builder;
151
Mike Stumpa4f668f2009-03-06 01:33:24 +0000152 BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B)
153 : CGM(cgm), CGF(cgf), Builder(B) {
Mike Stump797b6322009-03-05 01:23:13 +0000154 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
155 }
Mike Stump3947de52009-03-04 18:57:26 +0000156
Mike Stumpad75ab42009-03-04 19:03:44 +0000157 ImplicitParamDecl *BlockStructDecl;
158 ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
159
Mike Stumpa4f668f2009-03-06 01:33:24 +0000160 llvm::Constant *GenerateCopyHelperFunction();
161 llvm::Constant *GenerateDestroyHelperFunction();
162
163 llvm::Constant *BuildCopyHelper(int flag);
164 llvm::Constant *BuildDestroyHelper(int flag);
Mike Stump3947de52009-03-04 18:57:26 +0000165
Mike Stump797b6322009-03-05 01:23:13 +0000166 llvm::Value *getBlockObjectDispose();
167 void BuildBlockRelease(const VarDecl &D, llvm::Value *DeclPtr);
Mike Stump00470a12009-03-05 08:32:30 +0000168
169 bool BlockRequiresCopying(QualType Ty) {
170 if (Ty->isBlockPointerType())
171 return true;
172 if (getContext().isObjCNSObjectType(Ty))
173 return true;
174 return false;
175 }
Mike Stumpd883d842009-03-04 15:35:22 +0000176};
177
178} // end namespace CodeGen
179} // end namespace clang
180
181#endif