blob: ea03650b972f3b42bea6a07aaacd5c0bc1de9eb3 [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;
37 unsigned NoNestedBlockCalls;
38
39 ASTContext *Context;
40 SourceManager *SM;
41 unsigned MainFileID;
42 const char *MainFileStart, *MainFileEnd;
43
44 // Block expressions.
45 llvm::SmallVector<BlockExpr *, 32> Blocks;
46 llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs;
47 llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs;
48
49 // Block related declarations.
50 llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls;
51 llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls;
52
53 // The function/method we are rewriting.
54 FunctionDecl *CurFunctionDef;
55 ObjCMethodDecl *CurMethodDef;
56
57 bool IsHeader;
Steve Naroff13188952008-09-18 14:10:13 +000058 std::string InFileName;
59 std::string OutFileName;
Steve Naroffa0b75cf2008-10-02 23:30:43 +000060
61 std::string Preamble;
Steve Naroff1c9f81b2008-09-17 00:13:27 +000062public:
Steve Naroff13188952008-09-18 14:10:13 +000063 RewriteBlocks(std::string inFile, std::string outFile, Diagnostic &D,
64 const LangOptions &LOpts);
Steve Naroff1c9f81b2008-09-17 00:13:27 +000065 ~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.
84 virtual void HandleTopLevelDecl(Decl *D);
85 void HandleDeclInMainFile(Decl *D);
86
87 // Top level
88 Stmt *RewriteFunctionBody(Stmt *S);
89 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
90 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
91
92 // Block specific rewrite rules.
Steve Naroff9c3c9022008-09-17 18:37:59 +000093 void RewriteBlockExpr(BlockExpr *Exp);
Steve Naroff1c9f81b2008-09-17 00:13:27 +000094
95 void RewriteBlockCall(CallExpr *Exp);
96 void RewriteBlockPointerDecl(NamedDecl *VD);
97 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
98
99 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
100 const char *funcName, std::string Tag);
101 std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag);
102 std::string SynthesizeBlockCall(CallExpr *Exp);
103 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
104 const char *FunName);
105
106 void GetBlockDeclRefExprs(Stmt *S);
107 void GetBlockCallExprs(Stmt *S);
108
109 // We avoid calling Type::isBlockPointerType(), since it operates on the
110 // canonical type. We only care if the top-level type is a closure pointer.
111 bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); }
112
113 // FIXME: This predicate seems like it would be useful to add to ASTContext.
114 bool isObjCType(QualType T) {
115 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
116 return false;
117
118 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
119
120 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
121 OCT == Context->getCanonicalType(Context->getObjCClassType()))
122 return true;
123
124 if (const PointerType *PT = OCT->getAsPointerType()) {
125 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
126 isa<ObjCQualifiedIdType>(PT->getPointeeType()))
127 return true;
128 }
129 return false;
130 }
131 // ObjC rewrite methods.
132 void RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl);
133 void RewriteCategoryDecl(ObjCCategoryDecl *CatDecl);
134 void RewriteProtocolDecl(ObjCProtocolDecl *PDecl);
135 void RewriteMethodDecl(ObjCMethodDecl *MDecl);
Steve Naroffeab5f632008-09-23 19:24:41 +0000136
137 bool BlockPointerTypeTakesAnyBlockArguments(QualType QT);
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000138 void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000139};
140
141}
142
143static bool IsHeaderFile(const std::string &Filename) {
144 std::string::size_type DotPos = Filename.rfind('.');
145
146 if (DotPos == std::string::npos) {
147 // no file extension
148 return false;
149 }
150
151 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
152 // C header: .h
153 // C++ header: .hh or .H;
154 return Ext == "h" || Ext == "hh" || Ext == "H";
155}
156
Steve Naroff13188952008-09-18 14:10:13 +0000157RewriteBlocks::RewriteBlocks(std::string inFile, std::string outFile,
158 Diagnostic &D, const LangOptions &LOpts) :
159 Diags(D), LangOpts(LOpts) {
160 IsHeader = IsHeaderFile(inFile);
161 InFileName = inFile;
162 OutFileName = outFile;
163 CurFunctionDef = 0;
164 CurMethodDef = 0;
165 RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
166 "rewriting failed");
167 NoNestedBlockCalls = Diags.getCustomDiagID(Diagnostic::Warning,
168 "Rewrite support for closure calls nested within closure blocks is incomplete");
169}
170
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000171ASTConsumer *clang::CreateBlockRewriter(const std::string& InFile,
Steve Naroff13188952008-09-18 14:10:13 +0000172 const std::string& OutFile,
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000173 Diagnostic &Diags,
174 const LangOptions &LangOpts) {
Steve Naroff13188952008-09-18 14:10:13 +0000175 return new RewriteBlocks(InFile, OutFile, Diags, LangOpts);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000176}
177
178void RewriteBlocks::Initialize(ASTContext &context) {
179 Context = &context;
180 SM = &Context->getSourceManager();
181
182 // Get the ID and start/end of the main file.
183 MainFileID = SM->getMainFileID();
184 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
185 MainFileStart = MainBuf->getBufferStart();
186 MainFileEnd = MainBuf->getBufferEnd();
187
188 Rewrite.setSourceMgr(Context->getSourceManager());
189
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000190 if (IsHeader)
191 Preamble = "#pragma once\n";
192 Preamble += "#ifndef BLOCK_IMPL\n";
193 Preamble += "#define BLOCK_IMPL\n";
194 Preamble += "struct __block_impl {\n";
195 Preamble += " void *isa;\n";
196 Preamble += " int Flags;\n";
197 Preamble += " int Size;\n";
198 Preamble += " void *FuncPtr;\n";
199 Preamble += "};\n";
200 Preamble += "enum {\n";
201 Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n";
202 Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n";
203 Preamble += "};\n";
204 if (LangOpts.Microsoft)
205 Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n";
206 else
207 Preamble += "#define __OBJC_RW_EXTERN extern\n";
208 Preamble += "// Runtime copy/destroy helper functions\n";
209 Preamble += "__OBJC_RW_EXTERN void _Block_copy_assign(void *, void *);\n";
210 Preamble += "__OBJC_RW_EXTERN void _Block_byref_assign_copy(void *, void *);\n";
211 Preamble += "__OBJC_RW_EXTERN void _Block_destroy(void *);\n";
212 Preamble += "__OBJC_RW_EXTERN void _Block_byref_release(void *);\n";
Steve Naroff48a8c612008-10-03 12:09:49 +0000213 Preamble += "__OBJC_RW_EXTERN void *_NSConcreteGlobalBlock;\n";
214 Preamble += "__OBJC_RW_EXTERN void *_NSConcreteStackBlock;\n";
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000215 Preamble += "#endif\n";
216
217 InsertText(SourceLocation::getFileLoc(MainFileID, 0),
218 Preamble.c_str(), Preamble.size());
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000219}
220
221void RewriteBlocks::InsertText(SourceLocation Loc, const char *StrData,
222 unsigned StrLen)
223{
224 if (!Rewrite.InsertText(Loc, StrData, StrLen))
225 return;
226 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
227}
228
229void RewriteBlocks::ReplaceText(SourceLocation Start, unsigned OrigLength,
230 const char *NewStr, unsigned NewLength) {
231 if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength))
232 return;
233 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
234}
235
236void RewriteBlocks::RewriteMethodDecl(ObjCMethodDecl *Method) {
237 bool haveBlockPtrs = false;
238 for (ObjCMethodDecl::param_iterator I = Method->param_begin(),
239 E = Method->param_end(); I != E; ++I)
240 if (isBlockPointerType((*I)->getType()))
241 haveBlockPtrs = true;
242
243 if (!haveBlockPtrs)
244 return;
245
246 // Do a fuzzy rewrite.
247 // We have 1 or more arguments that have closure pointers.
248 SourceLocation Loc = Method->getLocStart();
249 SourceLocation LocEnd = Method->getLocEnd();
250 const char *startBuf = SM->getCharacterData(Loc);
251 const char *endBuf = SM->getCharacterData(LocEnd);
252
253 const char *methodPtr = startBuf;
Steve Naroff8af6a452008-10-02 17:12:56 +0000254 std::string Tag = "struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000255
256 while (*methodPtr++ && (methodPtr != endBuf)) {
257 switch (*methodPtr) {
258 case ':':
259 methodPtr++;
260 if (*methodPtr == '(') {
261 const char *scanType = ++methodPtr;
262 bool foundBlockPointer = false;
263 unsigned parenCount = 1;
264
265 while (parenCount) {
266 switch (*scanType) {
267 case '(':
268 parenCount++;
269 break;
270 case ')':
271 parenCount--;
272 break;
273 case '^':
274 foundBlockPointer = true;
275 break;
276 }
277 scanType++;
278 }
279 if (foundBlockPointer) {
280 // advance the location to startArgList.
281 Loc = Loc.getFileLocWithOffset(methodPtr-startBuf);
282 assert((Loc.isValid()) && "Invalid Loc");
283 ReplaceText(Loc, scanType-methodPtr-1, Tag.c_str(), Tag.size());
284
285 // Advance startBuf. Since the underlying buffer has changed,
286 // it's very important to advance startBuf (so we can correctly
287 // compute a relative Loc the next time around).
288 startBuf = methodPtr;
289 }
290 // Advance the method ptr to the end of the type.
291 methodPtr = scanType;
292 }
293 break;
294 }
295 }
296 return;
297}
298
299void RewriteBlocks::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
300 for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
301 E = ClassDecl->instmeth_end(); I != E; ++I)
302 RewriteMethodDecl(*I);
303 for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
304 E = ClassDecl->classmeth_end(); I != E; ++I)
305 RewriteMethodDecl(*I);
306}
307
308void RewriteBlocks::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
309 for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
310 E = CatDecl->instmeth_end(); I != E; ++I)
311 RewriteMethodDecl(*I);
312 for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
313 E = CatDecl->classmeth_end(); I != E; ++I)
314 RewriteMethodDecl(*I);
315}
316
317void RewriteBlocks::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
318 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
319 E = PDecl->instmeth_end(); I != E; ++I)
320 RewriteMethodDecl(*I);
321 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
322 E = PDecl->classmeth_end(); I != E; ++I)
323 RewriteMethodDecl(*I);
324}
325
326//===----------------------------------------------------------------------===//
327// Top Level Driver Code
328//===----------------------------------------------------------------------===//
329
330void RewriteBlocks::HandleTopLevelDecl(Decl *D) {
331 // Two cases: either the decl could be in the main file, or it could be in a
332 // #included file. If the former, rewrite it now. If the later, check to see
333 // if we rewrote the #include/#import.
334 SourceLocation Loc = D->getLocation();
335 Loc = SM->getLogicalLoc(Loc);
336
337 // If this is for a builtin, ignore it.
338 if (Loc.isInvalid()) return;
339
340 if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D))
341 RewriteInterfaceDecl(MD);
342 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D))
343 RewriteCategoryDecl(CD);
344 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
345 RewriteProtocolDecl(PD);
346
347 // If we have a decl in the main file, see if we should rewrite it.
348 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
349 HandleDeclInMainFile(D);
350 return;
351}
352
353std::string RewriteBlocks::SynthesizeBlockFunc(BlockExpr *CE, int i,
354 const char *funcName,
355 std::string Tag) {
356 const FunctionType *AFT = CE->getFunctionType();
357 QualType RT = AFT->getResultType();
Steve Naroff48a8c612008-10-03 12:09:49 +0000358 std::string StructRef = "struct " + Tag;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000359 std::string S = "static " + RT.getAsString() + " __" +
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000360 funcName + "_" + "block_func_" + utostr(i);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000361
362 if (isa<FunctionTypeNoProto>(AFT)) {
363 S += "()";
364 } else if (CE->arg_empty()) {
Steve Naroff48a8c612008-10-03 12:09:49 +0000365 S += "(" + StructRef + " *__cself)";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000366 } else {
367 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
368 assert(FT && "SynthesizeBlockFunc: No function proto");
369 S += '(';
370 // first add the implicit argument.
Steve Naroff48a8c612008-10-03 12:09:49 +0000371 S += StructRef + " *__cself, ";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000372 std::string ParamStr;
Steve Naroff9c3c9022008-09-17 18:37:59 +0000373 for (BlockExpr::arg_iterator AI = CE->arg_begin(),
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000374 E = CE->arg_end(); AI != E; ++AI) {
375 if (AI != CE->arg_begin()) S += ", ";
376 ParamStr = (*AI)->getName();
377 (*AI)->getType().getAsStringInternal(ParamStr);
378 S += ParamStr;
379 }
380 if (FT->isVariadic()) {
381 if (!CE->arg_empty()) S += ", ";
382 S += "...";
383 }
384 S += ')';
385 }
386 S += " {\n";
387
388 bool haveByRefDecls = false;
389
390 // Create local declarations to avoid rewriting all closure decl ref exprs.
391 // First, emit a declaration for all "by ref" decls.
392 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
393 E = BlockByRefDecls.end(); I != E; ++I) {
394 // Note: It is not possible to have "by ref" closure pointer decls.
395 haveByRefDecls = true;
396 S += " ";
397 std::string Name = (*I)->getName();
398 Context->getPointerType((*I)->getType()).getAsStringInternal(Name);
399 S += Name + " = __cself->" + (*I)->getName() + "; // bound by ref\n";
400 }
401 // Next, emit a declaration for all "by copy" declarations.
402 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
403 E = BlockByCopyDecls.end(); I != E; ++I) {
404 S += " ";
405 std::string Name = (*I)->getName();
406 // Handle nested closure invocation. For example:
407 //
408 // void (^myImportedClosure)(void);
409 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
410 //
411 // void (^anotherClosure)(void);
412 // anotherClosure = ^(void) {
413 // myImportedClosure(); // import and invoke the closure
414 // };
415 //
416 if (isBlockPointerType((*I)->getType()))
Steve Naroff8af6a452008-10-02 17:12:56 +0000417 S += "struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000418 else
419 (*I)->getType().getAsStringInternal(Name);
420 S += Name + " = __cself->" + (*I)->getName() + "; // bound by copy\n";
421 }
Steve Naroff9c3c9022008-09-17 18:37:59 +0000422 if (BlockExpr *CBE = dyn_cast<BlockExpr>(CE)) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000423 std::string BodyBuf;
424
425 SourceLocation BodyLocStart = CBE->getBody()->getLocStart();
426 SourceLocation BodyLocEnd = CBE->getBody()->getLocEnd();
427 const char *BodyStartBuf = SM->getCharacterData(BodyLocStart);
428 const char *BodyEndBuf = SM->getCharacterData(BodyLocEnd);
429
430 BodyBuf.append(BodyStartBuf, BodyEndBuf-BodyStartBuf+1);
431
432 if (BlockDeclRefs.size()) {
433 unsigned int nCharsAdded = 0;
434 for (unsigned i = 0; i < BlockDeclRefs.size(); i++) {
435 if (BlockDeclRefs[i]->isByRef()) {
436 // Add a level of indirection! The code below assumes
437 // the closure decl refs/locations are in strictly ascending
438 // order. The traversal performed by GetBlockDeclRefExprs()
439 // currently does this. FIXME: Wrap the *x with parens,
440 // just in case x is a more complex expression, like x->member,
441 // which needs to be rewritten to (*x)->member.
442 SourceLocation StarLoc = BlockDeclRefs[i]->getLocStart();
443 const char *StarBuf = SM->getCharacterData(StarLoc);
444 BodyBuf.insert(StarBuf-BodyStartBuf+nCharsAdded, 1, '*');
445 // Get a fresh buffer, the insert might have caused it to grow.
446 BodyStartBuf = SM->getCharacterData(BodyLocStart);
447 nCharsAdded++;
448 } else if (isBlockPointerType(BlockDeclRefs[i]->getType())) {
449 Diags.Report(NoNestedBlockCalls);
450
451 GetBlockCallExprs(CE);
452
453 // Rewrite the closure in place.
454 // The character based equivalent of RewriteBlockCall().
455 // Need to get the CallExpr associated with this BlockDeclRef.
456 std::string BlockCall = SynthesizeBlockCall(BlockCallExprs[BlockDeclRefs[i]]);
457
458 SourceLocation CallLocStart = BlockCallExprs[BlockDeclRefs[i]]->getLocStart();
459 SourceLocation CallLocEnd = BlockCallExprs[BlockDeclRefs[i]]->getLocEnd();
460 const char *CallStart = SM->getCharacterData(CallLocStart);
461 const char *CallEnd = SM->getCharacterData(CallLocEnd);
462 unsigned CallBytes = CallEnd-CallStart;
463 BodyBuf.replace(CallStart-BodyStartBuf, CallBytes, BlockCall.c_str());
464 nCharsAdded += CallBytes;
465 }
466 }
467 }
468 if (haveByRefDecls) {
469 // Remove |...|.
Steve Naroffeab5f632008-09-23 19:24:41 +0000470 //const char *firstBarPtr = strchr(BodyStartBuf, '|');
471 //const char *secondBarPtr = strchr(firstBarPtr+1, '|');
472 //BodyBuf.replace(firstBarPtr-BodyStartBuf, secondBarPtr-firstBarPtr+1, "");
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000473 }
474 S += " ";
475 S += BodyBuf;
476 }
477 S += "\n}\n";
478 return S;
479}
480
Steve Naroff48a8c612008-10-03 12:09:49 +0000481std::string RewriteBlocks::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag) {
482 std::string S = "struct " + Tag;
483 std::string Constructor = " " + Tag;
484
485 S += " {\n struct __block_impl impl;\n";
486 Constructor += "(void *fp";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000487
488 GetBlockDeclRefExprs(CE);
489 if (BlockDeclRefs.size()) {
490 // Unique all "by copy" declarations.
491 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
492 if (!BlockDeclRefs[i]->isByRef())
493 BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
494 // Unique all "by ref" declarations.
495 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
496 if (BlockDeclRefs[i]->isByRef())
497 BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
498
499 // Output all "by copy" declarations.
500 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
501 E = BlockByCopyDecls.end(); I != E; ++I) {
502 S += " ";
503 std::string Name = (*I)->getName();
Steve Naroff48a8c612008-10-03 12:09:49 +0000504 std::string ArgName = "_" + Name;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000505 // Handle nested closure invocation. For example:
506 //
507 // void (^myImportedBlock)(void);
508 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
509 //
510 // void (^anotherBlock)(void);
511 // anotherBlock = ^(void) {
512 // myImportedBlock(); // import and invoke the closure
513 // };
514 //
515 if (isBlockPointerType((*I)->getType()))
Steve Naroff8af6a452008-10-02 17:12:56 +0000516 S += "struct __block_impl *";
Steve Naroff48a8c612008-10-03 12:09:49 +0000517 else {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000518 (*I)->getType().getAsStringInternal(Name);
Steve Naroff48a8c612008-10-03 12:09:49 +0000519 (*I)->getType().getAsStringInternal(ArgName);
520 }
521 Constructor += ", " + ArgName;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000522 S += Name + ";\n";
523 }
524 // Output all "by ref" declarations.
525 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
526 E = BlockByRefDecls.end(); I != E; ++I) {
527 S += " ";
528 std::string Name = (*I)->getName();
Steve Naroff48a8c612008-10-03 12:09:49 +0000529 std::string ArgName = "_" + Name;
530
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000531 if (isBlockPointerType((*I)->getType()))
Steve Naroff8af6a452008-10-02 17:12:56 +0000532 S += "struct __block_impl *";
Steve Naroff48a8c612008-10-03 12:09:49 +0000533 else {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000534 Context->getPointerType((*I)->getType()).getAsStringInternal(Name);
Steve Naroff48a8c612008-10-03 12:09:49 +0000535 Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName);
536 }
537 Constructor += ", " + ArgName;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000538 S += Name + "; // by ref\n";
Steve Naroff48a8c612008-10-03 12:09:49 +0000539 }
540 // Finish writing the constructor.
541 // FIXME: handle NSConcreteGlobalBlock.
542 Constructor += ", int flags=0) {\n";
Steve Naroff83ba14e2008-10-03 15:04:50 +0000543 Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
Steve Naroff48a8c612008-10-03 12:09:49 +0000544 Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
545
546 // Initialize all "by copy" arguments.
547 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
548 E = BlockByCopyDecls.end(); I != E; ++I) {
549 std::string Name = (*I)->getName();
550 Constructor += " ";
551 Constructor += Name + " = _";
552 Constructor += Name + ";\n";
553 }
554 // Initialize all "by ref" arguments.
555 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
556 E = BlockByRefDecls.end(); I != E; ++I) {
557 std::string Name = (*I)->getName();
558 Constructor += " ";
559 Constructor += Name + " = _";
560 Constructor += Name + ";\n";
561 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000562 } else {
563 // Finish writing the constructor.
564 // FIXME: handle NSConcreteGlobalBlock.
565 Constructor += ", int flags=0) {\n";
566 Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
567 Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000568 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000569 Constructor += " ";
570 Constructor += "}\n";
571 S += Constructor;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000572 S += "};\n";
573 return S;
574}
575
576void RewriteBlocks::SynthesizeBlockLiterals(SourceLocation FunLocStart,
577 const char *FunName) {
578 // Insert closures that were part of the function.
579 for (unsigned i = 0; i < Blocks.size(); i++) {
580
Steve Naroff48a8c612008-10-03 12:09:49 +0000581 std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000582
583 std::string CI = SynthesizeBlockImpl(Blocks[i], Tag);
584
585 InsertText(FunLocStart, CI.c_str(), CI.size());
586
587 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag);
588
589 InsertText(FunLocStart, CF.c_str(), CF.size());
590
591 BlockDeclRefs.clear();
592 BlockByRefDecls.clear();
593 BlockByCopyDecls.clear();
594 BlockCallExprs.clear();
595 }
596 Blocks.clear();
597}
598
599void RewriteBlocks::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
Steve Naroff3ad29e22008-10-03 00:12:09 +0000600 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000601 const char *FuncName = FD->getName();
602
603 SynthesizeBlockLiterals(FunLocStart, FuncName);
604}
605
606void RewriteBlocks::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
607 SourceLocation FunLocStart = MD->getLocStart();
608 std::string FuncName = std::string(MD->getSelector().getName());
609 // Convert colons to underscores.
610 std::string::size_type loc = 0;
611 while ((loc = FuncName.find(":", loc)) != std::string::npos)
612 FuncName.replace(loc, 1, "_");
613
614 SynthesizeBlockLiterals(FunLocStart, FuncName.c_str());
615}
616
617/// HandleDeclInMainFile - This is called for each top-level decl defined in the
618/// main file of the input.
619void RewriteBlocks::HandleDeclInMainFile(Decl *D) {
620 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
621
622 // Since function prototypes don't have ParmDecl's, we check the function
623 // prototype. This enables us to rewrite function declarations and
624 // definitions using the same code.
625 QualType funcType = FD->getType();
626
627 if (FunctionTypeProto *fproto = dyn_cast<FunctionTypeProto>(funcType)) {
628 for (FunctionTypeProto::arg_type_iterator I = fproto->arg_type_begin(),
629 E = fproto->arg_type_end(); I && (I != E); ++I)
630 if (isBlockPointerType(*I)) {
631 // All the args are checked/rewritten. Don't call twice!
632 RewriteBlockPointerDecl(FD);
633 break;
634 }
635 }
636 if (Stmt *Body = FD->getBody()) {
637 CurFunctionDef = FD;
638 FD->setBody(RewriteFunctionBody(Body));
639 InsertBlockLiteralsWithinFunction(FD);
640 CurFunctionDef = 0;
641 }
642 return;
643 }
644 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
645 RewriteMethodDecl(MD);
646 if (Stmt *Body = MD->getBody()) {
647 CurMethodDef = MD;
648 RewriteFunctionBody(Body);
649 InsertBlockLiteralsWithinMethod(MD);
650 CurMethodDef = 0;
651 }
652 }
653 if (ValueDecl *ND = dyn_cast<ValueDecl>(D)) {
654 if (isBlockPointerType(ND->getType()))
655 RewriteBlockPointerDecl(ND);
656 return;
657 }
658 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
659 if (isBlockPointerType(TD->getUnderlyingType()))
660 RewriteBlockPointerDecl(TD);
661 return;
662 }
Steve Naroff83ba14e2008-10-03 15:04:50 +0000663 if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
664 if (RD->isDefinition()) {
665 for (RecordDecl::field_const_iterator i = RD->field_begin(),
666 e = RD->field_end(); i != e; ++i) {
667 FieldDecl *FD = *i;
668 if (isBlockPointerType(FD->getType()))
669 RewriteBlockPointerDecl(FD);
670 }
671 }
672 return;
673 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000674}
675
676void RewriteBlocks::GetBlockDeclRefExprs(Stmt *S) {
677 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
678 CI != E; ++CI)
679 if (*CI)
680 GetBlockDeclRefExprs(*CI);
681
682 // Handle specific things.
683 if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S))
684 // FIXME: Handle enums.
685 if (!isa<FunctionDecl>(CDRE->getDecl()))
686 BlockDeclRefs.push_back(CDRE);
687 return;
688}
689
690void RewriteBlocks::GetBlockCallExprs(Stmt *S) {
691 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
692 CI != E; ++CI)
693 if (*CI)
694 GetBlockCallExprs(*CI);
695
696 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
697 if (CE->getCallee()->getType()->isBlockPointerType())
698 BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE;
699 }
700 return;
701}
702
703//===----------------------------------------------------------------------===//
704// Function Body / Expression rewriting
705//===----------------------------------------------------------------------===//
706
707Stmt *RewriteBlocks::RewriteFunctionBody(Stmt *S) {
708 // Start by rewriting all children.
709 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
710 CI != E; ++CI)
711 if (*CI) {
Steve Naroff9c3c9022008-09-17 18:37:59 +0000712 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000713 // We intentionally avoid rewritting the contents of a closure block
714 // expr. InsertBlockLiteralsWithinFunction() will rewrite the body.
Steve Naroff9c3c9022008-09-17 18:37:59 +0000715 RewriteBlockExpr(CBE);
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000716 } else {
717 Stmt *newStmt = RewriteFunctionBody(*CI);
718 if (newStmt)
719 *CI = newStmt;
720 }
721 }
722 // Handle specific things.
723 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
724 if (CE->getCallee()->getType()->isBlockPointerType())
725 RewriteBlockCall(CE);
726 }
727 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
728 ScopedDecl *SD = DS->getDecl();
729 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
730 if (isBlockPointerType(ND->getType()))
731 RewriteBlockPointerDecl(ND);
732 }
733 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
734 if (isBlockPointerType(TD->getUnderlyingType()))
735 RewriteBlockPointerDecl(TD);
736 }
737 }
738 // Return this stmt unmodified.
739 return S;
740}
741
742std::string RewriteBlocks::SynthesizeBlockCall(CallExpr *Exp) {
743 // Navigate to relevant type information.
Steve Naroffcc2ece22008-09-24 22:46:45 +0000744 const char *closureName = 0;
745 const BlockPointerType *CPT = 0;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000746
747 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) {
748 closureName = DRE->getDecl()->getName();
749 CPT = DRE->getType()->getAsBlockPointerType();
750 } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) {
751 closureName = CDRE->getDecl()->getName();
752 CPT = CDRE->getType()->getAsBlockPointerType();
Steve Naroff83ba14e2008-10-03 15:04:50 +0000753 } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) {
754 closureName = MExpr->getMemberDecl()->getName();
755 CPT = MExpr->getType()->getAsBlockPointerType();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000756 } else {
757 assert(1 && "RewriteBlockClass: Bad type");
758 }
759 assert(CPT && "RewriteBlockClass: Bad type");
760 const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType();
761 assert(FT && "RewriteBlockClass: Bad type");
762 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FT);
763 // FTP will be null for closures that don't take arguments.
764
765 // Build a closure call - start with a paren expr to enforce precedence.
766 std::string BlockCall = "(";
767
768 // Synthesize the cast.
769 BlockCall += "(" + Exp->getType().getAsString() + "(*)";
Steve Naroff8af6a452008-10-02 17:12:56 +0000770 BlockCall += "(struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000771 if (FTP) {
772 for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(),
773 E = FTP->arg_type_end(); I && (I != E); ++I)
774 BlockCall += ", " + (*I).getAsString();
775 }
776 BlockCall += "))"; // close the argument list and paren expression.
777
Steve Naroff83ba14e2008-10-03 15:04:50 +0000778 // Invoke the closure. We need to cast it since the declaration type is
779 // bogus (it's a function pointer type)
780 BlockCall += "((struct __block_impl *)";
781 std::string closureExprBufStr;
782 llvm::raw_string_ostream closureExprBuf(closureExprBufStr);
783 Exp->getCallee()->printPretty(closureExprBuf);
784 BlockCall += closureExprBuf.str();
785 BlockCall += ")->FuncPtr)";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000786
787 // Add the arguments.
Steve Naroff83ba14e2008-10-03 15:04:50 +0000788 BlockCall += "((struct __block_impl *)";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000789 BlockCall += closureName;
790 for (CallExpr::arg_iterator I = Exp->arg_begin(),
791 E = Exp->arg_end(); I != E; ++I) {
792 std::string syncExprBufS;
793 llvm::raw_string_ostream Buf(syncExprBufS);
794 (*I)->printPretty(Buf);
795 BlockCall += ", " + Buf.str();
796 }
797 return BlockCall;
798}
799
800void RewriteBlocks::RewriteBlockCall(CallExpr *Exp) {
801 std::string BlockCall = SynthesizeBlockCall(Exp);
802
803 const char *startBuf = SM->getCharacterData(Exp->getLocStart());
804 const char *endBuf = SM->getCharacterData(Exp->getLocEnd());
805
806 ReplaceText(Exp->getLocStart(), endBuf-startBuf,
807 BlockCall.c_str(), BlockCall.size());
808}
809
810void RewriteBlocks::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
811 SourceLocation DeclLoc = FD->getLocation();
812 unsigned parenCount = 0, nArgs = 0;
813
814 // We have 1 or more arguments that have closure pointers.
815 const char *startBuf = SM->getCharacterData(DeclLoc);
816 const char *startArgList = strchr(startBuf, '(');
817
818 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
819
820 parenCount++;
821 // advance the location to startArgList.
822 DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf+1);
823 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
824
825 const char *topLevelCommaCursor = 0;
826 const char *argPtr = startArgList;
827 bool scannedBlockDecl = false;
Steve Naroff8af6a452008-10-02 17:12:56 +0000828 std::string Tag = "struct __block_impl *";
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000829
830 while (*argPtr++ && parenCount) {
831 switch (*argPtr) {
832 case '^':
833 scannedBlockDecl = true;
834 break;
835 case '(':
836 parenCount++;
837 break;
838 case ')':
839 parenCount--;
840 if (parenCount == 0) {
841 if (scannedBlockDecl) {
842 // If we are rewriting a definition, don't forget the arg name.
843 if (FD->getBody())
844 Tag += FD->getParamDecl(nArgs)->getName();
845 // The last argument is a closure pointer decl, rewrite it!
846 if (topLevelCommaCursor)
847 ReplaceText(DeclLoc, argPtr-topLevelCommaCursor-2, Tag.c_str(), Tag.size());
848 else
849 ReplaceText(DeclLoc, argPtr-startArgList-1, Tag.c_str(), Tag.size());
850 scannedBlockDecl = false; // reset.
851 }
852 nArgs++;
853 }
854 break;
855 case ',':
856 if (parenCount == 1) {
857 // Make sure the function takes more than one argument.
858 assert((FD->getNumParams() > 1) && "Rewriter fuzzy parser confused");
859 if (scannedBlockDecl) {
860 // If we are rewriting a definition, don't forget the arg name.
861 if (FD->getBody())
862 Tag += FD->getParamDecl(nArgs)->getName();
863 // The current argument is a closure pointer decl, rewrite it!
864 if (topLevelCommaCursor)
865 ReplaceText(DeclLoc, argPtr-topLevelCommaCursor-1, Tag.c_str(), Tag.size());
866 else
867 ReplaceText(DeclLoc, argPtr-startArgList-1, Tag.c_str(), Tag.size());
868 scannedBlockDecl = false;
869 }
870 nArgs++;
871 // advance the location to topLevelCommaCursor.
872 if (topLevelCommaCursor)
873 DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-topLevelCommaCursor);
874 else
875 DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList+1);
876 topLevelCommaCursor = argPtr;
877 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
878 }
879 break;
880 }
881 }
882 return;
883}
884
Steve Naroffeab5f632008-09-23 19:24:41 +0000885bool RewriteBlocks::BlockPointerTypeTakesAnyBlockArguments(QualType QT) {
886 const BlockPointerType *BPT = QT->getAsBlockPointerType();
887 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
888 const FunctionTypeProto *FTP = BPT->getPointeeType()->getAsFunctionTypeProto();
889 if (FTP) {
890 for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(),
891 E = FTP->arg_type_end(); I != E; ++I)
892 if (isBlockPointerType(*I))
893 return true;
894 }
895 return false;
896}
897
898void RewriteBlocks::GetExtentOfArgList(const char *Name,
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000899 const char *&LParen, const char *&RParen) {
900 const char *argPtr = strchr(Name, '(');
Steve Naroffeab5f632008-09-23 19:24:41 +0000901 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
902
903 LParen = argPtr; // output the start.
904 argPtr++; // skip past the left paren.
905 unsigned parenCount = 1;
906
907 while (*argPtr && parenCount) {
908 switch (*argPtr) {
909 case '(': parenCount++; break;
910 case ')': parenCount--; break;
911 default: break;
912 }
913 if (parenCount) argPtr++;
914 }
915 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
916 RParen = argPtr; // output the end
917}
918
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000919void RewriteBlocks::RewriteBlockPointerDecl(NamedDecl *ND) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000920 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
921 RewriteBlockPointerFunctionArgs(FD);
922 return;
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000923 }
924 // Handle Variables and Typedefs.
925 SourceLocation DeclLoc = ND->getLocation();
926 QualType DeclT;
927 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
928 DeclT = VD->getType();
929 else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND))
930 DeclT = TDD->getUnderlyingType();
Steve Naroff83ba14e2008-10-03 15:04:50 +0000931 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
932 DeclT = FD->getType();
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000933 else
934 assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled");
Steve Naroffeab5f632008-09-23 19:24:41 +0000935
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000936 const char *startBuf = SM->getCharacterData(DeclLoc);
937 const char *endBuf = startBuf;
938 // scan backward (from the decl location) for the end of the previous decl.
939 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
940 startBuf--;
941 assert((*startBuf == '^') &&
942 "RewriteBlockPointerDecl() scan error: no caret");
943 // Replace the '^' with '*', computing a negative offset.
944 DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf);
945 ReplaceText(DeclLoc, 1, "*", 1);
946
947 if (BlockPointerTypeTakesAnyBlockArguments(DeclT)) {
948 // Replace the '^' with '*' for arguments.
949 DeclLoc = ND->getLocation();
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000950 startBuf = SM->getCharacterData(DeclLoc);
Steve Naroff1f6c3ae2008-09-24 17:22:34 +0000951 const char *argListBegin, *argListEnd;
Steve Naroffca3bb4f2008-09-23 21:15:53 +0000952 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
953 while (argListBegin < argListEnd) {
954 if (*argListBegin == '^') {
955 SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf);
956 ReplaceText(CaretLoc, 1, "*", 1);
957 }
958 argListBegin++;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000959 }
Steve Naroffeab5f632008-09-23 19:24:41 +0000960 }
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000961 return;
962}
963
Steve Naroff9c3c9022008-09-17 18:37:59 +0000964void RewriteBlocks::RewriteBlockExpr(BlockExpr *Exp) {
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000965 Blocks.push_back(Exp);
966 bool haveByRefDecls = false;
967
968 // Add initializers for any closure decl refs.
969 GetBlockDeclRefExprs(Exp);
970 if (BlockDeclRefs.size()) {
971 // Unique all "by copy" declarations.
972 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
973 if (!BlockDeclRefs[i]->isByRef())
974 BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
975 // Unique all "by ref" declarations.
976 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
977 if (BlockDeclRefs[i]->isByRef()) {
978 haveByRefDecls = true;
979 BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
980 }
981 }
982 std::string FuncName;
983
984 if (CurFunctionDef)
985 FuncName = std::string(CurFunctionDef->getName());
986 else if (CurMethodDef) {
987 FuncName = std::string(CurMethodDef->getSelector().getName());
988 // Convert colons to underscores.
989 std::string::size_type loc = 0;
990 while ((loc = FuncName.find(":", loc)) != std::string::npos)
991 FuncName.replace(loc, 1, "_");
992 }
993 std::string BlockNumber = utostr(Blocks.size()-1);
994
Steve Naroff83ba14e2008-10-03 15:04:50 +0000995 std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
Steve Naroffa0b75cf2008-10-02 23:30:43 +0000996 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
Steve Naroff1c9f81b2008-09-17 00:13:27 +0000997
Steve Naroff83ba14e2008-10-03 15:04:50 +0000998 std::string FunkTypeStr;
999
1000 // Get a pointer to the function type so we can cast appropriately.
1001 Context->getPointerType(QualType(Exp->getFunctionType(),0)).getAsStringInternal(FunkTypeStr);
1002
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001003 // Rewrite the closure block with a compound literal. The first cast is
1004 // to prevent warnings from the C compiler.
Steve Naroff83ba14e2008-10-03 15:04:50 +00001005 std::string Init = "(" + FunkTypeStr;
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001006
Steve Naroff83ba14e2008-10-03 15:04:50 +00001007 Init += ")&" + Tag;
1008
1009 // Initialize the block function.
1010 Init += "((void*)" + Func;
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001011
1012 // Add initializers for any closure decl refs.
1013 if (BlockDeclRefs.size()) {
1014 // Output all "by copy" declarations.
1015 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
1016 E = BlockByCopyDecls.end(); I != E; ++I) {
1017 Init += ",";
1018 if (isObjCType((*I)->getType())) {
1019 Init += "[[";
1020 Init += (*I)->getName();
1021 Init += " retain] autorelease]";
1022 } else {
1023 Init += (*I)->getName();
1024 }
1025 }
1026 // Output all "by ref" declarations.
1027 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
1028 E = BlockByRefDecls.end(); I != E; ++I) {
1029 Init += ",&";
1030 Init += (*I)->getName();
1031 }
1032 }
Steve Naroff83ba14e2008-10-03 15:04:50 +00001033 Init += ")";
Steve Naroff1c9f81b2008-09-17 00:13:27 +00001034 BlockDeclRefs.clear();
1035 BlockByRefDecls.clear();
1036 BlockByCopyDecls.clear();
1037
1038 // Do the rewrite.
1039 const char *startBuf = SM->getCharacterData(Exp->getLocStart());
1040 const char *endBuf = SM->getCharacterData(Exp->getLocEnd());
1041 ReplaceText(Exp->getLocStart(), endBuf-startBuf+1, Init.c_str(), Init.size());
1042 return;
1043}