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