blob: 8c7e82e61c0ddd11666bd19b46e3dd66115aa18f [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 char Name[32];
Mike Stump075329c2009-02-13 19:36:03 +000069 sprintf(Name, "__block_descriptor_tmp_%d", CGM.getDescriptorUniqueCount());
Mike Stumpb95bc002009-02-13 16:19:19 +000070 C = new llvm::GlobalVariable(C->getType(), true,
71 llvm::GlobalValue::InternalLinkage,
72 C, Name, &CGM.getModule());
73 return C;
74}
75
Mike Stump5f87e9d2009-02-13 17:23:42 +000076llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
77 if (NSConcreteGlobalBlock)
78 return NSConcreteGlobalBlock;
79
80 const llvm::PointerType *PtrToInt8Ty
81 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000082 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump5f87e9d2009-02-13 17:23:42 +000083 // same thing as CreateRuntimeFunction if there's already a variable with
84 // the same name.
85 NSConcreteGlobalBlock
86 = new llvm::GlobalVariable(PtrToInt8Ty, false,
87 llvm::GlobalValue::ExternalLinkage,
88 0, "_NSConcreteGlobalBlock",
89 &getModule());
90
91 return NSConcreteGlobalBlock;
92}
93
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000094llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
95 if (NSConcreteStackBlock)
96 return NSConcreteStackBlock;
97
98 const llvm::PointerType *PtrToInt8Ty
99 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +0000100 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000101 // same thing as CreateRuntimeFunction if there's already a variable with
102 // the same name.
103 NSConcreteStackBlock
104 = new llvm::GlobalVariable(PtrToInt8Ty, false,
105 llvm::GlobalValue::ExternalLinkage,
106 0, "_NSConcreteStackBlock",
107 &getModule());
108
109 return NSConcreteStackBlock;
110}
111
Mike Stumpb95bc002009-02-13 16:19:19 +0000112llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp() {
113 // FIXME: Push up
114 bool BlockHasCopyDispose = false;
115 bool insideFunction = false;
116 bool BlockRefDeclList = false;
117 bool BlockByrefDeclList = false;
118
119 std::vector<llvm::Constant*> Elts;
120 llvm::Constant *C;
121
Mike Stumpb95bc002009-02-13 16:19:19 +0000122 {
123 // C = BuildBlockStructInitlist();
124 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
125
126 if (BlockHasCopyDispose)
127 flags |= BLOCK_HAS_COPY_DISPOSE;
128
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000129 C = CGM.getNSConcreteStackBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000130 if (!insideFunction ||
131 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stump5f87e9d2009-02-13 17:23:42 +0000132 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000133 flags |= BLOCK_IS_GLOBAL;
134 }
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000135 const llvm::PointerType *PtrToInt8Ty
136 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000137 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
138 Elts.push_back(C);
139
140 // __flags
141 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
142 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
143 C = llvm::ConstantInt::get(IntTy, flags);
144 Elts.push_back(C);
145
146 // __reserved
147 C = llvm::ConstantInt::get(IntTy, 0);
148 Elts.push_back(C);
149
150 // __FuncPtr
151 // FIXME: Build this up.
152 Elts.push_back(C);
153
154 // __descriptor
155 Elts.push_back(BuildDescriptorBlockDecl());
156
157 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
158 }
159
160 C = llvm::ConstantStruct::get(Elts);
161
162 char Name[32];
Mike Stumpef2c82f2009-02-13 18:36:05 +0000163 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpb95bc002009-02-13 16:19:19 +0000164 C = new llvm::GlobalVariable(C->getType(), true,
165 llvm::GlobalValue::InternalLinkage,
166 C, Name, &CGM.getModule());
167 return C;
168}
169
170
171
172
Mike Stump95e54802009-02-13 15:16:56 +0000173const llvm::Type *CodeGenModule::getBlockDescriptorType() {
174 if (BlockDescriptorType)
175 return BlockDescriptorType;
176
Mike Stumpc4ae9632009-02-13 15:32:32 +0000177 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000178 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000179
Mike Stump95e54802009-02-13 15:16:56 +0000180 // struct __block_descriptor {
181 // unsigned long reserved;
182 // unsigned long block_size;
183 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000184 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
185 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000186 NULL);
187
188 getModule().addTypeName("struct.__block_descriptor",
189 BlockDescriptorType);
190
191 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000192}
193
Mike Stump0dffa462009-02-13 15:25:34 +0000194const llvm::Type *
195CodeGenModule::getGenericBlockLiteralType() {
196 if (GenericBlockLiteralType)
197 return GenericBlockLiteralType;
198
Mike Stumpc4ae9632009-02-13 15:32:32 +0000199 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000200 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000201
202 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000203 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000204
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000205 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
206 getTypes().ConvertType(getContext().IntTy));
207
Mike Stump0dffa462009-02-13 15:25:34 +0000208 // struct __block_literal_generic {
209 // void *isa;
210 // int flags;
211 // int reserved;
212 // void (*invoke)(void *);
213 // struct __block_descriptor *descriptor;
214 // };
215 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000216 IntTy,
217 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000218 Int8PtrTy,
219 BlockDescPtrTy,
220 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000221
Mike Stump0dffa462009-02-13 15:25:34 +0000222 getModule().addTypeName("struct.__block_literal_generic",
223 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000224
Mike Stump0dffa462009-02-13 15:25:34 +0000225 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000226}
227
Mike Stumpc4ae9632009-02-13 15:32:32 +0000228/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000229/// function type for the block, including the first block literal argument.
230static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000231 const BlockPointerType *BPT) {
Anders Carlssond2a889b2009-02-12 00:39:25 +0000232 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000233
Anders Carlssond2a889b2009-02-12 00:39:25 +0000234 llvm::SmallVector<QualType, 8> Types;
235 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000236
Anders Carlssond2a889b2009-02-12 00:39:25 +0000237 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
238 e = FTy->arg_type_end(); i != e; ++i)
239 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000240
Anders Carlssond2a889b2009-02-12 00:39:25 +0000241 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000242 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000243 FTy->isVariadic(), 0);
244}
245
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000246RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000247 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000248 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000249
Anders Carlssond2a889b2009-02-12 00:39:25 +0000250 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
251
252 // Get a pointer to the generic block literal.
253 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000254 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000255
256 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000257 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000258 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
259
260 // Get the function pointer from the literal.
261 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
262 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
263
264 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000265 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000266 ConvertType(getBlockFunctionType(getContext(), BPT));
267 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
268 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
269
Mike Stumpc4ae9632009-02-13 15:32:32 +0000270 BlockLiteral =
271 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000272 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
273 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000274
Anders Carlssond2a889b2009-02-12 00:39:25 +0000275 // Add the block literal.
276 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
277 CallArgList Args;
278 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000279
Anders Carlssond2a889b2009-02-12 00:39:25 +0000280 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000281 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000282 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000283 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000284 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000285
Anders Carlssond2a889b2009-02-12 00:39:25 +0000286 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000287 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000288 Func, Args);
289}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000290
291llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000292 // Generate the block descriptor.
293 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000294 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
295 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000296
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000297 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000298
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000299 // Reserved
300 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000301
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000302 // Block literal size. For global blocks we just use the size of the generic
303 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000304 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000305 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000306 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000307
308 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000309 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000310
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000311 llvm::GlobalVariable *Descriptor =
312 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000313 llvm::GlobalVariable::InternalLinkage,
314 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000315 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000316
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000317 // Generate the constants for the block literal.
318 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000319
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000320 CodeGenFunction::BlockInfo Info(0, "global");
321 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000322
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000323 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000324 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000325
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000326 // Flags
Mike Stumpb95bc002009-02-13 16:19:19 +0000327 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000328
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000329 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000330 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000331
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000332 // Function
333 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000334
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000335 // Descriptor
336 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000337
338 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000339 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000340
341 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000342 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000343 llvm::GlobalVariable::InternalLinkage,
344 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000345 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000346
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000347 return BlockLiteral;
348}
349
350llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
351 const BlockInfo& Info)
352{
Mike Stumpc4ae9632009-02-13 15:32:32 +0000353 const FunctionTypeProto *FTy =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000354 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000355
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000356 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000357
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000358 const BlockDecl *BD = Expr->getBlockDecl();
359
360 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000361 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000362 ImplicitParamDecl::Create(getContext(), 0,
363 SourceLocation(), 0,
364 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000365
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000366 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000367
368 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000369 e = BD->param_end(); i != e; ++i)
370 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000371
372 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000373 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
374
375 std::string Name = std::string("__block_function_") + Info.NameSuffix;
376
377 CodeGenTypes &Types = CGM.getTypes();
378 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000379
380 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000381 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
382 Name,
383 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000384
385 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000386 Expr->getBody()->getLocEnd());
387 EmitStmt(Expr->getBody());
388 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
389
390 return Fn;
391}