blob: d6c248dcef76e391b07076199250151309961f64 [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
68 // FIXME: Should be in module?
69 static int desc_unique_count;
70 char Name[32];
71 sprintf(Name, "__block_descriptor_tmp_%d", ++desc_unique_count);
72 C = new llvm::GlobalVariable(C->getType(), true,
73 llvm::GlobalValue::InternalLinkage,
74 C, Name, &CGM.getModule());
75 return C;
76}
77
Mike Stump5f87e9d2009-02-13 17:23:42 +000078llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
79 if (NSConcreteGlobalBlock)
80 return NSConcreteGlobalBlock;
81
82 const llvm::PointerType *PtrToInt8Ty
83 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
84 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
85 // same thing as CreateRuntimeFunction if there's already a variable with
86 // the same name.
87 NSConcreteGlobalBlock
88 = new llvm::GlobalVariable(PtrToInt8Ty, false,
89 llvm::GlobalValue::ExternalLinkage,
90 0, "_NSConcreteGlobalBlock",
91 &getModule());
92
93 return NSConcreteGlobalBlock;
94}
95
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000096llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
97 if (NSConcreteStackBlock)
98 return NSConcreteStackBlock;
99
100 const llvm::PointerType *PtrToInt8Ty
101 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
102 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
103 // same thing as CreateRuntimeFunction if there's already a variable with
104 // the same name.
105 NSConcreteStackBlock
106 = new llvm::GlobalVariable(PtrToInt8Ty, false,
107 llvm::GlobalValue::ExternalLinkage,
108 0, "_NSConcreteStackBlock",
109 &getModule());
110
111 return NSConcreteStackBlock;
112}
113
Mike Stumpb95bc002009-02-13 16:19:19 +0000114llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp() {
115 // FIXME: Push up
116 bool BlockHasCopyDispose = false;
117 bool insideFunction = false;
118 bool BlockRefDeclList = false;
119 bool BlockByrefDeclList = false;
120
121 std::vector<llvm::Constant*> Elts;
122 llvm::Constant *C;
123
Mike Stumpb95bc002009-02-13 16:19:19 +0000124 {
125 // C = BuildBlockStructInitlist();
126 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
127
128 if (BlockHasCopyDispose)
129 flags |= BLOCK_HAS_COPY_DISPOSE;
130
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000131 C = CGM.getNSConcreteStackBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000132 if (!insideFunction ||
133 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stump5f87e9d2009-02-13 17:23:42 +0000134 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000135 flags |= BLOCK_IS_GLOBAL;
136 }
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000137 const llvm::PointerType *PtrToInt8Ty
138 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000139 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
140 Elts.push_back(C);
141
142 // __flags
143 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
144 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
145 C = llvm::ConstantInt::get(IntTy, flags);
146 Elts.push_back(C);
147
148 // __reserved
149 C = llvm::ConstantInt::get(IntTy, 0);
150 Elts.push_back(C);
151
152 // __FuncPtr
153 // FIXME: Build this up.
154 Elts.push_back(C);
155
156 // __descriptor
157 Elts.push_back(BuildDescriptorBlockDecl());
158
159 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
160 }
161
162 C = llvm::ConstantStruct::get(Elts);
163
164 char Name[32];
Mike Stumpef2c82f2009-02-13 18:36:05 +0000165 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpb95bc002009-02-13 16:19:19 +0000166 C = new llvm::GlobalVariable(C->getType(), true,
167 llvm::GlobalValue::InternalLinkage,
168 C, Name, &CGM.getModule());
169 return C;
170}
171
172
173
174
Mike Stump95e54802009-02-13 15:16:56 +0000175const llvm::Type *CodeGenModule::getBlockDescriptorType() {
176 if (BlockDescriptorType)
177 return BlockDescriptorType;
178
Mike Stumpc4ae9632009-02-13 15:32:32 +0000179 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000180 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000181
Mike Stump95e54802009-02-13 15:16:56 +0000182 // struct __block_descriptor {
183 // unsigned long reserved;
184 // unsigned long block_size;
185 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000186 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
187 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000188 NULL);
189
190 getModule().addTypeName("struct.__block_descriptor",
191 BlockDescriptorType);
192
193 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000194}
195
Mike Stump0dffa462009-02-13 15:25:34 +0000196const llvm::Type *
197CodeGenModule::getGenericBlockLiteralType() {
198 if (GenericBlockLiteralType)
199 return GenericBlockLiteralType;
200
Mike Stumpc4ae9632009-02-13 15:32:32 +0000201 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000202 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000203
204 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000205 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000206
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000207 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
208 getTypes().ConvertType(getContext().IntTy));
209
Mike Stump0dffa462009-02-13 15:25:34 +0000210 // struct __block_literal_generic {
211 // void *isa;
212 // int flags;
213 // int reserved;
214 // void (*invoke)(void *);
215 // struct __block_descriptor *descriptor;
216 // };
217 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000218 IntTy,
219 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000220 Int8PtrTy,
221 BlockDescPtrTy,
222 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000223
Mike Stump0dffa462009-02-13 15:25:34 +0000224 getModule().addTypeName("struct.__block_literal_generic",
225 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000226
Mike Stump0dffa462009-02-13 15:25:34 +0000227 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000228}
229
Mike Stumpc4ae9632009-02-13 15:32:32 +0000230/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000231/// function type for the block, including the first block literal argument.
232static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000233 const BlockPointerType *BPT) {
Anders Carlssond2a889b2009-02-12 00:39:25 +0000234 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000235
Anders Carlssond2a889b2009-02-12 00:39:25 +0000236 llvm::SmallVector<QualType, 8> Types;
237 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000238
Anders Carlssond2a889b2009-02-12 00:39:25 +0000239 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
240 e = FTy->arg_type_end(); i != e; ++i)
241 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000242
Anders Carlssond2a889b2009-02-12 00:39:25 +0000243 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000244 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000245 FTy->isVariadic(), 0);
246}
247
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000248RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000249 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000250 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000251
Anders Carlssond2a889b2009-02-12 00:39:25 +0000252 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
253
254 // Get a pointer to the generic block literal.
255 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000256 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000257
258 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000259 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000260 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
261
262 // Get the function pointer from the literal.
263 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
264 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
265
266 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000267 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000268 ConvertType(getBlockFunctionType(getContext(), BPT));
269 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
270 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
271
Mike Stumpc4ae9632009-02-13 15:32:32 +0000272 BlockLiteral =
273 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000274 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
275 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000276
Anders Carlssond2a889b2009-02-12 00:39:25 +0000277 // Add the block literal.
278 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
279 CallArgList Args;
280 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000281
Anders Carlssond2a889b2009-02-12 00:39:25 +0000282 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000283 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000284 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000285 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000286 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000287
Anders Carlssond2a889b2009-02-12 00:39:25 +0000288 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000289 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000290 Func, Args);
291}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000292
293llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000294 // Generate the block descriptor.
295 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000296 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
297 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000298
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000299 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000300
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000301 // Reserved
302 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000303
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000304 // Block literal size. For global blocks we just use the size of the generic
305 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000306 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000307 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000308 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000309
310 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000311 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000312
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000313 llvm::GlobalVariable *Descriptor =
314 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000315 llvm::GlobalVariable::InternalLinkage,
316 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000317 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000318
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000319 // Generate the constants for the block literal.
320 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000321
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000322 CodeGenFunction::BlockInfo Info(0, "global");
323 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000324
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000325 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000326 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000327
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000328 // Flags
Mike Stumpb95bc002009-02-13 16:19:19 +0000329 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000330
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000331 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000332 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000333
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000334 // Function
335 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000336
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000337 // Descriptor
338 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000339
340 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000341 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000342
343 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000344 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000345 llvm::GlobalVariable::InternalLinkage,
346 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000347 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000348
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000349 return BlockLiteral;
350}
351
352llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
353 const BlockInfo& Info)
354{
Mike Stumpc4ae9632009-02-13 15:32:32 +0000355 const FunctionTypeProto *FTy =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000356 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000357
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000358 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000359
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000360 const BlockDecl *BD = Expr->getBlockDecl();
361
362 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000363 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000364 ImplicitParamDecl::Create(getContext(), 0,
365 SourceLocation(), 0,
366 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000367
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000368 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000369
370 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000371 e = BD->param_end(); i != e; ++i)
372 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000373
374 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000375 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
376
377 std::string Name = std::string("__block_function_") + Info.NameSuffix;
378
379 CodeGenTypes &Types = CGM.getTypes();
380 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000381
382 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000383 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
384 Name,
385 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000386
387 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000388 Expr->getBody()->getLocEnd());
389 EmitStmt(Expr->getBody());
390 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
391
392 return Fn;
393}