12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
22437cbfaSAnders Carlsson //
32437cbfaSAnders Carlsson //                     The LLVM Compiler Infrastructure
42437cbfaSAnders Carlsson //
52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source
62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details.
72437cbfaSAnders Carlsson //
82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
92437cbfaSAnders Carlsson //
102437cbfaSAnders Carlsson // This contains code to emit blocks.
112437cbfaSAnders Carlsson //
122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
132437cbfaSAnders Carlsson 
142437cbfaSAnders Carlsson #include "CodeGenFunction.h"
152437cbfaSAnders Carlsson #include "CodeGenModule.h"
162437cbfaSAnders Carlsson #include "llvm/Module.h"
176a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h"
182437cbfaSAnders Carlsson 
192437cbfaSAnders Carlsson #include <algorithm>
202437cbfaSAnders Carlsson 
212437cbfaSAnders Carlsson using namespace clang;
222437cbfaSAnders Carlsson using namespace CodeGen;
232437cbfaSAnders Carlsson 
240874160bSMike Stump // Temporary code to enable testing of __block variables
250874160bSMike Stump // #include "clang/Frontend/CompileOptions.h"
260874160bSMike Stump #include "llvm/Support/CommandLine.h"
270874160bSMike Stump static llvm::cl::opt<bool>
280874160bSMike Stump Enable__block("f__block",
290874160bSMike Stump               // See all the FIXMEs for the various work that needs to be done
300874160bSMike Stump               llvm::cl::desc("temporary option to turn on __block precessing "
310874160bSMike Stump                              "even though the code isn't done yet"),
320874160bSMike Stump               llvm::cl::ValueDisallowed, llvm::cl::AllowInverse,
334446dcf0SMike Stump               llvm::cl::ZeroOrMore,
341ab20caeSMike Stump               llvm::cl::init(true));
350874160bSMike Stump 
36d6ef62f0SMike Stump llvm::Constant *CodeGenFunction::
37aeb0ffd5SMike Stump BuildDescriptorBlockDecl(uint64_t Size, const llvm::StructType* Ty,
38aeb0ffd5SMike Stump                          std::vector<HelperInfo> *NoteForHelper) {
39c2c38331SMike Stump   const llvm::Type *UnsignedLongTy
40c2c38331SMike Stump     = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
4185284bacSMike Stump   llvm::Constant *C;
4285284bacSMike Stump   std::vector<llvm::Constant*> Elts;
4385284bacSMike Stump 
4485284bacSMike Stump   // reserved
45c2c38331SMike Stump   C = llvm::ConstantInt::get(UnsignedLongTy, 0);
4685284bacSMike Stump   Elts.push_back(C);
4785284bacSMike Stump 
4885284bacSMike Stump   // Size
492ac40a9fSMike Stump   // FIXME: What is the right way to say this doesn't fit?  We should give
502ac40a9fSMike Stump   // a user diagnostic in that case.  Better fix would be to change the
512ac40a9fSMike Stump   // API to size_t.
52cb2fbcb0SMike Stump   C = llvm::ConstantInt::get(UnsignedLongTy, Size);
5385284bacSMike Stump   Elts.push_back(C);
5485284bacSMike Stump 
5585284bacSMike Stump   if (BlockHasCopyDispose) {
5685284bacSMike Stump     // copy_func_helper_decl
57aeb0ffd5SMike Stump     Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper));
5885284bacSMike Stump 
5985284bacSMike Stump     // destroy_func_decl
60aeb0ffd5SMike Stump     Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper));
6185284bacSMike Stump   }
6285284bacSMike Stump 
6385284bacSMike Stump   C = llvm::ConstantStruct::get(Elts);
6485284bacSMike Stump 
6585284bacSMike Stump   C = new llvm::GlobalVariable(C->getType(), true,
6685284bacSMike Stump                                llvm::GlobalValue::InternalLinkage,
67499ae7ecSMike Stump                                C, "__block_descriptor_tmp", &CGM.getModule());
6885284bacSMike Stump   return C;
6985284bacSMike Stump }
7085284bacSMike Stump 
7195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
72971f9b66SMike Stump   if (NSConcreteGlobalBlock)
73971f9b66SMike Stump     return NSConcreteGlobalBlock;
74971f9b66SMike Stump 
7552d9c49dSMike Stump   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
762d33d630SMike Stump   // same thing as CreateRuntimeFunction if there's already a variable with the
772d33d630SMike Stump   // same name.
78971f9b66SMike Stump   NSConcreteGlobalBlock
79971f9b66SMike Stump     = new llvm::GlobalVariable(PtrToInt8Ty, false,
80971f9b66SMike Stump                                llvm::GlobalValue::ExternalLinkage,
81971f9b66SMike Stump                                0, "_NSConcreteGlobalBlock",
82971f9b66SMike Stump                                &getModule());
83971f9b66SMike Stump 
84971f9b66SMike Stump   return NSConcreteGlobalBlock;
85971f9b66SMike Stump }
86971f9b66SMike Stump 
8795435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() {
887ab278dbSMike Stump   if (NSConcreteStackBlock)
897ab278dbSMike Stump     return NSConcreteStackBlock;
907ab278dbSMike Stump 
9152d9c49dSMike Stump   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
922d33d630SMike Stump   // same thing as CreateRuntimeFunction if there's already a variable with the
932d33d630SMike Stump   // same name.
947ab278dbSMike Stump   NSConcreteStackBlock
957ab278dbSMike Stump     = new llvm::GlobalVariable(PtrToInt8Ty, false,
967ab278dbSMike Stump                                llvm::GlobalValue::ExternalLinkage,
977ab278dbSMike Stump                                0, "_NSConcreteStackBlock",
987ab278dbSMike Stump                                &getModule());
997ab278dbSMike Stump 
1007ab278dbSMike Stump   return NSConcreteStackBlock;
1017ab278dbSMike Stump }
1027ab278dbSMike Stump 
103ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S,
104ed5e69feSAnders Carlsson                                     CodeGenFunction::BlockInfo &Info) {
105ed5e69feSAnders Carlsson   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
106ed5e69feSAnders Carlsson        I != E; ++I)
107dd942713SDaniel Dunbar     if (*I)
108ed5e69feSAnders Carlsson       CollectBlockDeclRefInfo(*I, Info);
109ed5e69feSAnders Carlsson 
110ed5e69feSAnders Carlsson   if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
111ed5e69feSAnders Carlsson     // FIXME: Handle enums.
112ed5e69feSAnders Carlsson     if (isa<FunctionDecl>(DE->getDecl()))
113ed5e69feSAnders Carlsson       return;
114ed5e69feSAnders Carlsson 
115ed5e69feSAnders Carlsson     if (DE->isByRef())
116ed5e69feSAnders Carlsson       Info.ByRefDeclRefs.push_back(DE);
117ed5e69feSAnders Carlsson     else
118ed5e69feSAnders Carlsson       Info.ByCopyDeclRefs.push_back(DE);
119ed5e69feSAnders Carlsson   }
120ed5e69feSAnders Carlsson }
121ed5e69feSAnders Carlsson 
1222d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
1232d33d630SMike Stump /// declared as a global variable instead of on the stack.
124ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
125ed5e69feSAnders Carlsson {
126ed5e69feSAnders Carlsson   return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
127ed5e69feSAnders Carlsson }
128ed5e69feSAnders Carlsson 
1292d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function
1302d33d630SMike Stump // name.
131b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
132ed5e69feSAnders Carlsson 
133ed5e69feSAnders Carlsson   std::string Name = CurFn->getName();
134ed5e69feSAnders Carlsson   CodeGenFunction::BlockInfo Info(0, Name.c_str());
135ed5e69feSAnders Carlsson   CollectBlockDeclRefInfo(BE->getBody(), Info);
136ed5e69feSAnders Carlsson 
137ed5e69feSAnders Carlsson   // Check if the block can be global.
1382d33d630SMike Stump   // FIXME: This test doesn't work for nested blocks yet.  Longer term, I'd like
1392d33d630SMike Stump   // to just have one code path.  We should move this function into CGM and pass
1402d33d630SMike Stump   // CGF, then we can just check to see if CGF is 0.
14197d01d50SMike Stump   if (0 && CanBlockBeGlobal(Info))
142ed5e69feSAnders Carlsson     return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
14385284bacSMike Stump 
1444446dcf0SMike Stump   std::vector<llvm::Constant*> Elts(5);
14585284bacSMike Stump   llvm::Constant *C;
146b750d928SMike Stump   llvm::Value *V;
14785284bacSMike Stump 
14885284bacSMike Stump   {
14985284bacSMike Stump     // C = BuildBlockStructInitlist();
15085284bacSMike Stump     unsigned int flags = BLOCK_HAS_DESCRIPTOR;
15185284bacSMike Stump 
1524446dcf0SMike Stump     // We run this first so that we set BlockHasCopyDispose from the entire
1534446dcf0SMike Stump     // block literal.
1544446dcf0SMike Stump     // __invoke
1554446dcf0SMike Stump     uint64_t subBlockSize, subBlockAlign;
1564446dcf0SMike Stump     llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
1574446dcf0SMike Stump     llvm::Function *Fn
1584446dcf0SMike Stump       = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
1594446dcf0SMike Stump                                                    subBlockAlign,
1604446dcf0SMike Stump                                                    subBlockDeclRefDecls,
1614446dcf0SMike Stump                                                    BlockHasCopyDispose);
1624446dcf0SMike Stump     Elts[3] = Fn;
1634446dcf0SMike Stump 
1644446dcf0SMike Stump     if (!Enable__block && BlockHasCopyDispose)
1654446dcf0SMike Stump       ErrorUnsupported(BE, "block literal that requires copy/dispose");
1664446dcf0SMike Stump 
16785284bacSMike Stump     if (BlockHasCopyDispose)
16885284bacSMike Stump       flags |= BLOCK_HAS_COPY_DISPOSE;
16985284bacSMike Stump 
170499ae7ecSMike Stump     // __isa
1717ab278dbSMike Stump     C = CGM.getNSConcreteStackBlock();
17285284bacSMike Stump     C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
1734446dcf0SMike Stump     Elts[0] = C;
17485284bacSMike Stump 
17585284bacSMike Stump     // __flags
17685284bacSMike Stump     const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
17785284bacSMike Stump       CGM.getTypes().ConvertType(CGM.getContext().IntTy));
17885284bacSMike Stump     C = llvm::ConstantInt::get(IntTy, flags);
1794446dcf0SMike Stump     Elts[1] = C;
18085284bacSMike Stump 
18185284bacSMike Stump     // __reserved
18285284bacSMike Stump     C = llvm::ConstantInt::get(IntTy, 0);
1834446dcf0SMike Stump     Elts[2] = C;
18485284bacSMike Stump 
185b750d928SMike Stump     if (subBlockDeclRefDecls.size() == 0) {
186d6ef62f0SMike Stump       // __descriptor
187aeb0ffd5SMike Stump       Elts[4] = BuildDescriptorBlockDecl(subBlockSize, 0, 0);
188d6ef62f0SMike Stump 
1893a139f37SMike Stump       // Optimize to being a global block.
1903a139f37SMike Stump       Elts[0] = CGM.getNSConcreteGlobalBlock();
1913a139f37SMike Stump       Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
1923a139f37SMike Stump 
19385284bacSMike Stump       C = llvm::ConstantStruct::get(Elts);
19485284bacSMike Stump 
19585284bacSMike Stump       char Name[32];
1969319db8aSMike Stump       sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
19785284bacSMike Stump       C = new llvm::GlobalVariable(C->getType(), true,
19885284bacSMike Stump                                    llvm::GlobalValue::InternalLinkage,
19985284bacSMike Stump                                    C, Name, &CGM.getModule());
2005d2534adSMike Stump       QualType BPT = BE->getType();
2015d2534adSMike Stump       C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
20285284bacSMike Stump       return C;
20385284bacSMike Stump     }
20485284bacSMike Stump 
205b750d928SMike Stump     std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
206aeb0ffd5SMike Stump     for (int i=0; i<4; ++i)
207b750d928SMike Stump       Types[i] = Elts[i]->getType();
208aeb0ffd5SMike Stump     Types[4] = PtrToInt8Ty;
20985284bacSMike Stump 
2101db7d04bSMike Stump     for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
2111db7d04bSMike Stump       const Expr *E = subBlockDeclRefDecls[i];
2121db7d04bSMike Stump       const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2131db7d04bSMike Stump       QualType Ty = E->getType();
21497d01d50SMike Stump       if (BDRE && BDRE->isByRef()) {
21597d01d50SMike Stump         uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
21697d01d50SMike Stump         Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
21797d01d50SMike Stump       } else
2181db7d04bSMike Stump         Types[i+5] = ConvertType(Ty);
2191db7d04bSMike Stump     }
220b750d928SMike Stump 
221aeb0ffd5SMike Stump     llvm::StructType *Ty = llvm::StructType::get(Types, true);
222b750d928SMike Stump 
223b750d928SMike Stump     llvm::AllocaInst *A = CreateTempAlloca(Ty);
224b750d928SMike Stump     A->setAlignment(subBlockAlign);
225b750d928SMike Stump     V = A;
226b750d928SMike Stump 
227aeb0ffd5SMike Stump     std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
228aeb0ffd5SMike Stump     int helpersize = 0;
229aeb0ffd5SMike Stump 
230aeb0ffd5SMike Stump     for (unsigned i=0; i<4; ++i)
231b750d928SMike Stump       Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
232b750d928SMike Stump 
233b750d928SMike Stump     for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
234b750d928SMike Stump       {
2351db7d04bSMike Stump         // FIXME: Push const down.
2361db7d04bSMike Stump         Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
2371db7d04bSMike Stump         DeclRefExpr *DR;
2381db7d04bSMike Stump         ValueDecl *VD;
239b750d928SMike Stump 
2401db7d04bSMike Stump         DR = dyn_cast<DeclRefExpr>(E);
2411db7d04bSMike Stump         // Skip padding.
2421db7d04bSMike Stump         if (DR) continue;
2431db7d04bSMike Stump 
2441db7d04bSMike Stump         BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2451db7d04bSMike Stump         VD = BDRE->getDecl();
2461db7d04bSMike Stump 
24797d01d50SMike Stump         llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
248aeb0ffd5SMike Stump         NoteForHelper[helpersize].index = i+5;
249aeb0ffd5SMike Stump         NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
250aeb0ffd5SMike Stump         NoteForHelper[helpersize].flag
251aeb0ffd5SMike Stump           = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
252aeb0ffd5SMike Stump 
2531db7d04bSMike Stump         if (LocalDeclMap[VD]) {
25497d01d50SMike Stump           if (BDRE->isByRef()) {
255aeb0ffd5SMike Stump             // FIXME: For only local, or all byrefs?
256aeb0ffd5SMike Stump             NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
25790d8daf5SMike Stump               // FIXME: Someone double check this.
25890d8daf5SMike Stump               (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
25997d01d50SMike Stump             const llvm::Type *Ty = Types[i+5];
26097d01d50SMike Stump             llvm::Value *Loc = LocalDeclMap[VD];
26197d01d50SMike Stump             Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
26297d01d50SMike Stump             Loc = Builder.CreateLoad(Loc, false);
26397d01d50SMike Stump             Loc = Builder.CreateBitCast(Loc, Ty);
26497d01d50SMike Stump             Builder.CreateStore(Loc, Addr);
265aeb0ffd5SMike Stump             ++helpersize;
26697d01d50SMike Stump             continue;
26797d01d50SMike Stump           } else
2681db7d04bSMike Stump             E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
2691db7d04bSMike Stump                                                 VD->getType(), SourceLocation(),
2701db7d04bSMike Stump                                                 false, false);
2711db7d04bSMike Stump         }
27297d01d50SMike Stump         if (BDRE->isByRef()) {
2731db7d04bSMike Stump           E = new (getContext())
2741db7d04bSMike Stump             UnaryOperator(E, UnaryOperator::AddrOf,
2751db7d04bSMike Stump                           getContext().getPointerType(E->getType()),
2761db7d04bSMike Stump                           SourceLocation());
27797d01d50SMike Stump         }
278aeb0ffd5SMike Stump         ++helpersize;
2791db7d04bSMike Stump 
2801db7d04bSMike Stump         RValue r = EmitAnyExpr(E, Addr, false);
28197d01d50SMike Stump         if (r.isScalar()) {
28297d01d50SMike Stump           llvm::Value *Loc = r.getScalarVal();
28397d01d50SMike Stump           const llvm::Type *Ty = Types[i+5];
28497d01d50SMike Stump           if  (BDRE->isByRef()) {
2852d33d630SMike Stump             // E is now the address of the value field, instead, we want the
2862d33d630SMike Stump             // address of the actual ByRef struct.  We optimize this slightly
2872d33d630SMike Stump             // compared to gcc by not grabbing the forwarding slot as this must
2882d33d630SMike Stump             // be done during Block_copy for us, and we can postpone the work
2892d33d630SMike Stump             // until then.
2902d33d630SMike Stump             uint64_t offset = BlockDecls[BDRE->getDecl()];
2912d33d630SMike Stump 
2922d33d630SMike Stump             llvm::Value *BlockLiteral = LoadBlockStruct();
2932d33d630SMike Stump 
2942d33d630SMike Stump             Loc = Builder.CreateGEP(BlockLiteral,
2952d33d630SMike Stump                                     llvm::ConstantInt::get(llvm::Type::Int64Ty,
2962d33d630SMike Stump                                                            offset),
2972d33d630SMike Stump                                     "block.literal");
2982d33d630SMike Stump             Ty = llvm::PointerType::get(Ty, 0);
29997d01d50SMike Stump             Loc = Builder.CreateBitCast(Loc, Ty);
3002d33d630SMike Stump             Loc = Builder.CreateLoad(Loc, false);
3012d33d630SMike Stump             // Loc = Builder.CreateBitCast(Loc, Ty);
30297d01d50SMike Stump           }
30397d01d50SMike Stump           Builder.CreateStore(Loc, Addr);
30497d01d50SMike Stump         } else if (r.isComplex())
305b750d928SMike Stump           // FIXME: implement
306b750d928SMike Stump           ErrorUnsupported(BE, "complex in block literal");
307b750d928SMike Stump         else if (r.isAggregate())
308b750d928SMike Stump           ; // Already created into the destination
309b750d928SMike Stump         else
310b750d928SMike Stump           assert (0 && "bad block variable");
311b750d928SMike Stump         // FIXME: Ensure that the offset created by the backend for
312b750d928SMike Stump         // the struct matches the previously computed offset in BlockDecls.
313b750d928SMike Stump       }
314aeb0ffd5SMike Stump     NoteForHelper.resize(helpersize);
315d6ef62f0SMike Stump 
316d6ef62f0SMike Stump     // __descriptor
317aeb0ffd5SMike Stump     llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockSize, Ty,
318aeb0ffd5SMike Stump                                                        &NoteForHelper);
319aeb0ffd5SMike Stump     Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
320aeb0ffd5SMike Stump     Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
321b750d928SMike Stump   }
322b750d928SMike Stump 
323b750d928SMike Stump   QualType BPT = BE->getType();
324b750d928SMike Stump   return Builder.CreateBitCast(V, ConvertType(BPT));
325b750d928SMike Stump }
32685284bacSMike Stump 
32785284bacSMike Stump 
32895435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() {
329650c932dSMike Stump   if (BlockDescriptorType)
330650c932dSMike Stump     return BlockDescriptorType;
3312437cbfaSAnders Carlsson 
3322437cbfaSAnders Carlsson   const llvm::Type *UnsignedLongTy =
333650c932dSMike Stump     getTypes().ConvertType(getContext().UnsignedLongTy);
3342437cbfaSAnders Carlsson 
3352437cbfaSAnders Carlsson   // struct __block_descriptor {
3362437cbfaSAnders Carlsson   //   unsigned long reserved;
3372437cbfaSAnders Carlsson   //   unsigned long block_size;
3382437cbfaSAnders Carlsson   // };
339650c932dSMike Stump   BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
3402437cbfaSAnders Carlsson                                               UnsignedLongTy,
3412437cbfaSAnders Carlsson                                               NULL);
3422437cbfaSAnders Carlsson 
343650c932dSMike Stump   getModule().addTypeName("struct.__block_descriptor",
344650c932dSMike Stump                           BlockDescriptorType);
3452437cbfaSAnders Carlsson 
346650c932dSMike Stump   return BlockDescriptorType;
3472437cbfaSAnders Carlsson }
3482437cbfaSAnders Carlsson 
34995435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() {
350005c9a62SMike Stump   if (GenericBlockLiteralType)
351005c9a62SMike Stump     return GenericBlockLiteralType;
3522437cbfaSAnders Carlsson 
3532437cbfaSAnders Carlsson   const llvm::Type *BlockDescPtrTy =
354005c9a62SMike Stump     llvm::PointerType::getUnqual(getBlockDescriptorType());
3552437cbfaSAnders Carlsson 
35692bbd6d4SMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
35792bbd6d4SMike Stump     getTypes().ConvertType(getContext().IntTy));
35892bbd6d4SMike Stump 
3592437cbfaSAnders Carlsson   // struct __block_literal_generic {
3605d2534adSMike Stump   //   void *__isa;
3615d2534adSMike Stump   //   int __flags;
3625d2534adSMike Stump   //   int __reserved;
3635d2534adSMike Stump   //   void (*__invoke)(void *);
3645d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
3652437cbfaSAnders Carlsson   // };
366626aecc4SMike Stump   GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
36792bbd6d4SMike Stump                                                   IntTy,
36892bbd6d4SMike Stump                                                   IntTy,
369626aecc4SMike Stump                                                   PtrToInt8Ty,
3702437cbfaSAnders Carlsson                                                   BlockDescPtrTy,
3712437cbfaSAnders Carlsson                                                   NULL);
3722437cbfaSAnders Carlsson 
373005c9a62SMike Stump   getModule().addTypeName("struct.__block_literal_generic",
374005c9a62SMike Stump                           GenericBlockLiteralType);
3752437cbfaSAnders Carlsson 
376005c9a62SMike Stump   return GenericBlockLiteralType;
3772437cbfaSAnders Carlsson }
3782437cbfaSAnders Carlsson 
37995435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
3805d2534adSMike Stump   if (GenericExtendedBlockLiteralType)
3815d2534adSMike Stump     return GenericExtendedBlockLiteralType;
3825d2534adSMike Stump 
3835d2534adSMike Stump   const llvm::Type *BlockDescPtrTy =
3845d2534adSMike Stump     llvm::PointerType::getUnqual(getBlockDescriptorType());
3855d2534adSMike Stump 
3865d2534adSMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
3875d2534adSMike Stump     getTypes().ConvertType(getContext().IntTy));
3885d2534adSMike Stump 
3895d2534adSMike Stump   // struct __block_literal_generic {
3905d2534adSMike Stump   //   void *__isa;
3915d2534adSMike Stump   //   int __flags;
3925d2534adSMike Stump   //   int __reserved;
3935d2534adSMike Stump   //   void (*__invoke)(void *);
3945d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
3955d2534adSMike Stump   //   void *__copy_func_helper_decl;
3965d2534adSMike Stump   //   void *__destroy_func_decl;
3975d2534adSMike Stump   // };
398626aecc4SMike Stump   GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
3995d2534adSMike Stump                                                           IntTy,
4005d2534adSMike Stump                                                           IntTy,
401626aecc4SMike Stump                                                           PtrToInt8Ty,
4025d2534adSMike Stump                                                           BlockDescPtrTy,
403626aecc4SMike Stump                                                           PtrToInt8Ty,
404626aecc4SMike Stump                                                           PtrToInt8Ty,
4055d2534adSMike Stump                                                           NULL);
4065d2534adSMike Stump 
4075d2534adSMike Stump   getModule().addTypeName("struct.__block_literal_extended_generic",
4085d2534adSMike Stump                           GenericExtendedBlockLiteralType);
4095d2534adSMike Stump 
4105d2534adSMike Stump   return GenericExtendedBlockLiteralType;
4115d2534adSMike Stump }
4125d2534adSMike Stump 
4132437cbfaSAnders Carlsson /// getBlockFunctionType - Given a BlockPointerType, will return the
4142437cbfaSAnders Carlsson /// function type for the block, including the first block literal argument.
4152437cbfaSAnders Carlsson static QualType getBlockFunctionType(ASTContext &Ctx,
4166a60fa24SAnders Carlsson                                      const BlockPointerType *BPT) {
417deaad8ccSDouglas Gregor   const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
4182437cbfaSAnders Carlsson 
4192437cbfaSAnders Carlsson   llvm::SmallVector<QualType, 8> Types;
4202437cbfaSAnders Carlsson   Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
4212437cbfaSAnders Carlsson 
422deaad8ccSDouglas Gregor   for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
4232437cbfaSAnders Carlsson        e = FTy->arg_type_end(); i != e; ++i)
4242437cbfaSAnders Carlsson     Types.push_back(*i);
4252437cbfaSAnders Carlsson 
4262437cbfaSAnders Carlsson   return Ctx.getFunctionType(FTy->getResultType(),
4272437cbfaSAnders Carlsson                              &Types[0], Types.size(),
4282437cbfaSAnders Carlsson                              FTy->isVariadic(), 0);
4292437cbfaSAnders Carlsson }
4302437cbfaSAnders Carlsson 
4316a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
4322437cbfaSAnders Carlsson   const BlockPointerType *BPT =
4332437cbfaSAnders Carlsson     E->getCallee()->getType()->getAsBlockPointerType();
4342437cbfaSAnders Carlsson 
4352437cbfaSAnders Carlsson   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
4362437cbfaSAnders Carlsson 
4372437cbfaSAnders Carlsson   // Get a pointer to the generic block literal.
4382437cbfaSAnders Carlsson   const llvm::Type *BlockLiteralTy =
439005c9a62SMike Stump     llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
4402437cbfaSAnders Carlsson 
4412437cbfaSAnders Carlsson   // Bitcast the callee to a block literal.
4422437cbfaSAnders Carlsson   llvm::Value *BlockLiteral =
4432437cbfaSAnders Carlsson     Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
4442437cbfaSAnders Carlsson 
4452437cbfaSAnders Carlsson   // Get the function pointer from the literal.
4462437cbfaSAnders Carlsson   llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
447624497c2SMike Stump   llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
4482437cbfaSAnders Carlsson 
4492437cbfaSAnders Carlsson   // Cast the function pointer to the right type.
4502437cbfaSAnders Carlsson   const llvm::Type *BlockFTy =
4512437cbfaSAnders Carlsson     ConvertType(getBlockFunctionType(getContext(), BPT));
4522437cbfaSAnders Carlsson   const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
4532437cbfaSAnders Carlsson   Func = Builder.CreateBitCast(Func, BlockFTyPtr);
4542437cbfaSAnders Carlsson 
4552437cbfaSAnders Carlsson   BlockLiteral =
4562437cbfaSAnders Carlsson     Builder.CreateBitCast(BlockLiteral,
4572437cbfaSAnders Carlsson                           llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
4582437cbfaSAnders Carlsson                           "tmp");
4592437cbfaSAnders Carlsson 
4602437cbfaSAnders Carlsson   // Add the block literal.
4612437cbfaSAnders Carlsson   QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
4622437cbfaSAnders Carlsson   CallArgList Args;
4632437cbfaSAnders Carlsson   Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
4642437cbfaSAnders Carlsson 
4652437cbfaSAnders Carlsson   // And the rest of the arguments.
4662437cbfaSAnders Carlsson   for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
4672437cbfaSAnders Carlsson        i != e; ++i)
4682437cbfaSAnders Carlsson     Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
4692437cbfaSAnders Carlsson                                   i->getType()));
4702437cbfaSAnders Carlsson 
4712437cbfaSAnders Carlsson   // And call the block.
4722437cbfaSAnders Carlsson   return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
4732437cbfaSAnders Carlsson                   Func, Args);
4742437cbfaSAnders Carlsson }
4756a60fa24SAnders Carlsson 
47697d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
47797d01d50SMike Stump   uint64_t &offset = BlockDecls[E->getDecl()];
47897d01d50SMike Stump 
47997d01d50SMike Stump   const llvm::Type *Ty;
48097d01d50SMike Stump   Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
48197d01d50SMike Stump 
4820874160bSMike Stump   if (!Enable__block && E->isByRef())
48397d01d50SMike Stump     ErrorUnsupported(E, "__block variable in block literal");
4840c743277SMike Stump   else if (!Enable__block && E->getType()->isBlockPointerType())
48597d01d50SMike Stump     ErrorUnsupported(E, "block pointer in block literal");
486d9728222SMike Stump   else if (!Enable__block && (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
487d9728222SMike Stump                               getContext().isObjCNSObjectType(E->getType())))
48897d01d50SMike Stump     ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
48997d01d50SMike Stump                      "literal");
4900c743277SMike Stump   else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType()))
49197d01d50SMike Stump     ErrorUnsupported(E, "Objective-C variable in block literal");
49297d01d50SMike Stump 
49397d01d50SMike Stump   // See if we have already allocated an offset for this variable.
49497d01d50SMike Stump   if (offset == 0) {
4954446dcf0SMike Stump     // Don't run the expensive check, unless we have to.
4964446dcf0SMike Stump     if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
4974446dcf0SMike Stump       BlockHasCopyDispose = true;
49897d01d50SMike Stump     // if not, allocate one now.
49997d01d50SMike Stump     offset = getBlockOffset(E);
50097d01d50SMike Stump   }
50197d01d50SMike Stump 
50297d01d50SMike Stump   llvm::Value *BlockLiteral = LoadBlockStruct();
50397d01d50SMike Stump   llvm::Value *V = Builder.CreateGEP(BlockLiteral,
50497d01d50SMike Stump                                      llvm::ConstantInt::get(llvm::Type::Int64Ty,
50597d01d50SMike Stump                                                             offset),
5062d33d630SMike Stump                                      "block.literal");
50797d01d50SMike Stump   if (E->isByRef()) {
50897d01d50SMike Stump     bool needsCopyDispose = BlockRequiresCopying(E->getType());
50997d01d50SMike Stump     uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
51097d01d50SMike Stump     const llvm::Type *PtrStructTy
51197d01d50SMike Stump       = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
51297d01d50SMike Stump     Ty = PtrStructTy;
51397d01d50SMike Stump     Ty = llvm::PointerType::get(Ty, 0);
51497d01d50SMike Stump     V = Builder.CreateBitCast(V, Ty);
51597d01d50SMike Stump     V = Builder.CreateLoad(V, false);
51697d01d50SMike Stump     V = Builder.CreateStructGEP(V, 1, "forwarding");
51797d01d50SMike Stump     V = Builder.CreateLoad(V, false);
51897d01d50SMike Stump     V = Builder.CreateBitCast(V, PtrStructTy);
51997d01d50SMike Stump     V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
52097d01d50SMike Stump   } else {
52197d01d50SMike Stump     Ty = llvm::PointerType::get(Ty, 0);
52297d01d50SMike Stump     V = Builder.CreateBitCast(V, Ty);
52397d01d50SMike Stump   }
52497d01d50SMike Stump   return V;
52597d01d50SMike Stump }
52697d01d50SMike Stump 
5272d5a2878SMike Stump llvm::Constant *
5286c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
5296a60fa24SAnders Carlsson   // Generate the block descriptor.
5306a60fa24SAnders Carlsson   const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
53192bbd6d4SMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
53292bbd6d4SMike Stump     getTypes().ConvertType(getContext().IntTy));
5336a60fa24SAnders Carlsson 
5346a60fa24SAnders Carlsson   llvm::Constant *DescriptorFields[2];
5356a60fa24SAnders Carlsson 
5366a60fa24SAnders Carlsson   // Reserved
5376a60fa24SAnders Carlsson   DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
5386a60fa24SAnders Carlsson 
5396a60fa24SAnders Carlsson   // Block literal size. For global blocks we just use the size of the generic
5406a60fa24SAnders Carlsson   // block literal struct.
5416a60fa24SAnders Carlsson   uint64_t BlockLiteralSize =
542005c9a62SMike Stump     TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
5436a60fa24SAnders Carlsson   DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
5446a60fa24SAnders Carlsson 
5456a60fa24SAnders Carlsson   llvm::Constant *DescriptorStruct =
5466a60fa24SAnders Carlsson     llvm::ConstantStruct::get(&DescriptorFields[0], 2);
5476a60fa24SAnders Carlsson 
5486a60fa24SAnders Carlsson   llvm::GlobalVariable *Descriptor =
5496a60fa24SAnders Carlsson     new llvm::GlobalVariable(DescriptorStruct->getType(), true,
5506a60fa24SAnders Carlsson                              llvm::GlobalVariable::InternalLinkage,
5516a60fa24SAnders Carlsson                              DescriptorStruct, "__block_descriptor_global",
5526a60fa24SAnders Carlsson                              &getModule());
5536a60fa24SAnders Carlsson 
5546a60fa24SAnders Carlsson   // Generate the constants for the block literal.
5556a60fa24SAnders Carlsson   llvm::Constant *LiteralFields[5];
5566a60fa24SAnders Carlsson 
5572d5a2878SMike Stump   CodeGenFunction::BlockInfo Info(0, n);
558b750d928SMike Stump   uint64_t subBlockSize, subBlockAlign;
5591db7d04bSMike Stump   llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
560a61bb4d1SDaniel Dunbar   bool subBlockHasCopyDispose = false;
561cb2fbcb0SMike Stump   llvm::Function *Fn
5626c39666aSMike Stump     = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
563b750d928SMike Stump                                                  subBlockAlign,
5644446dcf0SMike Stump                                                  subBlockDeclRefDecls,
5654446dcf0SMike Stump                                                  subBlockHasCopyDispose);
566cb2fbcb0SMike Stump   assert(subBlockSize == BlockLiteralSize
567cb2fbcb0SMike Stump          && "no imports allowed for global block");
568de373a03SMike Stump   // FIXME: This causes a failure on clang-i686-linux, not sure why,
569de373a03SMike Stump   // disable for now.
570a61bb4d1SDaniel Dunbar   assert(!subBlockHasCopyDispose && "no imports allowed for global block");
5716a60fa24SAnders Carlsson 
5726a60fa24SAnders Carlsson   // isa
573971f9b66SMike Stump   LiteralFields[0] = getNSConcreteGlobalBlock();
5746a60fa24SAnders Carlsson 
5756a60fa24SAnders Carlsson   // Flags
5765878c797SAnders Carlsson   LiteralFields[1] =
5775878c797SAnders Carlsson     llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
5786a60fa24SAnders Carlsson 
5796a60fa24SAnders Carlsson   // Reserved
58092bbd6d4SMike Stump   LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
5816a60fa24SAnders Carlsson 
5826a60fa24SAnders Carlsson   // Function
5836a60fa24SAnders Carlsson   LiteralFields[3] = Fn;
5846a60fa24SAnders Carlsson 
5856a60fa24SAnders Carlsson   // Descriptor
5866a60fa24SAnders Carlsson   LiteralFields[4] = Descriptor;
5876a60fa24SAnders Carlsson 
5886a60fa24SAnders Carlsson   llvm::Constant *BlockLiteralStruct =
5896a60fa24SAnders Carlsson     llvm::ConstantStruct::get(&LiteralFields[0], 5);
5906a60fa24SAnders Carlsson 
5916a60fa24SAnders Carlsson   llvm::GlobalVariable *BlockLiteral =
5926a60fa24SAnders Carlsson     new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
5936a60fa24SAnders Carlsson                              llvm::GlobalVariable::InternalLinkage,
5946a60fa24SAnders Carlsson                              BlockLiteralStruct, "__block_literal_global",
5956a60fa24SAnders Carlsson                              &getModule());
5966a60fa24SAnders Carlsson 
5976a60fa24SAnders Carlsson   return BlockLiteral;
5986a60fa24SAnders Carlsson }
5996a60fa24SAnders Carlsson 
600cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() {
601cb2fbcb0SMike Stump   return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
602cb2fbcb0SMike Stump }
603cb2fbcb0SMike Stump 
6044446dcf0SMike Stump llvm::Function *
6054446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
606cb2fbcb0SMike Stump                                        const BlockInfo& Info,
607b750d928SMike Stump                                        uint64_t &Size,
608b750d928SMike Stump                                        uint64_t &Align,
6094446dcf0SMike Stump                        llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
6104446dcf0SMike Stump                                        bool &subBlockHasCopyDispose) {
611deaad8ccSDouglas Gregor   const FunctionProtoType *FTy =
6123385fe12SChris Lattner     cast<FunctionProtoType>(BExpr->getFunctionType());
6136a60fa24SAnders Carlsson 
6146a60fa24SAnders Carlsson   FunctionArgList Args;
6156a60fa24SAnders Carlsson 
6163385fe12SChris Lattner   const BlockDecl *BD = BExpr->getBlockDecl();
6176a60fa24SAnders Carlsson 
6186a60fa24SAnders Carlsson   // FIXME: This leaks
6196a60fa24SAnders Carlsson   ImplicitParamDecl *SelfDecl =
6206a60fa24SAnders Carlsson     ImplicitParamDecl::Create(getContext(), 0,
6216a60fa24SAnders Carlsson                               SourceLocation(), 0,
6226a60fa24SAnders Carlsson                               getContext().getPointerType(getContext().VoidTy));
6236a60fa24SAnders Carlsson 
6246a60fa24SAnders Carlsson   Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
625cb2fbcb0SMike Stump   BlockStructDecl = SelfDecl;
6266a60fa24SAnders Carlsson 
627*c4b30e59SSteve Naroff   for (BlockDecl::param_const_iterator i = BD->param_begin(),
6286a60fa24SAnders Carlsson        e = BD->param_end(); i != e; ++i)
629eecd39f2SMike Stump     Args.push_back(std::make_pair(*i, (*i)->getType()));
6306a60fa24SAnders Carlsson 
6316a60fa24SAnders Carlsson   const CGFunctionInfo &FI =
6326a60fa24SAnders Carlsson     CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
6336a60fa24SAnders Carlsson 
6342d5a2878SMike Stump   std::string Name = std::string("__") + Info.Name + "_block_invoke_";
6356a60fa24SAnders Carlsson   CodeGenTypes &Types = CGM.getTypes();
6366a60fa24SAnders Carlsson   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
6376a60fa24SAnders Carlsson 
6386a60fa24SAnders Carlsson   llvm::Function *Fn =
6396a60fa24SAnders Carlsson     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
6406a60fa24SAnders Carlsson                            Name,
6416a60fa24SAnders Carlsson                            &CGM.getModule());
6426a60fa24SAnders Carlsson 
6436a60fa24SAnders Carlsson   StartFunction(BD, FTy->getResultType(), Fn, Args,
6443385fe12SChris Lattner                 BExpr->getBody()->getLocEnd());
6453385fe12SChris Lattner   EmitStmt(BExpr->getBody());
6463385fe12SChris Lattner   FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
6476a60fa24SAnders Carlsson 
648b750d928SMike Stump   // The runtime needs a minimum alignment of a void *.
649b750d928SMike Stump   uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
650b750d928SMike Stump   BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
651b750d928SMike Stump 
652cb2fbcb0SMike Stump   Size = BlockOffset;
653b750d928SMike Stump   Align = BlockAlign;
654b750d928SMike Stump   subBlockDeclRefDecls = BlockDeclRefDecls;
6554446dcf0SMike Stump   subBlockHasCopyDispose |= BlockHasCopyDispose;
6566a60fa24SAnders Carlsson   return Fn;
6576a60fa24SAnders Carlsson }
6581db7d04bSMike Stump 
659aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
6601db7d04bSMike Stump   const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
6611db7d04bSMike Stump 
6621db7d04bSMike Stump   uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
6631db7d04bSMike Stump   uint64_t Align = getContext().getDeclAlignInBytes(D);
6641db7d04bSMike Stump 
6651db7d04bSMike Stump   if (BDRE->isByRef()) {
6661db7d04bSMike Stump     Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
6671db7d04bSMike Stump     Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
6681db7d04bSMike Stump   }
6691db7d04bSMike Stump 
6701db7d04bSMike Stump   assert ((Align > 0) && "alignment must be 1 byte or more");
6711db7d04bSMike Stump 
6721db7d04bSMike Stump   uint64_t OldOffset = BlockOffset;
6731db7d04bSMike Stump 
6741db7d04bSMike Stump   // Ensure proper alignment, even if it means we have to have a gap
6751db7d04bSMike Stump   BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
6761db7d04bSMike Stump   BlockAlign = std::max(Align, BlockAlign);
6771db7d04bSMike Stump 
6781db7d04bSMike Stump   uint64_t Pad = BlockOffset - OldOffset;
6791db7d04bSMike Stump   if (Pad) {
6801db7d04bSMike Stump     llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
6811db7d04bSMike Stump     QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
6821db7d04bSMike Stump                                                        llvm::APInt(32, Pad),
6831db7d04bSMike Stump                                                        ArrayType::Normal, 0);
6841db7d04bSMike Stump     ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
6851db7d04bSMike Stump                                          0, QualType(PadTy), VarDecl::None,
6861db7d04bSMike Stump                                          SourceLocation());
6871db7d04bSMike Stump     Expr *E;
6881db7d04bSMike Stump     E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
6891db7d04bSMike Stump                                        SourceLocation(), false, false);
6901db7d04bSMike Stump     BlockDeclRefDecls.push_back(E);
6911db7d04bSMike Stump   }
6921db7d04bSMike Stump   BlockDeclRefDecls.push_back(BDRE);
6931db7d04bSMike Stump 
6941db7d04bSMike Stump   BlockOffset += Size;
6951db7d04bSMike Stump   return BlockOffset-Size;
6961db7d04bSMike Stump }
69797d01d50SMike Stump 
698aeb0ffd5SMike Stump llvm::Constant *BlockFunction::
699aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
700aeb0ffd5SMike Stump                            std::vector<HelperInfo> &NoteForHelper) {
7010c743277SMike Stump   QualType R = getContext().VoidTy;
7020c743277SMike Stump 
7030c743277SMike Stump   FunctionArgList Args;
7040c743277SMike Stump   // FIXME: This leaks
705aeb0ffd5SMike Stump   ImplicitParamDecl *Dst =
706aeb0ffd5SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
707aeb0ffd5SMike Stump                               getContext().getPointerType(getContext().VoidTy));
708aeb0ffd5SMike Stump   Args.push_back(std::make_pair(Dst, Dst->getType()));
7090c743277SMike Stump   ImplicitParamDecl *Src =
7100c743277SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
7110c743277SMike Stump                               getContext().getPointerType(getContext().VoidTy));
7120c743277SMike Stump   Args.push_back(std::make_pair(Src, Src->getType()));
7130c743277SMike Stump 
7140c743277SMike Stump   const CGFunctionInfo &FI =
7150c743277SMike Stump     CGM.getTypes().getFunctionInfo(R, Args);
7160c743277SMike Stump 
7170c743277SMike Stump   std::string Name = std::string("__copy_helper_block_");
7180c743277SMike Stump   CodeGenTypes &Types = CGM.getTypes();
7190c743277SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
7200c743277SMike Stump 
7210c743277SMike Stump   llvm::Function *Fn =
7220c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
7230c743277SMike Stump                            Name,
7240c743277SMike Stump                            &CGM.getModule());
7250c743277SMike Stump 
7260c743277SMike Stump   IdentifierInfo *II
7270c743277SMike Stump     = &CGM.getContext().Idents.get("__copy_helper_block_");
7280c743277SMike Stump 
7290c743277SMike Stump   FunctionDecl *FD = FunctionDecl::Create(getContext(),
7300c743277SMike Stump                                           getContext().getTranslationUnitDecl(),
7310c743277SMike Stump                                           SourceLocation(), II, R,
7320c743277SMike Stump                                           FunctionDecl::Static, false,
7330c743277SMike Stump                                           true);
7340c743277SMike Stump   CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
735aeb0ffd5SMike Stump 
736aeb0ffd5SMike Stump   llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
737aeb0ffd5SMike Stump   llvm::Type *PtrPtrT;
738aeb0ffd5SMike Stump   PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
739aeb0ffd5SMike Stump   SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
740aeb0ffd5SMike Stump   SrcObj = Builder.CreateLoad(SrcObj);
741aeb0ffd5SMike Stump 
742aeb0ffd5SMike Stump   llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
743aeb0ffd5SMike Stump   DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0));
744aeb0ffd5SMike Stump 
745aeb0ffd5SMike Stump   for (unsigned i=0; i < NoteForHelper.size(); ++i) {
746aeb0ffd5SMike Stump     int flag = NoteForHelper[i].flag;
747aeb0ffd5SMike Stump     int index = NoteForHelper[i].index;
748aeb0ffd5SMike Stump 
749aeb0ffd5SMike Stump     if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
750aeb0ffd5SMike Stump         || NoteForHelper[i].RequiresCopying) {
751aeb0ffd5SMike Stump       llvm::Value *Srcv = SrcObj;
752aeb0ffd5SMike Stump       Srcv = Builder.CreateStructGEP(Srcv, index);
753aeb0ffd5SMike Stump       Srcv = Builder.CreateBitCast(Srcv,
754aeb0ffd5SMike Stump                                    llvm::PointerType::get(PtrToInt8Ty, 0));
755aeb0ffd5SMike Stump       Srcv = Builder.CreateLoad(Srcv);
756aeb0ffd5SMike Stump 
757aeb0ffd5SMike Stump       llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
758aeb0ffd5SMike Stump       Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
759aeb0ffd5SMike Stump 
760aeb0ffd5SMike Stump       llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
761aeb0ffd5SMike Stump       llvm::Value *F = getBlockObjectAssign();
762aeb0ffd5SMike Stump       Builder.CreateCall3(F, Dstv, Srcv, N);
763aeb0ffd5SMike Stump     }
764aeb0ffd5SMike Stump   }
765aeb0ffd5SMike Stump 
7660c743277SMike Stump   CGF.FinishFunction();
7670c743277SMike Stump 
7680c743277SMike Stump   return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
76997d01d50SMike Stump }
77097d01d50SMike Stump 
771d6ef62f0SMike Stump llvm::Constant *BlockFunction::
772aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
773aeb0ffd5SMike Stump                               const llvm::StructType* T,
774aeb0ffd5SMike Stump                               std::vector<HelperInfo> &NoteForHelper) {
7750c743277SMike Stump   QualType R = getContext().VoidTy;
7760c743277SMike Stump 
7770c743277SMike Stump   FunctionArgList Args;
7780c743277SMike Stump   // FIXME: This leaks
7790c743277SMike Stump   ImplicitParamDecl *Src =
7800c743277SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
7810c743277SMike Stump                               getContext().getPointerType(getContext().VoidTy));
7820c743277SMike Stump 
7830c743277SMike Stump   Args.push_back(std::make_pair(Src, Src->getType()));
7840c743277SMike Stump 
7850c743277SMike Stump   const CGFunctionInfo &FI =
7860c743277SMike Stump     CGM.getTypes().getFunctionInfo(R, Args);
7870c743277SMike Stump 
7880c743277SMike Stump   std::string Name = std::string("__destroy_helper_block_");
7890c743277SMike Stump   CodeGenTypes &Types = CGM.getTypes();
7900c743277SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
7910c743277SMike Stump 
7920c743277SMike Stump   llvm::Function *Fn =
7930c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
7940c743277SMike Stump                            Name,
7950c743277SMike Stump                            &CGM.getModule());
7960c743277SMike Stump 
7970c743277SMike Stump   IdentifierInfo *II
7980c743277SMike Stump     = &CGM.getContext().Idents.get("__destroy_helper_block_");
7990c743277SMike Stump 
8000c743277SMike Stump   FunctionDecl *FD = FunctionDecl::Create(getContext(),
8010c743277SMike Stump                                           getContext().getTranslationUnitDecl(),
8020c743277SMike Stump                                           SourceLocation(), II, R,
8030c743277SMike Stump                                           FunctionDecl::Static, false,
8040c743277SMike Stump                                           true);
8050c743277SMike Stump   CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
8066f7d9f87SMike Stump 
8076f7d9f87SMike Stump   llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
8086f7d9f87SMike Stump   llvm::Type *PtrPtrT;
8096f7d9f87SMike Stump   PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
8106f7d9f87SMike Stump   SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
8116f7d9f87SMike Stump   SrcObj = Builder.CreateLoad(SrcObj);
8126f7d9f87SMike Stump 
8136f7d9f87SMike Stump   for (unsigned i=0; i < NoteForHelper.size(); ++i) {
8146f7d9f87SMike Stump     int flag = NoteForHelper[i].flag;
8156f7d9f87SMike Stump     int index = NoteForHelper[i].index;
8166f7d9f87SMike Stump 
8176f7d9f87SMike Stump     if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
8186f7d9f87SMike Stump         || NoteForHelper[i].RequiresCopying) {
8196f7d9f87SMike Stump       llvm::Value *Srcv = SrcObj;
8206f7d9f87SMike Stump       Srcv = Builder.CreateStructGEP(Srcv, index);
8216f7d9f87SMike Stump       Srcv = Builder.CreateBitCast(Srcv,
8226f7d9f87SMike Stump                                    llvm::PointerType::get(PtrToInt8Ty, 0));
8236f7d9f87SMike Stump       Srcv = Builder.CreateLoad(Srcv);
8246f7d9f87SMike Stump 
8256f7d9f87SMike Stump       BuildBlockRelease(Srcv, flag);
8266f7d9f87SMike Stump     }
8276f7d9f87SMike Stump   }
8286f7d9f87SMike Stump 
8290c743277SMike Stump   CGF.FinishFunction();
8300c743277SMike Stump 
8310c743277SMike Stump   return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
8320c743277SMike Stump }
8330c743277SMike Stump 
834aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
835aeb0ffd5SMike Stump                                        std::vector<HelperInfo> &NoteForHelper) {
836aeb0ffd5SMike Stump   return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
837aeb0ffd5SMike Stump                                                          T, NoteForHelper);
8380c743277SMike Stump }
8390c743277SMike Stump 
840aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
841aeb0ffd5SMike Stump                                        std::vector<HelperInfo> &NoteForHelper) {
842aeb0ffd5SMike Stump   return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
843aeb0ffd5SMike Stump                                                             T, NoteForHelper);
84497d01d50SMike Stump }
845626aecc4SMike Stump 
846f89230d9SMike Stump llvm::Constant *BlockFunction::
847f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
848ee2a5ee5SMike Stump   QualType R = getContext().VoidTy;
849ee2a5ee5SMike Stump 
850ee2a5ee5SMike Stump   FunctionArgList Args;
851ee2a5ee5SMike Stump   // FIXME: This leaks
852f89230d9SMike Stump   ImplicitParamDecl *Dst =
853f89230d9SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
854f89230d9SMike Stump                               getContext().getPointerType(getContext().VoidTy));
855f89230d9SMike Stump   Args.push_back(std::make_pair(Dst, Dst->getType()));
856f89230d9SMike Stump 
857f89230d9SMike Stump   // FIXME: This leaks
858ee2a5ee5SMike Stump   ImplicitParamDecl *Src =
859ee2a5ee5SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
860ee2a5ee5SMike Stump                               getContext().getPointerType(getContext().VoidTy));
861ee2a5ee5SMike Stump   Args.push_back(std::make_pair(Src, Src->getType()));
862ee2a5ee5SMike Stump 
863ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
864ee2a5ee5SMike Stump     CGM.getTypes().getFunctionInfo(R, Args);
865ee2a5ee5SMike Stump 
866ee2a5ee5SMike Stump   std::string Name = std::string("__Block_byref_id_object_copy_");
867ee2a5ee5SMike Stump   CodeGenTypes &Types = CGM.getTypes();
868ee2a5ee5SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
869ee2a5ee5SMike Stump 
870ee2a5ee5SMike Stump   llvm::Function *Fn =
871ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
872ee2a5ee5SMike Stump                            Name,
873ee2a5ee5SMike Stump                            &CGM.getModule());
874ee2a5ee5SMike Stump 
875ee2a5ee5SMike Stump   IdentifierInfo *II
876ee2a5ee5SMike Stump     = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
877ee2a5ee5SMike Stump 
878ee2a5ee5SMike Stump   FunctionDecl *FD = FunctionDecl::Create(getContext(),
879ee2a5ee5SMike Stump                                           getContext().getTranslationUnitDecl(),
880ee2a5ee5SMike Stump                                           SourceLocation(), II, R,
881ee2a5ee5SMike Stump                                           FunctionDecl::Static, false,
882ee2a5ee5SMike Stump                                           true);
883ee2a5ee5SMike Stump   CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
884f89230d9SMike Stump 
885f89230d9SMike Stump   // dst->x
886f89230d9SMike Stump   llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
887f89230d9SMike Stump   V = Builder.CreateBitCast(V, T);
888f89230d9SMike Stump   V = Builder.CreateStructGEP(V, 6, "x");
889f89230d9SMike Stump   llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
890f89230d9SMike Stump 
891f89230d9SMike Stump   // src->x
892f89230d9SMike Stump   V = CGF.GetAddrOfLocalVar(Src);
893f89230d9SMike Stump   V = Builder.CreateLoad(V);
894f89230d9SMike Stump   V = Builder.CreateBitCast(V, T);
895f89230d9SMike Stump   V = Builder.CreateStructGEP(V, 6, "x");
896f89230d9SMike Stump   V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
897f89230d9SMike Stump   llvm::Value *SrcObj = Builder.CreateLoad(V);
898f89230d9SMike Stump 
899f89230d9SMike Stump   flag |= BLOCK_BYREF_CALLER;
900f89230d9SMike Stump 
901f89230d9SMike Stump   llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
902f89230d9SMike Stump   llvm::Value *F = getBlockObjectAssign();
903f89230d9SMike Stump   Builder.CreateCall3(F, DstObj, SrcObj, N);
904f89230d9SMike Stump 
905ee2a5ee5SMike Stump   CGF.FinishFunction();
906ee2a5ee5SMike Stump 
907ee2a5ee5SMike Stump   return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
908ee2a5ee5SMike Stump }
909ee2a5ee5SMike Stump 
910fbe25dd1SMike Stump llvm::Constant *
911fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
912fbe25dd1SMike Stump                                                   int flag) {
913ee2a5ee5SMike Stump   QualType R = getContext().VoidTy;
914ee2a5ee5SMike Stump 
915ee2a5ee5SMike Stump   FunctionArgList Args;
916ee2a5ee5SMike Stump   // FIXME: This leaks
917ee2a5ee5SMike Stump   ImplicitParamDecl *Src =
918ee2a5ee5SMike Stump     ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
919ee2a5ee5SMike Stump                               getContext().getPointerType(getContext().VoidTy));
920ee2a5ee5SMike Stump 
921ee2a5ee5SMike Stump   Args.push_back(std::make_pair(Src, Src->getType()));
922ee2a5ee5SMike Stump 
923ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
924ee2a5ee5SMike Stump     CGM.getTypes().getFunctionInfo(R, Args);
925ee2a5ee5SMike Stump 
926ee2a5ee5SMike Stump   std::string Name = std::string("__Block_byref_id_object_dispose_");
927ee2a5ee5SMike Stump   CodeGenTypes &Types = CGM.getTypes();
928ee2a5ee5SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
929ee2a5ee5SMike Stump 
930ee2a5ee5SMike Stump   llvm::Function *Fn =
931ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
932ee2a5ee5SMike Stump                            Name,
933ee2a5ee5SMike Stump                            &CGM.getModule());
934ee2a5ee5SMike Stump 
935ee2a5ee5SMike Stump   IdentifierInfo *II
936ee2a5ee5SMike Stump     = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
937ee2a5ee5SMike Stump 
938ee2a5ee5SMike Stump   FunctionDecl *FD = FunctionDecl::Create(getContext(),
939ee2a5ee5SMike Stump                                           getContext().getTranslationUnitDecl(),
940ee2a5ee5SMike Stump                                           SourceLocation(), II, R,
941ee2a5ee5SMike Stump                                           FunctionDecl::Static, false,
942ee2a5ee5SMike Stump                                           true);
943ee2a5ee5SMike Stump   CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
944fbe25dd1SMike Stump 
945fbe25dd1SMike Stump   llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
946fbe25dd1SMike Stump   V = Builder.CreateBitCast(V, T);
947fbe25dd1SMike Stump   V = Builder.CreateStructGEP(V, 6, "x");
948fbe25dd1SMike Stump   V = Builder.CreateBitCast(V, PtrToInt8Ty);
949fbe25dd1SMike Stump 
950fbe25dd1SMike Stump   flag |= BLOCK_BYREF_CALLER;
951fbe25dd1SMike Stump   BuildBlockRelease(V, flag);
952ee2a5ee5SMike Stump   CGF.FinishFunction();
953ee2a5ee5SMike Stump 
954ee2a5ee5SMike Stump   return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
955ee2a5ee5SMike Stump }
956ee2a5ee5SMike Stump 
957f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
958f89230d9SMike Stump                                                     int flag) {
959f89230d9SMike Stump   return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
960ee2a5ee5SMike Stump }
961ee2a5ee5SMike Stump 
962fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
963fbe25dd1SMike Stump                                                        int flag) {
964fbe25dd1SMike Stump   return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
965ee2a5ee5SMike Stump }
966ee2a5ee5SMike Stump 
967626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() {
968626aecc4SMike Stump   if (CGM.BlockObjectDispose == 0) {
969626aecc4SMike Stump     const llvm::FunctionType *FTy;
970626aecc4SMike Stump     std::vector<const llvm::Type*> ArgTys;
971626aecc4SMike Stump     const llvm::Type *ResultType = llvm::Type::VoidTy;
972626aecc4SMike Stump     ArgTys.push_back(PtrToInt8Ty);
973626aecc4SMike Stump     ArgTys.push_back(llvm::Type::Int32Ty);
974626aecc4SMike Stump     FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
975626aecc4SMike Stump     CGM.BlockObjectDispose
976626aecc4SMike Stump       = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
977626aecc4SMike Stump   }
978626aecc4SMike Stump   return CGM.BlockObjectDispose;
979626aecc4SMike Stump }
980626aecc4SMike Stump 
981f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() {
982f89230d9SMike Stump   if (CGM.BlockObjectAssign == 0) {
983f89230d9SMike Stump     const llvm::FunctionType *FTy;
984f89230d9SMike Stump     std::vector<const llvm::Type*> ArgTys;
985f89230d9SMike Stump     const llvm::Type *ResultType = llvm::Type::VoidTy;
986f89230d9SMike Stump     ArgTys.push_back(PtrToInt8Ty);
987f89230d9SMike Stump     ArgTys.push_back(PtrToInt8Ty);
988f89230d9SMike Stump     ArgTys.push_back(llvm::Type::Int32Ty);
989f89230d9SMike Stump     FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
990f89230d9SMike Stump     CGM.BlockObjectAssign
991f89230d9SMike Stump       = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
992f89230d9SMike Stump   }
993f89230d9SMike Stump   return CGM.BlockObjectAssign;
994f89230d9SMike Stump }
995f89230d9SMike Stump 
996fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
997626aecc4SMike Stump   llvm::Value *F = getBlockObjectDispose();
998fbe25dd1SMike Stump   llvm::Value *N;
999626aecc4SMike Stump   V = Builder.CreateBitCast(V, PtrToInt8Ty);
1000fbe25dd1SMike Stump   N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
1001626aecc4SMike Stump   Builder.CreateCall2(F, V, N);
1002626aecc4SMike Stump }
10034446dcf0SMike Stump 
10044446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
1005aeb0ffd5SMike Stump 
1006aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1007aeb0ffd5SMike Stump                              CGBuilderTy &B)
1008aeb0ffd5SMike Stump   : CGM(cgm), CGF(cgf), Builder(B) {
1009aeb0ffd5SMike Stump   PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1010aeb0ffd5SMike Stump 
1011aeb0ffd5SMike Stump   BlockHasCopyDispose = false;
1012aeb0ffd5SMike Stump }
1013