Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 1 | //===--- RewriteBlocks.cpp ----------------------------------------------===// |
| 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 | // Hacks and fun related to the closure rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/IdentifierTable.h" |
| 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/SmallPtrSet.h" |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using llvm::utostr; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class RewriteBlocks : public ASTConsumer { |
| 32 | Rewriter Rewrite; |
| 33 | Diagnostic &Diags; |
| 34 | const LangOptions &LangOpts; |
| 35 | unsigned RewriteFailedDiag; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 36 | |
| 37 | ASTContext *Context; |
| 38 | SourceManager *SM; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 39 | FileID MainFileID; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 40 | const char *MainFileStart, *MainFileEnd; |
| 41 | |
| 42 | // Block expressions. |
| 43 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 44 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 45 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 47 | // Block related declarations. |
| 48 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 49 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 50 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 51 | |
| 52 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 54 | // The function/method we are rewriting. |
| 55 | FunctionDecl *CurFunctionDef; |
| 56 | ObjCMethodDecl *CurMethodDef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 58 | bool IsHeader; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 60 | std::string Preamble; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 61 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | RewriteBlocks(std::string inFile, Diagnostic &D, |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 63 | const LangOptions &LOpts); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 64 | ~RewriteBlocks() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | // Get the buffer corresponding to MainFileID. |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 66 | // If we haven't changed it, then we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 68 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 69 | std::string S(RewriteBuf->begin(), RewriteBuf->end()); |
| 70 | printf("%s\n", S.c_str()); |
| 71 | } else { |
| 72 | printf("No changes\n"); |
| 73 | } |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 76 | void Initialize(ASTContext &context); |
| 77 | |
| 78 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen); |
| 79 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 80 | const char *NewStr, unsigned NewLength); |
| 81 | |
| 82 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 83 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 84 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 85 | HandleTopLevelSingleDecl(*I); |
| 86 | } |
| 87 | void HandleTopLevelSingleDecl(Decl *D); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 88 | void HandleDeclInMainFile(Decl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
| 90 | // Top level |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 91 | Stmt *RewriteFunctionBody(Stmt *S); |
| 92 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 93 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 95 | // Block specific rewrite rules. |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 96 | std::string SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD=0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 98 | void RewriteBlockCall(CallExpr *Exp); |
| 99 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Steve Naroff | 5e52b17 | 2008-10-04 18:52:47 +0000 | [diff] [blame] | 100 | void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 101 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
| 103 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 104 | const char *funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 106 | const char *funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 108 | bool hasCopyDisposeHelpers); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 109 | std::string SynthesizeBlockCall(CallExpr *Exp); |
| 110 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 111 | const char *FunName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Steve Naroff | d3f7790 | 2008-10-05 00:06:12 +0000 | [diff] [blame] | 113 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 114 | void GetBlockCallExprs(Stmt *S); |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 115 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 117 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 118 | // canonical type. We only care if the top-level type is a closure pointer. |
| 119 | bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 121 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 122 | bool isObjCType(QualType T) { |
| 123 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 124 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 126 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 128 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 129 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 130 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 132 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 134 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | // ObjC rewrite methods. |
| 140 | void RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl); |
| 141 | void RewriteCategoryDecl(ObjCCategoryDecl *CatDecl); |
| 142 | void RewriteProtocolDecl(ObjCProtocolDecl *PDecl); |
| 143 | void RewriteMethodDecl(ObjCMethodDecl *MDecl); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 145 | void RewriteFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 146 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 147 | void RewriteCastExpr(CastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 149 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Steve Naroff | 1f6c3ae | 2008-09-24 17:22:34 +0000 | [diff] [blame] | 150 | void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 151 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static bool IsHeaderFile(const std::string &Filename) { |
| 156 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 158 | if (DotPos == std::string::npos) { |
| 159 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | return false; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 163 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 164 | // C header: .h |
| 165 | // C++ header: .hh or .H; |
| 166 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 168 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 169 | RewriteBlocks::RewriteBlocks(std::string inFile, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | Diagnostic &D, const LangOptions &LOpts) : |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 171 | Diags(D), LangOpts(LOpts) { |
| 172 | IsHeader = IsHeaderFile(inFile); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 173 | CurFunctionDef = 0; |
| 174 | CurMethodDef = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 176 | "rewriting failed"); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 179 | ASTConsumer *clang::CreateBlockRewriter(const std::string& InFile, |
| 180 | Diagnostic &Diags, |
| 181 | const LangOptions &LangOpts) { |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 182 | return new RewriteBlocks(InFile, Diags, LangOpts); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void RewriteBlocks::Initialize(ASTContext &context) { |
| 186 | Context = &context; |
| 187 | SM = &Context->getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 189 | // Get the ID and start/end of the main file. |
| 190 | MainFileID = SM->getMainFileID(); |
| 191 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 192 | MainFileStart = MainBuf->getBufferStart(); |
| 193 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 195 | Rewrite.setSourceMgr(Context->getSourceManager(), LangOpts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 197 | if (IsHeader) |
| 198 | Preamble = "#pragma once\n"; |
| 199 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 200 | Preamble += "#define BLOCK_IMPL\n"; |
| 201 | Preamble += "struct __block_impl {\n"; |
| 202 | Preamble += " void *isa;\n"; |
| 203 | Preamble += " int Flags;\n"; |
| 204 | Preamble += " int Size;\n"; |
| 205 | Preamble += " void *FuncPtr;\n"; |
| 206 | Preamble += "};\n"; |
| 207 | Preamble += "enum {\n"; |
| 208 | Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n"; |
| 209 | Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n"; |
| 210 | Preamble += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | if (LangOpts.Microsoft) |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 212 | Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n"; |
| 213 | else |
| 214 | Preamble += "#define __OBJC_RW_EXTERN extern\n"; |
| 215 | Preamble += "// Runtime copy/destroy helper functions\n"; |
| 216 | Preamble += "__OBJC_RW_EXTERN void _Block_copy_assign(void *, void *);\n"; |
| 217 | Preamble += "__OBJC_RW_EXTERN void _Block_byref_assign_copy(void *, void *);\n"; |
| 218 | Preamble += "__OBJC_RW_EXTERN void _Block_destroy(void *);\n"; |
| 219 | Preamble += "__OBJC_RW_EXTERN void _Block_byref_release(void *);\n"; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 220 | Preamble += "__OBJC_RW_EXTERN void *_NSConcreteGlobalBlock;\n"; |
| 221 | Preamble += "__OBJC_RW_EXTERN void *_NSConcreteStackBlock;\n"; |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 222 | Preamble += "#endif\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
| 224 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 225 | Preamble.c_str(), Preamble.size()); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | void RewriteBlocks::InsertText(SourceLocation Loc, const char *StrData, |
| 229 | unsigned StrLen) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 230 | if (!Rewrite.InsertText(Loc, StrData, StrLen)) |
| 231 | return; |
| 232 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 233 | } |
| 234 | |
| 235 | void RewriteBlocks::ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 236 | const char *NewStr, unsigned NewLength) { |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 237 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 238 | llvm::StringRef(NewStr, NewLength))) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 239 | return; |
| 240 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 241 | } |
| 242 | |
| 243 | void RewriteBlocks::RewriteMethodDecl(ObjCMethodDecl *Method) { |
| 244 | bool haveBlockPtrs = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | for (ObjCMethodDecl::param_iterator I = Method->param_begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 246 | E = Method->param_end(); I != E; ++I) |
| 247 | if (isBlockPointerType((*I)->getType())) |
| 248 | haveBlockPtrs = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 250 | if (!haveBlockPtrs) |
| 251 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 253 | // Do a fuzzy rewrite. |
| 254 | // We have 1 or more arguments that have closure pointers. |
| 255 | SourceLocation Loc = Method->getLocStart(); |
| 256 | SourceLocation LocEnd = Method->getLocEnd(); |
| 257 | const char *startBuf = SM->getCharacterData(Loc); |
| 258 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 259 | |
| 260 | const char *methodPtr = startBuf; |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 261 | std::string Tag = "struct __block_impl *"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 263 | while (*methodPtr++ && (methodPtr != endBuf)) { |
| 264 | switch (*methodPtr) { |
| 265 | case ':': |
| 266 | methodPtr++; |
| 267 | if (*methodPtr == '(') { |
| 268 | const char *scanType = ++methodPtr; |
| 269 | bool foundBlockPointer = false; |
| 270 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 272 | while (parenCount) { |
| 273 | switch (*scanType) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | case '(': |
| 275 | parenCount++; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 276 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | case ')': |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 278 | parenCount--; |
| 279 | break; |
| 280 | case '^': |
| 281 | foundBlockPointer = true; |
| 282 | break; |
| 283 | } |
| 284 | scanType++; |
| 285 | } |
| 286 | if (foundBlockPointer) { |
| 287 | // advance the location to startArgList. |
| 288 | Loc = Loc.getFileLocWithOffset(methodPtr-startBuf); |
| 289 | assert((Loc.isValid()) && "Invalid Loc"); |
| 290 | ReplaceText(Loc, scanType-methodPtr-1, Tag.c_str(), Tag.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 292 | // Advance startBuf. Since the underlying buffer has changed, |
| 293 | // it's very important to advance startBuf (so we can correctly |
| 294 | // compute a relative Loc the next time around). |
| 295 | startBuf = methodPtr; |
| 296 | } |
| 297 | // Advance the method ptr to the end of the type. |
| 298 | methodPtr = scanType; |
| 299 | } |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | void RewriteBlocks::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | for (ObjCInterfaceDecl::instmeth_iterator |
| 308 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 309 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 310 | RewriteMethodDecl(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 312 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 313 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 314 | RewriteMethodDecl(*I); |
| 315 | } |
| 316 | |
| 317 | void RewriteBlocks::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | for (ObjCCategoryDecl::instmeth_iterator |
| 319 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 320 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 321 | RewriteMethodDecl(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 323 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 324 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 325 | RewriteMethodDecl(*I); |
| 326 | } |
| 327 | |
| 328 | void RewriteBlocks::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | for (ObjCProtocolDecl::instmeth_iterator |
| 330 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 331 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 332 | RewriteMethodDecl(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | for (ObjCProtocolDecl::classmeth_iterator |
| 334 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 335 | I != E; ++I) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 336 | RewriteMethodDecl(*I); |
| 337 | } |
| 338 | |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | // Top Level Driver Code |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 343 | void RewriteBlocks::HandleTopLevelSingleDecl(Decl *D) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 344 | // Two cases: either the decl could be in the main file, or it could be in a |
| 345 | // #included file. If the former, rewrite it now. If the later, check to see |
| 346 | // if we rewrote the #include/#import. |
| 347 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 348 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 350 | // If this is for a builtin, ignore it. |
| 351 | if (Loc.isInvalid()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 353 | if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) |
| 354 | RewriteInterfaceDecl(MD); |
| 355 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) |
| 356 | RewriteCategoryDecl(CD); |
| 357 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) |
| 358 | RewriteProtocolDecl(PD); |
| 359 | |
| 360 | // If we have a decl in the main file, see if we should rewrite it. |
Chris Lattner | 23f2c58 | 2009-01-25 22:02:19 +0000 | [diff] [blame] | 361 | if (SM->isFromMainFile(Loc)) |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 362 | HandleDeclInMainFile(D); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | std::string RewriteBlocks::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 367 | const char *funcName, |
| 368 | std::string Tag) { |
| 369 | const FunctionType *AFT = CE->getFunctionType(); |
| 370 | QualType RT = AFT->getResultType(); |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 371 | std::string StructRef = "struct " + Tag; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 372 | std::string S = "static " + RT.getAsString() + " __" + |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 373 | funcName + "_" + "block_func_" + utostr(i); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 374 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 375 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 377 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 378 | S += "()"; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 379 | } else if (BD->param_empty()) { |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 380 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 381 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 382 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 383 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 384 | S += '('; |
| 385 | // first add the implicit argument. |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 386 | S += StructRef + " *__cself, "; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 387 | std::string ParamStr; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 388 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 389 | E = BD->param_end(); AI != E; ++AI) { |
| 390 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 391 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 392 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 393 | S += ParamStr; |
| 394 | } |
| 395 | if (FT->isVariadic()) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 396 | if (!BD->param_empty()) S += ", "; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 397 | S += "..."; |
| 398 | } |
| 399 | S += ')'; |
| 400 | } |
| 401 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 403 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 404 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 406 | E = BlockByRefDecls.end(); I != E; ++I) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 407 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 408 | std::string Name = (*I)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 409 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name, |
| 410 | Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 411 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 413 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 415 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 416 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 417 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 418 | // Handle nested closure invocation. For example: |
| 419 | // |
| 420 | // void (^myImportedClosure)(void); |
| 421 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | // |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 423 | // void (^anotherClosure)(void); |
| 424 | // anotherClosure = ^(void) { |
| 425 | // myImportedClosure(); // import and invoke the closure |
| 426 | // }; |
| 427 | // |
| 428 | if (isBlockPointerType((*I)->getType())) |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 429 | S += "struct __block_impl *"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 430 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 431 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 432 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 433 | } |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 434 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 435 | const char *cstr = RewrittenStr.c_str(); |
| 436 | while (*cstr++ != '{') ; |
| 437 | S += cstr; |
| 438 | S += "\n"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 439 | return S; |
| 440 | } |
| 441 | |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 442 | std::string RewriteBlocks::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 443 | const char *funcName, |
| 444 | std::string Tag) { |
| 445 | std::string StructRef = "struct " + Tag; |
| 446 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 448 | S += funcName; |
| 449 | S += "_block_copy_" + utostr(i); |
| 450 | S += "(" + StructRef; |
| 451 | S += "*dst, " + StructRef; |
| 452 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 454 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 455 | S += "_Block_copy_assign(&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 456 | S += (*I)->getNameAsString(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 457 | S += ", src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 458 | S += (*I)->getNameAsString(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 459 | S += ");}"; |
| 460 | } |
| 461 | S += "\nstatic void __"; |
| 462 | S += funcName; |
| 463 | S += "_block_dispose_" + utostr(i); |
| 464 | S += "(" + StructRef; |
| 465 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 467 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 468 | S += "_Block_destroy(src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 469 | S += (*I)->getNameAsString(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 470 | S += ");"; |
| 471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | S += "}\n"; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 473 | return S; |
| 474 | } |
| 475 | |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 476 | std::string RewriteBlocks::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 477 | bool hasCopyDisposeHelpers) { |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 478 | std::string S = "struct " + Tag; |
| 479 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 481 | S += " {\n struct __block_impl impl;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 483 | if (hasCopyDisposeHelpers) |
| 484 | S += " void *copy;\n void *dispose;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 486 | Constructor += "(void *fp"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 488 | if (hasCopyDisposeHelpers) |
| 489 | Constructor += ", void *copyHelp, void *disposeHelp"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 491 | if (BlockDeclRefs.size()) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 492 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 494 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 495 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 496 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 497 | std::string ArgName = "_" + FieldName; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 498 | // Handle nested closure invocation. For example: |
| 499 | // |
| 500 | // void (^myImportedBlock)(void); |
| 501 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | // |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 503 | // void (^anotherBlock)(void); |
| 504 | // anotherBlock = ^(void) { |
| 505 | // myImportedBlock(); // import and invoke the closure |
| 506 | // }; |
| 507 | // |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 508 | if (isBlockPointerType((*I)->getType())) { |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 509 | S += "struct __block_impl *"; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 510 | Constructor += ", void *" + ArgName; |
| 511 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 512 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 513 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 514 | Constructor += ", " + ArgName; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 515 | } |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 516 | S += FieldName + ";\n"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 517 | } |
| 518 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 520 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 521 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 522 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 523 | std::string ArgName = "_" + FieldName; |
| 524 | // Handle nested closure invocation. For example: |
| 525 | // |
| 526 | // void (^myImportedBlock)(void); |
| 527 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | // |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 529 | // void (^anotherBlock)(void); |
| 530 | // anotherBlock = ^(void) { |
| 531 | // myImportedBlock(); // import and invoke the closure |
| 532 | // }; |
| 533 | // |
| 534 | if (isBlockPointerType((*I)->getType())) { |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 535 | S += "struct __block_impl *"; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 536 | Constructor += ", void *" + ArgName; |
| 537 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 538 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName, |
| 539 | Context->PrintingPolicy); |
| 540 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName, |
| 541 | Context->PrintingPolicy); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 542 | Constructor += ", " + ArgName; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 543 | } |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 544 | S += FieldName + "; // by ref\n"; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 545 | } |
| 546 | // Finish writing the constructor. |
| 547 | // FIXME: handle NSConcreteGlobalBlock. |
| 548 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 549 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 550 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 552 | if (hasCopyDisposeHelpers) |
| 553 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 555 | // Initialize all "by copy" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 557 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 558 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 559 | Constructor += " "; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 560 | if (isBlockPointerType((*I)->getType())) |
| 561 | Constructor += Name + " = (struct __block_impl *)_"; |
| 562 | else |
| 563 | Constructor += Name + " = _"; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 564 | Constructor += Name + ";\n"; |
| 565 | } |
| 566 | // Initialize all "by ref" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 568 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 569 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 570 | Constructor += " "; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 571 | if (isBlockPointerType((*I)->getType())) |
| 572 | Constructor += Name + " = (struct __block_impl *)_"; |
| 573 | else |
| 574 | Constructor += Name + " = _"; |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 575 | Constructor += Name + ";\n"; |
| 576 | } |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 577 | } else { |
| 578 | // Finish writing the constructor. |
| 579 | // FIXME: handle NSConcreteGlobalBlock. |
| 580 | Constructor += ", int flags=0) {\n"; |
| 581 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 582 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 583 | if (hasCopyDisposeHelpers) |
| 584 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 585 | } |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 586 | Constructor += " "; |
| 587 | Constructor += "}\n"; |
| 588 | S += Constructor; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 589 | S += "};\n"; |
| 590 | return S; |
| 591 | } |
| 592 | |
| 593 | void RewriteBlocks::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 594 | const char *FunName) { |
| 595 | // Insert closures that were part of the function. |
| 596 | for (unsigned i = 0; i < Blocks.size(); i++) { |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 597 | |
Steve Naroff | d3f7790 | 2008-10-05 00:06:12 +0000 | [diff] [blame] | 598 | CollectBlockDeclRefInfo(Blocks[i]); |
| 599 | |
Steve Naroff | 48a8c61 | 2008-10-03 12:09:49 +0000 | [diff] [blame] | 600 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
| 602 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 603 | ImportedBlockDecls.size() > 0); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 604 | |
| 605 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 606 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 607 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 609 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 610 | |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 611 | if (ImportedBlockDecls.size()) { |
| 612 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 613 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 616 | BlockDeclRefs.clear(); |
| 617 | BlockByRefDecls.clear(); |
| 618 | BlockByCopyDecls.clear(); |
| 619 | BlockCallExprs.clear(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 620 | ImportedBlockDecls.clear(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 621 | } |
| 622 | Blocks.clear(); |
Steve Naroff | 8e9216d | 2008-10-04 17:10:02 +0000 | [diff] [blame] | 623 | RewrittenBlockExprs.clear(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void RewriteBlocks::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
Steve Naroff | 3ad29e2 | 2008-10-03 00:12:09 +0000 | [diff] [blame] | 627 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 628 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 630 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 631 | } |
| 632 | |
| 633 | void RewriteBlocks::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
| 634 | SourceLocation FunLocStart = MD->getLocStart(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 635 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 636 | // Convert colons to underscores. |
| 637 | std::string::size_type loc = 0; |
| 638 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 639 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 641 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 642 | } |
| 643 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 644 | void RewriteBlocks::GetBlockDeclRefExprs(Stmt *S) { |
| 645 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 646 | CI != E; ++CI) |
Steve Naroff | 84a969f | 2008-10-08 17:31:13 +0000 | [diff] [blame] | 647 | if (*CI) { |
| 648 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 649 | GetBlockDeclRefExprs(CBE->getBody()); |
| 650 | else |
| 651 | GetBlockDeclRefExprs(*CI); |
| 652 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 653 | // Handle specific things. |
| 654 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 655 | // FIXME: Handle enums. |
| 656 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 657 | BlockDeclRefs.push_back(CDRE); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | void RewriteBlocks::GetBlockCallExprs(Stmt *S) { |
| 662 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 663 | CI != E; ++CI) |
Steve Naroff | 84a969f | 2008-10-08 17:31:13 +0000 | [diff] [blame] | 664 | if (*CI) { |
| 665 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 666 | GetBlockCallExprs(CBE->getBody()); |
| 667 | else |
| 668 | GetBlockCallExprs(*CI); |
| 669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 671 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 672 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 673 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 674 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 675 | } |
| 676 | return; |
| 677 | } |
| 678 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 679 | std::string RewriteBlocks::SynthesizeBlockCall(CallExpr *Exp) { |
| 680 | // Navigate to relevant type information. |
Steve Naroff | cc2ece2 | 2008-09-24 22:46:45 +0000 | [diff] [blame] | 681 | const char *closureName = 0; |
| 682 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 684 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 685 | closureName = DRE->getDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 686 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 687 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 688 | closureName = CDRE->getDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 689 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 690 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 691 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 692 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 693 | } else { |
| 694 | assert(1 && "RewriteBlockClass: Bad type"); |
| 695 | } |
| 696 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 697 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 698 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 699 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 700 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 702 | // Build a closure call - start with a paren expr to enforce precedence. |
| 703 | std::string BlockCall = "("; |
| 704 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | // Synthesize the cast. |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 706 | BlockCall += "(" + Exp->getType().getAsString() + "(*)"; |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 707 | BlockCall += "(struct __block_impl *"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 708 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 710 | E = FTP->arg_type_end(); I && (I != E); ++I) |
| 711 | BlockCall += ", " + (*I).getAsString(); |
| 712 | } |
| 713 | BlockCall += "))"; // close the argument list and paren expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 715 | // Invoke the closure. We need to cast it since the declaration type is |
| 716 | // bogus (it's a function pointer type) |
| 717 | BlockCall += "((struct __block_impl *)"; |
| 718 | std::string closureExprBufStr; |
| 719 | llvm::raw_string_ostream closureExprBuf(closureExprBufStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 720 | Exp->getCallee()->printPretty(closureExprBuf, *Context, 0, |
| 721 | PrintingPolicy(LangOpts)); |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 722 | BlockCall += closureExprBuf.str(); |
| 723 | BlockCall += ")->FuncPtr)"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 725 | // Add the arguments. |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 726 | BlockCall += "((struct __block_impl *)"; |
Steve Naroff | b65a4f1 | 2008-10-04 17:45:51 +0000 | [diff] [blame] | 727 | BlockCall += closureExprBuf.str(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 729 | E = Exp->arg_end(); I != E; ++I) { |
| 730 | std::string syncExprBufS; |
| 731 | llvm::raw_string_ostream Buf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 732 | (*I)->printPretty(Buf, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 733 | BlockCall += ", " + Buf.str(); |
| 734 | } |
| 735 | return BlockCall; |
| 736 | } |
| 737 | |
| 738 | void RewriteBlocks::RewriteBlockCall(CallExpr *Exp) { |
| 739 | std::string BlockCall = SynthesizeBlockCall(Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 741 | const char *startBuf = SM->getCharacterData(Exp->getLocStart()); |
| 742 | const char *endBuf = SM->getCharacterData(Exp->getLocEnd()); |
| 743 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | ReplaceText(Exp->getLocStart(), endBuf-startBuf, |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 745 | BlockCall.c_str(), BlockCall.size()); |
| 746 | } |
| 747 | |
Steve Naroff | 5e52b17 | 2008-10-04 18:52:47 +0000 | [diff] [blame] | 748 | void RewriteBlocks::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 749 | // FIXME: Add more elaborate code generation required by the ABI. |
| 750 | InsertText(BDRE->getLocStart(), "*", 1); |
| 751 | } |
| 752 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 753 | void RewriteBlocks::RewriteCastExpr(CastExpr *CE) { |
| 754 | SourceLocation LocStart = CE->getLocStart(); |
| 755 | SourceLocation LocEnd = CE->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 757 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 758 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 760 | const char *startBuf = SM->getCharacterData(LocStart); |
| 761 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 763 | // advance the location to startArgList. |
| 764 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 766 | while (*argPtr++ && (argPtr < endBuf)) { |
| 767 | switch (*argPtr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | case '^': |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 769 | // Replace the '^' with '*'. |
| 770 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 771 | ReplaceText(LocStart, 1, "*", 1); |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | return; |
| 776 | } |
| 777 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 778 | void RewriteBlocks::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 779 | SourceLocation DeclLoc = FD->getLocation(); |
Steve Naroff | e0109a5 | 2008-10-10 15:33:34 +0000 | [diff] [blame] | 780 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 782 | // We have 1 or more arguments that have closure pointers. |
| 783 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 784 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 786 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 788 | parenCount++; |
| 789 | // advance the location to startArgList. |
Steve Naroff | e0109a5 | 2008-10-10 15:33:34 +0000 | [diff] [blame] | 790 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 791 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 793 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 795 | while (*argPtr++ && parenCount) { |
| 796 | switch (*argPtr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | case '^': |
Steve Naroff | e0109a5 | 2008-10-10 15:33:34 +0000 | [diff] [blame] | 798 | // Replace the '^' with '*'. |
| 799 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 800 | ReplaceText(DeclLoc, 1, "*", 1); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 801 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | case '(': |
| 803 | parenCount++; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 804 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | case ')': |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 806 | parenCount--; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | } |
| 810 | return; |
| 811 | } |
| 812 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 813 | bool RewriteBlocks::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 814 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 815 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 816 | if (PT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 817 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 818 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 819 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 820 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 821 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 822 | } |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 823 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 825 | E = FTP->arg_type_end(); I != E; ++I) |
| 826 | if (isBlockPointerType(*I)) |
| 827 | return true; |
| 828 | } |
| 829 | return false; |
| 830 | } |
| 831 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | void RewriteBlocks::GetExtentOfArgList(const char *Name, |
Steve Naroff | 1f6c3ae | 2008-09-24 17:22:34 +0000 | [diff] [blame] | 833 | const char *&LParen, const char *&RParen) { |
| 834 | const char *argPtr = strchr(Name, '('); |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 835 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 837 | LParen = argPtr; // output the start. |
| 838 | argPtr++; // skip past the left paren. |
| 839 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 841 | while (*argPtr && parenCount) { |
| 842 | switch (*argPtr) { |
| 843 | case '(': parenCount++; break; |
| 844 | case ')': parenCount--; break; |
| 845 | default: break; |
| 846 | } |
| 847 | if (parenCount) argPtr++; |
| 848 | } |
| 849 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 850 | RParen = argPtr; // output the end |
| 851 | } |
| 852 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 853 | void RewriteBlocks::RewriteBlockPointerDecl(NamedDecl *ND) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 854 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 855 | RewriteBlockPointerFunctionArgs(FD); |
| 856 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | } |
Steve Naroff | ca3bb4f | 2008-09-23 21:15:53 +0000 | [diff] [blame] | 858 | // Handle Variables and Typedefs. |
| 859 | SourceLocation DeclLoc = ND->getLocation(); |
| 860 | QualType DeclT; |
| 861 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 862 | DeclT = VD->getType(); |
| 863 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 864 | DeclT = TDD->getUnderlyingType(); |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 865 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 866 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | else |
Steve Naroff | ca3bb4f | 2008-09-23 21:15:53 +0000 | [diff] [blame] | 868 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Steve Naroff | ca3bb4f | 2008-09-23 21:15:53 +0000 | [diff] [blame] | 870 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 871 | const char *endBuf = startBuf; |
| 872 | // scan backward (from the decl location) for the end of the previous decl. |
| 873 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 874 | startBuf--; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 876 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 877 | // may take block argument types (which will be handled below). |
| 878 | if (*startBuf == '^') { |
| 879 | // Replace the '^' with '*', computing a negative offset. |
| 880 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 881 | ReplaceText(DeclLoc, 1, "*", 1); |
| 882 | } |
| 883 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
Steve Naroff | ca3bb4f | 2008-09-23 21:15:53 +0000 | [diff] [blame] | 884 | // Replace the '^' with '*' for arguments. |
| 885 | DeclLoc = ND->getLocation(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 886 | startBuf = SM->getCharacterData(DeclLoc); |
Steve Naroff | 1f6c3ae | 2008-09-24 17:22:34 +0000 | [diff] [blame] | 887 | const char *argListBegin, *argListEnd; |
Steve Naroff | ca3bb4f | 2008-09-23 21:15:53 +0000 | [diff] [blame] | 888 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 889 | while (argListBegin < argListEnd) { |
| 890 | if (*argListBegin == '^') { |
| 891 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 892 | ReplaceText(CaretLoc, 1, "*", 1); |
| 893 | } |
| 894 | argListBegin++; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 895 | } |
Steve Naroff | eab5f63 | 2008-09-23 19:24:41 +0000 | [diff] [blame] | 896 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | void RewriteBlocks::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 901 | // Add initializers for any closure decl refs. |
Steve Naroff | 84a969f | 2008-10-08 17:31:13 +0000 | [diff] [blame] | 902 | GetBlockDeclRefExprs(Exp->getBody()); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 903 | if (BlockDeclRefs.size()) { |
| 904 | // Unique all "by copy" declarations. |
| 905 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 906 | if (!BlockDeclRefs[i]->isByRef()) |
| 907 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 908 | // Unique all "by ref" declarations. |
| 909 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 910 | if (BlockDeclRefs[i]->isByRef()) { |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 911 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 912 | } |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 913 | // Find any imported blocks...they will need special attention. |
| 914 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 915 | if (isBlockPointerType(BlockDeclRefs[i]->getType())) { |
| 916 | GetBlockCallExprs(Blocks[i]); |
| 917 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 918 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 919 | } |
Steve Naroff | d3f7790 | 2008-10-05 00:06:12 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | std::string RewriteBlocks::SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD) { |
| 923 | Blocks.push_back(Exp); |
| 924 | |
| 925 | CollectBlockDeclRefInfo(Exp); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 926 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 928 | if (CurFunctionDef) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 929 | FuncName = std::string(CurFunctionDef->getNameAsString()); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 930 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 931 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 932 | // Convert colons to underscores. |
| 933 | std::string::size_type loc = 0; |
| 934 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 935 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 39622b9 | 2008-10-03 15:38:09 +0000 | [diff] [blame] | 936 | } else if (VD) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 937 | FuncName = std::string(VD->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 939 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 941 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
Steve Naroff | a0b75cf | 2008-10-02 23:30:43 +0000 | [diff] [blame] | 942 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 944 | std::string FunkTypeStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 946 | // Get a pointer to the function type so we can cast appropriately. |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 947 | Context->getPointerType(QualType(Exp->getFunctionType(),0)) |
| 948 | .getAsStringInternal(FunkTypeStr, Context->PrintingPolicy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 950 | // Rewrite the closure block with a compound literal. The first cast is |
| 951 | // to prevent warnings from the C compiler. |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 952 | std::string Init = "(" + FunkTypeStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 954 | Init += ")&" + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 956 | // Initialize the block function. |
| 957 | Init += "((void*)" + Func; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Steve Naroff | acba0f2 | 2008-10-04 23:47:37 +0000 | [diff] [blame] | 959 | if (ImportedBlockDecls.size()) { |
| 960 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
| 961 | Init += ",(void*)" + Buf; |
| 962 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
| 963 | Init += ",(void*)" + Buf; |
| 964 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 965 | // Add initializers for any closure decl refs. |
| 966 | if (BlockDeclRefs.size()) { |
| 967 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 969 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 970 | Init += ","; |
| 971 | if (isObjCType((*I)->getType())) { |
| 972 | Init += "[["; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 973 | Init += (*I)->getNameAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 974 | Init += " retain] autorelease]"; |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 975 | } else if (isBlockPointerType((*I)->getType())) { |
| 976 | Init += "(void *)"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 977 | Init += (*I)->getNameAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 978 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 979 | Init += (*I)->getNameAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 984 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 985 | Init += ",&"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 986 | Init += (*I)->getNameAsString(); |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
Steve Naroff | 83ba14e | 2008-10-03 15:04:50 +0000 | [diff] [blame] | 989 | Init += ")"; |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 990 | BlockDeclRefs.clear(); |
| 991 | BlockByRefDecls.clear(); |
| 992 | BlockByCopyDecls.clear(); |
Steve Naroff | 4e13b76 | 2008-10-03 20:28:15 +0000 | [diff] [blame] | 993 | ImportedBlockDecls.clear(); |
| 994 | |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 995 | return Init; |
| 996 | } |
| 997 | |
| 998 | //===----------------------------------------------------------------------===// |
| 999 | // Function Body / Expression rewriting |
| 1000 | //===----------------------------------------------------------------------===// |
| 1001 | |
| 1002 | Stmt *RewriteBlocks::RewriteFunctionBody(Stmt *S) { |
| 1003 | // Start by rewriting all children. |
| 1004 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1005 | CI != E; ++CI) |
| 1006 | if (*CI) { |
| 1007 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
Eli Friedman | 687abff | 2009-06-08 04:24:21 +0000 | [diff] [blame] | 1008 | RewriteFunctionBody(CBE->getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1010 | // We've just rewritten the block body in place. |
| 1011 | // Now we snarf the rewritten text and stash it away for later use. |
| 1012 | std::string S = Rewrite.getRewritenText(CBE->getSourceRange()); |
| 1013 | RewrittenBlockExprs[CBE] = S; |
| 1014 | std::string Init = SynthesizeBlockInitExpr(CBE); |
| 1015 | // Do the rewrite, using S.size() which contains the rewritten size. |
| 1016 | ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size()); |
| 1017 | } else { |
Eli Friedman | 687abff | 2009-06-08 04:24:21 +0000 | [diff] [blame] | 1018 | RewriteFunctionBody(*CI); |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | // Handle specific things. |
| 1022 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 1023 | if (CE->getCallee()->getType()->isBlockPointerType()) |
| 1024 | RewriteBlockCall(CE); |
| 1025 | } |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1026 | if (CastExpr *CE = dyn_cast<CastExpr>(S)) { |
| 1027 | RewriteCastExpr(CE); |
| 1028 | } |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1029 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
Ted Kremenek | fda4fed | 2008-10-06 18:47:09 +0000 | [diff] [blame] | 1030 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 1031 | DI != DE; ++DI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1033 | Decl *SD = *DI; |
Ted Kremenek | fda4fed | 2008-10-06 18:47:09 +0000 | [diff] [blame] | 1034 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 1035 | if (isBlockPointerType(ND->getType())) |
| 1036 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1038 | CheckFunctionPointerDecl(ND->getType(), ND); |
Ted Kremenek | fda4fed | 2008-10-06 18:47:09 +0000 | [diff] [blame] | 1039 | } |
| 1040 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
| 1041 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 1042 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1044 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Ted Kremenek | fda4fed | 2008-10-06 18:47:09 +0000 | [diff] [blame] | 1045 | } |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
Steve Naroff | 5e52b17 | 2008-10-04 18:52:47 +0000 | [diff] [blame] | 1048 | // Handle specific things. |
| 1049 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 1050 | if (BDRE->isByRef()) |
| 1051 | RewriteBlockDeclRefExpr(BDRE); |
| 1052 | } |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1053 | // Return this stmt unmodified. |
| 1054 | return S; |
| 1055 | } |
| 1056 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | void RewriteBlocks::RewriteFunctionProtoType(QualType funcType, NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1058 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1060 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 1061 | if (isBlockPointerType(*I)) { |
| 1062 | // All the args are checked/rewritten. Don't call twice! |
| 1063 | RewriteBlockPointerDecl(D); |
| 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | void RewriteBlocks::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1070 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1071 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1072 | RewriteFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1075 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 1076 | /// main file of the input. |
| 1077 | void RewriteBlocks::HandleDeclInMainFile(Decl *D) { |
| 1078 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1079 | // Since function prototypes don't have ParmDecl's, we check the function |
| 1080 | // prototype. This enables us to rewrite function declarations and |
| 1081 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1082 | RewriteFunctionProtoType(FD->getType(), FD); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1083 | |
| 1084 | // FIXME: Handle CXXTryStmt |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1085 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1086 | CurFunctionDef = FD; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 1087 | FD->setBody(cast_or_null<CompoundStmt>(RewriteFunctionBody(Body))); |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1088 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 1089 | // and any copy/dispose helper functions. |
| 1090 | InsertBlockLiteralsWithinFunction(FD); |
| 1091 | CurFunctionDef = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | } |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1093 | return; |
| 1094 | } |
| 1095 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 1096 | RewriteMethodDecl(MD); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1097 | if (Stmt *Body = MD->getBody()) { |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1098 | CurMethodDef = MD; |
| 1099 | RewriteFunctionBody(Body); |
| 1100 | InsertBlockLiteralsWithinMethod(MD); |
| 1101 | CurMethodDef = 0; |
| 1102 | } |
| 1103 | } |
| 1104 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1105 | if (isBlockPointerType(VD->getType())) { |
| 1106 | RewriteBlockPointerDecl(VD); |
| 1107 | if (VD->getInit()) { |
| 1108 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(VD->getInit())) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1109 | RewriteFunctionBody(CBE->getBody()); |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1110 | |
| 1111 | // We've just rewritten the block body in place. |
| 1112 | // Now we snarf the rewritten text and stash it away for later use. |
| 1113 | std::string S = Rewrite.getRewritenText(CBE->getSourceRange()); |
| 1114 | RewrittenBlockExprs[CBE] = S; |
| 1115 | std::string Init = SynthesizeBlockInitExpr(CBE, VD); |
| 1116 | // Do the rewrite, using S.size() which contains the rewritten size. |
| 1117 | ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1119 | VD->getNameAsCString()); |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1120 | } else if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) { |
| 1121 | RewriteCastExpr(CE); |
| 1122 | } |
| 1123 | } |
| 1124 | } else if (VD->getType()->isFunctionPointerType()) { |
| 1125 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 1126 | if (VD->getInit()) { |
| 1127 | if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) { |
| 1128 | RewriteCastExpr(CE); |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | return; |
| 1133 | } |
| 1134 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 1135 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 1136 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | ca74360 | 2008-10-15 18:38:58 +0000 | [diff] [blame] | 1138 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 1142 | if (RD->isDefinition()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1144 | e = RD->field_end(); i != e; ++i) { |
Steve Naroff | 70f9550 | 2008-10-04 17:06:23 +0000 | [diff] [blame] | 1145 | FieldDecl *FD = *i; |
| 1146 | if (isBlockPointerType(FD->getType())) |
| 1147 | RewriteBlockPointerDecl(FD); |
| 1148 | } |
| 1149 | } |
| 1150 | return; |
| 1151 | } |
Steve Naroff | 1c9f81b | 2008-09-17 00:13:27 +0000 | [diff] [blame] | 1152 | } |