2008-09-17 08:13:27 +08:00
|
|
|
//===--- RewriteBlocks.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Hacks and fun related to the closure rewriter.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 06:50:54 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2008-09-17 08:13:27 +08:00
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using llvm::utostr;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RewriteBlocks : public ASTConsumer {
|
|
|
|
Rewriter Rewrite;
|
|
|
|
Diagnostic &Diags;
|
|
|
|
const LangOptions &LangOpts;
|
|
|
|
unsigned RewriteFailedDiag;
|
|
|
|
|
|
|
|
ASTContext *Context;
|
|
|
|
SourceManager *SM;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID MainFileID;
|
2008-09-17 08:13:27 +08:00
|
|
|
const char *MainFileStart, *MainFileEnd;
|
|
|
|
|
|
|
|
// Block expressions.
|
|
|
|
llvm::SmallVector<BlockExpr *, 32> Blocks;
|
|
|
|
llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs;
|
|
|
|
llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Block related declarations.
|
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls;
|
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls;
|
2008-10-04 04:28:15 +08:00
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
|
2008-10-05 01:06:23 +08:00
|
|
|
|
|
|
|
llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// The function/method we are rewriting.
|
|
|
|
FunctionDecl *CurFunctionDef;
|
|
|
|
ObjCMethodDecl *CurMethodDef;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
bool IsHeader;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 07:30:43 +08:00
|
|
|
std::string Preamble;
|
2008-09-17 08:13:27 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
RewriteBlocks(std::string inFile, Diagnostic &D,
|
2008-09-18 22:10:13 +08:00
|
|
|
const LangOptions &LOpts);
|
2008-09-17 08:13:27 +08:00
|
|
|
~RewriteBlocks() {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Get the buffer corresponding to MainFileID.
|
2008-09-17 08:13:27 +08:00
|
|
|
// If we haven't changed it, then we are done.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const RewriteBuffer *RewriteBuf =
|
2008-09-17 08:13:27 +08:00
|
|
|
Rewrite.getRewriteBufferFor(MainFileID)) {
|
|
|
|
std::string S(RewriteBuf->begin(), RewriteBuf->end());
|
|
|
|
printf("%s\n", S.c_str());
|
|
|
|
} else {
|
|
|
|
printf("No changes\n");
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
|
|
|
|
void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen);
|
|
|
|
void ReplaceText(SourceLocation Start, unsigned OrigLength,
|
|
|
|
const char *NewStr, unsigned NewLength);
|
|
|
|
|
|
|
|
// Top Level Driver code.
|
2009-03-30 00:50:03 +08:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
|
|
|
|
HandleTopLevelSingleDecl(*I);
|
|
|
|
}
|
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
2008-09-17 08:13:27 +08:00
|
|
|
void HandleDeclInMainFile(Decl *D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Top level
|
2008-09-17 08:13:27 +08:00
|
|
|
Stmt *RewriteFunctionBody(Stmt *S);
|
|
|
|
void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
|
|
|
|
void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Block specific rewrite rules.
|
2008-10-05 01:06:23 +08:00
|
|
|
std::string SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD=0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
void RewriteBlockCall(CallExpr *Exp);
|
|
|
|
void RewriteBlockPointerDecl(NamedDecl *VD);
|
2008-10-05 02:52:47 +08:00
|
|
|
void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD);
|
2008-09-17 08:13:27 +08:00
|
|
|
void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
|
2008-10-04 04:28:15 +08:00
|
|
|
const char *funcName, std::string Tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
|
2008-09-17 08:13:27 +08:00
|
|
|
const char *funcName, std::string Tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
|
2008-10-05 07:47:37 +08:00
|
|
|
bool hasCopyDisposeHelpers);
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string SynthesizeBlockCall(CallExpr *Exp);
|
|
|
|
void SynthesizeBlockLiterals(SourceLocation FunLocStart,
|
|
|
|
const char *FunName);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 08:06:12 +08:00
|
|
|
void CollectBlockDeclRefInfo(BlockExpr *Exp);
|
2008-09-17 08:13:27 +08:00
|
|
|
void GetBlockCallExprs(Stmt *S);
|
2008-10-05 07:47:37 +08:00
|
|
|
void GetBlockDeclRefExprs(Stmt *S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// We avoid calling Type::isBlockPointerType(), since it operates on the
|
|
|
|
// canonical type. We only care if the top-level type is a closure pointer.
|
|
|
|
bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// FIXME: This predicate seems like it would be useful to add to ASTContext.
|
|
|
|
bool isObjCType(QualType T) {
|
|
|
|
if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
|
|
|
|
OCT == Context->getCanonicalType(Context->getObjCClassType()))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const PointerType *PT = OCT->getAs<PointerType>()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
|
2009-06-18 06:40:22 +08:00
|
|
|
PT->getPointeeType()->isObjCQualifiedIdType())
|
2008-09-17 08:13:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ObjC rewrite methods.
|
|
|
|
void RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl);
|
|
|
|
void RewriteCategoryDecl(ObjCCategoryDecl *CatDecl);
|
|
|
|
void RewriteProtocolDecl(ObjCProtocolDecl *PDecl);
|
|
|
|
void RewriteMethodDecl(ObjCMethodDecl *MDecl);
|
2008-10-16 02:38:58 +08:00
|
|
|
|
2009-02-27 07:50:07 +08:00
|
|
|
void RewriteFunctionProtoType(QualType funcType, NamedDecl *D);
|
2008-10-16 02:38:58 +08:00
|
|
|
void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
|
|
|
|
void RewriteCastExpr(CastExpr *CE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
bool PointerTypeTakesAnyBlockArguments(QualType QT);
|
2008-09-25 01:22:34 +08:00
|
|
|
void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen);
|
2008-09-17 08:13:27 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsHeaderFile(const std::string &Filename) {
|
|
|
|
std::string::size_type DotPos = Filename.rfind('.');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (DotPos == std::string::npos) {
|
|
|
|
// no file extension
|
2009-09-09 23:08:12 +08:00
|
|
|
return false;
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
|
|
|
|
// C header: .h
|
|
|
|
// C++ header: .hh or .H;
|
|
|
|
return Ext == "h" || Ext == "hh" || Ext == "H";
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
|
2009-05-19 06:20:00 +08:00
|
|
|
RewriteBlocks::RewriteBlocks(std::string inFile,
|
2009-09-09 23:08:12 +08:00
|
|
|
Diagnostic &D, const LangOptions &LOpts) :
|
2008-09-18 22:10:13 +08:00
|
|
|
Diags(D), LangOpts(LOpts) {
|
|
|
|
IsHeader = IsHeaderFile(inFile);
|
|
|
|
CurFunctionDef = 0;
|
|
|
|
CurMethodDef = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
|
2008-09-18 22:10:13 +08:00
|
|
|
"rewriting failed");
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
ASTConsumer *clang::CreateBlockRewriter(const std::string& InFile,
|
|
|
|
Diagnostic &Diags,
|
|
|
|
const LangOptions &LangOpts) {
|
2009-05-19 06:20:00 +08:00
|
|
|
return new RewriteBlocks(InFile, Diags, LangOpts);
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::Initialize(ASTContext &context) {
|
|
|
|
Context = &context;
|
|
|
|
SM = &Context->getSourceManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Get the ID and start/end of the main file.
|
|
|
|
MainFileID = SM->getMainFileID();
|
|
|
|
const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
|
|
|
|
MainFileStart = MainBuf->getBufferStart();
|
|
|
|
MainFileEnd = MainBuf->getBufferEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 07:22:57 +08:00
|
|
|
Rewrite.setSourceMgr(Context->getSourceManager(), LangOpts);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 07:30:43 +08:00
|
|
|
if (IsHeader)
|
|
|
|
Preamble = "#pragma once\n";
|
|
|
|
Preamble += "#ifndef BLOCK_IMPL\n";
|
|
|
|
Preamble += "#define BLOCK_IMPL\n";
|
|
|
|
Preamble += "struct __block_impl {\n";
|
|
|
|
Preamble += " void *isa;\n";
|
|
|
|
Preamble += " int Flags;\n";
|
|
|
|
Preamble += " int Size;\n";
|
|
|
|
Preamble += " void *FuncPtr;\n";
|
|
|
|
Preamble += "};\n";
|
|
|
|
Preamble += "enum {\n";
|
|
|
|
Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n";
|
|
|
|
Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n";
|
|
|
|
Preamble += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
if (LangOpts.Microsoft)
|
2008-10-03 07:30:43 +08:00
|
|
|
Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n";
|
|
|
|
else
|
|
|
|
Preamble += "#define __OBJC_RW_EXTERN extern\n";
|
|
|
|
Preamble += "// Runtime copy/destroy helper functions\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void _Block_copy_assign(void *, void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void _Block_byref_assign_copy(void *, void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void _Block_destroy(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void _Block_byref_release(void *);\n";
|
2008-10-03 20:09:49 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN void *_NSConcreteGlobalBlock;\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void *_NSConcreteStackBlock;\n";
|
2008-10-03 07:30:43 +08:00
|
|
|
Preamble += "#endif\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
InsertText(SM->getLocForStartOfFile(MainFileID),
|
2008-10-03 07:30:43 +08:00
|
|
|
Preamble.c_str(), Preamble.size());
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteBlocks::InsertText(SourceLocation Loc, const char *StrData,
|
|
|
|
unsigned StrLen) {
|
2008-09-17 08:13:27 +08:00
|
|
|
if (!Rewrite.InsertText(Loc, StrData, StrLen))
|
|
|
|
return;
|
|
|
|
Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
|
|
|
const char *NewStr, unsigned NewLength) {
|
2009-08-20 03:10:30 +08:00
|
|
|
if (!Rewrite.ReplaceText(Start, OrigLength,
|
|
|
|
llvm::StringRef(NewStr, NewLength)))
|
2008-09-17 08:13:27 +08:00
|
|
|
return;
|
|
|
|
Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::RewriteMethodDecl(ObjCMethodDecl *Method) {
|
|
|
|
bool haveBlockPtrs = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator I = Method->param_begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = Method->param_end(); I != E; ++I)
|
|
|
|
if (isBlockPointerType((*I)->getType()))
|
|
|
|
haveBlockPtrs = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (!haveBlockPtrs)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Do a fuzzy rewrite.
|
|
|
|
// We have 1 or more arguments that have closure pointers.
|
|
|
|
SourceLocation Loc = Method->getLocStart();
|
|
|
|
SourceLocation LocEnd = Method->getLocEnd();
|
|
|
|
const char *startBuf = SM->getCharacterData(Loc);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
|
|
|
|
|
|
|
const char *methodPtr = startBuf;
|
2008-10-03 01:12:56 +08:00
|
|
|
std::string Tag = "struct __block_impl *";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
while (*methodPtr++ && (methodPtr != endBuf)) {
|
|
|
|
switch (*methodPtr) {
|
|
|
|
case ':':
|
|
|
|
methodPtr++;
|
|
|
|
if (*methodPtr == '(') {
|
|
|
|
const char *scanType = ++methodPtr;
|
|
|
|
bool foundBlockPointer = false;
|
|
|
|
unsigned parenCount = 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
while (parenCount) {
|
|
|
|
switch (*scanType) {
|
2009-09-09 23:08:12 +08:00
|
|
|
case '(':
|
|
|
|
parenCount++;
|
2008-09-17 08:13:27 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case ')':
|
2008-09-17 08:13:27 +08:00
|
|
|
parenCount--;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
foundBlockPointer = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
scanType++;
|
|
|
|
}
|
|
|
|
if (foundBlockPointer) {
|
|
|
|
// advance the location to startArgList.
|
|
|
|
Loc = Loc.getFileLocWithOffset(methodPtr-startBuf);
|
|
|
|
assert((Loc.isValid()) && "Invalid Loc");
|
|
|
|
ReplaceText(Loc, scanType-methodPtr-1, Tag.c_str(), Tag.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Advance startBuf. Since the underlying buffer has changed,
|
|
|
|
// it's very important to advance startBuf (so we can correctly
|
|
|
|
// compute a relative Loc the next time around).
|
|
|
|
startBuf = methodPtr;
|
|
|
|
}
|
|
|
|
// Advance the method ptr to the end of the type.
|
|
|
|
methodPtr = scanType;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator
|
|
|
|
I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCCategoryDecl::instmeth_iterator
|
|
|
|
I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCCategoryDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::instmeth_iterator
|
|
|
|
I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::classmeth_iterator
|
|
|
|
I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2008-09-17 08:13:27 +08:00
|
|
|
RewriteMethodDecl(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top Level Driver Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
void RewriteBlocks::HandleTopLevelSingleDecl(Decl *D) {
|
2008-09-17 08:13:27 +08:00
|
|
|
// Two cases: either the decl could be in the main file, or it could be in a
|
|
|
|
// #included file. If the former, rewrite it now. If the later, check to see
|
|
|
|
// if we rewrote the #include/#import.
|
|
|
|
SourceLocation Loc = D->getLocation();
|
2009-01-16 15:36:28 +08:00
|
|
|
Loc = SM->getInstantiationLoc(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// If this is for a builtin, ignore it.
|
|
|
|
if (Loc.isInvalid()) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D))
|
|
|
|
RewriteInterfaceDecl(MD);
|
|
|
|
else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D))
|
|
|
|
RewriteCategoryDecl(CD);
|
|
|
|
else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
|
|
|
|
RewriteProtocolDecl(PD);
|
|
|
|
|
|
|
|
// If we have a decl in the main file, see if we should rewrite it.
|
2009-01-26 06:02:19 +08:00
|
|
|
if (SM->isFromMainFile(Loc))
|
2008-09-17 08:13:27 +08:00
|
|
|
HandleDeclInMainFile(D);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RewriteBlocks::SynthesizeBlockFunc(BlockExpr *CE, int i,
|
|
|
|
const char *funcName,
|
|
|
|
std::string Tag) {
|
|
|
|
const FunctionType *AFT = CE->getFunctionType();
|
|
|
|
QualType RT = AFT->getResultType();
|
2008-10-03 20:09:49 +08:00
|
|
|
std::string StructRef = "struct " + Tag;
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string S = "static " + RT.getAsString() + " __" +
|
2008-10-03 07:30:43 +08:00
|
|
|
funcName + "_" + "block_func_" + utostr(i);
|
2008-09-17 08:13:27 +08:00
|
|
|
|
2008-10-09 01:01:13 +08:00
|
|
|
BlockDecl *BD = CE->getBlockDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-27 07:50:07 +08:00
|
|
|
if (isa<FunctionNoProtoType>(AFT)) {
|
2008-09-17 08:13:27 +08:00
|
|
|
S += "()";
|
2008-10-09 01:01:13 +08:00
|
|
|
} else if (BD->param_empty()) {
|
2008-10-03 20:09:49 +08:00
|
|
|
S += "(" + StructRef + " *__cself)";
|
2008-09-17 08:13:27 +08:00
|
|
|
} else {
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
|
2008-09-17 08:13:27 +08:00
|
|
|
assert(FT && "SynthesizeBlockFunc: No function proto");
|
|
|
|
S += '(';
|
|
|
|
// first add the implicit argument.
|
2008-10-03 20:09:49 +08:00
|
|
|
S += StructRef + " *__cself, ";
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string ParamStr;
|
2008-10-09 01:01:13 +08:00
|
|
|
for (BlockDecl::param_iterator AI = BD->param_begin(),
|
|
|
|
E = BD->param_end(); AI != E; ++AI) {
|
|
|
|
if (AI != BD->param_begin()) S += ", ";
|
2008-11-24 13:29:24 +08:00
|
|
|
ParamStr = (*AI)->getNameAsString();
|
2009-05-30 04:38:28 +08:00
|
|
|
(*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy);
|
2008-09-17 08:13:27 +08:00
|
|
|
S += ParamStr;
|
|
|
|
}
|
|
|
|
if (FT->isVariadic()) {
|
2008-10-09 01:01:13 +08:00
|
|
|
if (!BD->param_empty()) S += ", ";
|
2008-09-17 08:13:27 +08:00
|
|
|
S += "...";
|
|
|
|
}
|
|
|
|
S += ')';
|
|
|
|
}
|
|
|
|
S += " {\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Create local declarations to avoid rewriting all closure decl ref exprs.
|
|
|
|
// First, emit a declaration for all "by ref" decls.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(Name,
|
|
|
|
Context->PrintingPolicy);
|
2008-11-24 13:29:24 +08:00
|
|
|
S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
// Next, emit a declaration for all "by copy" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
// Handle nested closure invocation. For example:
|
|
|
|
//
|
|
|
|
// void (^myImportedClosure)(void);
|
|
|
|
// myImportedClosure = ^(void) { setGlobalInt(x + y); };
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-09-17 08:13:27 +08:00
|
|
|
// void (^anotherClosure)(void);
|
|
|
|
// anotherClosure = ^(void) {
|
|
|
|
// myImportedClosure(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
if (isBlockPointerType((*I)->getType()))
|
2008-10-03 01:12:56 +08:00
|
|
|
S += "struct __block_impl *";
|
2008-09-17 08:13:27 +08:00
|
|
|
else
|
2009-05-30 04:38:28 +08:00
|
|
|
(*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy);
|
2008-11-24 13:29:24 +08:00
|
|
|
S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
2008-10-05 01:06:23 +08:00
|
|
|
std::string RewrittenStr = RewrittenBlockExprs[CE];
|
|
|
|
const char *cstr = RewrittenStr.c_str();
|
|
|
|
while (*cstr++ != '{') ;
|
|
|
|
S += cstr;
|
|
|
|
S += "\n";
|
2008-09-17 08:13:27 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2008-10-04 04:28:15 +08:00
|
|
|
std::string RewriteBlocks::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
|
|
|
|
const char *funcName,
|
|
|
|
std::string Tag) {
|
|
|
|
std::string StructRef = "struct " + Tag;
|
|
|
|
std::string S = "static void __";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 04:28:15 +08:00
|
|
|
S += funcName;
|
|
|
|
S += "_block_copy_" + utostr(i);
|
|
|
|
S += "(" + StructRef;
|
|
|
|
S += "*dst, " + StructRef;
|
|
|
|
S += "*src) {";
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
|
2008-10-04 04:28:15 +08:00
|
|
|
E = ImportedBlockDecls.end(); I != E; ++I) {
|
|
|
|
S += "_Block_copy_assign(&dst->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-10-04 04:28:15 +08:00
|
|
|
S += ", src->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-10-04 04:28:15 +08:00
|
|
|
S += ");}";
|
|
|
|
}
|
|
|
|
S += "\nstatic void __";
|
|
|
|
S += funcName;
|
|
|
|
S += "_block_dispose_" + utostr(i);
|
|
|
|
S += "(" + StructRef;
|
|
|
|
S += "*src) {";
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
|
2008-10-04 04:28:15 +08:00
|
|
|
E = ImportedBlockDecls.end(); I != E; ++I) {
|
|
|
|
S += "_Block_destroy(src->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-10-04 04:28:15 +08:00
|
|
|
S += ");";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
S += "}\n";
|
2008-10-04 04:28:15 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2008-10-05 07:47:37 +08:00
|
|
|
std::string RewriteBlocks::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
|
|
|
|
bool hasCopyDisposeHelpers) {
|
2008-10-03 20:09:49 +08:00
|
|
|
std::string S = "struct " + Tag;
|
|
|
|
std::string Constructor = " " + Tag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 20:09:49 +08:00
|
|
|
S += " {\n struct __block_impl impl;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 07:47:37 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
S += " void *copy;\n void *dispose;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += "(void *fp";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 07:47:37 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += ", void *copyHelp, void *disposeHelp";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (BlockDeclRefs.size()) {
|
|
|
|
// Output all "by copy" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string FieldName = (*I)->getNameAsString();
|
2008-10-04 04:28:15 +08:00
|
|
|
std::string ArgName = "_" + FieldName;
|
2008-09-17 08:13:27 +08:00
|
|
|
// Handle nested closure invocation. For example:
|
|
|
|
//
|
|
|
|
// void (^myImportedBlock)(void);
|
|
|
|
// myImportedBlock = ^(void) { setGlobalInt(x + y); };
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-09-17 08:13:27 +08:00
|
|
|
// void (^anotherBlock)(void);
|
|
|
|
// anotherBlock = ^(void) {
|
|
|
|
// myImportedBlock(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
2008-10-04 04:28:15 +08:00
|
|
|
if (isBlockPointerType((*I)->getType())) {
|
2008-10-03 01:12:56 +08:00
|
|
|
S += "struct __block_impl *";
|
2008-10-04 04:28:15 +08:00
|
|
|
Constructor += ", void *" + ArgName;
|
|
|
|
} else {
|
2009-05-30 04:38:28 +08:00
|
|
|
(*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy);
|
|
|
|
(*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy);
|
2008-10-04 04:28:15 +08:00
|
|
|
Constructor += ", " + ArgName;
|
2008-10-03 20:09:49 +08:00
|
|
|
}
|
2008-10-04 04:28:15 +08:00
|
|
|
S += FieldName + ";\n";
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
// Output all "by ref" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string FieldName = (*I)->getNameAsString();
|
2008-10-04 04:28:15 +08:00
|
|
|
std::string ArgName = "_" + FieldName;
|
|
|
|
// Handle nested closure invocation. For example:
|
|
|
|
//
|
|
|
|
// void (^myImportedBlock)(void);
|
|
|
|
// myImportedBlock = ^(void) { setGlobalInt(x + y); };
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-10-04 04:28:15 +08:00
|
|
|
// void (^anotherBlock)(void);
|
|
|
|
// anotherBlock = ^(void) {
|
|
|
|
// myImportedBlock(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
if (isBlockPointerType((*I)->getType())) {
|
2008-10-03 01:12:56 +08:00
|
|
|
S += "struct __block_impl *";
|
2008-10-04 04:28:15 +08:00
|
|
|
Constructor += ", void *" + ArgName;
|
|
|
|
} else {
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName,
|
|
|
|
Context->PrintingPolicy);
|
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName,
|
|
|
|
Context->PrintingPolicy);
|
2008-10-04 04:28:15 +08:00
|
|
|
Constructor += ", " + ArgName;
|
2008-10-03 20:09:49 +08:00
|
|
|
}
|
2008-10-04 04:28:15 +08:00
|
|
|
S += FieldName + "; // by ref\n";
|
2008-10-03 20:09:49 +08:00
|
|
|
}
|
|
|
|
// Finish writing the constructor.
|
|
|
|
// FIXME: handle NSConcreteGlobalBlock.
|
|
|
|
Constructor += ", int flags=0) {\n";
|
2008-10-03 23:04:50 +08:00
|
|
|
Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 07:47:37 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 20:09:49 +08:00
|
|
|
// Initialize all "by copy" arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
|
2008-10-03 20:09:49 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += " ";
|
2008-10-04 04:28:15 +08:00
|
|
|
if (isBlockPointerType((*I)->getType()))
|
|
|
|
Constructor += Name + " = (struct __block_impl *)_";
|
|
|
|
else
|
|
|
|
Constructor += Name + " = _";
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += Name + ";\n";
|
|
|
|
}
|
|
|
|
// Initialize all "by ref" arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
|
2008-10-03 20:09:49 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += " ";
|
2008-10-04 04:28:15 +08:00
|
|
|
if (isBlockPointerType((*I)->getType()))
|
|
|
|
Constructor += Name + " = (struct __block_impl *)_";
|
|
|
|
else
|
|
|
|
Constructor += Name + " = _";
|
2008-10-03 20:09:49 +08:00
|
|
|
Constructor += Name + ";\n";
|
|
|
|
}
|
2008-10-03 23:04:50 +08:00
|
|
|
} else {
|
|
|
|
// Finish writing the constructor.
|
|
|
|
// FIXME: handle NSConcreteGlobalBlock.
|
|
|
|
Constructor += ", int flags=0) {\n";
|
|
|
|
Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof(";
|
|
|
|
Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
|
2008-10-05 07:47:37 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
2008-10-03 23:04:50 +08:00
|
|
|
Constructor += " ";
|
|
|
|
Constructor += "}\n";
|
|
|
|
S += Constructor;
|
2008-09-17 08:13:27 +08:00
|
|
|
S += "};\n";
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::SynthesizeBlockLiterals(SourceLocation FunLocStart,
|
|
|
|
const char *FunName) {
|
|
|
|
// Insert closures that were part of the function.
|
|
|
|
for (unsigned i = 0; i < Blocks.size(); i++) {
|
2008-10-05 07:47:37 +08:00
|
|
|
|
2008-10-05 08:06:12 +08:00
|
|
|
CollectBlockDeclRefInfo(Blocks[i]);
|
|
|
|
|
2008-10-03 20:09:49 +08:00
|
|
|
std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
std::string CI = SynthesizeBlockImpl(Blocks[i], Tag,
|
2008-10-05 07:47:37 +08:00
|
|
|
ImportedBlockDecls.size() > 0);
|
2008-09-17 08:13:27 +08:00
|
|
|
|
|
|
|
InsertText(FunLocStart, CI.c_str(), CI.size());
|
2008-10-04 04:28:15 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
InsertText(FunLocStart, CF.c_str(), CF.size());
|
|
|
|
|
2008-10-04 04:28:15 +08:00
|
|
|
if (ImportedBlockDecls.size()) {
|
|
|
|
std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag);
|
|
|
|
InsertText(FunLocStart, HF.c_str(), HF.size());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockDeclRefs.clear();
|
|
|
|
BlockByRefDecls.clear();
|
|
|
|
BlockByCopyDecls.clear();
|
|
|
|
BlockCallExprs.clear();
|
2008-10-04 04:28:15 +08:00
|
|
|
ImportedBlockDecls.clear();
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
Blocks.clear();
|
2008-10-05 01:10:02 +08:00
|
|
|
RewrittenBlockExprs.clear();
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
|
2008-10-03 08:12:09 +08:00
|
|
|
SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
|
2008-11-24 11:54:41 +08:00
|
|
|
const char *FuncName = FD->getNameAsCString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
SynthesizeBlockLiterals(FunLocStart, FuncName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
|
|
|
|
SourceLocation FunLocStart = MD->getLocStart();
|
2008-11-24 11:33:13 +08:00
|
|
|
std::string FuncName = MD->getSelector().getAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
// Convert colons to underscores.
|
|
|
|
std::string::size_type loc = 0;
|
|
|
|
while ((loc = FuncName.find(":", loc)) != std::string::npos)
|
|
|
|
FuncName.replace(loc, 1, "_");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
SynthesizeBlockLiterals(FunLocStart, FuncName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::GetBlockDeclRefExprs(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
2008-10-09 01:31:13 +08:00
|
|
|
if (*CI) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
|
|
|
|
GetBlockDeclRefExprs(CBE->getBody());
|
|
|
|
else
|
|
|
|
GetBlockDeclRefExprs(*CI);
|
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
// Handle specific things.
|
|
|
|
if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S))
|
|
|
|
// FIXME: Handle enums.
|
|
|
|
if (!isa<FunctionDecl>(CDRE->getDecl()))
|
|
|
|
BlockDeclRefs.push_back(CDRE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::GetBlockCallExprs(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
2008-10-09 01:31:13 +08:00
|
|
|
if (*CI) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
|
|
|
|
GetBlockCallExprs(CBE->getBody());
|
|
|
|
else
|
|
|
|
GetBlockCallExprs(*CI);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
2008-10-04 04:28:15 +08:00
|
|
|
if (CE->getCallee()->getType()->isBlockPointerType()) {
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE;
|
2008-10-04 04:28:15 +08:00
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RewriteBlocks::SynthesizeBlockCall(CallExpr *Exp) {
|
|
|
|
// Navigate to relevant type information.
|
2008-09-25 06:46:45 +08:00
|
|
|
const char *closureName = 0;
|
|
|
|
const BlockPointerType *CPT = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) {
|
2008-11-24 11:54:41 +08:00
|
|
|
closureName = DRE->getDecl()->getNameAsCString();
|
2009-07-30 05:53:49 +08:00
|
|
|
CPT = DRE->getType()->getAs<BlockPointerType>();
|
2008-09-17 08:13:27 +08:00
|
|
|
} else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) {
|
2008-11-24 11:54:41 +08:00
|
|
|
closureName = CDRE->getDecl()->getNameAsCString();
|
2009-07-30 05:53:49 +08:00
|
|
|
CPT = CDRE->getType()->getAs<BlockPointerType>();
|
2008-10-03 23:04:50 +08:00
|
|
|
} else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) {
|
2008-11-24 11:54:41 +08:00
|
|
|
closureName = MExpr->getMemberDecl()->getNameAsCString();
|
2009-07-30 05:53:49 +08:00
|
|
|
CPT = MExpr->getType()->getAs<BlockPointerType>();
|
2008-09-17 08:13:27 +08:00
|
|
|
} else {
|
|
|
|
assert(1 && "RewriteBlockClass: Bad type");
|
|
|
|
}
|
|
|
|
assert(CPT && "RewriteBlockClass: Bad type");
|
2009-09-22 07:43:11 +08:00
|
|
|
const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
|
2008-09-17 08:13:27 +08:00
|
|
|
assert(FT && "RewriteBlockClass: Bad type");
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
|
2008-09-17 08:13:27 +08:00
|
|
|
// FTP will be null for closures that don't take arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Build a closure call - start with a paren expr to enforce precedence.
|
|
|
|
std::string BlockCall = "(";
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Synthesize the cast.
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockCall += "(" + Exp->getType().getAsString() + "(*)";
|
2008-10-03 01:12:56 +08:00
|
|
|
BlockCall += "(struct __block_impl *";
|
2008-09-17 08:13:27 +08:00
|
|
|
if (FTP) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = FTP->arg_type_end(); I && (I != E); ++I)
|
|
|
|
BlockCall += ", " + (*I).getAsString();
|
|
|
|
}
|
|
|
|
BlockCall += "))"; // close the argument list and paren expression.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
// Invoke the closure. We need to cast it since the declaration type is
|
|
|
|
// bogus (it's a function pointer type)
|
|
|
|
BlockCall += "((struct __block_impl *)";
|
|
|
|
std::string closureExprBufStr;
|
|
|
|
llvm::raw_string_ostream closureExprBuf(closureExprBufStr);
|
2009-06-30 09:26:17 +08:00
|
|
|
Exp->getCallee()->printPretty(closureExprBuf, *Context, 0,
|
|
|
|
PrintingPolicy(LangOpts));
|
2008-10-03 23:04:50 +08:00
|
|
|
BlockCall += closureExprBuf.str();
|
|
|
|
BlockCall += ")->FuncPtr)";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Add the arguments.
|
2008-10-03 23:04:50 +08:00
|
|
|
BlockCall += "((struct __block_impl *)";
|
2008-10-05 01:45:51 +08:00
|
|
|
BlockCall += closureExprBuf.str();
|
2009-09-09 23:08:12 +08:00
|
|
|
for (CallExpr::arg_iterator I = Exp->arg_begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = Exp->arg_end(); I != E; ++I) {
|
|
|
|
std::string syncExprBufS;
|
|
|
|
llvm::raw_string_ostream Buf(syncExprBufS);
|
2009-06-30 09:26:17 +08:00
|
|
|
(*I)->printPretty(Buf, *Context, 0, PrintingPolicy(LangOpts));
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockCall += ", " + Buf.str();
|
|
|
|
}
|
|
|
|
return BlockCall;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::RewriteBlockCall(CallExpr *Exp) {
|
|
|
|
std::string BlockCall = SynthesizeBlockCall(Exp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(Exp->getLocStart());
|
|
|
|
const char *endBuf = SM->getCharacterData(Exp->getLocEnd());
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ReplaceText(Exp->getLocStart(), endBuf-startBuf,
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockCall.c_str(), BlockCall.size());
|
|
|
|
}
|
|
|
|
|
2008-10-05 02:52:47 +08:00
|
|
|
void RewriteBlocks::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) {
|
|
|
|
// FIXME: Add more elaborate code generation required by the ABI.
|
|
|
|
InsertText(BDRE->getLocStart(), "*", 1);
|
|
|
|
}
|
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
void RewriteBlocks::RewriteCastExpr(CastExpr *CE) {
|
|
|
|
SourceLocation LocStart = CE->getLocStart();
|
|
|
|
SourceLocation LocEnd = CE->getLocEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-03 19:20:24 +08:00
|
|
|
if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
// advance the location to startArgList.
|
|
|
|
const char *argPtr = startBuf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
while (*argPtr++ && (argPtr < endBuf)) {
|
|
|
|
switch (*argPtr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
case '^':
|
2008-10-16 02:38:58 +08:00
|
|
|
// Replace the '^' with '*'.
|
|
|
|
LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf);
|
|
|
|
ReplaceText(LocStart, 1, "*", 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
void RewriteBlocks::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
|
|
|
|
SourceLocation DeclLoc = FD->getLocation();
|
2008-10-10 23:33:34 +08:00
|
|
|
unsigned parenCount = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// We have 1 or more arguments that have closure pointers.
|
|
|
|
const char *startBuf = SM->getCharacterData(DeclLoc);
|
|
|
|
const char *startArgList = strchr(startBuf, '(');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
parenCount++;
|
|
|
|
// advance the location to startArgList.
|
2008-10-10 23:33:34 +08:00
|
|
|
DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf);
|
2008-09-17 08:13:27 +08:00
|
|
|
assert((DeclLoc.isValid()) && "Invalid DeclLoc");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
const char *argPtr = startArgList;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
while (*argPtr++ && parenCount) {
|
|
|
|
switch (*argPtr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
case '^':
|
2008-10-10 23:33:34 +08:00
|
|
|
// Replace the '^' with '*'.
|
|
|
|
DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList);
|
|
|
|
ReplaceText(DeclLoc, 1, "*", 1);
|
2008-09-17 08:13:27 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case '(':
|
|
|
|
parenCount++;
|
2008-09-17 08:13:27 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case ')':
|
2008-09-17 08:13:27 +08:00
|
|
|
parenCount--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
bool RewriteBlocks::PointerTypeTakesAnyBlockArguments(QualType QT) {
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *FTP;
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = QT->getAs<PointerType>();
|
2008-10-16 02:38:58 +08:00
|
|
|
if (PT) {
|
2009-09-22 07:43:11 +08:00
|
|
|
FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
|
2008-10-16 02:38:58 +08:00
|
|
|
} else {
|
2009-07-30 05:53:49 +08:00
|
|
|
const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
|
2008-10-16 02:38:58 +08:00
|
|
|
assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
|
2009-09-22 07:43:11 +08:00
|
|
|
FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
|
2008-10-16 02:38:58 +08:00
|
|
|
}
|
2008-09-24 03:24:41 +08:00
|
|
|
if (FTP) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
|
2008-09-24 03:24:41 +08:00
|
|
|
E = FTP->arg_type_end(); I != E; ++I)
|
|
|
|
if (isBlockPointerType(*I))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteBlocks::GetExtentOfArgList(const char *Name,
|
2008-09-25 01:22:34 +08:00
|
|
|
const char *&LParen, const char *&RParen) {
|
|
|
|
const char *argPtr = strchr(Name, '(');
|
2008-09-24 03:24:41 +08:00
|
|
|
assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-24 03:24:41 +08:00
|
|
|
LParen = argPtr; // output the start.
|
|
|
|
argPtr++; // skip past the left paren.
|
|
|
|
unsigned parenCount = 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-24 03:24:41 +08:00
|
|
|
while (*argPtr && parenCount) {
|
|
|
|
switch (*argPtr) {
|
|
|
|
case '(': parenCount++; break;
|
|
|
|
case ')': parenCount--; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (parenCount) argPtr++;
|
|
|
|
}
|
|
|
|
assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
|
|
|
|
RParen = argPtr; // output the end
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
void RewriteBlocks::RewriteBlockPointerDecl(NamedDecl *ND) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
|
|
|
|
RewriteBlockPointerFunctionArgs(FD);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-09-24 05:15:53 +08:00
|
|
|
// Handle Variables and Typedefs.
|
|
|
|
SourceLocation DeclLoc = ND->getLocation();
|
|
|
|
QualType DeclT;
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(ND))
|
|
|
|
DeclT = VD->getType();
|
|
|
|
else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND))
|
|
|
|
DeclT = TDD->getUnderlyingType();
|
2008-10-03 23:04:50 +08:00
|
|
|
else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
|
|
|
|
DeclT = FD->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2008-09-24 05:15:53 +08:00
|
|
|
assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-24 05:15:53 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(DeclLoc);
|
|
|
|
const char *endBuf = startBuf;
|
|
|
|
// scan backward (from the decl location) for the end of the previous decl.
|
|
|
|
while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
|
|
|
|
startBuf--;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 02:38:58 +08:00
|
|
|
// *startBuf != '^' if we are dealing with a pointer to function that
|
|
|
|
// may take block argument types (which will be handled below).
|
|
|
|
if (*startBuf == '^') {
|
|
|
|
// Replace the '^' with '*', computing a negative offset.
|
|
|
|
DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf);
|
|
|
|
ReplaceText(DeclLoc, 1, "*", 1);
|
|
|
|
}
|
|
|
|
if (PointerTypeTakesAnyBlockArguments(DeclT)) {
|
2008-09-24 05:15:53 +08:00
|
|
|
// Replace the '^' with '*' for arguments.
|
|
|
|
DeclLoc = ND->getLocation();
|
2008-09-17 08:13:27 +08:00
|
|
|
startBuf = SM->getCharacterData(DeclLoc);
|
2008-09-25 01:22:34 +08:00
|
|
|
const char *argListBegin, *argListEnd;
|
2008-09-24 05:15:53 +08:00
|
|
|
GetExtentOfArgList(startBuf, argListBegin, argListEnd);
|
|
|
|
while (argListBegin < argListEnd) {
|
|
|
|
if (*argListBegin == '^') {
|
|
|
|
SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf);
|
|
|
|
ReplaceText(CaretLoc, 1, "*", 1);
|
|
|
|
}
|
|
|
|
argListBegin++;
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
2008-09-24 03:24:41 +08:00
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteBlocks::CollectBlockDeclRefInfo(BlockExpr *Exp) {
|
2008-09-17 08:13:27 +08:00
|
|
|
// Add initializers for any closure decl refs.
|
2008-10-09 01:31:13 +08:00
|
|
|
GetBlockDeclRefExprs(Exp->getBody());
|
2008-09-17 08:13:27 +08:00
|
|
|
if (BlockDeclRefs.size()) {
|
|
|
|
// Unique all "by copy" declarations.
|
|
|
|
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
|
|
|
|
if (!BlockDeclRefs[i]->isByRef())
|
|
|
|
BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
|
|
|
|
// Unique all "by ref" declarations.
|
|
|
|
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
|
|
|
|
if (BlockDeclRefs[i]->isByRef()) {
|
|
|
|
BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
|
|
|
|
}
|
2008-10-05 07:47:37 +08:00
|
|
|
// Find any imported blocks...they will need special attention.
|
|
|
|
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
|
|
|
|
if (isBlockPointerType(BlockDeclRefs[i]->getType())) {
|
|
|
|
GetBlockCallExprs(Blocks[i]);
|
|
|
|
ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
|
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
2008-10-05 08:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string RewriteBlocks::SynthesizeBlockInitExpr(BlockExpr *Exp, VarDecl *VD) {
|
|
|
|
Blocks.push_back(Exp);
|
|
|
|
|
|
|
|
CollectBlockDeclRefInfo(Exp);
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string FuncName;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
if (CurFunctionDef)
|
2008-11-24 13:29:24 +08:00
|
|
|
FuncName = std::string(CurFunctionDef->getNameAsString());
|
2008-09-17 08:13:27 +08:00
|
|
|
else if (CurMethodDef) {
|
2008-11-24 11:33:13 +08:00
|
|
|
FuncName = CurMethodDef->getSelector().getAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
// Convert colons to underscores.
|
|
|
|
std::string::size_type loc = 0;
|
|
|
|
while ((loc = FuncName.find(":", loc)) != std::string::npos)
|
|
|
|
FuncName.replace(loc, 1, "_");
|
2008-10-03 23:38:09 +08:00
|
|
|
} else if (VD)
|
2008-11-24 13:29:24 +08:00
|
|
|
FuncName = std::string(VD->getNameAsString());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
std::string BlockNumber = utostr(Blocks.size()-1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
|
2008-10-03 07:30:43 +08:00
|
|
|
std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
std::string FunkTypeStr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
// Get a pointer to the function type so we can cast appropriately.
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->getPointerType(QualType(Exp->getFunctionType(),0))
|
|
|
|
.getAsStringInternal(FunkTypeStr, Context->PrintingPolicy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-17 08:13:27 +08:00
|
|
|
// Rewrite the closure block with a compound literal. The first cast is
|
|
|
|
// to prevent warnings from the C compiler.
|
2008-10-03 23:04:50 +08:00
|
|
|
std::string Init = "(" + FunkTypeStr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
Init += ")&" + Tag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-03 23:04:50 +08:00
|
|
|
// Initialize the block function.
|
|
|
|
Init += "((void*)" + Func;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 07:47:37 +08:00
|
|
|
if (ImportedBlockDecls.size()) {
|
|
|
|
std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber;
|
|
|
|
Init += ",(void*)" + Buf;
|
|
|
|
Buf = "__" + FuncName + "_block_dispose_" + BlockNumber;
|
|
|
|
Init += ",(void*)" + Buf;
|
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
// Add initializers for any closure decl refs.
|
|
|
|
if (BlockDeclRefs.size()) {
|
|
|
|
// Output all "by copy" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
Init += ",";
|
|
|
|
if (isObjCType((*I)->getType())) {
|
|
|
|
Init += "[[";
|
2008-11-24 13:29:24 +08:00
|
|
|
Init += (*I)->getNameAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
Init += " retain] autorelease]";
|
2008-10-04 04:28:15 +08:00
|
|
|
} else if (isBlockPointerType((*I)->getType())) {
|
|
|
|
Init += "(void *)";
|
2008-11-24 13:29:24 +08:00
|
|
|
Init += (*I)->getNameAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
} else {
|
2008-11-24 13:29:24 +08:00
|
|
|
Init += (*I)->getNameAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Output all "by ref" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
|
2008-09-17 08:13:27 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
|
|
|
Init += ",&";
|
2008-11-24 13:29:24 +08:00
|
|
|
Init += (*I)->getNameAsString();
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|
|
|
|
}
|
2008-10-03 23:04:50 +08:00
|
|
|
Init += ")";
|
2008-09-17 08:13:27 +08:00
|
|
|
BlockDeclRefs.clear();
|
|
|
|
BlockByRefDecls.clear();
|
|
|
|
BlockByCopyDecls.clear();
|
2008-10-04 04:28:15 +08:00
|
|
|
ImportedBlockDecls.clear();
|
|
|
|
|
2008-10-05 01:06:23 +08:00
|
|
|
return Init;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Body / Expression rewriting
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Stmt *RewriteBlocks::RewriteFunctionBody(Stmt *S) {
|
|
|
|
// Start by rewriting all children.
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
|
2009-06-08 12:24:21 +08:00
|
|
|
RewriteFunctionBody(CBE->getBody());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 01:06:23 +08:00
|
|
|
// We've just rewritten the block body in place.
|
|
|
|
// Now we snarf the rewritten text and stash it away for later use.
|
|
|
|
std::string S = Rewrite.getRewritenText(CBE->getSourceRange());
|
|
|
|
RewrittenBlockExprs[CBE] = S;
|
|
|
|
std::string Init = SynthesizeBlockInitExpr(CBE);
|
|
|
|
// Do the rewrite, using S.size() which contains the rewritten size.
|
|
|
|
ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size());
|
|
|
|
} else {
|
2009-06-08 12:24:21 +08:00
|
|
|
RewriteFunctionBody(*CI);
|
2008-10-05 01:06:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Handle specific things.
|
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
|
|
|
if (CE->getCallee()->getType()->isBlockPointerType())
|
|
|
|
RewriteBlockCall(CE);
|
|
|
|
}
|
2008-10-16 02:38:58 +08:00
|
|
|
if (CastExpr *CE = dyn_cast<CastExpr>(S)) {
|
|
|
|
RewriteCastExpr(CE);
|
|
|
|
}
|
2008-10-05 01:06:23 +08:00
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
|
2008-10-07 02:47:09 +08:00
|
|
|
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
|
|
|
|
DI != DE; ++DI) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-20 09:17:11 +08:00
|
|
|
Decl *SD = *DI;
|
2008-10-07 02:47:09 +08:00
|
|
|
if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
|
|
|
|
if (isBlockPointerType(ND->getType()))
|
|
|
|
RewriteBlockPointerDecl(ND);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (ND->getType()->isFunctionPointerType())
|
2008-10-16 02:38:58 +08:00
|
|
|
CheckFunctionPointerDecl(ND->getType(), ND);
|
2008-10-07 02:47:09 +08:00
|
|
|
}
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
|
|
|
if (isBlockPointerType(TD->getUnderlyingType()))
|
|
|
|
RewriteBlockPointerDecl(TD);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (TD->getUnderlyingType()->isFunctionPointerType())
|
2008-10-16 02:38:58 +08:00
|
|
|
CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
|
2008-10-07 02:47:09 +08:00
|
|
|
}
|
2008-10-05 01:06:23 +08:00
|
|
|
}
|
|
|
|
}
|
2008-10-05 02:52:47 +08:00
|
|
|
// Handle specific things.
|
|
|
|
if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
|
|
|
|
if (BDRE->isByRef())
|
|
|
|
RewriteBlockDeclRefExpr(BDRE);
|
|
|
|
}
|
2008-10-05 01:06:23 +08:00
|
|
|
// Return this stmt unmodified.
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteBlocks::RewriteFunctionProtoType(QualType funcType, NamedDecl *D) {
|
2009-02-27 07:50:07 +08:00
|
|
|
if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(),
|
2008-10-16 02:38:58 +08:00
|
|
|
E = fproto->arg_type_end(); I && (I != E); ++I)
|
|
|
|
if (isBlockPointerType(*I)) {
|
|
|
|
// All the args are checked/rewritten. Don't call twice!
|
|
|
|
RewriteBlockPointerDecl(D);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBlocks::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = funcType->getAs<PointerType>();
|
2008-10-16 02:38:58 +08:00
|
|
|
if (PT && PointerTypeTakesAnyBlockArguments(funcType))
|
2009-02-27 07:50:07 +08:00
|
|
|
RewriteFunctionProtoType(PT->getPointeeType(), ND);
|
2008-10-16 02:38:58 +08:00
|
|
|
}
|
|
|
|
|
2008-10-05 01:06:23 +08:00
|
|
|
/// HandleDeclInMainFile - This is called for each top-level decl defined in the
|
|
|
|
/// main file of the input.
|
|
|
|
void RewriteBlocks::HandleDeclInMainFile(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// Since function prototypes don't have ParmDecl's, we check the function
|
|
|
|
// prototype. This enables us to rewrite function declarations and
|
|
|
|
// definitions using the same code.
|
2009-02-27 07:50:07 +08:00
|
|
|
RewriteFunctionProtoType(FD->getType(), FD);
|
2009-04-27 04:35:05 +08:00
|
|
|
|
|
|
|
// FIXME: Handle CXXTryStmt
|
2009-06-30 10:35:26 +08:00
|
|
|
if (CompoundStmt *Body = FD->getCompoundBody()) {
|
2008-10-05 01:06:23 +08:00
|
|
|
CurFunctionDef = FD;
|
2009-03-13 02:33:24 +08:00
|
|
|
FD->setBody(cast_or_null<CompoundStmt>(RewriteFunctionBody(Body)));
|
2008-10-05 01:06:23 +08:00
|
|
|
// This synthesizes and inserts the block "impl" struct, invoke function,
|
|
|
|
// and any copy/dispose helper functions.
|
|
|
|
InsertBlockLiteralsWithinFunction(FD);
|
|
|
|
CurFunctionDef = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-05 01:06:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
RewriteMethodDecl(MD);
|
2009-06-30 10:35:26 +08:00
|
|
|
if (Stmt *Body = MD->getBody()) {
|
2008-10-05 01:06:23 +08:00
|
|
|
CurMethodDef = MD;
|
|
|
|
RewriteFunctionBody(Body);
|
|
|
|
InsertBlockLiteralsWithinMethod(MD);
|
|
|
|
CurMethodDef = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (isBlockPointerType(VD->getType())) {
|
|
|
|
RewriteBlockPointerDecl(VD);
|
|
|
|
if (VD->getInit()) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(VD->getInit())) {
|
2009-06-30 10:35:26 +08:00
|
|
|
RewriteFunctionBody(CBE->getBody());
|
2008-10-05 01:06:23 +08:00
|
|
|
|
|
|
|
// We've just rewritten the block body in place.
|
|
|
|
// Now we snarf the rewritten text and stash it away for later use.
|
|
|
|
std::string S = Rewrite.getRewritenText(CBE->getSourceRange());
|
|
|
|
RewrittenBlockExprs[CBE] = S;
|
|
|
|
std::string Init = SynthesizeBlockInitExpr(CBE, VD);
|
|
|
|
// Do the rewrite, using S.size() which contains the rewritten size.
|
|
|
|
ReplaceText(CBE->getLocStart(), S.size(), Init.c_str(), Init.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(),
|
2008-11-24 11:54:41 +08:00
|
|
|
VD->getNameAsCString());
|
2008-10-16 02:38:58 +08:00
|
|
|
} else if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) {
|
|
|
|
RewriteCastExpr(CE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (VD->getType()->isFunctionPointerType()) {
|
|
|
|
CheckFunctionPointerDecl(VD->getType(), VD);
|
|
|
|
if (VD->getInit()) {
|
|
|
|
if (CastExpr *CE = dyn_cast<CastExpr>(VD->getInit())) {
|
|
|
|
RewriteCastExpr(CE);
|
2008-10-05 01:06:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
if (isBlockPointerType(TD->getUnderlyingType()))
|
|
|
|
RewriteBlockPointerDecl(TD);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (TD->getUnderlyingType()->isFunctionPointerType())
|
2008-10-16 02:38:58 +08:00
|
|
|
CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
|
2008-10-05 01:06:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
|
|
|
|
if (RD->isDefinition()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
2009-06-30 10:36:12 +08:00
|
|
|
e = RD->field_end(); i != e; ++i) {
|
2008-10-05 01:06:23 +08:00
|
|
|
FieldDecl *FD = *i;
|
|
|
|
if (isBlockPointerType(FD->getType()))
|
|
|
|
RewriteBlockPointerDecl(FD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-09-17 08:13:27 +08:00
|
|
|
}
|