blob: 4a940960730e5778c916607021e9d17dd241de4b [file] [log] [blame]
Anders Carlssond2a889b2009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
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 contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "llvm/Module.h"
Anders Carlsson1f1cd392009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssond2a889b2009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlsson1f1cd392009-02-12 17:55:02 +000024enum {
Mike Stumpb95bc002009-02-13 16:19:19 +000025 BLOCK_NEEDS_FREE = (1 << 24),
26 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
27 BLOCK_HAS_CXX_OBJ = (1 << 26),
28 BLOCK_IS_GC = (1 << 27),
29 BLOCK_IS_GLOBAL = (1 << 28),
30 BLOCK_HAS_DESCRIPTOR = (1 << 29)
Anders Carlsson1f1cd392009-02-12 17:55:02 +000031};
32
Mike Stumpb95bc002009-02-13 16:19:19 +000033llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl() {
34 // FIXME: Push up.
35 bool BlockHasCopyDispose = false;
36
37 const llvm::PointerType *PtrToInt8Ty
38 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpff8f0872009-02-13 16:55:51 +000039 const llvm::Type *UnsignedLongTy
40 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb95bc002009-02-13 16:19:19 +000041 llvm::Constant *C;
42 std::vector<llvm::Constant*> Elts;
43
44 // reserved
Mike Stumpff8f0872009-02-13 16:55:51 +000045 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000046 Elts.push_back(C);
47
48 // Size
Mike Stumpac9b4df2009-02-13 17:03:17 +000049 int sz = CGM.getTargetData()
50 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
51 C = llvm::ConstantInt::get(UnsignedLongTy, sz);
Mike Stumpb95bc002009-02-13 16:19:19 +000052 Elts.push_back(C);
53
54 if (BlockHasCopyDispose) {
55 // copy_func_helper_decl
Mike Stumpff8f0872009-02-13 16:55:51 +000056 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000057 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
58 Elts.push_back(C);
59
60 // destroy_func_decl
Mike Stumpff8f0872009-02-13 16:55:51 +000061 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000062 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
63 Elts.push_back(C);
64 }
65
66 C = llvm::ConstantStruct::get(Elts);
67
Mike Stumpb95bc002009-02-13 16:19:19 +000068 C = new llvm::GlobalVariable(C->getType(), true,
69 llvm::GlobalValue::InternalLinkage,
Mike Stump92ea8882009-02-13 20:17:16 +000070 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpb95bc002009-02-13 16:19:19 +000071 return C;
72}
73
Mike Stump5f87e9d2009-02-13 17:23:42 +000074llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
75 if (NSConcreteGlobalBlock)
76 return NSConcreteGlobalBlock;
77
78 const llvm::PointerType *PtrToInt8Ty
79 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000080 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump5f87e9d2009-02-13 17:23:42 +000081 // same thing as CreateRuntimeFunction if there's already a variable with
82 // the same name.
83 NSConcreteGlobalBlock
84 = new llvm::GlobalVariable(PtrToInt8Ty, false,
85 llvm::GlobalValue::ExternalLinkage,
86 0, "_NSConcreteGlobalBlock",
87 &getModule());
88
89 return NSConcreteGlobalBlock;
90}
91
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000092llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
93 if (NSConcreteStackBlock)
94 return NSConcreteStackBlock;
95
96 const llvm::PointerType *PtrToInt8Ty
97 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000098 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000099 // same thing as CreateRuntimeFunction if there's already a variable with
100 // the same name.
101 NSConcreteStackBlock
102 = new llvm::GlobalVariable(PtrToInt8Ty, false,
103 llvm::GlobalValue::ExternalLinkage,
104 0, "_NSConcreteStackBlock",
105 &getModule());
106
107 return NSConcreteStackBlock;
108}
109
Mike Stump084ba462009-02-14 22:16:35 +0000110llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000111 // FIXME: Push up
112 bool BlockHasCopyDispose = false;
113 bool insideFunction = false;
114 bool BlockRefDeclList = false;
115 bool BlockByrefDeclList = false;
116
117 std::vector<llvm::Constant*> Elts;
118 llvm::Constant *C;
119
Mike Stumpb95bc002009-02-13 16:19:19 +0000120 {
121 // C = BuildBlockStructInitlist();
122 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
123
124 if (BlockHasCopyDispose)
125 flags |= BLOCK_HAS_COPY_DISPOSE;
126
Mike Stump92ea8882009-02-13 20:17:16 +0000127 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000128 C = CGM.getNSConcreteStackBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000129 if (!insideFunction ||
130 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stump5f87e9d2009-02-13 17:23:42 +0000131 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000132 flags |= BLOCK_IS_GLOBAL;
133 }
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000134 const llvm::PointerType *PtrToInt8Ty
135 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000136 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
137 Elts.push_back(C);
138
139 // __flags
140 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
141 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
142 C = llvm::ConstantInt::get(IntTy, flags);
143 Elts.push_back(C);
144
145 // __reserved
146 C = llvm::ConstantInt::get(IntTy, 0);
147 Elts.push_back(C);
148
149 // __FuncPtr
Mike Stump11c46e22009-02-17 17:18:36 +0000150 const char *Name = "";
Mike Stump084ba462009-02-14 22:16:35 +0000151 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
Mike Stump459207f2009-02-17 23:25:52 +0000152 if (ND->getIdentifier())
153 Name = ND->getNameAsCString();
Mike Stump084ba462009-02-14 22:16:35 +0000154 BlockInfo Info(0, Name);
155 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
156 Elts.push_back(Fn);
Mike Stumpb95bc002009-02-13 16:19:19 +0000157
158 // __descriptor
159 Elts.push_back(BuildDescriptorBlockDecl());
160
161 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
162 }
163
164 C = llvm::ConstantStruct::get(Elts);
165
166 char Name[32];
Mike Stumpef2c82f2009-02-13 18:36:05 +0000167 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpb95bc002009-02-13 16:19:19 +0000168 C = new llvm::GlobalVariable(C->getType(), true,
169 llvm::GlobalValue::InternalLinkage,
170 C, Name, &CGM.getModule());
171 return C;
172}
173
174
175
176
Mike Stump95e54802009-02-13 15:16:56 +0000177const llvm::Type *CodeGenModule::getBlockDescriptorType() {
178 if (BlockDescriptorType)
179 return BlockDescriptorType;
180
Mike Stumpc4ae9632009-02-13 15:32:32 +0000181 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000182 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000183
Mike Stump95e54802009-02-13 15:16:56 +0000184 // struct __block_descriptor {
185 // unsigned long reserved;
186 // unsigned long block_size;
187 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000188 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
189 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000190 NULL);
191
192 getModule().addTypeName("struct.__block_descriptor",
193 BlockDescriptorType);
194
195 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000196}
197
Mike Stump0dffa462009-02-13 15:25:34 +0000198const llvm::Type *
199CodeGenModule::getGenericBlockLiteralType() {
200 if (GenericBlockLiteralType)
201 return GenericBlockLiteralType;
202
Mike Stumpc4ae9632009-02-13 15:32:32 +0000203 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000204 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000205
206 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000207 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000208
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000209 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
210 getTypes().ConvertType(getContext().IntTy));
211
Mike Stump0dffa462009-02-13 15:25:34 +0000212 // struct __block_literal_generic {
213 // void *isa;
214 // int flags;
215 // int reserved;
216 // void (*invoke)(void *);
217 // struct __block_descriptor *descriptor;
218 // };
219 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000220 IntTy,
221 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000222 Int8PtrTy,
223 BlockDescPtrTy,
224 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000225
Mike Stump0dffa462009-02-13 15:25:34 +0000226 getModule().addTypeName("struct.__block_literal_generic",
227 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000228
Mike Stump0dffa462009-02-13 15:25:34 +0000229 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000230}
231
Mike Stumpc4ae9632009-02-13 15:32:32 +0000232/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000233/// function type for the block, including the first block literal argument.
234static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000235 const BlockPointerType *BPT) {
Anders Carlssond2a889b2009-02-12 00:39:25 +0000236 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000237
Anders Carlssond2a889b2009-02-12 00:39:25 +0000238 llvm::SmallVector<QualType, 8> Types;
239 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000240
Anders Carlssond2a889b2009-02-12 00:39:25 +0000241 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
242 e = FTy->arg_type_end(); i != e; ++i)
243 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000244
Anders Carlssond2a889b2009-02-12 00:39:25 +0000245 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000246 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000247 FTy->isVariadic(), 0);
248}
249
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000250RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000251 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000252 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000253
Anders Carlssond2a889b2009-02-12 00:39:25 +0000254 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
255
256 // Get a pointer to the generic block literal.
257 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000258 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000259
260 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000261 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000262 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
263
264 // Get the function pointer from the literal.
265 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
266 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
267
268 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000269 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000270 ConvertType(getBlockFunctionType(getContext(), BPT));
271 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
272 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
273
Mike Stumpc4ae9632009-02-13 15:32:32 +0000274 BlockLiteral =
275 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000276 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
277 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000278
Anders Carlssond2a889b2009-02-12 00:39:25 +0000279 // Add the block literal.
280 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
281 CallArgList Args;
282 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000283
Anders Carlssond2a889b2009-02-12 00:39:25 +0000284 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000285 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000286 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000287 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000288 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000289
Anders Carlssond2a889b2009-02-12 00:39:25 +0000290 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000291 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000292 Func, Args);
293}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000294
Mike Stump084ba462009-02-14 22:16:35 +0000295llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000296CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000297 // Generate the block descriptor.
298 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000299 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
300 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000301
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000302 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000303
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000304 // Reserved
305 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000306
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000307 // Block literal size. For global blocks we just use the size of the generic
308 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000309 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000310 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000311 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000312
313 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000314 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000315
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000316 llvm::GlobalVariable *Descriptor =
317 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000318 llvm::GlobalVariable::InternalLinkage,
319 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000320 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000321
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000322 // Generate the constants for the block literal.
323 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000324
Mike Stump084ba462009-02-14 22:16:35 +0000325 CodeGenFunction::BlockInfo Info(0, n);
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000326 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000327
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000328 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000329 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000330
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000331 // Flags
Mike Stumpb95bc002009-02-13 16:19:19 +0000332 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000333
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000334 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000335 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000336
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000337 // Function
338 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000339
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000340 // Descriptor
341 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000342
343 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000344 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000345
346 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000347 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000348 llvm::GlobalVariable::InternalLinkage,
349 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000350 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000351
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000352 return BlockLiteral;
353}
354
355llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
356 const BlockInfo& Info)
357{
Mike Stumpc4ae9632009-02-13 15:32:32 +0000358 const FunctionTypeProto *FTy =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000359 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000360
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000361 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000362
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000363 const BlockDecl *BD = Expr->getBlockDecl();
364
365 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000366 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000367 ImplicitParamDecl::Create(getContext(), 0,
368 SourceLocation(), 0,
369 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000370
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000371 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000372
373 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000374 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000375 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000376
377 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000378 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
379
Mike Stump084ba462009-02-14 22:16:35 +0000380 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000381 CodeGenTypes &Types = CGM.getTypes();
382 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000383
384 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000385 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
386 Name,
387 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000388
389 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000390 Expr->getBody()->getLocEnd());
391 EmitStmt(Expr->getBody());
392 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
393
394 return Fn;
395}