blob: cab4b817811598caa60ed25695f86b0379bd4739 [file] [log] [blame]
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001//===--- 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
14#include "ASTConsumers.h"
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
27using namespace clang;
28using llvm::utostr;
29
30namespace {
31
32class RewriteBlocks : public ASTConsumer {
33 Rewriter Rewrite;
34 Diagnostic &Diags;
35 const LangOptions &LangOpts;
36 unsigned RewriteFailedDiag;
Steve Naroff1c9f81b2008-09-17 00:13:27 +000037
38 ASTContext *Context;
39 SourceManager *SM;
40 unsigned MainFileID;
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 Naroff4e13b762008-10-03 20:28:15 +000051 llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
Steve Naroff70f95502008-10-04 17:06:23 +000052
53 llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
Steve Naroff1c9f81b2008-09-17 00:13:27 +000054
55 // The function/method we are rewriting.
56 FunctionDecl *CurFunctionDef;
57 ObjCMethodDecl *CurMethodDef;
58
59 bool IsHeader;
Steve Naroff13188952008-09-18 14:10:13 +000060 std::string InFileName;
61 std::string OutFileName;
Steve Naroffa0b75cf2008-10-02 23:30:43 +000062
63 std::string Preamble;
Steve Naroff1c9f81b2008-09-17 00:13:27 +000064public:
Steve Naroff13188952008-09-18 14:10:13 +000065 RewriteBlocks(std::string inFile, std::string outFile, Diagnostic &D,
66 const LangOptions &LOpts);
Steve Naroff1c9f81b2008-09-17 00:13:27 +000067 ~RewriteBlocks() {
68 // Get the buffer corresponding to MainFileID.
69 // If we haven't changed it, then we are done.
70 if (const RewriteBuffer *RewriteBuf =
71 Rewrite.getRewriteBufferFor(MainFileID)) {
72 std::string S(RewriteBuf->begin(), RewriteBuf->end());
73 printf("%s\n", S.c_str());
74 } else {
75 printf("No changes\n");
76 }
77 }
78
79 void Initialize(ASTContext &context);
80
81 void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen);
82 void ReplaceText(SourceLocation Start, unsigned OrigLength,
83 const char *NewStr, unsigned NewLength);
84
85 // Top Level Driver code.
86 virtual void HandleTopLevelDecl(Decl *D);
87 void HandleDeclInMainFile(Decl *D);
88
89 // Top level
90 Stmt *RewriteFunctionBody(Stmt *S);
91 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
92 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
93
94 // Block specific rewrite rules.
Steve Naroff70f95502008-10-04 17:06:23 +000095 std::string SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD=0);
Steve Naroff1c9f81b2008-09-17 00:13:27 +000096
97 void RewriteBlockCall(CallExpr *Exp);
98 void RewriteBlockPointerDecl(NamedDecl *VD);
Steve Naroff5e52b172008-10-04 18:52:47 +000099 void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000100 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
101
Steve Naroff4e13b762008-10-03 20:28:15 +0000102 std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
103 const char *funcName, std::string Tag);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000104 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
105 const char *funcName, std::string Tag);
Steve Naroffacba0f22008-10-04 23:47:37 +0000106 std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
107 bool hasCopyDisposeHelpers);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000108 std::string SynthesizeBlockCall(CallExpr *Exp);
109 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
110 const char *FunName);
111
Steve Naroffd3f77902008-10-05 00:06:12 +0000112 void CollectBlockDeclRefInfo(BlockExpr *Exp);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000113 void GetBlockCallExprs(Stmt *S);
Steve Naroffacba0f22008-10-04 23:47:37 +0000114 void GetBlockDeclRefExprs(Stmt *S);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000115
116 // We avoid calling Type::isBlockPointerType(), since it operates on the
117 // canonical type. We only care if the top-level type is a closure pointer.
118 bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); }
119
120 // FIXME: This predicate seems like it would be useful to add to ASTContext.
121 bool isObjCType(QualType T) {
122 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
123 return false;
124
125 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
126
127 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
128 OCT == Context->getCanonicalType(Context->getObjCClassType()))
129 return true;
130
131 if (const PointerType *PT = OCT->getAsPointerType()) {
132 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
133 isa<ObjCQualifiedIdType>(PT->getPointeeType()))
134 return true;
135 }
136 return false;
137 }
138 // ObjC rewrite methods.
139 void RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl);
140 void RewriteCategoryDecl(ObjCCategoryDecl *CatDecl);
141 void RewriteProtocolDecl(ObjCProtocolDecl *PDecl);
142 void RewriteMethodDecl(ObjCMethodDecl *MDecl);
Steve Naroffca743602008-10-15 18:38:58 +0000143
144 void RewriteFunctionTypeProto(QualType funcType, NamedDecl *D);
145 void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
146 void RewriteCastExpr(CastExpr *CE);
Steve Naroffeab5f632008-09-23 19:24:41 +0000147
Steve Naroffca743602008-10-15 18:38:58 +0000148 bool PointerTypeTakesAnyBlockArguments(QualType QT);
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000149 void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000150};
151
152}
153
154static bool IsHeaderFile(const std::string &Filename) {
155 std::string::size_type DotPos = Filename.rfind('.');
156
157 if (DotPos == std::string::npos) {
158 // no file extension
159 return false;
160 }
161
162 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
163 // C header: .h
164 // C++ header: .hh or .H;
165 return Ext == "h" || Ext == "hh" || Ext == "H";
166}
167
Steve Naroff13188952008-09-18 14:10:13 +0000168RewriteBlocks::RewriteBlocks(std::string inFile, std::string outFile,
169 Diagnostic &D, const LangOptions &LOpts) :
170 Diags(D), LangOpts(LOpts) {
171 IsHeader = IsHeaderFile(inFile);
172 InFileName = inFile;
173 OutFileName = outFile;
174 CurFunctionDef = 0;
175 CurMethodDef = 0;
176 RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
177 "rewriting failed");
Steve Naroff13188952008-09-18 14:10:13 +0000178}
179
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000180ASTConsumer *clang::CreateBlockRewriter(const std::string& InFile,
Steve Naroff13188952008-09-18 14:10:13 +0000181 const std::string& OutFile,
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000182 Diagnostic &Diags,
183 const LangOptions &LangOpts) {
Steve Naroff13188952008-09-18 14:10:13 +0000184 return new RewriteBlocks(InFile, OutFile, Diags, LangOpts);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000185}
186
187void RewriteBlocks::Initialize(ASTContext &context) {
188 Context = &context;
189 SM = &Context->getSourceManager();
190
191 // Get the ID and start/end of the main file.
192 MainFileID = SM->getMainFileID();
193 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
194 MainFileStart = MainBuf->getBufferStart();
195 MainFileEnd = MainBuf->getBufferEnd();
196
197 Rewrite.setSourceMgr(Context->getSourceManager());
198
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000199 if (IsHeader)
200 Preamble = "#pragma once\n";
201 Preamble += "#ifndef BLOCK_IMPL\n";
202 Preamble += "#define BLOCK_IMPL\n";
203 Preamble += "struct __block_impl {\n";
204 Preamble += " void *isa;\n";
205 Preamble += " int Flags;\n";
206 Preamble += " int Size;\n";
207 Preamble += " void *FuncPtr;\n";
208 Preamble += "};\n";
209 Preamble += "enum {\n";
210 Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n";
211 Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n";
212 Preamble += "};\n";
213 if (LangOpts.Microsoft)
214 Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n";
215 else
216 Preamble += "#define __OBJC_RW_EXTERN extern\n";
217 Preamble += "// Runtime copy/destroy helper functions\n";
218 Preamble += "__OBJC_RW_EXTERN void _Block_copy_assign(void *, void *);\n";
219 Preamble += "__OBJC_RW_EXTERN void _Block_byref_assign_copy(void *, void *);\n";
220 Preamble += "__OBJC_RW_EXTERN void _Block_destroy(void *);\n";
221 Preamble += "__OBJC_RW_EXTERN void _Block_byref_release(void *);\n";
Steve Naroff48a8c612008-10-03 12:09:49 +0000222 Preamble += "__OBJC_RW_EXTERN void *_NSConcreteGlobalBlock;\n";
223 Preamble += "__OBJC_RW_EXTERN void *_NSConcreteStackBlock;\n";
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000224 Preamble += "#endif\n";
225
226 InsertText(SourceLocation::getFileLoc(MainFileID, 0),
227 Preamble.c_str(), Preamble.size());
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000228}
229
230void RewriteBlocks::InsertText(SourceLocation Loc, const char *StrData,
231 unsigned StrLen)
232{
233 if (!Rewrite.InsertText(Loc, StrData, StrLen))
234 return;
235 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
236}
237
238void RewriteBlocks::ReplaceText(SourceLocation Start, unsigned OrigLength,
239 const char *NewStr, unsigned NewLength) {
240 if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength))
241 return;
242 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
243}
244
245void 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 Naroff8af6a452008-10-02 17:12:56 +0000263 std::string Tag = "struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000264
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
308void RewriteBlocks::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
309 for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
310 E = ClassDecl->instmeth_end(); I != E; ++I)
311 RewriteMethodDecl(*I);
312 for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
313 E = ClassDecl->classmeth_end(); I != E; ++I)
314 RewriteMethodDecl(*I);
315}
316
317void RewriteBlocks::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
318 for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
319 E = CatDecl->instmeth_end(); I != E; ++I)
320 RewriteMethodDecl(*I);
321 for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
322 E = CatDecl->classmeth_end(); I != E; ++I)
323 RewriteMethodDecl(*I);
324}
325
326void RewriteBlocks::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
327 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
328 E = PDecl->instmeth_end(); I != E; ++I)
329 RewriteMethodDecl(*I);
330 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
331 E = PDecl->classmeth_end(); I != E; ++I)
332 RewriteMethodDecl(*I);
333}
334
335//===----------------------------------------------------------------------===//
336// Top Level Driver Code
337//===----------------------------------------------------------------------===//
338
339void RewriteBlocks::HandleTopLevelDecl(Decl *D) {
340 // Two cases: either the decl could be in the main file, or it could be in a
341 // #included file. If the former, rewrite it now. If the later, check to see
342 // if we rewrote the #include/#import.
343 SourceLocation Loc = D->getLocation();
344 Loc = SM->getLogicalLoc(Loc);
345
346 // If this is for a builtin, ignore it.
347 if (Loc.isInvalid()) return;
348
349 if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D))
350 RewriteInterfaceDecl(MD);
351 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D))
352 RewriteCategoryDecl(CD);
353 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
354 RewriteProtocolDecl(PD);
355
356 // If we have a decl in the main file, see if we should rewrite it.
357 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
358 HandleDeclInMainFile(D);
359 return;
360}
361
362std::string RewriteBlocks::SynthesizeBlockFunc(BlockExpr *CE, int i,
363 const char *funcName,
364 std::string Tag) {
365 const FunctionType *AFT = CE->getFunctionType();
366 QualType RT = AFT->getResultType();
Steve Naroff48a8c612008-10-03 12:09:49 +0000367 std::string StructRef = "struct " + Tag;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000368 std::string S = "static " + RT.getAsString() + " __" +
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000369 funcName + "_" + "block_func_" + utostr(i);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000370
Steve Naroff56ee6892008-10-08 17:01:13 +0000371 BlockDecl *BD = CE->getBlockDecl();
372
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000373 if (isa<FunctionTypeNoProto>(AFT)) {
374 S += "()";
Steve Naroff56ee6892008-10-08 17:01:13 +0000375 } else if (BD->param_empty()) {
Steve Naroff48a8c612008-10-03 12:09:49 +0000376 S += "(" + StructRef + " *__cself)";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000377 } else {
378 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
379 assert(FT && "SynthesizeBlockFunc: No function proto");
380 S += '(';
381 // first add the implicit argument.
Steve Naroff48a8c612008-10-03 12:09:49 +0000382 S += StructRef + " *__cself, ";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000383 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +0000384 for (BlockDecl::param_iterator AI = BD->param_begin(),
385 E = BD->param_end(); AI != E; ++AI) {
386 if (AI != BD->param_begin()) S += ", ";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000387 ParamStr = (*AI)->getName();
388 (*AI)->getType().getAsStringInternal(ParamStr);
389 S += ParamStr;
390 }
391 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +0000392 if (!BD->param_empty()) S += ", ";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000393 S += "...";
394 }
395 S += ')';
396 }
397 S += " {\n";
398
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000399 // Create local declarations to avoid rewriting all closure decl ref exprs.
400 // First, emit a declaration for all "by ref" decls.
401 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
402 E = BlockByRefDecls.end(); I != E; ++I) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000403 S += " ";
404 std::string Name = (*I)->getName();
405 Context->getPointerType((*I)->getType()).getAsStringInternal(Name);
406 S += Name + " = __cself->" + (*I)->getName() + "; // bound by ref\n";
407 }
408 // Next, emit a declaration for all "by copy" declarations.
409 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
410 E = BlockByCopyDecls.end(); I != E; ++I) {
411 S += " ";
412 std::string Name = (*I)->getName();
413 // Handle nested closure invocation. For example:
414 //
415 // void (^myImportedClosure)(void);
416 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
417 //
418 // void (^anotherClosure)(void);
419 // anotherClosure = ^(void) {
420 // myImportedClosure(); // import and invoke the closure
421 // };
422 //
423 if (isBlockPointerType((*I)->getType()))
Steve Naroff8af6a452008-10-02 17:12:56 +0000424 S += "struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000425 else
426 (*I)->getType().getAsStringInternal(Name);
427 S += Name + " = __cself->" + (*I)->getName() + "; // bound by copy\n";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000428 }
Steve Naroff70f95502008-10-04 17:06:23 +0000429 std::string RewrittenStr = RewrittenBlockExprs[CE];
430 const char *cstr = RewrittenStr.c_str();
431 while (*cstr++ != '{') ;
432 S += cstr;
433 S += "\n";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000434 return S;
435}
436
Steve Naroff4e13b762008-10-03 20:28:15 +0000437std::string RewriteBlocks::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
438 const char *funcName,
439 std::string Tag) {
440 std::string StructRef = "struct " + Tag;
441 std::string S = "static void __";
442
443 S += funcName;
444 S += "_block_copy_" + utostr(i);
445 S += "(" + StructRef;
446 S += "*dst, " + StructRef;
447 S += "*src) {";
448 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
449 E = ImportedBlockDecls.end(); I != E; ++I) {
450 S += "_Block_copy_assign(&dst->";
451 S += (*I)->getName();
452 S += ", src->";
453 S += (*I)->getName();
454 S += ");}";
455 }
456 S += "\nstatic void __";
457 S += funcName;
458 S += "_block_dispose_" + utostr(i);
459 S += "(" + StructRef;
460 S += "*src) {";
461 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
462 E = ImportedBlockDecls.end(); I != E; ++I) {
463 S += "_Block_destroy(src->";
464 S += (*I)->getName();
465 S += ");";
466 }
467 S += "}\n";
468 return S;
469}
470
Steve Naroffacba0f22008-10-04 23:47:37 +0000471std::string RewriteBlocks::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
472 bool hasCopyDisposeHelpers) {
Steve Naroff48a8c612008-10-03 12:09:49 +0000473 std::string S = "struct " + Tag;
474 std::string Constructor = " " + Tag;
475
476 S += " {\n struct __block_impl impl;\n";
Steve Naroffacba0f22008-10-04 23:47:37 +0000477
478 if (hasCopyDisposeHelpers)
479 S += " void *copy;\n void *dispose;\n";
480
Steve Naroff48a8c612008-10-03 12:09:49 +0000481 Constructor += "(void *fp";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000482
Steve Naroffacba0f22008-10-04 23:47:37 +0000483 if (hasCopyDisposeHelpers)
484 Constructor += ", void *copyHelp, void *disposeHelp";
485
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000486 if (BlockDeclRefs.size()) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000487 // Output all "by copy" declarations.
488 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
489 E = BlockByCopyDecls.end(); I != E; ++I) {
490 S += " ";
Steve Naroff4e13b762008-10-03 20:28:15 +0000491 std::string FieldName = (*I)->getName();
492 std::string ArgName = "_" + FieldName;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000493 // Handle nested closure invocation. For example:
494 //
495 // void (^myImportedBlock)(void);
496 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
497 //
498 // void (^anotherBlock)(void);
499 // anotherBlock = ^(void) {
500 // myImportedBlock(); // import and invoke the closure
501 // };
502 //
Steve Naroff4e13b762008-10-03 20:28:15 +0000503 if (isBlockPointerType((*I)->getType())) {
Steve Naroff8af6a452008-10-02 17:12:56 +0000504 S += "struct __block_impl *";
Steve Naroff4e13b762008-10-03 20:28:15 +0000505 Constructor += ", void *" + ArgName;
506 } else {
507 (*I)->getType().getAsStringInternal(FieldName);
Steve Naroff48a8c612008-10-03 12:09:49 +0000508 (*I)->getType().getAsStringInternal(ArgName);
Steve Naroff4e13b762008-10-03 20:28:15 +0000509 Constructor += ", " + ArgName;
Steve Naroff48a8c612008-10-03 12:09:49 +0000510 }
Steve Naroff4e13b762008-10-03 20:28:15 +0000511 S += FieldName + ";\n";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000512 }
513 // Output all "by ref" declarations.
514 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
515 E = BlockByRefDecls.end(); I != E; ++I) {
516 S += " ";
Steve Naroff4e13b762008-10-03 20:28:15 +0000517 std::string FieldName = (*I)->getName();
518 std::string ArgName = "_" + FieldName;
519 // Handle nested closure invocation. For example:
520 //
521 // void (^myImportedBlock)(void);
522 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
523 //
524 // void (^anotherBlock)(void);
525 // anotherBlock = ^(void) {
526 // myImportedBlock(); // import and invoke the closure
527 // };
528 //
529 if (isBlockPointerType((*I)->getType())) {
Steve Naroff8af6a452008-10-02 17:12:56 +0000530 S += "struct __block_impl *";
Steve Naroff4e13b762008-10-03 20:28:15 +0000531 Constructor += ", void *" + ArgName;
532 } else {
533 Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName);
Steve Naroff48a8c612008-10-03 12:09:49 +0000534 Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName);
Steve Naroff4e13b762008-10-03 20:28:15 +0000535 Constructor += ", " + ArgName;
Steve Naroff48a8c612008-10-03 12:09:49 +0000536 }
Steve Naroff4e13b762008-10-03 20:28:15 +0000537 S += FieldName + "; // by ref\n";
Steve Naroff48a8c612008-10-03 12:09:49 +0000538 }
539 // Finish writing the constructor.
540 // FIXME: handle NSConcreteGlobalBlock.
541 Constructor += ", int flags=0) {\n";
Steve Naroff83ba14e2008-10-03 15:04:50 +0000542 Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
Steve Naroff48a8c612008-10-03 12:09:49 +0000543 Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
544
Steve Naroffacba0f22008-10-04 23:47:37 +0000545 if (hasCopyDisposeHelpers)
546 Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
547
Steve Naroff48a8c612008-10-03 12:09:49 +0000548 // Initialize all "by copy" arguments.
549 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
550 E = BlockByCopyDecls.end(); I != E; ++I) {
551 std::string Name = (*I)->getName();
552 Constructor += " ";
Steve Naroff4e13b762008-10-03 20:28:15 +0000553 if (isBlockPointerType((*I)->getType()))
554 Constructor += Name + " = (struct __block_impl *)_";
555 else
556 Constructor += Name + " = _";
Steve Naroff48a8c612008-10-03 12:09:49 +0000557 Constructor += Name + ";\n";
558 }
559 // Initialize all "by ref" arguments.
560 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
561 E = BlockByRefDecls.end(); I != E; ++I) {
562 std::string Name = (*I)->getName();
563 Constructor += " ";
Steve Naroff4e13b762008-10-03 20:28:15 +0000564 if (isBlockPointerType((*I)->getType()))
565 Constructor += Name + " = (struct __block_impl *)_";
566 else
567 Constructor += Name + " = _";
Steve Naroff48a8c612008-10-03 12:09:49 +0000568 Constructor += Name + ";\n";
569 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000570 } else {
571 // Finish writing the constructor.
572 // FIXME: handle NSConcreteGlobalBlock.
573 Constructor += ", int flags=0) {\n";
574 Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
575 Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
Steve Naroffacba0f22008-10-04 23:47:37 +0000576 if (hasCopyDisposeHelpers)
577 Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000578 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000579 Constructor += " ";
580 Constructor += "}\n";
581 S += Constructor;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000582 S += "};\n";
583 return S;
584}
585
586void RewriteBlocks::SynthesizeBlockLiterals(SourceLocation FunLocStart,
587 const char *FunName) {
588 // Insert closures that were part of the function.
589 for (unsigned i = 0; i < Blocks.size(); i++) {
Steve Naroffacba0f22008-10-04 23:47:37 +0000590
Steve Naroffd3f77902008-10-05 00:06:12 +0000591 CollectBlockDeclRefInfo(Blocks[i]);
592
Steve Naroff48a8c612008-10-03 12:09:49 +0000593 std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000594
Steve Naroffacba0f22008-10-04 23:47:37 +0000595 std::string CI = SynthesizeBlockImpl(Blocks[i], Tag,
596 ImportedBlockDecls.size() > 0);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000597
598 InsertText(FunLocStart, CI.c_str(), CI.size());
Steve Naroff4e13b762008-10-03 20:28:15 +0000599
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000600 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag);
601
602 InsertText(FunLocStart, CF.c_str(), CF.size());
603
Steve Naroff4e13b762008-10-03 20:28:15 +0000604 if (ImportedBlockDecls.size()) {
605 std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag);
606 InsertText(FunLocStart, HF.c_str(), HF.size());
607 }
608
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000609 BlockDeclRefs.clear();
610 BlockByRefDecls.clear();
611 BlockByCopyDecls.clear();
612 BlockCallExprs.clear();
Steve Naroff4e13b762008-10-03 20:28:15 +0000613 ImportedBlockDecls.clear();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000614 }
615 Blocks.clear();
Steve Naroff8e9216d2008-10-04 17:10:02 +0000616 RewrittenBlockExprs.clear();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000617}
618
619void RewriteBlocks::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
Steve Naroff3ad29e22008-10-03 00:12:09 +0000620 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000621 const char *FuncName = FD->getName();
622
623 SynthesizeBlockLiterals(FunLocStart, FuncName);
624}
625
626void RewriteBlocks::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
627 SourceLocation FunLocStart = MD->getLocStart();
628 std::string FuncName = std::string(MD->getSelector().getName());
629 // Convert colons to underscores.
630 std::string::size_type loc = 0;
631 while ((loc = FuncName.find(":", loc)) != std::string::npos)
632 FuncName.replace(loc, 1, "_");
633
634 SynthesizeBlockLiterals(FunLocStart, FuncName.c_str());
635}
636
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000637void RewriteBlocks::GetBlockDeclRefExprs(Stmt *S) {
638 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
639 CI != E; ++CI)
Steve Naroff84a969f2008-10-08 17:31:13 +0000640 if (*CI) {
641 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
642 GetBlockDeclRefExprs(CBE->getBody());
643 else
644 GetBlockDeclRefExprs(*CI);
645 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000646 // Handle specific things.
647 if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S))
648 // FIXME: Handle enums.
649 if (!isa<FunctionDecl>(CDRE->getDecl()))
650 BlockDeclRefs.push_back(CDRE);
651 return;
652}
653
654void RewriteBlocks::GetBlockCallExprs(Stmt *S) {
655 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
656 CI != E; ++CI)
Steve Naroff84a969f2008-10-08 17:31:13 +0000657 if (*CI) {
658 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
659 GetBlockCallExprs(CBE->getBody());
660 else
661 GetBlockCallExprs(*CI);
662 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000663
664 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
Steve Naroff4e13b762008-10-03 20:28:15 +0000665 if (CE->getCallee()->getType()->isBlockPointerType()) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000666 BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE;
Steve Naroff4e13b762008-10-03 20:28:15 +0000667 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000668 }
669 return;
670}
671
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000672std::string RewriteBlocks::SynthesizeBlockCall(CallExpr *Exp) {
673 // Navigate to relevant type information.
Steve Naroffcc2ece22008-09-24 22:46:45 +0000674 const char *closureName = 0;
675 const BlockPointerType *CPT = 0;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000676
677 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) {
678 closureName = DRE->getDecl()->getName();
679 CPT = DRE->getType()->getAsBlockPointerType();
680 } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) {
681 closureName = CDRE->getDecl()->getName();
682 CPT = CDRE->getType()->getAsBlockPointerType();
Steve Naroff83ba14e2008-10-03 15:04:50 +0000683 } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) {
684 closureName = MExpr->getMemberDecl()->getName();
685 CPT = MExpr->getType()->getAsBlockPointerType();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000686 } else {
687 assert(1 && "RewriteBlockClass: Bad type");
688 }
689 assert(CPT && "RewriteBlockClass: Bad type");
690 const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType();
691 assert(FT && "RewriteBlockClass: Bad type");
692 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FT);
693 // FTP will be null for closures that don't take arguments.
694
695 // Build a closure call - start with a paren expr to enforce precedence.
696 std::string BlockCall = "(";
697
698 // Synthesize the cast.
699 BlockCall += "(" + Exp->getType().getAsString() + "(*)";
Steve Naroff8af6a452008-10-02 17:12:56 +0000700 BlockCall += "(struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000701 if (FTP) {
702 for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(),
703 E = FTP->arg_type_end(); I && (I != E); ++I)
704 BlockCall += ", " + (*I).getAsString();
705 }
706 BlockCall += "))"; // close the argument list and paren expression.
707
Steve Naroff83ba14e2008-10-03 15:04:50 +0000708 // Invoke the closure. We need to cast it since the declaration type is
709 // bogus (it's a function pointer type)
710 BlockCall += "((struct __block_impl *)";
711 std::string closureExprBufStr;
712 llvm::raw_string_ostream closureExprBuf(closureExprBufStr);
713 Exp->getCallee()->printPretty(closureExprBuf);
714 BlockCall += closureExprBuf.str();
715 BlockCall += ")->FuncPtr)";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000716
717 // Add the arguments.
Steve Naroff83ba14e2008-10-03 15:04:50 +0000718 BlockCall += "((struct __block_impl *)";
Steve Naroffb65a4f12008-10-04 17:45:51 +0000719 BlockCall += closureExprBuf.str();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000720 for (CallExpr::arg_iterator I = Exp->arg_begin(),
721 E = Exp->arg_end(); I != E; ++I) {
722 std::string syncExprBufS;
723 llvm::raw_string_ostream Buf(syncExprBufS);
724 (*I)->printPretty(Buf);
725 BlockCall += ", " + Buf.str();
726 }
727 return BlockCall;
728}
729
730void RewriteBlocks::RewriteBlockCall(CallExpr *Exp) {
731 std::string BlockCall = SynthesizeBlockCall(Exp);
732
733 const char *startBuf = SM->getCharacterData(Exp->getLocStart());
734 const char *endBuf = SM->getCharacterData(Exp->getLocEnd());
735
736 ReplaceText(Exp->getLocStart(), endBuf-startBuf,
737 BlockCall.c_str(), BlockCall.size());
738}
739
Steve Naroff5e52b172008-10-04 18:52:47 +0000740void RewriteBlocks::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) {
741 // FIXME: Add more elaborate code generation required by the ABI.
742 InsertText(BDRE->getLocStart(), "*", 1);
743}
744
Steve Naroffca743602008-10-15 18:38:58 +0000745void RewriteBlocks::RewriteCastExpr(CastExpr *CE) {
746 SourceLocation LocStart = CE->getLocStart();
747 SourceLocation LocEnd = CE->getLocEnd();
748
749 const char *startBuf = SM->getCharacterData(LocStart);
750 const char *endBuf = SM->getCharacterData(LocEnd);
751
752 // advance the location to startArgList.
753 const char *argPtr = startBuf;
754
755 while (*argPtr++ && (argPtr < endBuf)) {
756 switch (*argPtr) {
757 case '^':
758 // Replace the '^' with '*'.
759 LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf);
760 ReplaceText(LocStart, 1, "*", 1);
761 break;
762 }
763 }
764 return;
765}
766
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000767void RewriteBlocks::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
768 SourceLocation DeclLoc = FD->getLocation();
Steve Naroffe0109a52008-10-10 15:33:34 +0000769 unsigned parenCount = 0;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000770
771 // We have 1 or more arguments that have closure pointers.
772 const char *startBuf = SM->getCharacterData(DeclLoc);
773 const char *startArgList = strchr(startBuf, '(');
774
775 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
776
777 parenCount++;
778 // advance the location to startArgList.
Steve Naroffe0109a52008-10-10 15:33:34 +0000779 DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000780 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
781
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000782 const char *argPtr = startArgList;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000783
784 while (*argPtr++ && parenCount) {
785 switch (*argPtr) {
786 case '^':
Steve Naroffe0109a52008-10-10 15:33:34 +0000787 // Replace the '^' with '*'.
788 DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList);
789 ReplaceText(DeclLoc, 1, "*", 1);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000790 break;
791 case '(':
792 parenCount++;
793 break;
794 case ')':
795 parenCount--;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000796 break;
797 }
798 }
799 return;
800}
801
Steve Naroffca743602008-10-15 18:38:58 +0000802bool RewriteBlocks::PointerTypeTakesAnyBlockArguments(QualType QT) {
803 const FunctionTypeProto *FTP;
804 const PointerType *PT = QT->getAsPointerType();
805 if (PT) {
806 FTP = PT->getPointeeType()->getAsFunctionTypeProto();
Steve Naroffca743602008-10-15 18:38:58 +0000807 } else {
808 const BlockPointerType *BPT = QT->getAsBlockPointerType();
809 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
810 FTP = BPT->getPointeeType()->getAsFunctionTypeProto();
811 }
Steve Naroffeab5f632008-09-23 19:24:41 +0000812 if (FTP) {
813 for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(),
814 E = FTP->arg_type_end(); I != E; ++I)
815 if (isBlockPointerType(*I))
816 return true;
817 }
818 return false;
819}
820
821void RewriteBlocks::GetExtentOfArgList(const char *Name,
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000822 const char *&LParen, const char *&RParen) {
823 const char *argPtr = strchr(Name, '(');
Steve Naroffeab5f632008-09-23 19:24:41 +0000824 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
825
826 LParen = argPtr; // output the start.
827 argPtr++; // skip past the left paren.
828 unsigned parenCount = 1;
829
830 while (*argPtr && parenCount) {
831 switch (*argPtr) {
832 case '(': parenCount++; break;
833 case ')': parenCount--; break;
834 default: break;
835 }
836 if (parenCount) argPtr++;
837 }
838 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
839 RParen = argPtr; // output the end
840}
841
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000842void RewriteBlocks::RewriteBlockPointerDecl(NamedDecl *ND) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000843 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
844 RewriteBlockPointerFunctionArgs(FD);
845 return;
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000846 }
847 // Handle Variables and Typedefs.
848 SourceLocation DeclLoc = ND->getLocation();
849 QualType DeclT;
850 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
851 DeclT = VD->getType();
852 else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND))
853 DeclT = TDD->getUnderlyingType();
Steve Naroff83ba14e2008-10-03 15:04:50 +0000854 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
855 DeclT = FD->getType();
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000856 else
857 assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled");
Steve Naroffeab5f632008-09-23 19:24:41 +0000858
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000859 const char *startBuf = SM->getCharacterData(DeclLoc);
860 const char *endBuf = startBuf;
861 // scan backward (from the decl location) for the end of the previous decl.
862 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
863 startBuf--;
Steve Naroffca743602008-10-15 18:38:58 +0000864
865 // *startBuf != '^' if we are dealing with a pointer to function that
866 // may take block argument types (which will be handled below).
867 if (*startBuf == '^') {
868 // Replace the '^' with '*', computing a negative offset.
869 DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf);
870 ReplaceText(DeclLoc, 1, "*", 1);
871 }
872 if (PointerTypeTakesAnyBlockArguments(DeclT)) {
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000873 // Replace the '^' with '*' for arguments.
874 DeclLoc = ND->getLocation();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000875 startBuf = SM->getCharacterData(DeclLoc);
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000876 const char *argListBegin, *argListEnd;
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000877 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
878 while (argListBegin < argListEnd) {
879 if (*argListBegin == '^') {
880 SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf);
881 ReplaceText(CaretLoc, 1, "*", 1);
882 }
883 argListBegin++;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000884 }
Steve Naroffeab5f632008-09-23 19:24:41 +0000885 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000886 return;
887}
888
Steve Naroffd3f77902008-10-05 00:06:12 +0000889void RewriteBlocks::CollectBlockDeclRefInfo(BlockExpr *Exp) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000890 // Add initializers for any closure decl refs.
Steve Naroff84a969f2008-10-08 17:31:13 +0000891 GetBlockDeclRefExprs(Exp->getBody());
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000892 if (BlockDeclRefs.size()) {
893 // Unique all "by copy" declarations.
894 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
895 if (!BlockDeclRefs[i]->isByRef())
896 BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
897 // Unique all "by ref" declarations.
898 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
899 if (BlockDeclRefs[i]->isByRef()) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000900 BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
901 }
Steve Naroffacba0f22008-10-04 23:47:37 +0000902 // Find any imported blocks...they will need special attention.
903 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
904 if (isBlockPointerType(BlockDeclRefs[i]->getType())) {
905 GetBlockCallExprs(Blocks[i]);
906 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
907 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000908 }
Steve Naroffd3f77902008-10-05 00:06:12 +0000909}
910
911std::string RewriteBlocks::SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD) {
912 Blocks.push_back(Exp);
913
914 CollectBlockDeclRefInfo(Exp);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000915 std::string FuncName;
916
917 if (CurFunctionDef)
918 FuncName = std::string(CurFunctionDef->getName());
919 else if (CurMethodDef) {
920 FuncName = std::string(CurMethodDef->getSelector().getName());
921 // Convert colons to underscores.
922 std::string::size_type loc = 0;
923 while ((loc = FuncName.find(":", loc)) != std::string::npos)
924 FuncName.replace(loc, 1, "_");
Steve Naroff39622b92008-10-03 15:38:09 +0000925 } else if (VD)
926 FuncName = std::string(VD->getName());
927
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000928 std::string BlockNumber = utostr(Blocks.size()-1);
929
Steve Naroff83ba14e2008-10-03 15:04:50 +0000930 std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000931 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000932
Steve Naroff83ba14e2008-10-03 15:04:50 +0000933 std::string FunkTypeStr;
934
935 // Get a pointer to the function type so we can cast appropriately.
936 Context->getPointerType(QualType(Exp->getFunctionType(),0)).getAsStringInternal(FunkTypeStr);
937
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000938 // Rewrite the closure block with a compound literal. The first cast is
939 // to prevent warnings from the C compiler.
Steve Naroff83ba14e2008-10-03 15:04:50 +0000940 std::string Init = "(" + FunkTypeStr;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000941
Steve Naroff83ba14e2008-10-03 15:04:50 +0000942 Init += ")&" + Tag;
943
944 // Initialize the block function.
945 Init += "((void*)" + Func;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000946
Steve Naroffacba0f22008-10-04 23:47:37 +0000947 if (ImportedBlockDecls.size()) {
948 std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber;
949 Init += ",(void*)" + Buf;
950 Buf = "__" + FuncName + "_block_dispose_" + BlockNumber;
951 Init += ",(void*)" + Buf;
952 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000953 // Add initializers for any closure decl refs.
954 if (BlockDeclRefs.size()) {
955 // Output all "by copy" declarations.
956 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
957 E = BlockByCopyDecls.end(); I != E; ++I) {
958 Init += ",";
959 if (isObjCType((*I)->getType())) {
960 Init += "[[";
961 Init += (*I)->getName();
962 Init += " retain] autorelease]";
Steve Naroff4e13b762008-10-03 20:28:15 +0000963 } else if (isBlockPointerType((*I)->getType())) {
964 Init += "(void *)";
965 Init += (*I)->getName();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000966 } else {
967 Init += (*I)->getName();
968 }
969 }
970 // Output all "by ref" declarations.
971 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
972 E = BlockByRefDecls.end(); I != E; ++I) {
973 Init += ",&";
974 Init += (*I)->getName();
975 }
976 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000977 Init += ")";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000978 BlockDeclRefs.clear();
979 BlockByRefDecls.clear();
980 BlockByCopyDecls.clear();
Steve Naroff4e13b762008-10-03 20:28:15 +0000981 ImportedBlockDecls.clear();
982
Steve Naroff70f95502008-10-04 17:06:23 +0000983 return Init;
984}
985
986//===----------------------------------------------------------------------===//
987// Function Body / Expression rewriting
988//===----------------------------------------------------------------------===//
989
990Stmt *RewriteBlocks::RewriteFunctionBody(Stmt *S) {
991 // Start by rewriting all children.
992 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
993 CI != E; ++CI)
994 if (*CI) {
995 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
Steve Naroff84a969f2008-10-08 17:31:13 +0000996 Stmt *newStmt = RewriteFunctionBody(CBE->getBody());
Steve Naroff70f95502008-10-04 17:06:23 +0000997 if (newStmt)
998 *CI = newStmt;
999
1000 // We've just rewritten the block body in place.
1001 // Now we snarf the rewritten text and stash it away for later use.
1002 std::string S = Rewrite.getRewritenText(CBE->getSourceRange());
1003 RewrittenBlockExprs[CBE] = S;
1004 std::string Init = SynthesizeBlockInitExpr(CBE);
1005 // Do the rewrite, using S.size() which contains the rewritten size.
1006 ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size());
1007 } else {
1008 Stmt *newStmt = RewriteFunctionBody(*CI);
1009 if (newStmt)
1010 *CI = newStmt;
1011 }
1012 }
1013 // Handle specific things.
1014 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1015 if (CE->getCallee()->getType()->isBlockPointerType())
1016 RewriteBlockCall(CE);
1017 }
Steve Naroffca743602008-10-15 18:38:58 +00001018 if (CastExpr *CE = dyn_cast<CastExpr>(S)) {
1019 RewriteCastExpr(CE);
1020 }
Steve Naroff70f95502008-10-04 17:06:23 +00001021 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekfda4fed2008-10-06 18:47:09 +00001022 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
1023 DI != DE; ++DI) {
1024
1025 ScopedDecl *SD = *DI;
1026 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
1027 if (isBlockPointerType(ND->getType()))
1028 RewriteBlockPointerDecl(ND);
Steve Naroffca743602008-10-15 18:38:58 +00001029 else if (ND->getType()->isFunctionPointerType())
1030 CheckFunctionPointerDecl(ND->getType(), ND);
Ted Kremenekfda4fed2008-10-06 18:47:09 +00001031 }
1032 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
1033 if (isBlockPointerType(TD->getUnderlyingType()))
1034 RewriteBlockPointerDecl(TD);
Steve Naroffca743602008-10-15 18:38:58 +00001035 else if (TD->getUnderlyingType()->isFunctionPointerType())
1036 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
Ted Kremenekfda4fed2008-10-06 18:47:09 +00001037 }
Steve Naroff70f95502008-10-04 17:06:23 +00001038 }
1039 }
Steve Naroff5e52b172008-10-04 18:52:47 +00001040 // Handle specific things.
1041 if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
1042 if (BDRE->isByRef())
1043 RewriteBlockDeclRefExpr(BDRE);
1044 }
Steve Naroff70f95502008-10-04 17:06:23 +00001045 // Return this stmt unmodified.
1046 return S;
1047}
1048
Steve Naroffca743602008-10-15 18:38:58 +00001049void RewriteBlocks::RewriteFunctionTypeProto(QualType funcType, NamedDecl *D) {
1050 if (FunctionTypeProto *fproto = dyn_cast<FunctionTypeProto>(funcType)) {
1051 for (FunctionTypeProto::arg_type_iterator I = fproto->arg_type_begin(),
1052 E = fproto->arg_type_end(); I && (I != E); ++I)
1053 if (isBlockPointerType(*I)) {
1054 // All the args are checked/rewritten. Don't call twice!
1055 RewriteBlockPointerDecl(D);
1056 break;
1057 }
1058 }
1059}
1060
1061void RewriteBlocks::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
1062 const PointerType *PT = funcType->getAsPointerType();
1063 if (PT && PointerTypeTakesAnyBlockArguments(funcType))
1064 RewriteFunctionTypeProto(PT->getPointeeType(), ND);
1065}
1066
Steve Naroff70f95502008-10-04 17:06:23 +00001067/// HandleDeclInMainFile - This is called for each top-level decl defined in the
1068/// main file of the input.
1069void RewriteBlocks::HandleDeclInMainFile(Decl *D) {
1070 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroff70f95502008-10-04 17:06:23 +00001071 // Since function prototypes don't have ParmDecl's, we check the function
1072 // prototype. This enables us to rewrite function declarations and
1073 // definitions using the same code.
Steve Naroffca743602008-10-15 18:38:58 +00001074 RewriteFunctionTypeProto(FD->getType(), FD);
Steve Naroff70f95502008-10-04 17:06:23 +00001075
Steve Naroff70f95502008-10-04 17:06:23 +00001076 if (Stmt *Body = FD->getBody()) {
1077 CurFunctionDef = FD;
1078 FD->setBody(RewriteFunctionBody(Body));
1079 // This synthesizes and inserts the block "impl" struct, invoke function,
1080 // and any copy/dispose helper functions.
1081 InsertBlockLiteralsWithinFunction(FD);
1082 CurFunctionDef = 0;
1083 }
1084 return;
1085 }
1086 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
1087 RewriteMethodDecl(MD);
1088 if (Stmt *Body = MD->getBody()) {
1089 CurMethodDef = MD;
1090 RewriteFunctionBody(Body);
1091 InsertBlockLiteralsWithinMethod(MD);
1092 CurMethodDef = 0;
1093 }
1094 }
1095 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1096 if (isBlockPointerType(VD->getType())) {
1097 RewriteBlockPointerDecl(VD);
1098 if (VD->getInit()) {
1099 if (BlockExpr *CBE = dyn_cast<BlockExpr>(VD->getInit())) {
Steve Naroff84a969f2008-10-08 17:31:13 +00001100 RewriteFunctionBody(CBE->getBody());
Steve Naroff70f95502008-10-04 17:06:23 +00001101
1102 // We've just rewritten the block body in place.
1103 // Now we snarf the rewritten text and stash it away for later use.
1104 std::string S = Rewrite.getRewritenText(CBE->getSourceRange());
1105 RewrittenBlockExprs[CBE] = S;
1106 std::string Init = SynthesizeBlockInitExpr(CBE, VD);
1107 // Do the rewrite, using S.size() which contains the rewritten size.
1108 ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size());
1109 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName());
Steve Naroffca743602008-10-15 18:38:58 +00001110 } else if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) {
1111 RewriteCastExpr(CE);
1112 }
1113 }
1114 } else if (VD->getType()->isFunctionPointerType()) {
1115 CheckFunctionPointerDecl(VD->getType(), VD);
1116 if (VD->getInit()) {
1117 if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) {
1118 RewriteCastExpr(CE);
Steve Naroff70f95502008-10-04 17:06:23 +00001119 }
1120 }
1121 }
1122 return;
1123 }
1124 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1125 if (isBlockPointerType(TD->getUnderlyingType()))
1126 RewriteBlockPointerDecl(TD);
Steve Naroffca743602008-10-15 18:38:58 +00001127 else if (TD->getUnderlyingType()->isFunctionPointerType())
1128 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
Steve Naroff70f95502008-10-04 17:06:23 +00001129 return;
1130 }
1131 if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1132 if (RD->isDefinition()) {
1133 for (RecordDecl::field_const_iterator i = RD->field_begin(),
1134 e = RD->field_end(); i != e; ++i) {
1135 FieldDecl *FD = *i;
1136 if (isBlockPointerType(FD->getType()))
1137 RewriteBlockPointerDecl(FD);
1138 }
1139 }
1140 return;
1141 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001142}