2008-04-15 06:03:09 +08:00
|
|
|
//===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===//
|
2007-10-11 08:43:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-11 08:43:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Hacks and fun related to the code rewriter.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 06:50:54 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2007-10-12 02:38:32 +08:00
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
2007-10-11 08:43:27 +08:00
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2008-12-09 00:43:47 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2007-10-12 02:38:32 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-10-24 07:50:29 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2007-12-01 06:53:43 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-12-02 09:13:47 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-10-26 01:07:24 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-10-27 03:46:17 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2007-10-11 08:43:27 +08:00
|
|
|
using namespace clang;
|
2007-10-26 01:07:24 +08:00
|
|
|
using llvm::utostr;
|
2007-10-11 08:43:27 +08:00
|
|
|
|
|
|
|
namespace {
|
2008-04-15 06:03:09 +08:00
|
|
|
class RewriteObjC : public ASTConsumer {
|
2007-10-17 05:07:07 +08:00
|
|
|
Rewriter Rewrite;
|
2007-12-01 06:25:36 +08:00
|
|
|
Diagnostic &Diags;
|
2008-03-11 04:43:59 +08:00
|
|
|
const LangOptions &LangOpts;
|
2008-01-31 03:17:43 +08:00
|
|
|
unsigned RewriteFailedDiag;
|
2008-12-06 01:03:39 +08:00
|
|
|
unsigned TryFinallyContainsReturnDiag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-18 06:35:30 +08:00
|
|
|
ASTContext *Context;
|
2007-10-11 08:43:27 +08:00
|
|
|
SourceManager *SM;
|
2008-04-17 22:40:12 +08:00
|
|
|
TranslationUnitDecl *TUDecl;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID MainFileID;
|
2007-12-02 09:13:47 +08:00
|
|
|
const char *MainFileStart, *MainFileEnd;
|
2007-10-17 05:07:07 +08:00
|
|
|
SourceLocation LastIncLoc;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
|
|
|
|
llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
|
|
|
|
llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
|
2008-05-07 02:26:51 +08:00
|
|
|
llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols;
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls;
|
|
|
|
llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
|
2008-01-16 07:58:23 +08:00
|
|
|
llvm::SmallVector<Stmt *, 32> Stmts;
|
|
|
|
llvm::SmallVector<int, 8> ObjCBcLabelNo;
|
2009-04-30 00:37:50 +08:00
|
|
|
// Remember all the @protocol(<expr>) expressions.
|
|
|
|
llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-15 08:55:56 +08:00
|
|
|
unsigned NumObjCStringLiterals;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 07:50:29 +08:00
|
|
|
FunctionDecl *MsgSendFunctionDecl;
|
2007-11-15 18:28:18 +08:00
|
|
|
FunctionDecl *MsgSendSuperFunctionDecl;
|
2007-12-04 03:17:29 +08:00
|
|
|
FunctionDecl *MsgSendStretFunctionDecl;
|
|
|
|
FunctionDecl *MsgSendSuperStretFunctionDecl;
|
2007-12-04 05:26:48 +08:00
|
|
|
FunctionDecl *MsgSendFpretFunctionDecl;
|
2007-10-24 07:50:29 +08:00
|
|
|
FunctionDecl *GetClassFunctionDecl;
|
2007-12-07 11:50:46 +08:00
|
|
|
FunctionDecl *GetMetaClassFunctionDecl;
|
2007-10-25 06:48:43 +08:00
|
|
|
FunctionDecl *SelGetUidFunctionDecl;
|
2007-11-08 22:30:50 +08:00
|
|
|
FunctionDecl *CFStringFunctionDecl;
|
2008-03-12 01:37:02 +08:00
|
|
|
FunctionDecl *SuperContructorFunctionDecl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-03 19:27:19 +08:00
|
|
|
// ObjC string constant support.
|
2008-04-16 06:42:06 +08:00
|
|
|
VarDecl *ConstantStringClassReference;
|
2007-11-03 19:27:19 +08:00
|
|
|
RecordDecl *NSStringRecord;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-16 08:09:11 +08:00
|
|
|
// ObjC foreach break/continue generation support.
|
2008-01-16 07:58:23 +08:00
|
|
|
int BcLabelCount;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// Needed for super.
|
2008-10-28 01:20:55 +08:00
|
|
|
ObjCMethodDecl *CurMethodDef;
|
2007-11-15 18:28:18 +08:00
|
|
|
RecordDecl *SuperStructDecl;
|
2008-03-15 08:55:56 +08:00
|
|
|
RecordDecl *ConstantStringDecl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
TypeDecl *ProtocolTypeDecl;
|
|
|
|
QualType getProtocolType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-18 09:15:54 +08:00
|
|
|
// Needed for header files being rewritten
|
|
|
|
bool IsHeader;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-29 06:26:09 +08:00
|
|
|
std::string InFileName;
|
2009-05-19 06:20:00 +08:00
|
|
|
llvm::raw_ostream* OutFile;
|
2009-05-19 06:39:16 +08:00
|
|
|
|
|
|
|
bool SilenceRewriteMacroWarning;
|
|
|
|
|
2008-03-28 06:29:16 +08:00
|
|
|
std::string Preamble;
|
2008-10-28 01:20:55 +08:00
|
|
|
|
|
|
|
// 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-10-28 01:20:55 +08:00
|
|
|
// Block related declarations.
|
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls;
|
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls;
|
|
|
|
llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
|
|
|
|
|
|
|
|
llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
|
|
|
|
|
2008-12-05 00:24:46 +08:00
|
|
|
// This maps a property to it's assignment statement.
|
|
|
|
llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters;
|
2008-12-09 00:43:47 +08:00
|
|
|
// This maps a property to it's synthesied message expression.
|
|
|
|
// This allows us to rewrite chained getters (e.g. o.a.b.c).
|
|
|
|
llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-05 07:50:32 +08:00
|
|
|
// This maps an original source AST to it's rewritten form. This allows
|
|
|
|
// us to avoid rewriting the same node twice (which is very uncommon).
|
|
|
|
// This is needed to support some of the exotic property rewriting.
|
|
|
|
llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes;
|
2008-12-03 08:56:33 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
FunctionDecl *CurFunctionDef;
|
2008-10-30 02:15:37 +08:00
|
|
|
VarDecl *GlobalVarDecl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 20:56:34 +08:00
|
|
|
bool DisableReplaceStmt;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-19 03:23:00 +08:00
|
|
|
static const int OBJC_ABI_VERSION =7 ;
|
2007-10-11 08:43:27 +08:00
|
|
|
public:
|
2008-06-01 04:11:04 +08:00
|
|
|
virtual void Initialize(ASTContext &context);
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// 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);
|
2007-10-17 05:07:07 +08:00
|
|
|
void HandleDeclInMainFile(Decl *D);
|
2009-05-19 06:20:00 +08:00
|
|
|
RewriteObjC(std::string inFile, llvm::raw_ostream *OS,
|
2009-05-19 06:39:16 +08:00
|
|
|
Diagnostic &D, const LangOptions &LOpts,
|
|
|
|
bool silenceMacroWarn);
|
2008-08-08 12:15:52 +08:00
|
|
|
|
|
|
|
~RewriteObjC() {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:37:57 +08:00
|
|
|
void ReplaceStmt(Stmt *Old, Stmt *New) {
|
2008-12-05 07:50:32 +08:00
|
|
|
Stmt *ReplacingStmt = ReplacedNodes[Old];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-05 07:50:32 +08:00
|
|
|
if (ReplacingStmt)
|
|
|
|
return; // We can't rewrite the same node twice.
|
|
|
|
|
2008-12-09 20:56:34 +08:00
|
|
|
if (DisableReplaceStmt)
|
|
|
|
return; // Used when rewriting the assignment of a property setter.
|
|
|
|
|
2008-02-01 03:42:41 +08:00
|
|
|
// If replacement succeeded or warning disabled return with no warning.
|
2008-12-05 07:50:32 +08:00
|
|
|
if (!Rewrite.ReplaceStmt(Old, New)) {
|
|
|
|
ReplacedNodes[Old] = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SilenceRewriteMacroWarning)
|
2008-02-01 03:37:57 +08:00
|
|
|
return;
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
|
|
|
|
<< Old->getSourceRange();
|
2008-02-01 03:37:57 +08:00
|
|
|
}
|
2008-12-09 20:56:34 +08:00
|
|
|
|
|
|
|
void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
|
|
|
|
// Measaure the old text.
|
|
|
|
int Size = Rewrite.getRangeSize(SrcRange);
|
|
|
|
if (Size == -1) {
|
|
|
|
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
|
|
|
|
<< Old->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get the new text.
|
|
|
|
std::string SStr;
|
|
|
|
llvm::raw_string_ostream S(SStr);
|
2009-06-30 09:26:17 +08:00
|
|
|
New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts));
|
2008-12-09 20:56:34 +08:00
|
|
|
const std::string &Str = S.str();
|
|
|
|
|
|
|
|
// If replacement succeeded or warning disabled return with no warning.
|
2009-08-20 03:10:30 +08:00
|
|
|
if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
|
2008-12-09 20:56:34 +08:00
|
|
|
ReplacedNodes[Old] = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SilenceRewriteMacroWarning)
|
|
|
|
return;
|
|
|
|
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
|
|
|
|
<< Old->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2008-03-28 06:29:16 +08:00
|
|
|
void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen,
|
|
|
|
bool InsertAfter = true) {
|
2008-02-01 03:51:04 +08:00
|
|
|
// If insertion succeeded or warning disabled return with no warning.
|
2009-08-20 03:10:30 +08:00
|
|
|
if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen),
|
|
|
|
InsertAfter) ||
|
2008-02-01 03:42:41 +08:00
|
|
|
SilenceRewriteMacroWarning)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:42:41 +08:00
|
|
|
Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
void RemoveText(SourceLocation Loc, unsigned StrLen) {
|
|
|
|
// If removal succeeded or warning disabled return with no warning.
|
|
|
|
if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
|
|
|
|
}
|
2007-10-25 01:06:59 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
void ReplaceText(SourceLocation Start, unsigned OrigLength,
|
|
|
|
const char *NewStr, unsigned NewLength) {
|
|
|
|
// If removal succeeded or warning disabled return with no warning.
|
2009-08-20 03:10:30 +08:00
|
|
|
if (!Rewrite.ReplaceText(Start, OrigLength,
|
|
|
|
llvm::StringRef(NewStr, NewLength)) ||
|
2008-02-01 03:51:04 +08:00
|
|
|
SilenceRewriteMacroWarning)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Syntactic Rewriting.
|
2007-11-05 06:37:50 +08:00
|
|
|
void RewritePrologue(SourceLocation Loc);
|
2008-01-19 08:30:35 +08:00
|
|
|
void RewriteInclude();
|
2007-10-25 01:06:59 +08:00
|
|
|
void RewriteTabs();
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteForwardClassDecl(ObjCClassDecl *Dcl);
|
2008-12-03 01:36:43 +08:00
|
|
|
void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
|
|
|
|
ObjCImplementationDecl *IMD,
|
|
|
|
ObjCCategoryImplDecl *CID);
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
|
2009-01-20 09:17:11 +08:00
|
|
|
void RewriteImplementationDecl(Decl *Dcl);
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr);
|
|
|
|
void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
|
|
|
|
void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
|
|
|
|
void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl);
|
|
|
|
void RewriteMethodDeclaration(ObjCMethodDecl *Method);
|
2009-01-11 09:06:09 +08:00
|
|
|
void RewriteProperty(ObjCPropertyDecl *prop);
|
2007-10-31 07:14:51 +08:00
|
|
|
void RewriteFunctionDecl(FunctionDecl *FD);
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
|
2008-07-30 02:15:38 +08:00
|
|
|
void RewriteObjCQualifiedInterfaceTypes(Expr *E);
|
2007-11-01 21:24:47 +08:00
|
|
|
bool needToScanForQualifiers(QualType T);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr);
|
2007-11-15 18:28:18 +08:00
|
|
|
QualType getSuperStructType();
|
2008-03-15 08:55:56 +08:00
|
|
|
QualType getConstantStringStructType();
|
2008-05-31 22:15:04 +08:00
|
|
|
bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Expression Rewriting.
|
2007-11-09 23:20:18 +08:00
|
|
|
Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
|
2008-12-05 00:24:46 +08:00
|
|
|
void CollectPropertySetters(Stmt *S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 00:43:47 +08:00
|
|
|
Stmt *CurrentBody;
|
|
|
|
ParentMap *PropParentMap; // created lazily.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 00:57:36 +08:00
|
|
|
Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
|
2008-05-24 04:40:52 +08:00
|
|
|
Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart);
|
2008-12-05 00:24:46 +08:00
|
|
|
Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt,
|
2008-12-09 20:56:34 +08:00
|
|
|
SourceRange SrcRange);
|
2007-11-05 22:50:49 +08:00
|
|
|
Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
|
2007-10-25 00:57:36 +08:00
|
|
|
Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
|
2007-11-03 19:27:19 +08:00
|
|
|
Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
|
2007-12-08 02:47:10 +08:00
|
|
|
Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
|
2008-12-06 01:03:39 +08:00
|
|
|
void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S);
|
2008-01-08 03:49:32 +08:00
|
|
|
Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
|
2008-01-30 06:59:37 +08:00
|
|
|
Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
|
2008-01-08 03:49:32 +08:00
|
|
|
Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S);
|
|
|
|
Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S);
|
|
|
|
Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
|
2008-01-31 13:10:40 +08:00
|
|
|
Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
|
|
|
|
SourceLocation OrigEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
|
2007-10-25 06:48:43 +08:00
|
|
|
Expr **args, unsigned nargs);
|
2008-01-09 06:06:28 +08:00
|
|
|
Stmt *SynthMessageExpr(ObjCMessageExpr *Exp);
|
2008-01-16 07:58:23 +08:00
|
|
|
Stmt *RewriteBreakStmt(BreakStmt *S);
|
|
|
|
Stmt *RewriteContinueStmt(ContinueStmt *S);
|
2008-01-09 06:06:28 +08:00
|
|
|
void SynthCountByEnumWithState(std::string &buf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-31 07:14:51 +08:00
|
|
|
void SynthMsgSendFunctionDecl();
|
2007-11-15 18:28:18 +08:00
|
|
|
void SynthMsgSendSuperFunctionDecl();
|
2007-12-04 03:17:29 +08:00
|
|
|
void SynthMsgSendStretFunctionDecl();
|
2007-12-04 05:26:48 +08:00
|
|
|
void SynthMsgSendFpretFunctionDecl();
|
2007-12-04 03:17:29 +08:00
|
|
|
void SynthMsgSendSuperStretFunctionDecl();
|
2007-10-31 07:14:51 +08:00
|
|
|
void SynthGetClassFunctionDecl();
|
2007-12-07 11:50:46 +08:00
|
|
|
void SynthGetMetaClassFunctionDecl();
|
2007-12-05 05:47:40 +08:00
|
|
|
void SynthSelGetUidFunctionDecl();
|
2008-03-12 01:37:02 +08:00
|
|
|
void SynthSuperContructorFunctionDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Metadata emission.
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
|
2007-10-26 04:55:25 +08:00
|
|
|
std::string &Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
|
2007-10-26 04:55:25 +08:00
|
|
|
std::string &Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-23 09:02:12 +08:00
|
|
|
template<typename MethodIterator>
|
|
|
|
void RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
|
|
|
|
MethodIterator MethodEnd,
|
2007-10-25 08:14:44 +08:00
|
|
|
bool IsInstanceMethod,
|
2007-10-25 03:23:36 +08:00
|
|
|
const char *prefix,
|
2007-10-26 01:07:24 +08:00
|
|
|
const char *ClassName,
|
|
|
|
std::string &Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
|
|
|
|
const char *prefix,
|
|
|
|
const char *ClassName,
|
|
|
|
std::string &Result);
|
|
|
|
void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots,
|
2009-09-09 23:08:12 +08:00
|
|
|
const char *prefix,
|
2009-04-30 00:37:50 +08:00
|
|
|
const char *ClassName,
|
|
|
|
std::string &Result);
|
2008-01-08 03:49:32 +08:00
|
|
|
void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
|
2007-10-27 03:46:17 +08:00
|
|
|
std::string &Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
|
|
|
|
ObjCIvarDecl *ivar,
|
2007-10-27 03:46:17 +08:00
|
|
|
std::string &Result);
|
2008-11-14 04:07:04 +08:00
|
|
|
void RewriteImplementations();
|
|
|
|
void SynthesizeMetaDataIntoBuffer(std::string &Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
// Block rewriting.
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D);
|
2008-10-28 01:20:55 +08:00
|
|
|
void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
|
|
|
|
void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Block specific rewrite rules.
|
2008-10-28 01:20:55 +08:00
|
|
|
void RewriteBlockCall(CallExpr *Exp);
|
|
|
|
void RewriteBlockPointerDecl(NamedDecl *VD);
|
2009-04-30 00:37:50 +08:00
|
|
|
Stmt *RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD);
|
2008-10-28 01:20:55 +08:00
|
|
|
void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
|
2008-10-28 01:20:55 +08:00
|
|
|
const char *funcName, std::string Tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
|
2008-10-28 01:20:55 +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-28 01:20:55 +08:00
|
|
|
bool hasCopyDisposeHelpers);
|
2008-10-30 18:07:53 +08:00
|
|
|
Stmt *SynthesizeBlockCall(CallExpr *Exp);
|
2008-10-28 01:20:55 +08:00
|
|
|
void SynthesizeBlockLiterals(SourceLocation FunLocStart,
|
|
|
|
const char *FunName);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
void CollectBlockDeclRefInfo(BlockExpr *Exp);
|
|
|
|
void GetBlockCallExprs(Stmt *S);
|
|
|
|
void GetBlockDeclRefExprs(Stmt *S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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.
|
2009-02-07 09:47:29 +08:00
|
|
|
bool isTopLevelBlockPointerType(QualType T) {
|
|
|
|
return isa<BlockPointerType>(T);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool PointerTypeTakesAnyBlockArguments(QualType QT);
|
2009-02-07 09:47:29 +08:00
|
|
|
void GetExtentOfArgList(const char *Name, const char *&LParen,
|
|
|
|
const char *&RParen);
|
2008-11-04 07:29:32 +08:00
|
|
|
void RewriteCastExpr(CStyleCastExpr *CE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
FunctionDecl *SynthBlockInitFunctionDecl(const char *name);
|
2008-10-30 02:15:37 +08:00
|
|
|
Stmt *SynthBlockInitExpr(BlockExpr *Exp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
void QuoteDoublequotes(std::string &From, std::string &To) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (unsigned i = 0; i < From.length(); i++) {
|
2009-04-30 00:37:50 +08:00
|
|
|
if (From[i] == '"')
|
|
|
|
To += "\\\"";
|
|
|
|
else
|
|
|
|
To += From[i];
|
|
|
|
}
|
|
|
|
}
|
2007-10-11 08:43:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::RewriteBlocksInFunctionProtoType(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-28 01:20:55 +08:00
|
|
|
E = fproto->arg_type_end(); I && (I != E); ++I)
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(*I)) {
|
2008-10-28 01:20:55 +08:00
|
|
|
// All the args are checked/rewritten. Don't call twice!
|
|
|
|
RewriteBlockPointerDecl(D);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = funcType->getAs<PointerType>();
|
2008-10-28 01:20:55 +08:00
|
|
|
if (PT && PointerTypeTakesAnyBlockArguments(funcType))
|
2009-02-27 07:50:07 +08:00
|
|
|
RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
|
2008-01-18 09:15:54 +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-01-18 09:15:54 +08:00
|
|
|
if (DotPos == std::string::npos) {
|
|
|
|
// no file extension
|
2009-09-09 23:08:12 +08:00
|
|
|
return false;
|
2008-01-18 09:15:54 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-18 09:15:54 +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-01-18 09:15:54 +08:00
|
|
|
|
2009-05-19 06:20:00 +08:00
|
|
|
RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS,
|
2009-05-19 06:39:16 +08:00
|
|
|
Diagnostic &D, const LangOptions &LOpts,
|
|
|
|
bool silenceMacroWarn)
|
|
|
|
: Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS),
|
|
|
|
SilenceRewriteMacroWarning(silenceMacroWarn) {
|
2008-03-29 06:26:09 +08:00
|
|
|
IsHeader = IsHeaderFile(inFile);
|
2009-09-09 23:08:12 +08:00
|
|
|
RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
|
2008-03-29 06:26:09 +08:00
|
|
|
"rewriting sub-expression within a macro (may not be correct)");
|
2009-09-09 23:08:12 +08:00
|
|
|
TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning,
|
2009-02-07 09:47:29 +08:00
|
|
|
"rewriter doesn't support user-specified control flow semantics "
|
|
|
|
"for @try/@finally (code may not execute properly)");
|
2008-03-29 06:26:09 +08:00
|
|
|
}
|
|
|
|
|
2009-05-19 06:29:17 +08:00
|
|
|
ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile,
|
|
|
|
llvm::raw_ostream* OS,
|
2009-09-09 23:08:12 +08:00
|
|
|
Diagnostic &Diags,
|
2009-05-19 06:39:16 +08:00
|
|
|
const LangOptions &LOpts,
|
|
|
|
bool SilenceRewriteMacroWarning) {
|
|
|
|
return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning);
|
2007-12-01 06:25:36 +08:00
|
|
|
}
|
2007-10-11 08:43:27 +08:00
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::Initialize(ASTContext &context) {
|
2008-02-01 03:38:44 +08:00
|
|
|
Context = &context;
|
|
|
|
SM = &Context->getSourceManager();
|
2008-04-17 22:40:12 +08:00
|
|
|
TUDecl = Context->getTranslationUnitDecl();
|
2008-02-01 03:38:44 +08:00
|
|
|
MsgSendFunctionDecl = 0;
|
|
|
|
MsgSendSuperFunctionDecl = 0;
|
|
|
|
MsgSendStretFunctionDecl = 0;
|
|
|
|
MsgSendSuperStretFunctionDecl = 0;
|
|
|
|
MsgSendFpretFunctionDecl = 0;
|
|
|
|
GetClassFunctionDecl = 0;
|
|
|
|
GetMetaClassFunctionDecl = 0;
|
|
|
|
SelGetUidFunctionDecl = 0;
|
|
|
|
CFStringFunctionDecl = 0;
|
|
|
|
ConstantStringClassReference = 0;
|
|
|
|
NSStringRecord = 0;
|
2008-10-28 01:20:55 +08:00
|
|
|
CurMethodDef = 0;
|
|
|
|
CurFunctionDef = 0;
|
2008-12-09 20:56:34 +08:00
|
|
|
GlobalVarDecl = 0;
|
2008-02-01 03:38:44 +08:00
|
|
|
SuperStructDecl = 0;
|
2009-04-30 00:37:50 +08:00
|
|
|
ProtocolTypeDecl = 0;
|
2008-03-28 06:59:54 +08:00
|
|
|
ConstantStringDecl = 0;
|
2008-02-01 03:38:44 +08:00
|
|
|
BcLabelCount = 0;
|
2008-03-12 01:37:02 +08:00
|
|
|
SuperContructorFunctionDecl = 0;
|
2008-03-15 08:55:56 +08:00
|
|
|
NumObjCStringLiterals = 0;
|
2008-12-09 04:01:41 +08:00
|
|
|
PropParentMap = 0;
|
|
|
|
CurrentBody = 0;
|
2008-12-09 20:56:34 +08:00
|
|
|
DisableReplaceStmt = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:38:44 +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(), Context->getLangOptions());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:38:44 +08:00
|
|
|
// declaring objc_selector outside the parameter list removes a silly
|
|
|
|
// scope related warning...
|
2008-03-28 06:29:16 +08:00
|
|
|
if (IsHeader)
|
2009-02-04 04:39:18 +08:00
|
|
|
Preamble = "#pragma once\n";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "struct objc_selector; struct objc_class;\n";
|
2008-12-24 04:11:22 +08:00
|
|
|
Preamble += "struct __rw_objc_super { struct objc_object *object; ";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "struct objc_object *superClass; ";
|
2008-03-12 01:37:02 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
// Add a constructor for creating temporary objects.
|
2009-02-07 09:47:29 +08:00
|
|
|
Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) "
|
|
|
|
": ";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "object(o), superClass(s) {} ";
|
2008-03-12 01:37:02 +08:00
|
|
|
}
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "};\n";
|
|
|
|
Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
|
|
|
|
Preamble += "typedef struct objc_object Protocol;\n";
|
|
|
|
Preamble += "#define _REWRITER_typedef_Protocol\n";
|
|
|
|
Preamble += "#endif\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n";
|
|
|
|
Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n";
|
|
|
|
} else
|
|
|
|
Preamble += "#define __OBJC_RW_DLLIMPORT extern\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(const char *);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(const char *);\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match";
|
2008-05-10 05:17:56 +08:00
|
|
|
Preamble += "(struct objc_class *, struct objc_object *);\n";
|
2008-07-17 02:58:11 +08:00
|
|
|
// @synchronized hooks.
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n";
|
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "#ifndef __FASTENUMERATIONSTATE\n";
|
|
|
|
Preamble += "struct __objcFastEnumerationState {\n\t";
|
|
|
|
Preamble += "unsigned long state;\n\t";
|
2008-04-05 06:58:22 +08:00
|
|
|
Preamble += "void **itemsPtr;\n\t";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "unsigned long *mutationsPtr;\n\t";
|
|
|
|
Preamble += "unsigned long extra[5];\n};\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "#define __FASTENUMERATIONSTATE\n";
|
|
|
|
Preamble += "#endif\n";
|
|
|
|
Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n";
|
|
|
|
Preamble += "struct __NSConstantStringImpl {\n";
|
|
|
|
Preamble += " int *isa;\n";
|
|
|
|
Preamble += " int flags;\n";
|
|
|
|
Preamble += " char *str;\n";
|
|
|
|
Preamble += " long length;\n";
|
|
|
|
Preamble += "};\n";
|
2008-08-06 04:04:48 +08:00
|
|
|
Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n";
|
|
|
|
Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n";
|
|
|
|
Preamble += "#else\n";
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n";
|
2008-08-06 04:04:48 +08:00
|
|
|
Preamble += "#endif\n";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "#define __NSCONSTANTSTRINGIMPL\n";
|
|
|
|
Preamble += "#endif\n";
|
2008-10-28 01:20:55 +08:00
|
|
|
// Blocks preamble.
|
|
|
|
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";
|
2008-12-16 23:50:30 +08:00
|
|
|
Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n";
|
|
|
|
Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n";
|
|
|
|
Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n";
|
|
|
|
Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n";
|
|
|
|
Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n";
|
2008-10-28 01:20:55 +08:00
|
|
|
Preamble += "#endif\n";
|
2008-10-28 02:50:14 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
2008-12-09 01:30:33 +08:00
|
|
|
Preamble += "#undef __OBJC_RW_DLLIMPORT\n";
|
|
|
|
Preamble += "#undef __OBJC_RW_STATICIMPORT\n";
|
2008-10-28 02:50:14 +08:00
|
|
|
Preamble += "#define __attribute__(X)\n";
|
|
|
|
}
|
2008-02-01 03:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top Level Driver Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) {
|
2007-10-17 05:07:07 +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
|
|
|
|
2007-10-17 05:07:07 +08:00
|
|
|
// If this is for a builtin, ignore it.
|
|
|
|
if (Loc.isInvalid()) return;
|
|
|
|
|
2007-10-24 07:50:29 +08:00
|
|
|
// Look for built-in declarations that we need to refer during the rewrite.
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2007-10-31 07:14:51 +08:00
|
|
|
RewriteFunctionDecl(FD);
|
2008-04-16 06:42:06 +08:00
|
|
|
} else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) {
|
2007-11-03 19:27:19 +08:00
|
|
|
// declared in <Foundation/NSString.h>
|
2008-11-24 11:54:41 +08:00
|
|
|
if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) {
|
2007-11-03 19:27:19 +08:00
|
|
|
ConstantStringClassReference = FVD;
|
|
|
|
return;
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) {
|
2007-10-27 04:53:56 +08:00
|
|
|
RewriteInterfaceDecl(MD);
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
|
2007-10-30 21:30:57 +08:00
|
|
|
RewriteCategoryDecl(CD);
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
|
2007-10-31 00:42:30 +08:00
|
|
|
RewriteProtocolDecl(PD);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (ObjCForwardProtocolDecl *FP =
|
2008-01-08 03:49:32 +08:00
|
|
|
dyn_cast<ObjCForwardProtocolDecl>(D)){
|
2007-11-14 08:42:16 +08:00
|
|
|
RewriteForwardProtocolDecl(FP);
|
2009-01-09 08:49:46 +08:00
|
|
|
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
|
|
|
|
// Recurse into linkage specifications
|
2009-06-30 10:36:12 +08:00
|
|
|
for (DeclContext::decl_iterator DI = LSD->decls_begin(),
|
|
|
|
DIEnd = LSD->decls_end();
|
2009-01-09 08:49:46 +08:00
|
|
|
DI != DIEnd; ++DI)
|
2009-03-30 00:50:03 +08:00
|
|
|
HandleTopLevelSingleDecl(*DI);
|
2007-10-24 07:50:29 +08:00
|
|
|
}
|
2007-10-25 01:06:59 +08:00
|
|
|
// If we have a decl in the main file, see if we should rewrite it.
|
2008-04-15 05:24:13 +08:00
|
|
|
if (SM->isFromMainFile(Loc))
|
2007-10-17 05:07:07 +08:00
|
|
|
return HandleDeclInMainFile(D);
|
|
|
|
}
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Syntactic (non-AST) Rewriting Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteInclude() {
|
2009-01-17 14:22:33 +08:00
|
|
|
SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
|
2008-01-19 08:30:35 +08:00
|
|
|
std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
|
|
|
|
const char *MainBufStart = MainBuf.first;
|
|
|
|
const char *MainBufEnd = MainBuf.second;
|
|
|
|
size_t ImportLen = strlen("import");
|
|
|
|
size_t IncludeLen = strlen("include");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-19 09:03:17 +08:00
|
|
|
// Loop over the whole file, looking for includes.
|
2008-01-19 08:30:35 +08:00
|
|
|
for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
|
|
|
|
if (*BufPtr == '#') {
|
|
|
|
if (++BufPtr == MainBufEnd)
|
|
|
|
return;
|
|
|
|
while (*BufPtr == ' ' || *BufPtr == '\t')
|
|
|
|
if (++BufPtr == MainBufEnd)
|
|
|
|
return;
|
|
|
|
if (!strncmp(BufPtr, "import", ImportLen)) {
|
|
|
|
// replace import with include
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation ImportLoc =
|
2008-01-19 08:30:35 +08:00
|
|
|
LocStart.getFileLocWithOffset(BufPtr-MainBufStart);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(ImportLoc, ImportLen, "include", IncludeLen);
|
2008-01-19 08:30:35 +08:00
|
|
|
BufPtr += ImportLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteTabs() {
|
2007-10-25 01:06:59 +08:00
|
|
|
std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
|
|
|
|
const char *MainBufStart = MainBuf.first;
|
|
|
|
const char *MainBufEnd = MainBuf.second;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Loop over the whole file, looking for tabs.
|
|
|
|
for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
|
|
|
|
if (*BufPtr != '\t')
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Okay, we found a tab. This tab will turn into at least one character,
|
|
|
|
// but it depends on which 'virtual column' it is in. Compute that now.
|
|
|
|
unsigned VCol = 0;
|
|
|
|
while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
|
|
|
|
BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
|
|
|
|
++VCol;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Okay, now that we know the virtual column, we know how many spaces to
|
|
|
|
// insert. We assume 8-character tab-stops.
|
|
|
|
unsigned Spaces = 8-(VCol & 7);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Get the location of the tab.
|
2009-01-17 14:22:33 +08:00
|
|
|
SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID);
|
|
|
|
TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Rewrite the single tab character into a sequence of spaces.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(TabLoc, 1, " ", Spaces);
|
2007-10-25 01:06:59 +08:00
|
|
|
}
|
2007-10-17 05:07:07 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2008-12-02 23:48:25 +08:00
|
|
|
static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl,
|
|
|
|
ObjCIvarDecl *OID) {
|
|
|
|
std::string S;
|
|
|
|
S = "((struct ";
|
|
|
|
S += ClassDecl->getIdentifier()->getName();
|
|
|
|
S += "_IMPL *)self)->";
|
|
|
|
S += OID->getNameAsCString();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2008-12-03 01:36:43 +08:00
|
|
|
void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
|
|
|
|
ObjCImplementationDecl *IMD,
|
|
|
|
ObjCCategoryImplDecl *CID) {
|
2008-12-02 04:33:01 +08:00
|
|
|
SourceLocation startLoc = PID->getLocStart();
|
|
|
|
InsertText(startLoc, "// ", 3);
|
2008-12-02 23:48:25 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
|
|
|
assert((*startBuf == '@') && "bogus @synthesize location");
|
|
|
|
const char *semiBuf = strchr(startBuf, ';');
|
|
|
|
assert((*semiBuf == ';') && "@synthesize: can't find ';'");
|
2009-02-07 09:47:29 +08:00
|
|
|
SourceLocation onePastSemiLoc =
|
|
|
|
startLoc.getFileLocWithOffset(semiBuf-startBuf+1);
|
2008-12-02 23:48:25 +08:00
|
|
|
|
|
|
|
if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
return; // FIXME: is this correct?
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-02 23:48:25 +08:00
|
|
|
// Generate the 'getter' function.
|
|
|
|
ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface();
|
|
|
|
ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 00:05:55 +08:00
|
|
|
if (!OID)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 00:05:55 +08:00
|
|
|
std::string Getr;
|
|
|
|
RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr);
|
|
|
|
Getr += "{ ";
|
|
|
|
// Synthesize an explicit cast to gain access to the ivar.
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: deal with code generation implications for various property
|
|
|
|
// attributes (copy, retain, nonatomic).
|
2008-12-03 01:54:50 +08:00
|
|
|
// See objc-act.c:objc_synthesize_new_getter() for details.
|
2008-12-03 00:05:55 +08:00
|
|
|
Getr += "return " + getIvarAccessString(ClassDecl, OID);
|
|
|
|
Getr += "; }";
|
2008-12-02 23:48:25 +08:00
|
|
|
InsertText(onePastSemiLoc, Getr.c_str(), Getr.size());
|
|
|
|
if (PD->isReadOnly())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-02 23:48:25 +08:00
|
|
|
// Generate the 'setter' function.
|
|
|
|
std::string Setr;
|
|
|
|
RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr);
|
|
|
|
Setr += "{ ";
|
2008-12-03 00:05:55 +08:00
|
|
|
// Synthesize an explicit cast to initialize the ivar.
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: deal with code generation implications for various property
|
|
|
|
// attributes (copy, retain, nonatomic).
|
2008-12-03 08:56:33 +08:00
|
|
|
// See objc-act.c:objc_synthesize_new_setter() for details.
|
2008-12-03 00:05:55 +08:00
|
|
|
Setr += getIvarAccessString(ClassDecl, OID) + " = ";
|
2008-12-12 03:29:16 +08:00
|
|
|
Setr += PD->getNameAsCString();
|
2008-12-03 00:05:55 +08:00
|
|
|
Setr += "; }";
|
2008-12-02 23:48:25 +08:00
|
|
|
InsertText(onePastSemiLoc, Setr.c_str(), Setr.size());
|
2008-12-02 04:33:01 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
|
2007-10-25 01:06:59 +08:00
|
|
|
// Get the start location and compute the semi location.
|
|
|
|
SourceLocation startLoc = ClassDecl->getLocation();
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
|
|
|
const char *semiPtr = strchr(startBuf, ';');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Translate to typedef's that forward reference structs with the same name
|
|
|
|
// as the class. As a convenience, we include the original declaration
|
|
|
|
// as a comment.
|
|
|
|
std::string typedefString;
|
|
|
|
typedefString += "// ";
|
2007-10-25 06:48:43 +08:00
|
|
|
typedefString.append(startBuf, semiPtr-startBuf+1);
|
|
|
|
typedefString += "\n";
|
2009-02-21 02:04:31 +08:00
|
|
|
for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ObjCInterfaceDecl *ForwardDecl = *I;
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += "#ifndef _REWRITER_typedef_";
|
2008-11-24 13:29:24 +08:00
|
|
|
typedefString += ForwardDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += "\n";
|
|
|
|
typedefString += "#define _REWRITER_typedef_";
|
2008-11-24 13:29:24 +08:00
|
|
|
typedefString += ForwardDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += "\n";
|
2007-11-05 22:36:37 +08:00
|
|
|
typedefString += "typedef struct objc_object ";
|
2008-11-24 13:29:24 +08:00
|
|
|
typedefString += ForwardDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += ";\n#endif\n";
|
2007-10-25 06:48:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
// Replace the @class with typedefs corresponding to the classes.
|
2009-09-09 23:08:12 +08:00
|
|
|
ReplaceText(startLoc, semiPtr-startBuf+1,
|
2008-02-01 03:51:04 +08:00
|
|
|
typedefString.c_str(), typedefString.size());
|
2007-10-25 01:06:59 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
|
2007-12-15 07:37:57 +08:00
|
|
|
SourceLocation LocStart = Method->getLocStart();
|
|
|
|
SourceLocation LocEnd = Method->getLocEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-04 09:06:56 +08:00
|
|
|
if (SM->getInstantiationLineNumber(LocEnd) >
|
|
|
|
SM->getInstantiationLineNumber(LocStart)) {
|
2008-10-21 21:37:27 +08:00
|
|
|
InsertText(LocStart, "#if 0\n", 6);
|
|
|
|
ReplaceText(LocEnd, 1, ";\n#endif\n", 9);
|
2007-12-15 07:37:57 +08:00
|
|
|
} else {
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(LocStart, "// ", 3);
|
2007-10-30 21:30:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) {
|
2009-01-11 09:06:09 +08:00
|
|
|
SourceLocation Loc = prop->getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-11 09:06:09 +08:00
|
|
|
ReplaceText(Loc, 0, "// ", 3);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-11 09:06:09 +08:00
|
|
|
// FIXME: handle properties that are declared across multiple lines.
|
2007-11-07 08:09:37 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
|
2007-10-30 21:30:57 +08:00
|
|
|
SourceLocation LocStart = CatDecl->getLocStart();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-30 21:30:57 +08:00
|
|
|
// FIXME: handle category headers that are declared across multiple lines.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, 0, "// ", 3);
|
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)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*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)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*I);
|
|
|
|
|
2007-10-30 21:30:57 +08:00
|
|
|
// Lastly, comment out the @end.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
|
2007-10-30 21:30:57 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
|
2007-11-14 09:37:46 +08:00
|
|
|
std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-31 00:42:30 +08:00
|
|
|
SourceLocation LocStart = PDecl->getLocStart();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-31 00:42:30 +08:00
|
|
|
// FIXME: handle protocol headers that are declared across multiple lines.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, 0, "// ", 3);
|
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)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*I);
|
2009-04-10 05:40:53 +08:00
|
|
|
for (ObjCProtocolDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*I);
|
|
|
|
|
2007-10-31 00:42:30 +08:00
|
|
|
// Lastly, comment out the @end.
|
2007-11-14 09:37:46 +08:00
|
|
|
SourceLocation LocEnd = PDecl->getAtEndLoc();
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocEnd, 0, "// ", 3);
|
2007-11-14 23:03:57 +08:00
|
|
|
|
2007-11-14 09:37:46 +08:00
|
|
|
// Must comment out @optional/@required
|
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
|
|
|
for (const char *p = startBuf; p < endBuf; p++) {
|
|
|
|
if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
|
|
|
|
std::string CommentedOptional = "/* @optional */";
|
2007-11-14 23:03:57 +08:00
|
|
|
SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(OptionalLoc, strlen("@optional"),
|
|
|
|
CommentedOptional.c_str(), CommentedOptional.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 09:37:46 +08:00
|
|
|
}
|
|
|
|
else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
|
|
|
|
std::string CommentedRequired = "/* @required */";
|
2007-11-14 23:03:57 +08:00
|
|
|
SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(OptionalLoc, strlen("@required"),
|
|
|
|
CommentedRequired.c_str(), CommentedRequired.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 09:37:46 +08:00
|
|
|
}
|
|
|
|
}
|
2007-10-31 00:42:30 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) {
|
2007-11-14 08:42:16 +08:00
|
|
|
SourceLocation LocStart = PDecl->getLocation();
|
2007-11-14 11:37:28 +08:00
|
|
|
if (LocStart.isInvalid())
|
|
|
|
assert(false && "Invalid SourceLocation");
|
2007-11-14 08:42:16 +08:00
|
|
|
// FIXME: handle forward protocol that are declared across multiple lines.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, 0, "// ", 3);
|
2007-11-14 08:42:16 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD,
|
2007-11-14 02:44:14 +08:00
|
|
|
std::string &ResultStr) {
|
2008-10-30 20:09:33 +08:00
|
|
|
//fprintf(stderr,"In RewriteObjCMethodDecl\n");
|
2008-07-16 22:40:40 +08:00
|
|
|
const FunctionType *FPRetType = 0;
|
2007-11-14 02:44:14 +08:00
|
|
|
ResultStr += "\nstatic ";
|
2008-01-08 03:49:32 +08:00
|
|
|
if (OMD->getResultType()->isObjCQualifiedIdType())
|
2007-12-18 05:03:50 +08:00
|
|
|
ResultStr += "id";
|
2008-12-12 03:29:16 +08:00
|
|
|
else if (OMD->getResultType()->isFunctionPointerType() ||
|
|
|
|
OMD->getResultType()->isBlockPointerType()) {
|
2008-07-16 22:40:40 +08:00
|
|
|
// needs special handling, since pointer-to-functions have special
|
|
|
|
// syntax (where a decaration models use).
|
|
|
|
QualType retType = OMD->getResultType();
|
2008-12-12 03:29:16 +08:00
|
|
|
QualType PointeeTy;
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const PointerType* PT = retType->getAs<PointerType>())
|
2008-12-12 03:29:16 +08:00
|
|
|
PointeeTy = PT->getPointeeType();
|
2009-07-30 05:53:49 +08:00
|
|
|
else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
|
2008-12-12 03:29:16 +08:00
|
|
|
PointeeTy = BPT->getPointeeType();
|
|
|
|
if ((FPRetType = PointeeTy->getAsFunctionType())) {
|
|
|
|
ResultStr += FPRetType->getResultType().getAsString();
|
|
|
|
ResultStr += "(*";
|
2008-07-16 22:40:40 +08:00
|
|
|
}
|
|
|
|
} else
|
2007-12-18 05:03:50 +08:00
|
|
|
ResultStr += OMD->getResultType().getAsString();
|
2008-01-10 09:39:52 +08:00
|
|
|
ResultStr += " ";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
// Unique method name
|
2007-11-14 05:02:00 +08:00
|
|
|
std::string NameStr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-10 01:18:27 +08:00
|
|
|
if (OMD->isInstanceMethod())
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += "_I_";
|
2007-11-14 02:44:14 +08:00
|
|
|
else
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += "_C_";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 13:29:24 +08:00
|
|
|
NameStr += OMD->getClassInterface()->getNameAsString();
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += "_";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (ObjCCategoryImplDecl *CID =
|
2009-01-09 03:41:02 +08:00
|
|
|
dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
|
2008-11-24 13:29:24 +08:00
|
|
|
NameStr += CID->getNameAsString();
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += "_";
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
// Append selector names, replacing ':' with '_'
|
2008-11-24 11:33:13 +08:00
|
|
|
{
|
|
|
|
std::string selString = OMD->getSelector().getAsString();
|
2007-11-14 02:44:14 +08:00
|
|
|
int len = selString.size();
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
if (selString[i] == ':')
|
|
|
|
selString[i] = '_';
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += selString;
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2007-11-14 05:02:00 +08:00
|
|
|
// Remember this name for metadata emission
|
|
|
|
MethodInternalNames[OMD] = NameStr;
|
|
|
|
ResultStr += NameStr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
// Rewrite arguments
|
|
|
|
ResultStr += "(";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
// invisible arguments
|
2009-01-10 01:18:27 +08:00
|
|
|
if (OMD->isInstanceMethod()) {
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface());
|
2007-11-14 02:44:14 +08:00
|
|
|
selfTy = Context->getPointerType(selfTy);
|
2008-03-12 08:25:36 +08:00
|
|
|
if (!LangOpts.Microsoft) {
|
|
|
|
if (ObjCSynthesizedStructs.count(OMD->getClassInterface()))
|
|
|
|
ResultStr += "struct ";
|
|
|
|
}
|
|
|
|
// When rewriting for Microsoft, explicitly omit the structure name.
|
2008-11-24 13:29:24 +08:00
|
|
|
ResultStr += OMD->getClassInterface()->getNameAsString();
|
2008-03-11 07:16:54 +08:00
|
|
|
ResultStr += " *";
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
|
|
|
else
|
2009-04-30 00:37:50 +08:00
|
|
|
ResultStr += Context->getObjCClassType().getAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
ResultStr += " self, ";
|
2008-01-08 03:49:32 +08:00
|
|
|
ResultStr += Context->getObjCSelType().getAsString();
|
2007-11-14 02:44:14 +08:00
|
|
|
ResultStr += " _cmd";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
// Method arguments.
|
2009-02-21 02:43:26 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
|
|
|
|
E = OMD->param_end(); PI != E; ++PI) {
|
|
|
|
ParmVarDecl *PDecl = *PI;
|
2007-11-14 02:44:14 +08:00
|
|
|
ResultStr += ", ";
|
2008-04-19 05:13:19 +08:00
|
|
|
if (PDecl->getType()->isObjCQualifiedIdType()) {
|
|
|
|
ResultStr += "id ";
|
2008-11-24 13:29:24 +08:00
|
|
|
ResultStr += PDecl->getNameAsString();
|
2008-04-19 05:13:19 +08:00
|
|
|
} else {
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = PDecl->getNameAsString();
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(PDecl->getType())) {
|
2008-10-30 22:45:29 +08:00
|
|
|
// Make sure we convert "t (^)(...)" to "t (*)(...)".
|
2009-07-30 05:53:49 +08:00
|
|
|
const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>();
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name,
|
|
|
|
Context->PrintingPolicy);
|
2008-10-30 22:45:29 +08:00
|
|
|
} else
|
2009-05-30 04:38:28 +08:00
|
|
|
PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy);
|
2008-04-19 05:13:19 +08:00
|
|
|
ResultStr += Name;
|
|
|
|
}
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2008-01-22 04:14:23 +08:00
|
|
|
if (OMD->isVariadic())
|
|
|
|
ResultStr += ", ...";
|
2008-01-10 09:39:52 +08:00
|
|
|
ResultStr += ") ";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-16 22:40:40 +08:00
|
|
|
if (FPRetType) {
|
|
|
|
ResultStr += ")"; // close the precedence "scope" for "*".
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-16 22:40:40 +08:00
|
|
|
// Now, emit the argument types (if any).
|
2009-02-27 07:50:07 +08:00
|
|
|
if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) {
|
2008-07-16 22:40:40 +08:00
|
|
|
ResultStr += "(";
|
|
|
|
for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
|
|
|
|
if (i) ResultStr += ", ";
|
|
|
|
std::string ParamStr = FT->getArgType(i).getAsString();
|
|
|
|
ResultStr += ParamStr;
|
|
|
|
}
|
|
|
|
if (FT->isVariadic()) {
|
|
|
|
if (FT->getNumArgs()) ResultStr += ", ";
|
|
|
|
ResultStr += "...";
|
|
|
|
}
|
|
|
|
ResultStr += ")";
|
|
|
|
} else {
|
|
|
|
ResultStr += "()";
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2009-01-20 09:17:11 +08:00
|
|
|
void RewriteObjC::RewriteImplementationDecl(Decl *OID) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
|
|
|
|
ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 04:04:28 +08:00
|
|
|
if (IMD)
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(IMD->getLocStart(), "// ", 3);
|
2007-11-14 04:04:28 +08:00
|
|
|
else
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(CID->getLocStart(), "// ", 3);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryImplDecl::instmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
|
|
|
|
E = IMD ? IMD->instmeth_end() : CID->instmeth_end();
|
2009-04-23 09:02:12 +08:00
|
|
|
I != E; ++I) {
|
2007-11-14 02:44:14 +08:00
|
|
|
std::string ResultStr;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *OMD = *I;
|
|
|
|
RewriteObjCMethodDecl(OMD, ResultStr);
|
2007-11-14 02:44:14 +08:00
|
|
|
SourceLocation LocStart = OMD->getLocStart();
|
2009-06-30 10:35:26 +08:00
|
|
|
SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
|
2009-04-27 04:35:05 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, endBuf-startBuf,
|
|
|
|
ResultStr.c_str(), ResultStr.size());
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryImplDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
|
|
|
|
E = IMD ? IMD->classmeth_end() : CID->classmeth_end();
|
2009-04-23 09:02:12 +08:00
|
|
|
I != E; ++I) {
|
2007-11-14 02:44:14 +08:00
|
|
|
std::string ResultStr;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *OMD = *I;
|
|
|
|
RewriteObjCMethodDecl(OMD, ResultStr);
|
2007-11-14 02:44:14 +08:00
|
|
|
SourceLocation LocStart = OMD->getLocStart();
|
2009-06-30 10:35:26 +08:00
|
|
|
SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, endBuf-startBuf,
|
2009-09-09 23:08:12 +08:00
|
|
|
ResultStr.c_str(), ResultStr.size());
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2008-12-02 04:33:01 +08:00
|
|
|
for (ObjCCategoryImplDecl::propimpl_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(),
|
2009-09-09 23:08:12 +08:00
|
|
|
E = IMD ? IMD->propimpl_end() : CID->propimpl_end();
|
2009-04-23 09:02:12 +08:00
|
|
|
I != E; ++I) {
|
2008-12-03 01:36:43 +08:00
|
|
|
RewritePropertyImplDecl(*I, IMD, CID);
|
2008-12-02 04:33:01 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 04:04:28 +08:00
|
|
|
if (IMD)
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(IMD->getLocEnd(), "// ", 3);
|
2007-11-14 04:04:28 +08:00
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
InsertText(CID->getLocEnd(), "// ", 3);
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
|
2007-10-30 10:23:23 +08:00
|
|
|
std::string ResultStr;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (!ObjCForwardDecls.count(ClassDecl)) {
|
2007-11-01 11:35:41 +08:00
|
|
|
// we haven't seen a forward decl - generate a typedef.
|
2007-11-15 07:02:56 +08:00
|
|
|
ResultStr = "#ifndef _REWRITER_typedef_";
|
2008-11-24 13:29:24 +08:00
|
|
|
ResultStr += ClassDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
ResultStr += "\n";
|
|
|
|
ResultStr += "#define _REWRITER_typedef_";
|
2008-11-24 13:29:24 +08:00
|
|
|
ResultStr += ClassDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
ResultStr += "\n";
|
2008-03-11 07:16:54 +08:00
|
|
|
ResultStr += "typedef struct objc_object ";
|
2008-11-24 13:29:24 +08:00
|
|
|
ResultStr += ClassDecl->getNameAsString();
|
2007-11-09 20:50:28 +08:00
|
|
|
ResultStr += ";\n#endif\n";
|
2007-11-01 11:35:41 +08:00
|
|
|
// Mark this typedef as having been generated.
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCForwardDecls.insert(ClassDecl);
|
2007-11-01 11:35:41 +08:00
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
|
2009-06-30 10:36:12 +08:00
|
|
|
E = ClassDecl->prop_end(); I != E; ++I)
|
2009-01-11 09:06:09 +08:00
|
|
|
RewriteProperty(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator
|
|
|
|
I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I)
|
2007-12-15 07:37:57 +08:00
|
|
|
RewriteMethodDeclaration(*I);
|
|
|
|
|
2007-10-30 11:43:13 +08:00
|
|
|
// Lastly, comment out the @end.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
|
2007-10-27 04:53:56 +08:00
|
|
|
}
|
|
|
|
|
2008-12-09 20:56:34 +08:00
|
|
|
Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt,
|
|
|
|
SourceRange SrcRange) {
|
2008-12-05 00:24:46 +08:00
|
|
|
// Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr.
|
|
|
|
// This allows us to reuse all the fun and games in SynthMessageExpr().
|
|
|
|
ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS());
|
|
|
|
ObjCMessageExpr *MsgExpr;
|
|
|
|
ObjCPropertyDecl *PDecl = PropRefExpr->getProperty();
|
|
|
|
llvm::SmallVector<Expr *, 1> ExprVec;
|
|
|
|
ExprVec.push_back(newStmt);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 00:43:47 +08:00
|
|
|
Stmt *Receiver = PropRefExpr->getBase();
|
|
|
|
ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver);
|
|
|
|
if (PRE && PropGetters[PRE]) {
|
|
|
|
// This allows us to handle chain/nested property getters.
|
|
|
|
Receiver = PropGetters[PRE];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver),
|
|
|
|
PDecl->getSetterName(), PDecl->getType(),
|
|
|
|
PDecl->getSetterMethodDecl(),
|
|
|
|
SourceLocation(), SourceLocation(),
|
2008-12-05 00:24:46 +08:00
|
|
|
&ExprVec[0], 1);
|
|
|
|
Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-05 00:24:46 +08:00
|
|
|
// Now do the actual rewrite.
|
2008-12-09 20:56:34 +08:00
|
|
|
ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange);
|
2008-12-10 22:53:27 +08:00
|
|
|
//delete BinOp;
|
2009-02-07 09:47:29 +08:00
|
|
|
// NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
|
|
|
|
// to things that stay around.
|
|
|
|
Context->Deallocate(MsgExpr);
|
2008-12-05 00:24:46 +08:00
|
|
|
return ReplacingStmt;
|
2008-12-03 08:56:33 +08:00
|
|
|
}
|
|
|
|
|
2008-12-05 00:24:46 +08:00
|
|
|
Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) {
|
2008-12-03 08:56:33 +08:00
|
|
|
// Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr.
|
|
|
|
// This allows us to reuse all the fun and games in SynthMessageExpr().
|
|
|
|
ObjCMessageExpr *MsgExpr;
|
|
|
|
ObjCPropertyDecl *PDecl = PropRefExpr->getProperty();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 00:43:47 +08:00
|
|
|
Stmt *Receiver = PropRefExpr->getBase();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 00:43:47 +08:00
|
|
|
ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver);
|
|
|
|
if (PRE && PropGetters[PRE]) {
|
|
|
|
// This allows us to handle chain/nested property getters.
|
|
|
|
Receiver = PropGetters[PRE];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver),
|
|
|
|
PDecl->getGetterName(), PDecl->getType(),
|
|
|
|
PDecl->getGetterMethodDecl(),
|
|
|
|
SourceLocation(), SourceLocation(),
|
2008-12-03 08:56:33 +08:00
|
|
|
0, 0);
|
|
|
|
|
2008-12-05 07:50:32 +08:00
|
|
|
Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr);
|
2008-12-09 00:43:47 +08:00
|
|
|
|
|
|
|
if (!PropParentMap)
|
|
|
|
PropParentMap = new ParentMap(CurrentBody);
|
|
|
|
|
|
|
|
Stmt *Parent = PropParentMap->getParent(PropRefExpr);
|
|
|
|
if (Parent && isa<ObjCPropertyRefExpr>(Parent)) {
|
|
|
|
// We stash away the ReplacingStmt since actually doing the
|
|
|
|
// replacement/rewrite won't work for nested getters (e.g. obj.p.i)
|
|
|
|
PropGetters[PropRefExpr] = ReplacingStmt;
|
2009-02-07 09:47:29 +08:00
|
|
|
// NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
|
|
|
|
// to things that stay around.
|
|
|
|
Context->Deallocate(MsgExpr);
|
2008-12-09 00:43:47 +08:00
|
|
|
return PropRefExpr; // return the original...
|
|
|
|
} else {
|
|
|
|
ReplaceStmt(PropRefExpr, ReplacingStmt);
|
2009-09-09 23:08:12 +08:00
|
|
|
// delete PropRefExpr; elsewhere...
|
2009-02-07 09:47:29 +08:00
|
|
|
// NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
|
|
|
|
// to things that stay around.
|
|
|
|
Context->Deallocate(MsgExpr);
|
2008-12-09 00:43:47 +08:00
|
|
|
return ReplacingStmt;
|
|
|
|
}
|
2008-12-03 08:56:33 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV,
|
2008-05-24 04:40:52 +08:00
|
|
|
SourceLocation OrigStart) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIvarDecl *D = IV->getDecl();
|
2008-10-28 01:20:55 +08:00
|
|
|
if (CurMethodDef) {
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) {
|
2009-02-07 09:47:29 +08:00
|
|
|
ObjCInterfaceType *iFaceDecl =
|
|
|
|
dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
|
2008-05-09 01:52:16 +08:00
|
|
|
// lookup which class implements the instance variable.
|
|
|
|
ObjCInterfaceDecl *clsDeclared = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
|
2009-04-10 05:40:53 +08:00
|
|
|
clsDeclared);
|
2008-05-09 01:52:16 +08:00
|
|
|
assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-09 01:52:16 +08:00
|
|
|
// Synthesize an explicit cast to gain access to the ivar.
|
|
|
|
std::string RecName = clsDeclared->getIdentifier()->getName();
|
|
|
|
RecName += "_IMPL";
|
|
|
|
IdentifierInfo *II = &Context->Idents.get(RecName.c_str());
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2008-09-05 09:34:33 +08:00
|
|
|
SourceLocation(), II);
|
2008-05-09 01:52:16 +08:00
|
|
|
assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
|
|
|
|
QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
|
2009-09-09 23:08:12 +08:00
|
|
|
CastExpr *castExpr = new (Context) CStyleCastExpr(castT,
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
|
|
|
IV->getBase(),
|
|
|
|
castT,SourceLocation(),
|
|
|
|
SourceLocation());
|
2008-05-09 01:52:16 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2009-02-07 09:47:29 +08:00
|
|
|
ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(),
|
|
|
|
IV->getBase()->getLocEnd(),
|
|
|
|
castExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (IV->isFreeIvar() &&
|
2008-10-28 01:20:55 +08:00
|
|
|
CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) {
|
2009-02-07 09:47:29 +08:00
|
|
|
MemberExpr *ME = new (Context) MemberExpr(PE, true, D,
|
|
|
|
IV->getLocation(),
|
|
|
|
D->getType());
|
2008-05-09 01:52:16 +08:00
|
|
|
ReplaceStmt(IV, ME);
|
2008-12-05 07:50:32 +08:00
|
|
|
// delete IV; leak for now, see RewritePropertySetter() usage for more info.
|
2008-05-09 01:52:16 +08:00
|
|
|
return ME;
|
2007-11-15 19:33:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-24 04:40:52 +08:00
|
|
|
ReplaceStmt(IV->getBase(), PE);
|
|
|
|
// Cannot delete IV->getBase(), since PE points to it.
|
|
|
|
// Replace the old base with the cast. This is important when doing
|
|
|
|
// embedded rewrites. For example, [newInv->_container addObject:0].
|
2009-09-09 23:08:12 +08:00
|
|
|
IV->setBase(PE);
|
2008-05-24 04:40:52 +08:00
|
|
|
return IV;
|
2007-11-15 19:33:00 +08:00
|
|
|
}
|
2008-04-19 05:55:08 +08:00
|
|
|
} else { // we are outside a method.
|
2008-05-07 07:20:07 +08:00
|
|
|
assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-07 07:20:07 +08:00
|
|
|
// Explicit ivar refs need to have a cast inserted.
|
|
|
|
// FIXME: consider sharing some of this code with the code above.
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) {
|
2008-05-09 01:52:16 +08:00
|
|
|
ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
|
2008-05-07 07:20:07 +08:00
|
|
|
// lookup which class implements the instance variable.
|
|
|
|
ObjCInterfaceDecl *clsDeclared = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
|
2009-04-10 05:40:53 +08:00
|
|
|
clsDeclared);
|
2008-05-07 07:20:07 +08:00
|
|
|
assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-07 07:20:07 +08:00
|
|
|
// Synthesize an explicit cast to gain access to the ivar.
|
|
|
|
std::string RecName = clsDeclared->getIdentifier()->getName();
|
|
|
|
RecName += "_IMPL";
|
|
|
|
IdentifierInfo *II = &Context->Idents.get(RecName.c_str());
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2008-09-05 09:34:33 +08:00
|
|
|
SourceLocation(), II);
|
2008-05-07 07:20:07 +08:00
|
|
|
assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
|
|
|
|
QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
|
2009-09-09 23:08:12 +08:00
|
|
|
CastExpr *castExpr = new (Context) CStyleCastExpr(castT,
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
|
|
|
IV->getBase(),
|
2009-02-07 09:47:29 +08:00
|
|
|
castT, SourceLocation(),
|
|
|
|
SourceLocation());
|
2008-05-07 07:20:07 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2009-02-07 09:47:29 +08:00
|
|
|
ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(),
|
2008-05-29 00:38:23 +08:00
|
|
|
IV->getBase()->getLocEnd(), castExpr);
|
2008-05-07 07:20:07 +08:00
|
|
|
ReplaceStmt(IV->getBase(), PE);
|
|
|
|
// Cannot delete IV->getBase(), since PE points to it.
|
|
|
|
// Replace the old base with the cast. This is important when doing
|
|
|
|
// embedded rewrites. For example, [newInv->_container addObject:0].
|
2009-09-09 23:08:12 +08:00
|
|
|
IV->setBase(PE);
|
2008-05-07 07:20:07 +08:00
|
|
|
return IV;
|
|
|
|
}
|
2007-11-15 19:33:00 +08:00
|
|
|
}
|
2008-04-19 05:55:08 +08:00
|
|
|
return IV;
|
2007-11-15 10:58:25 +08:00
|
|
|
}
|
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
/// SynthCountByEnumWithState - To print:
|
|
|
|
/// ((unsigned int (*)
|
|
|
|
/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
|
2009-09-09 23:08:12 +08:00
|
|
|
/// (void *)objc_msgSend)((id)l_collection,
|
2008-10-29 04:29:00 +08:00
|
|
|
/// sel_registerName(
|
2009-09-09 23:08:12 +08:00
|
|
|
/// "countByEnumeratingWithState:objects:count:"),
|
|
|
|
/// &enumState,
|
2008-10-29 04:29:00 +08:00
|
|
|
/// (id *)items, (unsigned int)16)
|
|
|
|
///
|
|
|
|
void RewriteObjC::SynthCountByEnumWithState(std::string &buf) {
|
|
|
|
buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
|
|
|
|
"id *, unsigned int))(void *)objc_msgSend)";
|
|
|
|
buf += "\n\t\t";
|
|
|
|
buf += "((id)l_collection,\n\t\t";
|
|
|
|
buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
|
|
|
|
buf += "\n\t\t";
|
|
|
|
buf += "&enumState, "
|
|
|
|
"(id *)items, (unsigned int)16)";
|
|
|
|
}
|
2007-11-05 22:50:49 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
|
|
|
|
/// statement to exit to its outer synthesized loop.
|
|
|
|
///
|
|
|
|
Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) {
|
|
|
|
if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
|
|
|
|
return S;
|
|
|
|
// replace break with goto __break_label
|
|
|
|
std::string buf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
buf = "goto __break_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
|
|
|
ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size());
|
2007-11-07 23:32:26 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-01-30 06:59:37 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
|
|
|
|
/// statement to continue with its inner synthesized loop.
|
|
|
|
///
|
|
|
|
Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) {
|
|
|
|
if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
|
|
|
|
return S;
|
|
|
|
// replace continue with goto __continue_label
|
|
|
|
std::string buf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
buf = "goto __continue_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
|
|
|
ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-16 07:58:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 06:59:37 +08:00
|
|
|
/// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
|
2008-01-08 05:40:22 +08:00
|
|
|
/// It rewrites:
|
|
|
|
/// for ( type elem in collection) { stmts; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 05:40:22 +08:00
|
|
|
/// Into:
|
|
|
|
/// {
|
2009-09-09 23:08:12 +08:00
|
|
|
/// type elem;
|
2008-01-09 06:06:28 +08:00
|
|
|
/// struct __objcFastEnumerationState enumState = { 0 };
|
2008-01-08 05:40:22 +08:00
|
|
|
/// id items[16];
|
2008-01-09 06:06:28 +08:00
|
|
|
/// id l_collection = (id)collection;
|
2009-09-09 23:08:12 +08:00
|
|
|
/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
|
2008-01-09 06:06:28 +08:00
|
|
|
/// objects:items count:16];
|
2008-01-08 05:40:22 +08:00
|
|
|
/// if (limit) {
|
|
|
|
/// unsigned long startMutations = *enumState.mutationsPtr;
|
|
|
|
/// do {
|
|
|
|
/// unsigned long counter = 0;
|
|
|
|
/// do {
|
2009-09-09 23:08:12 +08:00
|
|
|
/// if (startMutations != *enumState.mutationsPtr)
|
2008-01-09 06:06:28 +08:00
|
|
|
/// objc_enumerationMutation(l_collection);
|
2008-01-10 02:15:42 +08:00
|
|
|
/// elem = (type)enumState.itemsPtr[counter++];
|
2008-01-08 05:40:22 +08:00
|
|
|
/// stmts;
|
2008-01-16 07:58:23 +08:00
|
|
|
/// __continue_label: ;
|
2008-01-08 05:40:22 +08:00
|
|
|
/// } while (counter < limit);
|
2009-09-09 23:08:12 +08:00
|
|
|
/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
|
2008-01-09 06:06:28 +08:00
|
|
|
/// objects:items count:16]);
|
|
|
|
/// elem = nil;
|
2008-01-16 07:58:23 +08:00
|
|
|
/// __break_label: ;
|
2008-01-08 05:40:22 +08:00
|
|
|
/// }
|
|
|
|
/// else
|
|
|
|
/// elem = nil;
|
|
|
|
/// }
|
|
|
|
///
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
|
2008-01-31 13:10:40 +08:00
|
|
|
SourceLocation OrigEnd) {
|
2008-01-16 07:58:23 +08:00
|
|
|
assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
|
2008-01-16 07:58:23 +08:00
|
|
|
"ObjCForCollectionStmt Statement stack mismatch");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!ObjCBcLabelNo.empty() &&
|
2008-01-16 07:58:23 +08:00
|
|
|
"ObjCForCollectionStmt - Label No stack empty");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
|
|
|
const char *elementName;
|
2008-01-10 02:15:42 +08:00
|
|
|
std::string elementTypeAsString;
|
2008-01-09 06:06:28 +08:00
|
|
|
std::string buf;
|
|
|
|
buf = "\n{\n\t";
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
|
|
|
|
// type elem;
|
2009-03-28 14:33:19 +08:00
|
|
|
NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
|
2008-10-07 06:16:13 +08:00
|
|
|
QualType ElementType = cast<ValueDecl>(D)->getType();
|
2008-01-10 02:15:42 +08:00
|
|
|
elementTypeAsString = ElementType.getAsString();
|
|
|
|
buf += elementTypeAsString;
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += " ";
|
2008-11-24 11:54:41 +08:00
|
|
|
elementName = D->getNameAsCString();
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += elementName;
|
|
|
|
buf += ";\n\t";
|
|
|
|
}
|
2008-04-08 13:52:18 +08:00
|
|
|
else {
|
|
|
|
DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
|
2008-11-24 11:54:41 +08:00
|
|
|
elementName = DR->getDecl()->getNameAsCString();
|
2009-09-09 23:08:12 +08:00
|
|
|
elementTypeAsString
|
2008-10-22 00:13:35 +08:00
|
|
|
= cast<ValueDecl>(DR->getDecl())->getType().getAsString();
|
2008-01-10 02:15:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
// struct __objcFastEnumerationState enumState = { 0 };
|
|
|
|
buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
|
|
|
|
// id items[16];
|
|
|
|
buf += "id items[16];\n\t";
|
|
|
|
// id l_collection = (id)
|
|
|
|
buf += "id l_collection = (id)";
|
2008-01-10 08:24:29 +08:00
|
|
|
// Find start location of 'collection' the hard way!
|
|
|
|
const char *startCollectionBuf = startBuf;
|
|
|
|
startCollectionBuf += 3; // skip 'for'
|
|
|
|
startCollectionBuf = strchr(startCollectionBuf, '(');
|
|
|
|
startCollectionBuf++; // skip '('
|
|
|
|
// find 'in' and skip it.
|
|
|
|
while (*startCollectionBuf != ' ' ||
|
|
|
|
*(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
|
|
|
|
(*(startCollectionBuf+3) != ' ' &&
|
|
|
|
*(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
|
|
|
|
startCollectionBuf++;
|
|
|
|
startCollectionBuf += 3;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Replace: "for (type element in" with string constructed thus far.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, startCollectionBuf - startBuf,
|
|
|
|
buf.c_str(), buf.size());
|
2008-01-09 06:06:28 +08:00
|
|
|
// Replace ')' in for '(' type elem in collection ')' with ';'
|
2008-01-10 08:24:29 +08:00
|
|
|
SourceLocation rightParenLoc = S->getRParenLoc();
|
|
|
|
const char *rparenBuf = SM->getCharacterData(rightParenLoc);
|
|
|
|
SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf);
|
2008-01-09 06:06:28 +08:00
|
|
|
buf = ";\n\t";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
|
|
|
|
// objects:items count:16];
|
|
|
|
// which is synthesized into:
|
2009-09-09 23:08:12 +08:00
|
|
|
// unsigned int limit =
|
2008-01-09 06:06:28 +08:00
|
|
|
// ((unsigned int (*)
|
|
|
|
// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
|
2009-09-09 23:08:12 +08:00
|
|
|
// (void *)objc_msgSend)((id)l_collection,
|
2008-01-09 06:06:28 +08:00
|
|
|
// sel_registerName(
|
2009-09-09 23:08:12 +08:00
|
|
|
// "countByEnumeratingWithState:objects:count:"),
|
|
|
|
// (struct __objcFastEnumerationState *)&state,
|
2008-01-09 06:06:28 +08:00
|
|
|
// (id *)items, (unsigned int)16);
|
|
|
|
buf += "unsigned long limit =\n\t\t";
|
|
|
|
SynthCountByEnumWithState(buf);
|
|
|
|
buf += ";\n\t";
|
|
|
|
/// if (limit) {
|
|
|
|
/// unsigned long startMutations = *enumState.mutationsPtr;
|
|
|
|
/// do {
|
|
|
|
/// unsigned long counter = 0;
|
|
|
|
/// do {
|
2009-09-09 23:08:12 +08:00
|
|
|
/// if (startMutations != *enumState.mutationsPtr)
|
2008-01-09 06:06:28 +08:00
|
|
|
/// objc_enumerationMutation(l_collection);
|
2008-01-10 02:15:42 +08:00
|
|
|
/// elem = (type)enumState.itemsPtr[counter++];
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += "if (limit) {\n\t";
|
|
|
|
buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
|
|
|
|
buf += "do {\n\t\t";
|
|
|
|
buf += "unsigned long counter = 0;\n\t\t";
|
|
|
|
buf += "do {\n\t\t\t";
|
|
|
|
buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
|
|
|
|
buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
|
|
|
|
buf += elementName;
|
2008-01-10 02:15:42 +08:00
|
|
|
buf += " = (";
|
|
|
|
buf += elementTypeAsString;
|
|
|
|
buf += ")enumState.itemsPtr[counter++];";
|
2008-01-09 06:06:28 +08:00
|
|
|
// Replace ')' in for '(' type elem in collection ')' with all of these.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(lparenLoc, 1, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-16 07:58:23 +08:00
|
|
|
/// __continue_label: ;
|
2008-01-09 06:06:28 +08:00
|
|
|
/// } while (counter < limit);
|
2009-09-09 23:08:12 +08:00
|
|
|
/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
|
2008-01-09 06:06:28 +08:00
|
|
|
/// objects:items count:16]);
|
|
|
|
/// elem = nil;
|
2008-01-16 07:58:23 +08:00
|
|
|
/// __break_label: ;
|
2008-01-09 06:06:28 +08:00
|
|
|
/// }
|
|
|
|
/// else
|
|
|
|
/// elem = nil;
|
|
|
|
/// }
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2008-01-16 07:58:23 +08:00
|
|
|
buf = ";\n\t";
|
|
|
|
buf += "__continue_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
|
|
|
buf += ": ;";
|
|
|
|
buf += "\n\t\t";
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += "} while (counter < limit);\n\t";
|
|
|
|
buf += "} while (limit = ";
|
|
|
|
SynthCountByEnumWithState(buf);
|
|
|
|
buf += ");\n\t";
|
|
|
|
buf += elementName;
|
2008-12-17 22:24:39 +08:00
|
|
|
buf += " = ((id)0);\n\t";
|
2008-01-16 07:58:23 +08:00
|
|
|
buf += "__break_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
|
|
|
buf += ": ;\n\t";
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += "}\n\t";
|
|
|
|
buf += "else\n\t\t";
|
|
|
|
buf += elementName;
|
2008-12-17 22:24:39 +08:00
|
|
|
buf += " = ((id)0);\n";
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += "}\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
// Insert all these *after* the statement body.
|
2009-04-27 04:35:05 +08:00
|
|
|
// FIXME: If this should support Obj-C++, support CXXTryStmt
|
2008-07-22 02:26:02 +08:00
|
|
|
if (isa<CompoundStmt>(S->getBody())) {
|
|
|
|
SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1);
|
|
|
|
InsertText(endBodyLoc, buf.c_str(), buf.size());
|
|
|
|
} else {
|
|
|
|
/* Need to treat single statements specially. For example:
|
|
|
|
*
|
|
|
|
* for (A *a in b) if (stuff()) break;
|
|
|
|
* for (A *a in b) xxxyy;
|
|
|
|
*
|
|
|
|
* The following code simply scans ahead to the semi to find the actual end.
|
|
|
|
*/
|
|
|
|
const char *stmtBuf = SM->getCharacterData(OrigEnd);
|
|
|
|
const char *semiBuf = strchr(stmtBuf, ';');
|
|
|
|
assert(semiBuf && "Can't find ';'");
|
|
|
|
SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1);
|
|
|
|
InsertText(endBodyLoc, buf.c_str(), buf.size());
|
|
|
|
}
|
2008-01-16 07:58:23 +08:00
|
|
|
Stmts.pop_back();
|
|
|
|
ObjCBcLabelNo.pop_back();
|
2008-01-09 06:06:28 +08:00
|
|
|
return 0;
|
2008-01-08 05:40:22 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// RewriteObjCSynchronizedStmt -
|
2008-01-30 06:59:37 +08:00
|
|
|
/// This routine rewrites @synchronized(expr) stmt;
|
|
|
|
/// into:
|
|
|
|
/// objc_sync_enter(expr);
|
|
|
|
/// @try stmt @finally { objc_sync_exit(expr); }
|
|
|
|
///
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
|
2008-01-30 06:59:37 +08:00
|
|
|
// Get the start location and compute the semi location.
|
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:59:37 +08:00
|
|
|
assert((*startBuf == '@') && "bogus @synchronized location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
std::string buf;
|
2008-08-21 21:03:03 +08:00
|
|
|
buf = "objc_sync_enter((id)";
|
|
|
|
const char *lparenBuf = startBuf;
|
|
|
|
while (*lparenBuf != '(') lparenBuf++;
|
|
|
|
ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
// We can't use S->getSynchExpr()->getLocEnd() to find the end location, since
|
|
|
|
// the sync expression is typically a message expression that's already
|
2008-08-19 21:04:19 +08:00
|
|
|
// been rewritten! (which implies the SourceLocation's are invalid).
|
|
|
|
SourceLocation endLoc = S->getSynchBody()->getLocStart();
|
2008-01-30 06:59:37 +08:00
|
|
|
const char *endBuf = SM->getCharacterData(endLoc);
|
2008-08-19 21:04:19 +08:00
|
|
|
while (*endBuf != ')') endBuf--;
|
|
|
|
SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf);
|
2008-01-30 06:59:37 +08:00
|
|
|
buf = ");\n";
|
|
|
|
// declare a new scope with two variables, _stack and _rethrow.
|
|
|
|
buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n";
|
|
|
|
buf += "int buf[18/*32-bit i386*/];\n";
|
|
|
|
buf += "char *pointers[4];} _stack;\n";
|
|
|
|
buf += "id volatile _rethrow = 0;\n";
|
|
|
|
buf += "objc_exception_try_enter(&_stack);\n";
|
|
|
|
buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(rparenLoc, 1, buf.c_str(), buf.size());
|
2008-01-30 06:59:37 +08:00
|
|
|
startLoc = S->getSynchBody()->getLocEnd();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-19 21:04:19 +08:00
|
|
|
assert((*startBuf == '}') && "bogus @synchronized block");
|
2008-01-30 06:59:37 +08:00
|
|
|
SourceLocation lastCurlyLoc = startLoc;
|
|
|
|
buf = "}\nelse {\n";
|
|
|
|
buf += " _rethrow = objc_exception_extract(&_stack);\n";
|
2009-04-30 00:37:50 +08:00
|
|
|
buf += "}\n";
|
|
|
|
buf += "{ /* implicit finally clause */\n";
|
2008-01-30 06:59:37 +08:00
|
|
|
buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
|
2008-07-17 03:47:39 +08:00
|
|
|
buf += " objc_sync_exit(";
|
2009-09-09 23:08:12 +08:00
|
|
|
Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
S->getSynchExpr(),
|
2009-02-07 09:47:29 +08:00
|
|
|
Context->getObjCIdType(),
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation());
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string syncExprBufS;
|
|
|
|
llvm::raw_string_ostream syncExprBuf(syncExprBufS);
|
2009-06-30 09:26:17 +08:00
|
|
|
syncExpr->printPretty(syncExprBuf, *Context, 0,
|
|
|
|
PrintingPolicy(LangOpts));
|
2008-07-17 03:47:39 +08:00
|
|
|
buf += syncExprBuf.str();
|
|
|
|
buf += ");\n";
|
2008-01-30 06:59:37 +08:00
|
|
|
buf += " if (_rethrow) objc_exception_throw(_rethrow);\n";
|
|
|
|
buf += "}\n";
|
|
|
|
buf += "}";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size());
|
2008-01-30 06:59:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) {
|
2008-12-06 01:03:39 +08:00
|
|
|
// Perform a bottom up traversal of all children.
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI)
|
|
|
|
WarnAboutReturnGotoContinueOrBreakStmts(*CI);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) ||
|
2008-12-06 01:03:39 +08:00
|
|
|
isa<BreakStmt>(S) || isa<GotoStmt>(S)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diags.Report(Context->getFullLoc(S->getLocStart()),
|
2008-12-06 01:03:39 +08:00
|
|
|
TryFinallyContainsReturnDiag);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
|
2007-11-07 12:08:17 +08:00
|
|
|
// Get the start location and compute the semi location.
|
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
assert((*startBuf == '@') && "bogus @try location");
|
|
|
|
|
|
|
|
std::string buf;
|
|
|
|
// declare a new scope with two variables, _stack and _rethrow.
|
|
|
|
buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
|
|
|
|
buf += "int buf[18/*32-bit i386*/];\n";
|
|
|
|
buf += "char *pointers[4];} _stack;\n";
|
|
|
|
buf += "id volatile _rethrow = 0;\n";
|
|
|
|
buf += "objc_exception_try_enter(&_stack);\n";
|
2007-11-08 02:43:40 +08:00
|
|
|
buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
|
2007-11-07 12:08:17 +08:00
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, 4, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
startLoc = S->getTryBody()->getLocEnd();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
|
|
|
|
|
|
|
assert((*startBuf == '}') && "bogus @try block");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-16 23:31:30 +08:00
|
|
|
SourceLocation lastCurlyLoc = startLoc;
|
|
|
|
ObjCAtCatchStmt *catchList = S->getCatchStmts();
|
|
|
|
if (catchList) {
|
|
|
|
startLoc = startLoc.getFileLocWithOffset(1);
|
|
|
|
buf = " /* @catch begin */ else {\n";
|
|
|
|
buf += " id _caught = objc_exception_extract(&_stack);\n";
|
|
|
|
buf += " objc_exception_try_enter (&_stack);\n";
|
|
|
|
buf += " if (_setjmp(_stack.buf))\n";
|
|
|
|
buf += " _rethrow = objc_exception_extract(&_stack);\n";
|
|
|
|
buf += " else { /* @catch continue */";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-16 23:31:30 +08:00
|
|
|
InsertText(startLoc, buf.c_str(), buf.size());
|
2008-09-10 03:59:12 +08:00
|
|
|
} else { /* no catch list */
|
|
|
|
buf = "}\nelse {\n";
|
|
|
|
buf += " _rethrow = objc_exception_extract(&_stack);\n";
|
|
|
|
buf += "}";
|
|
|
|
ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size());
|
2008-07-16 23:31:30 +08:00
|
|
|
}
|
2007-11-07 12:08:17 +08:00
|
|
|
bool sawIdTypedCatch = false;
|
|
|
|
Stmt *lastCatchBody = 0;
|
|
|
|
while (catchList) {
|
2009-03-04 03:52:17 +08:00
|
|
|
ParmVarDecl *catchDecl = catchList->getCatchParamDecl();
|
2007-11-07 12:08:17 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (catchList == S->getCatchStmts())
|
2007-11-07 12:08:17 +08:00
|
|
|
buf = "if ("; // we are generating code for the first catch clause
|
|
|
|
else
|
|
|
|
buf = "else if (";
|
|
|
|
startLoc = catchList->getLocStart();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
assert((*startBuf == '@') && "bogus @catch location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
const char *lParenLoc = strchr(startBuf, '(');
|
|
|
|
|
2008-02-02 06:08:12 +08:00
|
|
|
if (catchList->hasEllipsis()) {
|
2008-02-02 04:02:07 +08:00
|
|
|
// Now rewrite the body...
|
|
|
|
lastCatchBody = catchList->getCatchBody();
|
|
|
|
SourceLocation bodyLoc = lastCatchBody->getLocStart();
|
|
|
|
const char *bodyBuf = SM->getCharacterData(bodyLoc);
|
2008-04-08 13:52:18 +08:00
|
|
|
assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' &&
|
|
|
|
"bogus @catch paren location");
|
2008-02-02 04:02:07 +08:00
|
|
|
assert((*bodyBuf == '{') && "bogus @catch body location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-02 04:02:07 +08:00
|
|
|
buf += "1) { id _tmp = _caught;";
|
2009-08-20 03:10:30 +08:00
|
|
|
Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf);
|
2009-03-04 03:52:17 +08:00
|
|
|
} else if (catchDecl) {
|
|
|
|
QualType t = catchDecl->getType();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (t == Context->getObjCIdType()) {
|
2007-11-07 12:08:17 +08:00
|
|
|
buf += "1) { ";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size());
|
2007-11-07 12:08:17 +08:00
|
|
|
sawIdTypedCatch = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (const PointerType *pType = t->getAs<PointerType>()) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceType *cls; // Should be a pointer to a class.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr());
|
2007-11-07 12:08:17 +08:00
|
|
|
if (cls) {
|
2007-11-08 02:43:40 +08:00
|
|
|
buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
|
2008-11-24 13:29:24 +08:00
|
|
|
buf += cls->getDecl()->getNameAsString();
|
2007-11-08 02:43:40 +08:00
|
|
|
buf += "\"), (struct objc_object *)_caught)) { ";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size());
|
2007-11-07 12:08:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now rewrite the body...
|
|
|
|
lastCatchBody = catchList->getCatchBody();
|
|
|
|
SourceLocation rParenLoc = catchList->getRParenLoc();
|
|
|
|
SourceLocation bodyLoc = lastCatchBody->getLocStart();
|
|
|
|
const char *bodyBuf = SM->getCharacterData(bodyLoc);
|
|
|
|
const char *rParenBuf = SM->getCharacterData(rParenLoc);
|
|
|
|
assert((*rParenBuf == ')') && "bogus @catch paren location");
|
|
|
|
assert((*bodyBuf == '{') && "bogus @catch body location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
buf = " = _caught;";
|
2009-09-09 23:08:12 +08:00
|
|
|
// Here we replace ") {" with "= _caught;" (which initializes and
|
2007-11-07 12:08:17 +08:00
|
|
|
// declares the @catch parameter).
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size());
|
2009-03-04 03:52:17 +08:00
|
|
|
} else {
|
2007-11-07 12:08:17 +08:00
|
|
|
assert(false && "@catch rewrite bug");
|
2007-11-07 23:32:26 +08:00
|
|
|
}
|
2008-02-02 04:02:07 +08:00
|
|
|
// make sure all the catch bodies get rewritten!
|
2007-11-07 12:08:17 +08:00
|
|
|
catchList = catchList->getNextCatchStmt();
|
|
|
|
}
|
|
|
|
// Complete the catch list...
|
|
|
|
if (lastCatchBody) {
|
|
|
|
SourceLocation bodyLoc = lastCatchBody->getLocEnd();
|
2008-04-08 13:52:18 +08:00
|
|
|
assert(*SM->getCharacterData(bodyLoc) == '}' &&
|
|
|
|
"bogus @catch body location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-11 23:29:03 +08:00
|
|
|
// Insert the last (implicit) else clause *before* the right curly brace.
|
|
|
|
bodyLoc = bodyLoc.getFileLocWithOffset(-1);
|
|
|
|
buf = "} /* last catch end */\n";
|
|
|
|
buf += "else {\n";
|
|
|
|
buf += " _rethrow = _caught;\n";
|
|
|
|
buf += " objc_exception_try_exit(&_stack);\n";
|
|
|
|
buf += "} } /* @catch end */\n";
|
|
|
|
if (!S->getFinallyStmt())
|
|
|
|
buf += "}\n";
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(bodyLoc, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
// Set lastCurlyLoc
|
|
|
|
lastCurlyLoc = lastCatchBody->getLocEnd();
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
|
2007-11-07 12:08:17 +08:00
|
|
|
startLoc = finalStmt->getLocStart();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
|
|
|
assert((*startBuf == '@') && "bogus @finally start");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
buf = "/* @finally */";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, 8, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
Stmt *body = finalStmt->getFinallyBody();
|
|
|
|
SourceLocation startLoc = body->getLocStart();
|
|
|
|
SourceLocation endLoc = body->getLocEnd();
|
2008-04-08 13:52:18 +08:00
|
|
|
assert(*SM->getCharacterData(startLoc) == '{' &&
|
|
|
|
"bogus @finally body location");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(*SM->getCharacterData(endLoc) == '}' &&
|
2008-04-08 13:52:18 +08:00
|
|
|
"bogus @finally body location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
startLoc = startLoc.getFileLocWithOffset(1);
|
|
|
|
buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(startLoc, buf.c_str(), buf.size());
|
2007-11-07 12:08:17 +08:00
|
|
|
endLoc = endLoc.getFileLocWithOffset(-1);
|
|
|
|
buf = " if (_rethrow) objc_exception_throw(_rethrow);\n";
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(endLoc, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 12:08:17 +08:00
|
|
|
// Set lastCurlyLoc
|
|
|
|
lastCurlyLoc = body->getLocEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-06 01:03:39 +08:00
|
|
|
// Now check for any return/continue/go statements within the @try.
|
|
|
|
WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody());
|
2008-09-11 23:29:03 +08:00
|
|
|
} else { /* no finally clause - make sure we synthesize an implicit one */
|
|
|
|
buf = "{ /* implicit finally clause */\n";
|
|
|
|
buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
|
|
|
|
buf += " if (_rethrow) objc_exception_throw(_rethrow);\n";
|
|
|
|
buf += "}";
|
|
|
|
ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size());
|
2007-11-07 12:08:17 +08:00
|
|
|
}
|
|
|
|
// Now emit the final closing curly brace...
|
|
|
|
lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1);
|
|
|
|
buf = " } /* @try scope end */\n";
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(lastCurlyLoc, buf.c_str(), buf.size());
|
2007-11-06 01:47:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) {
|
2007-11-06 01:47:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) {
|
2007-11-06 01:47:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// This can't be done with ReplaceStmt(S, ThrowExpr), since
|
|
|
|
// the throw expression is typically a message expression that's already
|
2007-11-07 23:32:26 +08:00
|
|
|
// been rewritten! (which implies the SourceLocation's are invalid).
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
|
2007-11-07 23:32:26 +08:00
|
|
|
// Get the start location and compute the semi location.
|
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 23:32:26 +08:00
|
|
|
assert((*startBuf == '@') && "bogus @throw location");
|
|
|
|
|
|
|
|
std::string buf;
|
|
|
|
/* void objc_exception_throw(id) __attribute__((noreturn)); */
|
2008-01-19 08:42:38 +08:00
|
|
|
if (S->getThrowExpr())
|
|
|
|
buf = "objc_exception_throw(";
|
|
|
|
else // add an implicit argument
|
|
|
|
buf = "objc_exception_throw(_caught";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-25 23:41:30 +08:00
|
|
|
// handle "@ throw" correctly.
|
|
|
|
const char *wBuf = strchr(startBuf, 'w');
|
|
|
|
assert((*wBuf == 'w') && "@throw: can't find 'w'");
|
|
|
|
ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-07 23:32:26 +08:00
|
|
|
const char *semiBuf = strchr(startBuf, ';');
|
|
|
|
assert((*semiBuf == ';') && "@throw: can't find ';'");
|
|
|
|
SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf);
|
|
|
|
buf = ");";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(semiLoc, 1, buf.c_str(), buf.size());
|
2007-11-07 23:32:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-06 01:47:33 +08:00
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
|
2007-10-18 06:35:30 +08:00
|
|
|
// Create a new string expression.
|
|
|
|
QualType StrType = Context->getPointerType(Context->CharTy);
|
2007-10-29 13:01:08 +08:00
|
|
|
std::string StrEncoding;
|
2008-10-18 04:21:44 +08:00
|
|
|
Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
|
2009-02-18 14:40:38 +08:00
|
|
|
Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(),
|
|
|
|
StrEncoding.length(), false,StrType,
|
|
|
|
SourceLocation());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, Replacement);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-01 06:53:43 +08:00
|
|
|
// Replace this subexpr in the parent.
|
2008-12-05 07:50:32 +08:00
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2007-10-25 00:57:36 +08:00
|
|
|
return Replacement;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
|
2008-12-23 06:16:07 +08:00
|
|
|
if (!SelGetUidFunctionDecl)
|
|
|
|
SynthSelGetUidFunctionDecl();
|
2007-11-05 22:50:49 +08:00
|
|
|
assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
|
|
|
|
// Create a call to sel_registerName("selName").
|
|
|
|
llvm::SmallVector<Expr*, 8> SelExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
SelExprs.push_back(StringLiteral::Create(*Context,
|
2009-02-07 03:55:15 +08:00
|
|
|
Exp->getSelector().getAsString().c_str(),
|
2008-11-24 11:33:13 +08:00
|
|
|
Exp->getSelector().getAsString().size(),
|
2009-02-18 13:49:11 +08:00
|
|
|
false, argType, SourceLocation()));
|
2007-11-05 22:50:49 +08:00
|
|
|
CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
|
|
|
|
&SelExprs[0], SelExprs.size());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, SelExp);
|
2008-12-05 07:50:32 +08:00
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2007-11-05 22:50:49 +08:00
|
|
|
return SelExp;
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl(
|
2007-10-25 06:48:43 +08:00
|
|
|
FunctionDecl *FD, Expr **args, unsigned nargs) {
|
2007-10-24 07:50:29 +08:00
|
|
|
// Get the type, we will need to reference it in a couple spots.
|
2007-10-25 06:48:43 +08:00
|
|
|
QualType msgSendType = FD->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 07:50:29 +08:00
|
|
|
// Create a reference to the objc_msgSend() declaration.
|
2009-02-07 09:47:29 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 07:50:29 +08:00
|
|
|
// Now, we cast the reference to a pointer to the objc_msgSend type.
|
2007-10-25 01:06:59 +08:00
|
|
|
QualType pToFunc = Context->getPointerType(msgSendType);
|
2009-09-09 23:08:12 +08:00
|
|
|
ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc,
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
DRE,
|
Implement support for operator overloading using candidate operator
functions for built-in operators, e.g., the builtin
bool operator==(int const*, int const*)
can be used for the expression "x1 == x2" given:
struct X {
operator int const*();
} x1, x2;
The scheme for handling these built-in operators is relatively simple:
for each candidate required by the standard, create a special kind of
candidate function for the built-in. If overload resolution picks the
built-in operator, we perform the appropriate conversions on the
arguments and then let the normal built-in operator take care of it.
There may be some optimization opportunity left: if we can reduce the
number of built-in operator overloads we generate, overload resolution
for these cases will go faster. However, one must be careful when
doing this: GCC generates too few operator overloads in our little
test program, and fails to compile it because none of the overloads it
generates match.
Note that we only support operator overload for non-member binary
operators at the moment. The other operators will follow.
As part of this change, ImplicitCastExpr can now be an lvalue.
llvm-svn: 59148
2008-11-13 01:17:38 +08:00
|
|
|
/*isLvalue=*/false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 07:50:29 +08:00
|
|
|
const FunctionType *FT = msgSendType->getAsFunctionType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 04:51:47 +08:00
|
|
|
return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(),
|
|
|
|
SourceLocation());
|
2007-10-25 06:48:43 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 21:24:47 +08:00
|
|
|
static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
|
|
|
|
const char *&startRef, const char *&endRef) {
|
|
|
|
while (startBuf < endBuf) {
|
|
|
|
if (*startBuf == '<')
|
|
|
|
startRef = startBuf; // mark the start.
|
|
|
|
if (*startBuf == '>') {
|
2007-11-09 20:50:28 +08:00
|
|
|
if (startRef && *startRef == '<') {
|
|
|
|
endRef = startBuf; // mark the end.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-11-01 21:24:47 +08:00
|
|
|
}
|
|
|
|
startBuf++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-12 06:50:14 +08:00
|
|
|
static void scanToNextArgument(const char *&argRef) {
|
|
|
|
int angle = 0;
|
|
|
|
while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
|
|
|
|
if (*argRef == '<')
|
|
|
|
angle++;
|
|
|
|
else if (*argRef == '>')
|
|
|
|
angle--;
|
|
|
|
argRef++;
|
|
|
|
}
|
|
|
|
assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
|
|
|
|
}
|
2007-12-12 07:04:08 +08:00
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
bool RewriteObjC::needToScanForQualifiers(QualType T) {
|
2009-07-18 23:33:26 +08:00
|
|
|
return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType();
|
2007-11-01 21:24:47 +08:00
|
|
|
}
|
|
|
|
|
2008-07-30 02:15:38 +08:00
|
|
|
void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
|
|
|
|
QualType Type = E->getType();
|
|
|
|
if (needToScanForQualifiers(Type)) {
|
2008-11-20 05:15:47 +08:00
|
|
|
SourceLocation Loc, EndLoc;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-20 05:15:47 +08:00
|
|
|
if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) {
|
|
|
|
Loc = ECE->getLParenLoc();
|
|
|
|
EndLoc = ECE->getRParenLoc();
|
|
|
|
} else {
|
|
|
|
Loc = E->getLocStart();
|
|
|
|
EndLoc = E->getLocEnd();
|
|
|
|
}
|
|
|
|
// This will defend against trying to rewrite synthesized expressions.
|
|
|
|
if (Loc.isInvalid() || EndLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
2008-07-30 02:15:38 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(Loc);
|
2008-11-20 05:15:47 +08:00
|
|
|
const char *endBuf = SM->getCharacterData(EndLoc);
|
2008-07-30 02:15:38 +08:00
|
|
|
const char *startRef = 0, *endRef = 0;
|
|
|
|
if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
|
|
|
|
// Get the locations of the startRef, endRef.
|
|
|
|
SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf);
|
|
|
|
SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1);
|
|
|
|
// Comment out the protocol references.
|
|
|
|
InsertText(LessLoc, "/*", 2);
|
|
|
|
InsertText(GreaterLoc, "*/", 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
|
2007-12-12 06:50:14 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
QualType Type;
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *proto = 0;
|
2007-12-12 06:50:14 +08:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
|
|
|
|
Loc = VD->getLocation();
|
|
|
|
Type = VD->getType();
|
|
|
|
}
|
|
|
|
else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
|
|
|
|
Loc = FD->getLocation();
|
|
|
|
// Check for ObjC 'id' and class types that have been adorned with protocol
|
|
|
|
// information (id<p>, C<p>*). The protocol references need to be rewritten!
|
|
|
|
const FunctionType *funcType = FD->getType()->getAsFunctionType();
|
|
|
|
assert(funcType && "missing function type");
|
2009-02-27 07:50:07 +08:00
|
|
|
proto = dyn_cast<FunctionProtoType>(funcType);
|
2007-12-12 06:50:14 +08:00
|
|
|
if (!proto)
|
|
|
|
return;
|
|
|
|
Type = proto->getResultType();
|
|
|
|
}
|
|
|
|
else
|
2007-12-12 03:56:36 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 06:50:14 +08:00
|
|
|
if (needToScanForQualifiers(Type)) {
|
2007-11-01 21:24:47 +08:00
|
|
|
// Since types are unique, we need to scan the buffer.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-01 21:24:47 +08:00
|
|
|
const char *endBuf = SM->getCharacterData(Loc);
|
|
|
|
const char *startBuf = endBuf;
|
2008-05-31 13:02:17 +08:00
|
|
|
while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart)
|
2007-11-01 21:24:47 +08:00
|
|
|
startBuf--; // scan backward (from the decl location) for return type.
|
|
|
|
const char *startRef = 0, *endRef = 0;
|
|
|
|
if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
|
|
|
|
// Get the locations of the startRef, endRef.
|
|
|
|
SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
|
|
|
|
SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
|
|
|
|
// Comment out the protocol references.
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(LessLoc, "/*", 2);
|
|
|
|
InsertText(GreaterLoc, "*/", 2);
|
2007-11-01 21:24:47 +08:00
|
|
|
}
|
|
|
|
}
|
2007-12-12 06:50:14 +08:00
|
|
|
if (!proto)
|
|
|
|
return; // most likely, was a variable
|
2007-10-31 12:38:33 +08:00
|
|
|
// Now check arguments.
|
2007-12-12 06:50:14 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(Loc);
|
|
|
|
const char *startFuncBuf = startBuf;
|
2007-10-31 12:38:33 +08:00
|
|
|
for (unsigned i = 0; i < proto->getNumArgs(); i++) {
|
2007-11-01 21:24:47 +08:00
|
|
|
if (needToScanForQualifiers(proto->getArgType(i))) {
|
|
|
|
// Since types are unique, we need to scan the buffer.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-01 21:24:47 +08:00
|
|
|
const char *endBuf = startBuf;
|
2007-12-12 06:50:14 +08:00
|
|
|
// scan forward (from the decl location) for argument types.
|
|
|
|
scanToNextArgument(endBuf);
|
2007-11-01 21:24:47 +08:00
|
|
|
const char *startRef = 0, *endRef = 0;
|
|
|
|
if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
|
|
|
|
// Get the locations of the startRef, endRef.
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation LessLoc =
|
2007-12-12 07:04:08 +08:00
|
|
|
Loc.getFileLocWithOffset(startRef-startFuncBuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation GreaterLoc =
|
2007-12-12 07:04:08 +08:00
|
|
|
Loc.getFileLocWithOffset(endRef-startFuncBuf+1);
|
2007-11-01 21:24:47 +08:00
|
|
|
// Comment out the protocol references.
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(LessLoc, "/*", 2);
|
|
|
|
InsertText(GreaterLoc, "*/", 2);
|
2007-11-01 21:24:47 +08:00
|
|
|
}
|
2007-12-12 06:50:14 +08:00
|
|
|
startBuf = ++endBuf;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-06 23:58:23 +08:00
|
|
|
// If the function name is derived from a macro expansion, then the
|
|
|
|
// argument buffer will not follow the name. Need to speak with Chris.
|
|
|
|
while (*startBuf && *startBuf != ')' && *startBuf != ',')
|
2007-12-12 06:50:14 +08:00
|
|
|
startBuf++; // scan forward (from the decl location) for argument types.
|
|
|
|
startBuf++;
|
|
|
|
}
|
2007-10-31 12:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 05:47:40 +08:00
|
|
|
// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthSelGetUidFunctionDecl() {
|
2007-12-05 05:47:40 +08:00
|
|
|
IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
ArgTys.push_back(Context->getPointerType(
|
|
|
|
Context->CharTy.getQualifiedType(QualType::Const)));
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(),
|
2007-12-05 05:47:40 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
false /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
SelGetUidIdent, getFuncType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-12-05 05:47:40 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) {
|
2007-10-31 07:14:51 +08:00
|
|
|
// declared in <objc/objc.h>
|
2009-01-09 09:47:02 +08:00
|
|
|
if (FD->getIdentifier() &&
|
|
|
|
strcmp(FD->getNameAsCString(), "sel_registerName") == 0) {
|
2007-10-31 07:14:51 +08:00
|
|
|
SelGetUidFunctionDecl = FD;
|
2007-10-31 12:38:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCQualifiedInterfaceTypes(FD);
|
2007-10-31 07:14:51 +08:00
|
|
|
}
|
|
|
|
|
2008-03-12 01:37:02 +08:00
|
|
|
// SynthSuperContructorFunctionDecl - id objc_super(id obj, id super);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthSuperContructorFunctionDecl() {
|
2008-03-12 01:37:02 +08:00
|
|
|
if (SuperContructorFunctionDecl)
|
|
|
|
return;
|
2008-12-24 04:11:22 +08:00
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
|
2008-03-12 01:37:02 +08:00
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
QualType argT = Context->getObjCIdType();
|
|
|
|
assert(!argT.isNull() && "Can't find 'id' type");
|
|
|
|
ArgTys.push_back(argT);
|
|
|
|
ArgTys.push_back(argT);
|
|
|
|
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
|
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
false, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2008-03-12 01:37:02 +08:00
|
|
|
}
|
|
|
|
|
2007-10-31 07:14:51 +08:00
|
|
|
// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendFunctionDecl() {
|
2007-10-31 07:14:51 +08:00
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType argT = Context->getObjCIdType();
|
2007-10-31 07:14:51 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'id' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
argT = Context->getObjCSelType();
|
2007-10-31 07:14:51 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'SEL' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-10-31 07:14:51 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
true /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-10-31 07:14:51 +08:00
|
|
|
}
|
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendSuperFunctionDecl() {
|
2007-11-15 18:28:18 +08:00
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-09-05 09:34:33 +08:00
|
|
|
&Context->Idents.get("objc_super"));
|
2007-11-15 18:28:18 +08:00
|
|
|
QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
|
|
|
|
assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
argT = Context->getObjCSelType();
|
2007-11-15 18:28:18 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'SEL' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-11-15 18:28:18 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
true /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-11-15 18:28:18 +08:00
|
|
|
}
|
|
|
|
|
2007-12-04 03:17:29 +08:00
|
|
|
// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendStretFunctionDecl() {
|
2007-12-04 03:17:29 +08:00
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType argT = Context->getObjCIdType();
|
2007-12-04 03:17:29 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'id' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
argT = Context->getObjCSelType();
|
2007-12-04 03:17:29 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'SEL' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-12-04 03:17:29 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
true /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-12-04 03:17:29 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// SynthMsgSendSuperStretFunctionDecl -
|
2007-12-04 03:17:29 +08:00
|
|
|
// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() {
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo *msgSendIdent =
|
2007-12-04 03:17:29 +08:00
|
|
|
&Context->Idents.get("objc_msgSendSuper_stret");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-09-05 09:34:33 +08:00
|
|
|
&Context->Idents.get("objc_super"));
|
2007-12-04 03:17:29 +08:00
|
|
|
QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
|
|
|
|
assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
argT = Context->getObjCSelType();
|
2007-12-04 03:17:29 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'SEL' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-12-04 03:17:29 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
true /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-12-04 03:17:29 +08:00
|
|
|
}
|
|
|
|
|
2008-05-09 06:02:18 +08:00
|
|
|
// SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendFpretFunctionDecl() {
|
2007-12-04 05:26:48 +08:00
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType argT = Context->getObjCIdType();
|
2007-12-04 05:26:48 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'id' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-01-08 03:49:32 +08:00
|
|
|
argT = Context->getObjCSelType();
|
2007-12-04 05:26:48 +08:00
|
|
|
assert(!argT.isNull() && "Can't find 'SEL' type");
|
|
|
|
ArgTys.push_back(argT);
|
2008-05-09 06:02:18 +08:00
|
|
|
QualType msgSendType = Context->getFunctionType(Context->DoubleTy,
|
2007-12-04 05:26:48 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
true /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
msgSendIdent, msgSendType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-12-04 05:26:48 +08:00
|
|
|
}
|
|
|
|
|
2007-10-31 07:14:51 +08:00
|
|
|
// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthGetClassFunctionDecl() {
|
2007-10-31 07:14:51 +08:00
|
|
|
IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
ArgTys.push_back(Context->getPointerType(
|
|
|
|
Context->CharTy.getQualifiedType(QualType::Const)));
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-10-31 07:14:51 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
false /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
getClassIdent, getClassType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-10-31 07:14:51 +08:00
|
|
|
}
|
|
|
|
|
2007-12-07 11:50:46 +08:00
|
|
|
// SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthGetMetaClassFunctionDecl() {
|
2007-12-07 11:50:46 +08:00
|
|
|
IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
ArgTys.push_back(Context->getPointerType(
|
|
|
|
Context->CharTy.getQualifiedType(QualType::Const)));
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
|
2007-12-07 11:50:46 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
false /*isVariadic*/, 0);
|
2008-04-17 22:40:12 +08:00
|
|
|
GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
getClassIdent, getClassType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
FunctionDecl::Extern, false);
|
2007-12-07 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
|
2008-03-15 08:55:56 +08:00
|
|
|
QualType strType = getConstantStringStructType();
|
|
|
|
|
|
|
|
std::string S = "__NSConstantStringImpl_";
|
2008-05-31 11:35:42 +08:00
|
|
|
|
|
|
|
std::string tmpName = InFileName;
|
|
|
|
unsigned i;
|
|
|
|
for (i=0; i < tmpName.length(); i++) {
|
|
|
|
char c = tmpName.at(i);
|
|
|
|
// replace any non alphanumeric characters with '_'.
|
|
|
|
if (!isalpha(c) && (c < '0' || c > '9'))
|
|
|
|
tmpName[i] = '_';
|
|
|
|
}
|
|
|
|
S += tmpName;
|
|
|
|
S += "_";
|
2008-03-15 08:55:56 +08:00
|
|
|
S += utostr(NumObjCStringLiterals++);
|
|
|
|
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "static __NSConstantStringImpl " + S;
|
|
|
|
Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
|
|
|
|
Preamble += "0x000007c8,"; // utf8_str
|
2008-03-15 08:55:56 +08:00
|
|
|
// The pretty printer for StringLiteral handles escape characters properly.
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string prettyBufS;
|
|
|
|
llvm::raw_string_ostream prettyBuf(prettyBufS);
|
2009-06-30 09:26:17 +08:00
|
|
|
Exp->getString()->printPretty(prettyBuf, *Context, 0,
|
|
|
|
PrintingPolicy(LangOpts));
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += prettyBuf.str();
|
|
|
|
Preamble += ",";
|
2008-03-15 09:36:04 +08:00
|
|
|
// The minus 2 removes the begin/end double quotes.
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += utostr(prettyBuf.str().size()-2) + "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
|
|
|
|
&Context->Idents.get(S.c_str()), strType, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
VarDecl::Static);
|
2009-02-07 09:47:29 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation());
|
|
|
|
Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(DRE->getType()),
|
2008-03-15 08:55:56 +08:00
|
|
|
SourceLocation());
|
2007-11-08 22:30:50 +08:00
|
|
|
// cast to NSConstantString *
|
2009-09-09 23:08:12 +08:00
|
|
|
CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
Unop, Exp->getType(),
|
|
|
|
SourceLocation(),
|
2009-07-31 08:48:10 +08:00
|
|
|
SourceLocation());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, cast);
|
2008-12-05 07:50:32 +08:00
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2007-11-08 22:30:50 +08:00
|
|
|
return cast;
|
2007-11-03 19:27:19 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) {
|
2007-12-07 11:50:46 +08:00
|
|
|
// check if we are sending a message to 'super'
|
2009-01-10 01:18:27 +08:00
|
|
|
if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-04 22:56:14 +08:00
|
|
|
if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
const ObjCObjectPointerType *OPT =
|
2009-07-11 07:34:53 +08:00
|
|
|
Super->getType()->getAsObjCObjectPointerType();
|
|
|
|
assert(OPT);
|
|
|
|
const ObjCInterfaceType *IT = OPT->getInterfaceType();
|
2008-06-22 02:04:54 +08:00
|
|
|
return IT->getDecl();
|
|
|
|
}
|
|
|
|
return 0;
|
2007-11-15 18:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// struct objc_super { struct objc_object *receiver; struct objc_class *super; };
|
2008-04-15 06:03:09 +08:00
|
|
|
QualType RewriteObjC::getSuperStructType() {
|
2007-11-15 18:28:18 +08:00
|
|
|
if (!SuperStructDecl) {
|
2008-06-10 07:19:58 +08:00
|
|
|
SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2008-09-05 09:34:33 +08:00
|
|
|
&Context->Idents.get("objc_super"));
|
2007-11-15 18:28:18 +08:00
|
|
|
QualType FieldTypes[2];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_object *receiver;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[0] = Context->getObjCIdType();
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_class *super;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[1] = Context->getObjCClassType();
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// Create fields
|
2008-12-12 00:49:14 +08:00
|
|
|
for (unsigned i = 0; i < 2; ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
|
|
|
|
SourceLocation(), 0,
|
2009-08-19 09:27:57 +08:00
|
|
|
FieldTypes[i], 0,
|
|
|
|
/*BitWidth=*/0,
|
2009-01-20 09:17:11 +08:00
|
|
|
/*Mutable=*/false));
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 00:49:14 +08:00
|
|
|
SuperStructDecl->completeDefinition(*Context);
|
2007-11-15 18:28:18 +08:00
|
|
|
}
|
|
|
|
return Context->getTagDeclType(SuperStructDecl);
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
QualType RewriteObjC::getConstantStringStructType() {
|
2008-03-15 08:55:56 +08:00
|
|
|
if (!ConstantStringDecl) {
|
2008-06-10 07:19:58 +08:00
|
|
|
ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2008-09-05 09:34:33 +08:00
|
|
|
&Context->Idents.get("__NSConstantStringImpl"));
|
2008-03-15 08:55:56 +08:00
|
|
|
QualType FieldTypes[4];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-15 08:55:56 +08:00
|
|
|
// struct objc_object *receiver;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[0] = Context->getObjCIdType();
|
2008-03-15 08:55:56 +08:00
|
|
|
// int flags;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[1] = Context->IntTy;
|
2008-03-15 08:55:56 +08:00
|
|
|
// char *str;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[2] = Context->getPointerType(Context->CharTy);
|
2008-03-15 08:55:56 +08:00
|
|
|
// long length;
|
2009-09-09 23:08:12 +08:00
|
|
|
FieldTypes[3] = Context->LongTy;
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2008-03-15 08:55:56 +08:00
|
|
|
// Create fields
|
2008-12-12 00:49:14 +08:00
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
|
|
|
|
ConstantStringDecl,
|
2008-12-12 00:49:14 +08:00
|
|
|
SourceLocation(), 0,
|
2009-08-19 09:27:57 +08:00
|
|
|
FieldTypes[i], 0,
|
2008-12-12 00:49:14 +08:00
|
|
|
/*BitWidth=*/0,
|
2009-01-20 09:17:11 +08:00
|
|
|
/*Mutable=*/true));
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstantStringDecl->completeDefinition(*Context);
|
2008-03-15 08:55:56 +08:00
|
|
|
}
|
|
|
|
return Context->getTagDeclType(ConstantStringDecl);
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
|
2007-12-05 05:47:40 +08:00
|
|
|
if (!SelGetUidFunctionDecl)
|
|
|
|
SynthSelGetUidFunctionDecl();
|
2007-10-31 07:14:51 +08:00
|
|
|
if (!MsgSendFunctionDecl)
|
|
|
|
SynthMsgSendFunctionDecl();
|
2007-11-15 18:28:18 +08:00
|
|
|
if (!MsgSendSuperFunctionDecl)
|
|
|
|
SynthMsgSendSuperFunctionDecl();
|
2007-12-04 03:17:29 +08:00
|
|
|
if (!MsgSendStretFunctionDecl)
|
|
|
|
SynthMsgSendStretFunctionDecl();
|
|
|
|
if (!MsgSendSuperStretFunctionDecl)
|
|
|
|
SynthMsgSendSuperStretFunctionDecl();
|
2007-12-04 05:26:48 +08:00
|
|
|
if (!MsgSendFpretFunctionDecl)
|
|
|
|
SynthMsgSendFpretFunctionDecl();
|
2007-10-31 07:14:51 +08:00
|
|
|
if (!GetClassFunctionDecl)
|
|
|
|
SynthGetClassFunctionDecl();
|
2007-12-07 11:50:46 +08:00
|
|
|
if (!GetMetaClassFunctionDecl)
|
|
|
|
SynthGetMetaClassFunctionDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// default to objc_msgSend().
|
2007-12-04 03:17:29 +08:00
|
|
|
FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
|
|
|
|
// May need to use objc_msgSend_stret() as well.
|
|
|
|
FunctionDecl *MsgSendStretFlavor = 0;
|
2009-04-30 00:37:50 +08:00
|
|
|
if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
|
|
|
|
QualType resultType = mDecl->getResultType();
|
2008-07-27 06:36:27 +08:00
|
|
|
if (resultType->isStructureType() || resultType->isUnionType())
|
2007-12-04 03:17:29 +08:00
|
|
|
MsgSendStretFlavor = MsgSendStretFunctionDecl;
|
2008-07-27 06:36:27 +08:00
|
|
|
else if (resultType->isRealFloatingType())
|
2007-12-04 05:26:48 +08:00
|
|
|
MsgSendFlavor = MsgSendFpretFunctionDecl;
|
2007-12-04 03:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
// Synthesize a call to objc_msgSend().
|
|
|
|
llvm::SmallVector<Expr*, 8> MsgExprs;
|
|
|
|
IdentifierInfo *clsName = Exp->getClassName();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
// Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
|
|
|
|
if (clsName) { // class message.
|
2008-07-25 03:44:33 +08:00
|
|
|
// FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle
|
|
|
|
// the 'super' idiom within a class method.
|
2007-12-07 11:50:46 +08:00
|
|
|
if (!strcmp(clsName->getName(), "super")) {
|
|
|
|
MsgSendFlavor = MsgSendSuperFunctionDecl;
|
|
|
|
if (MsgSendStretFlavor)
|
|
|
|
MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
|
|
|
|
assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCInterfaceDecl *SuperDecl =
|
2008-10-28 01:20:55 +08:00
|
|
|
CurMethodDef->getClassInterface()->getSuperClass();
|
2007-12-07 11:50:46 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-07 11:50:46 +08:00
|
|
|
// set the receiver to self, the first argument to all methods.
|
2009-04-30 00:37:50 +08:00
|
|
|
InitExprs.push_back(
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
|
2009-04-30 00:37:50 +08:00
|
|
|
Context->getObjCIdType(),
|
|
|
|
SourceLocation()),
|
|
|
|
Context->getObjCIdType(),
|
|
|
|
SourceLocation(), SourceLocation())); // set the 'receiver'.
|
|
|
|
|
2007-12-07 11:50:46 +08:00
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2009-02-18 14:40:38 +08:00
|
|
|
ClsExprs.push_back(StringLiteral::Create(*Context,
|
2009-09-09 23:08:12 +08:00
|
|
|
SuperDecl->getIdentifier()->getName(),
|
2009-02-07 03:55:15 +08:00
|
|
|
SuperDecl->getIdentifier()->getLength(),
|
2009-02-18 13:49:11 +08:00
|
|
|
false, argType, SourceLocation()));
|
2007-12-07 11:50:46 +08:00
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
&ClsExprs[0],
|
2007-12-07 11:50:46 +08:00
|
|
|
ClsExprs.size());
|
|
|
|
// To turn off a warning, type-cast to 'id'
|
2008-10-28 03:41:14 +08:00
|
|
|
InitExprs.push_back( // set 'super class', using objc_getClass().
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2008-10-28 03:41:14 +08:00
|
|
|
Cls, Context->getObjCIdType(),
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(), SourceLocation()));
|
2007-12-07 11:50:46 +08:00
|
|
|
// struct objc_super
|
|
|
|
QualType superType = getSuperStructType();
|
2008-03-12 02:14:26 +08:00
|
|
|
Expr *SuperRep;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-12 02:14:26 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
SynthSuperContructorFunctionDecl();
|
|
|
|
// Simulate a contructor call...
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
|
2008-03-12 02:14:26 +08:00
|
|
|
superType, SourceLocation());
|
2009-02-10 04:51:47 +08:00
|
|
|
SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.size(),
|
2009-02-10 04:51:47 +08:00
|
|
|
superType, SourceLocation());
|
2008-12-24 04:11:22 +08:00
|
|
|
// The code for super is a little tricky to prevent collision with
|
|
|
|
// the structure definition in the header. The rewriter has it's own
|
|
|
|
// internal definition (__rw_objc_super) that is uses. This is why
|
|
|
|
// we need the cast below. For example:
|
|
|
|
// (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
|
|
|
|
//
|
2009-02-07 09:47:29 +08:00
|
|
|
SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(SuperRep->getType()),
|
2008-12-24 04:11:22 +08:00
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType),
|
|
|
|
CastExpr::CK_Unknown, SuperRep,
|
2009-07-31 08:48:10 +08:00
|
|
|
Context->getPointerType(superType),
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
|
|
|
} else {
|
2008-03-12 02:14:26 +08:00
|
|
|
// (struct objc_super) { <exprs from above> }
|
2009-09-09 23:08:12 +08:00
|
|
|
InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(),
|
|
|
|
&InitExprs[0], InitExprs.size(),
|
2009-01-29 05:54:33 +08:00
|
|
|
SourceLocation());
|
2009-02-07 09:47:29 +08:00
|
|
|
SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE,
|
2008-10-27 07:43:26 +08:00
|
|
|
false);
|
2008-12-24 04:11:22 +08:00
|
|
|
// struct objc_super *
|
2009-02-07 09:47:29 +08:00
|
|
|
SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(SuperRep->getType()),
|
2008-12-24 04:11:22 +08:00
|
|
|
SourceLocation());
|
2008-03-12 02:14:26 +08:00
|
|
|
}
|
2008-12-24 04:11:22 +08:00
|
|
|
MsgExprs.push_back(SuperRep);
|
2007-12-07 11:50:46 +08:00
|
|
|
} else {
|
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2009-02-18 14:40:38 +08:00
|
|
|
ClsExprs.push_back(StringLiteral::Create(*Context,
|
2009-09-09 23:08:12 +08:00
|
|
|
clsName->getName(),
|
2009-02-18 14:40:38 +08:00
|
|
|
clsName->getLength(),
|
|
|
|
false, argType,
|
|
|
|
SourceLocation()));
|
2007-12-07 11:50:46 +08:00
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
&ClsExprs[0],
|
2007-12-07 11:50:46 +08:00
|
|
|
ClsExprs.size());
|
|
|
|
MsgExprs.push_back(Cls);
|
|
|
|
}
|
2007-11-15 07:54:14 +08:00
|
|
|
} else { // instance message.
|
|
|
|
Expr *recExpr = Exp->getReceiver();
|
2007-11-15 18:28:18 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) {
|
2007-11-15 18:28:18 +08:00
|
|
|
MsgSendFlavor = MsgSendSuperFunctionDecl;
|
2007-12-04 03:17:29 +08:00
|
|
|
if (MsgSendStretFlavor)
|
|
|
|
MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
|
2007-11-15 18:28:18 +08:00
|
|
|
assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-05 06:32:58 +08:00
|
|
|
InitExprs.push_back(
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
|
2008-07-17 06:35:27 +08:00
|
|
|
Context->getObjCIdType(),
|
2008-10-28 03:41:14 +08:00
|
|
|
SourceLocation()),
|
|
|
|
Context->getObjCIdType(),
|
2008-11-04 07:29:32 +08:00
|
|
|
SourceLocation(), SourceLocation())); // set the 'receiver'.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
ClsExprs.push_back(StringLiteral::Create(*Context,
|
|
|
|
SuperDecl->getIdentifier()->getName(),
|
2009-02-07 03:55:15 +08:00
|
|
|
SuperDecl->getIdentifier()->getLength(),
|
2009-02-18 13:49:11 +08:00
|
|
|
false, argType, SourceLocation()));
|
2007-11-15 18:28:18 +08:00
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
&ClsExprs[0],
|
2007-12-05 06:32:58 +08:00
|
|
|
ClsExprs.size());
|
2007-12-06 01:29:46 +08:00
|
|
|
// To turn off a warning, type-cast to 'id'
|
2007-12-05 06:32:58 +08:00
|
|
|
InitExprs.push_back(
|
2008-10-28 03:41:14 +08:00
|
|
|
// set 'super class', using objc_getClass().
|
2009-09-09 23:08:12 +08:00
|
|
|
new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation()));
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_super
|
|
|
|
QualType superType = getSuperStructType();
|
2008-03-12 01:37:02 +08:00
|
|
|
Expr *SuperRep;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-12 01:37:02 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
SynthSuperContructorFunctionDecl();
|
|
|
|
// Simulate a contructor call...
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
|
2008-03-12 01:37:02 +08:00
|
|
|
superType, SourceLocation());
|
2009-02-10 04:51:47 +08:00
|
|
|
SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.size(),
|
2009-02-10 04:51:47 +08:00
|
|
|
superType, SourceLocation());
|
2008-12-24 04:11:22 +08:00
|
|
|
// The code for super is a little tricky to prevent collision with
|
|
|
|
// the structure definition in the header. The rewriter has it's own
|
|
|
|
// internal definition (__rw_objc_super) that is uses. This is why
|
|
|
|
// we need the cast below. For example:
|
|
|
|
// (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
|
|
|
|
//
|
2009-02-07 09:47:29 +08:00
|
|
|
SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(SuperRep->getType()),
|
2008-12-24 04:11:22 +08:00
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2008-12-24 04:11:22 +08:00
|
|
|
SuperRep, Context->getPointerType(superType),
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2008-03-12 01:37:02 +08:00
|
|
|
} else {
|
|
|
|
// (struct objc_super) { <exprs from above> }
|
2009-09-09 23:08:12 +08:00
|
|
|
InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(),
|
|
|
|
&InitExprs[0], InitExprs.size(),
|
2009-01-29 05:54:33 +08:00
|
|
|
SourceLocation());
|
2009-02-07 09:47:29 +08:00
|
|
|
SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false);
|
2008-03-12 01:37:02 +08:00
|
|
|
}
|
2008-12-24 04:11:22 +08:00
|
|
|
MsgExprs.push_back(SuperRep);
|
2007-11-15 18:28:18 +08:00
|
|
|
} else {
|
2007-12-08 05:21:21 +08:00
|
|
|
// Remove all type-casts because it may contain objc-style types; e.g.
|
|
|
|
// Foo<Proto> *.
|
2008-10-28 23:36:24 +08:00
|
|
|
while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr))
|
2007-12-08 05:21:21 +08:00
|
|
|
recExpr = CE->getSubExpr();
|
2009-09-09 23:08:12 +08:00
|
|
|
recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown, recExpr,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getObjCIdType(),
|
2008-11-04 07:29:32 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2007-11-15 18:28:18 +08:00
|
|
|
MsgExprs.push_back(recExpr);
|
|
|
|
}
|
2007-11-15 07:54:14 +08:00
|
|
|
}
|
2007-11-03 19:27:19 +08:00
|
|
|
// Create a call to sel_registerName("selName"), it will be the 2nd argument.
|
2007-10-25 06:48:43 +08:00
|
|
|
llvm::SmallVector<Expr*, 8> SelExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
SelExprs.push_back(StringLiteral::Create(*Context,
|
2009-02-07 03:55:15 +08:00
|
|
|
Exp->getSelector().getAsString().c_str(),
|
2008-11-24 11:33:13 +08:00
|
|
|
Exp->getSelector().getAsString().size(),
|
2009-02-18 13:49:11 +08:00
|
|
|
false, argType, SourceLocation()));
|
2007-10-25 06:48:43 +08:00
|
|
|
CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
|
|
|
|
&SelExprs[0], SelExprs.size());
|
|
|
|
MsgExprs.push_back(SelExp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
// Now push any user supplied arguments.
|
|
|
|
for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
|
2007-11-15 07:54:14 +08:00
|
|
|
Expr *userExpr = Exp->getArg(i);
|
2007-11-15 10:58:25 +08:00
|
|
|
// Make all implicit casts explicit...ICE comes in handy:-)
|
|
|
|
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
|
|
|
|
// Reuse the ICE type, it is exactly what the doctor ordered.
|
2008-10-28 03:41:14 +08:00
|
|
|
QualType type = ICE->getType()->isObjCQualifiedIdType()
|
2008-01-08 03:49:32 +08:00
|
|
|
? Context->getObjCIdType()
|
2008-10-28 03:41:14 +08:00
|
|
|
: ICE->getType();
|
2009-07-31 08:48:10 +08:00
|
|
|
userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
userExpr, type, SourceLocation(),
|
2009-07-31 08:48:10 +08:00
|
|
|
SourceLocation());
|
2007-12-19 05:33:44 +08:00
|
|
|
}
|
|
|
|
// Make id<P...> cast into an 'id' cast.
|
2008-10-28 23:36:24 +08:00
|
|
|
else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) {
|
2008-01-08 03:49:32 +08:00
|
|
|
if (CE->getType()->isObjCQualifiedIdType()) {
|
2008-10-28 23:36:24 +08:00
|
|
|
while ((CE = dyn_cast<CStyleCastExpr>(userExpr)))
|
2007-12-19 05:33:44 +08:00
|
|
|
userExpr = CE->getSubExpr();
|
2009-09-09 23:08:12 +08:00
|
|
|
userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(),
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
2009-09-09 23:08:12 +08:00
|
|
|
userExpr, Context->getObjCIdType(),
|
2008-11-04 07:29:32 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2007-12-19 05:33:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2007-11-15 07:54:14 +08:00
|
|
|
MsgExprs.push_back(userExpr);
|
2009-04-30 00:37:50 +08:00
|
|
|
// We've transferred the ownership to MsgExprs. For now, we *don't* null
|
|
|
|
// out the argument in the original expression (since we aren't deleting
|
|
|
|
// the ObjCMessageExpr). See RewritePropertySetter() usage for more info.
|
|
|
|
//Exp->setArg(i, 0);
|
2007-10-25 06:48:43 +08:00
|
|
|
}
|
2007-11-05 06:37:50 +08:00
|
|
|
// Generate the funky cast.
|
|
|
|
CastExpr *cast;
|
|
|
|
llvm::SmallVector<QualType, 8> ArgTypes;
|
|
|
|
QualType returnType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-05 06:37:50 +08:00
|
|
|
// Push 'id' and 'SEL', the 2 implicit arguments.
|
2007-11-15 18:43:57 +08:00
|
|
|
if (MsgSendFlavor == MsgSendSuperFunctionDecl)
|
|
|
|
ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
|
|
|
|
else
|
2008-01-08 03:49:32 +08:00
|
|
|
ArgTypes.push_back(Context->getObjCIdType());
|
|
|
|
ArgTypes.push_back(Context->getObjCSelType());
|
2009-02-21 02:43:26 +08:00
|
|
|
if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
|
2007-11-05 06:37:50 +08:00
|
|
|
// Push any user argument types.
|
2009-02-21 02:43:26 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
|
|
|
|
E = OMD->param_end(); PI != E; ++PI) {
|
|
|
|
QualType t = (*PI)->getType()->isObjCQualifiedIdType()
|
2009-09-09 23:08:12 +08:00
|
|
|
? Context->getObjCIdType()
|
2009-02-21 02:43:26 +08:00
|
|
|
: (*PI)->getType();
|
2008-10-29 22:49:46 +08:00
|
|
|
// Make sure we convert "t (^)(...)" to "t (*)(...)".
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(t)) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const BlockPointerType *BPT = t->getAs<BlockPointerType>();
|
2008-10-29 22:49:46 +08:00
|
|
|
t = Context->getPointerType(BPT->getPointeeType());
|
|
|
|
}
|
2007-11-05 22:36:37 +08:00
|
|
|
ArgTypes.push_back(t);
|
|
|
|
}
|
2009-02-21 02:43:26 +08:00
|
|
|
returnType = OMD->getResultType()->isObjCQualifiedIdType()
|
|
|
|
? Context->getObjCIdType() : OMD->getResultType();
|
2007-11-05 06:37:50 +08:00
|
|
|
} else {
|
2008-01-08 03:49:32 +08:00
|
|
|
returnType = Context->getObjCIdType();
|
2007-11-05 06:37:50 +08:00
|
|
|
}
|
|
|
|
// Get the type, we will need to reference it in a couple spots.
|
2007-11-15 18:28:18 +08:00
|
|
|
QualType msgSendType = MsgSendFlavor->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-05 06:37:50 +08:00
|
|
|
// Create a reference to the objc_msgSend() declaration.
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType,
|
2007-12-04 03:17:29 +08:00
|
|
|
SourceLocation());
|
2007-11-05 06:37:50 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
|
2007-11-05 06:37:50 +08:00
|
|
|
// If we don't do this cast, we get the following bizarre warning/note:
|
|
|
|
// xx.m:13: warning: function called through a non-compatible type
|
|
|
|
// xx.m:13: note: if this code is reached, the program will abort
|
2009-09-09 23:08:12 +08:00
|
|
|
cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy),
|
|
|
|
CastExpr::CK_Unknown, DRE,
|
2008-10-28 03:41:14 +08:00
|
|
|
Context->getPointerType(Context->VoidTy),
|
2008-11-04 07:29:32 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-05 06:37:50 +08:00
|
|
|
// Now do the "normal" pointer to function cast.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType castType = Context->getFunctionType(returnType,
|
2007-12-07 03:49:56 +08:00
|
|
|
&ArgTypes[0], ArgTypes.size(),
|
2008-03-18 10:02:04 +08:00
|
|
|
// If we don't have a method decl, force a variadic cast.
|
2008-10-27 00:43:14 +08:00
|
|
|
Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0);
|
2007-11-05 06:37:50 +08:00
|
|
|
castType = Context->getPointerType(castType);
|
2009-09-09 23:08:12 +08:00
|
|
|
cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast,
|
|
|
|
castType, SourceLocation(),
|
2009-07-31 08:48:10 +08:00
|
|
|
SourceLocation());
|
2007-11-05 06:37:50 +08:00
|
|
|
|
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2009-02-07 09:47:29 +08:00
|
|
|
ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-05 06:37:50 +08:00
|
|
|
const FunctionType *FT = msgSendType->getAsFunctionType();
|
2009-02-10 04:51:47 +08:00
|
|
|
CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
|
2009-09-09 23:08:12 +08:00
|
|
|
MsgExprs.size(),
|
2009-02-10 04:51:47 +08:00
|
|
|
FT->getResultType(), SourceLocation());
|
2008-01-09 06:06:28 +08:00
|
|
|
Stmt *ReplacingStmt = CE;
|
2007-12-04 03:17:29 +08:00
|
|
|
if (MsgSendStretFlavor) {
|
|
|
|
// We have the method which returns a struct/union. Must also generate
|
|
|
|
// call to objc_msgSend_stret and hang both varieties on a conditional
|
|
|
|
// expression which dictate which one to envoke depending on size of
|
|
|
|
// method's return type.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-04 03:17:29 +08:00
|
|
|
// Create a reference to the objc_msgSend_stret() declaration.
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType,
|
2007-12-04 03:17:29 +08:00
|
|
|
SourceLocation());
|
|
|
|
// Need to cast objc_msgSend_stret to "void *" (see above comment).
|
2009-09-09 23:08:12 +08:00
|
|
|
cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy),
|
|
|
|
CastExpr::CK_Unknown, STDRE,
|
2008-10-28 03:41:14 +08:00
|
|
|
Context->getPointerType(Context->VoidTy),
|
2008-11-04 07:29:32 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2007-12-04 03:17:29 +08:00
|
|
|
// Now do the "normal" pointer to function cast.
|
2009-09-09 23:08:12 +08:00
|
|
|
castType = Context->getFunctionType(returnType,
|
2007-12-07 03:49:56 +08:00
|
|
|
&ArgTypes[0], ArgTypes.size(),
|
2008-10-27 00:43:14 +08:00
|
|
|
Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0);
|
2007-12-04 03:17:29 +08:00
|
|
|
castType = Context->getPointerType(castType);
|
2009-07-31 08:48:10 +08:00
|
|
|
cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown,
|
|
|
|
cast, castType, SourceLocation(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-04 03:17:29 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2009-02-07 09:47:29 +08:00
|
|
|
PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-04 03:17:29 +08:00
|
|
|
FT = msgSendType->getAsFunctionType();
|
2009-02-10 04:51:47 +08:00
|
|
|
CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
|
2009-09-09 23:08:12 +08:00
|
|
|
MsgExprs.size(),
|
2009-02-10 04:51:47 +08:00
|
|
|
FT->getResultType(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-04 03:17:29 +08:00
|
|
|
// Build sizeof(returnType)
|
2009-09-09 23:08:12 +08:00
|
|
|
SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true,
|
2009-03-14 05:01:28 +08:00
|
|
|
returnType,
|
2008-11-12 01:56:53 +08:00
|
|
|
Context->getSizeType(),
|
|
|
|
SourceLocation(), SourceLocation());
|
2007-12-04 03:17:29 +08:00
|
|
|
// (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
|
|
|
|
// FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
|
|
|
|
// For X86 it is more complicated and some kind of target specific routine
|
|
|
|
// is needed to decide what to do.
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned IntSize =
|
2008-03-06 02:54:05 +08:00
|
|
|
static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
|
2009-09-09 23:08:12 +08:00
|
|
|
IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8),
|
2007-12-04 03:17:29 +08:00
|
|
|
Context->IntTy,
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit,
|
|
|
|
BinaryOperator::LE,
|
|
|
|
Context->IntTy,
|
2007-12-04 03:17:29 +08:00
|
|
|
SourceLocation());
|
|
|
|
// (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
|
2009-09-09 23:08:12 +08:00
|
|
|
ConditionalOperator *CondExpr =
|
2009-08-26 22:37:04 +08:00
|
|
|
new (Context) ConditionalOperator(lessThanExpr,
|
|
|
|
SourceLocation(), CE,
|
|
|
|
SourceLocation(), STCE, returnType);
|
2009-02-07 09:47:29 +08:00
|
|
|
ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
|
2007-12-04 03:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2008-01-09 06:06:28 +08:00
|
|
|
return ReplacingStmt;
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) {
|
2008-01-09 06:06:28 +08:00
|
|
|
Stmt *ReplacingStmt = SynthMessageExpr(Exp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
// Now do the actual rewrite.
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, ReplacingStmt);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2008-01-09 06:06:28 +08:00
|
|
|
return ReplacingStmt;
|
2007-10-24 07:50:29 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// typedef struct objc_object Protocol;
|
|
|
|
QualType RewriteObjC::getProtocolType() {
|
|
|
|
if (!ProtocolTypeDecl) {
|
|
|
|
ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation(),
|
2009-04-30 00:37:50 +08:00
|
|
|
&Context->Idents.get("Protocol"),
|
|
|
|
Context->getObjCIdType());
|
|
|
|
}
|
|
|
|
return Context->getTypeDeclType(ProtocolTypeDecl);
|
|
|
|
}
|
|
|
|
|
2007-12-08 02:47:10 +08:00
|
|
|
/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
|
2009-04-30 00:37:50 +08:00
|
|
|
/// a synthesized/forward data reference (to the protocol's metadata).
|
|
|
|
/// The forward references (and metadata) are generated in
|
|
|
|
/// RewriteObjC::HandleTranslationUnit().
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
|
2009-04-30 00:37:50 +08:00
|
|
|
std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString();
|
|
|
|
IdentifierInfo *ID = &Context->Idents.get(Name);
|
2009-09-09 23:08:12 +08:00
|
|
|
VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
ID, QualType()/*UNUSED*/, 0, VarDecl::Extern);
|
2009-04-30 00:37:50 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation());
|
|
|
|
Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf,
|
|
|
|
Context->getPointerType(DRE->getType()),
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(),
|
|
|
|
CastExpr::CK_Unknown,
|
|
|
|
DerefExpr, DerefExpr->getType(),
|
2009-04-30 00:37:50 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
|
|
|
ReplaceStmt(Exp, castExpr);
|
|
|
|
ProtocolExprDecls.insert(Exp->getProtocol());
|
2009-09-09 23:08:12 +08:00
|
|
|
// delete Exp; leak for now, see RewritePropertySetter() usage for more info.
|
2009-04-30 00:37:50 +08:00
|
|
|
return castExpr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-08 02:47:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf,
|
2008-05-31 22:15:04 +08:00
|
|
|
const char *endBuf) {
|
|
|
|
while (startBuf < endBuf) {
|
|
|
|
if (*startBuf == '#') {
|
|
|
|
// Skip whitespace.
|
|
|
|
for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf)
|
|
|
|
;
|
|
|
|
if (!strncmp(startBuf, "if", strlen("if")) ||
|
|
|
|
!strncmp(startBuf, "ifdef", strlen("ifdef")) ||
|
|
|
|
!strncmp(startBuf, "ifndef", strlen("ifndef")) ||
|
|
|
|
!strncmp(startBuf, "define", strlen("define")) ||
|
|
|
|
!strncmp(startBuf, "undef", strlen("undef")) ||
|
|
|
|
!strncmp(startBuf, "else", strlen("else")) ||
|
|
|
|
!strncmp(startBuf, "elif", strlen("elif")) ||
|
|
|
|
!strncmp(startBuf, "endif", strlen("endif")) ||
|
|
|
|
!strncmp(startBuf, "pragma", strlen("pragma")) ||
|
|
|
|
!strncmp(startBuf, "include", strlen("include")) ||
|
|
|
|
!strncmp(startBuf, "import", strlen("import")) ||
|
|
|
|
!strncmp(startBuf, "include_next", strlen("include_next")))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
startBuf++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to
|
2007-10-27 03:46:17 +08:00
|
|
|
/// an objective-c class with ivars.
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
|
2007-10-27 03:46:17 +08:00
|
|
|
std::string &Result) {
|
2008-01-08 03:49:32 +08:00
|
|
|
assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(CDecl->getNameAsCString() &&
|
2008-11-17 22:58:09 +08:00
|
|
|
"Name missing in SynthesizeObjCInternalStruct");
|
2007-11-01 07:08:24 +08:00
|
|
|
// Do not synthesize more than once.
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCSynthesizedStructs.count(CDecl))
|
2007-11-01 07:08:24 +08:00
|
|
|
return;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
|
2008-03-17 05:08:55 +08:00
|
|
|
int NumIvars = CDecl->ivar_size();
|
2007-11-27 03:52:57 +08:00
|
|
|
SourceLocation LocStart = CDecl->getLocStart();
|
|
|
|
SourceLocation LocEnd = CDecl->getLocEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-27 03:52:57 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-01 06:57:04 +08:00
|
|
|
// If no ivars and no root or if its root, directly or indirectly,
|
2007-11-01 07:53:01 +08:00
|
|
|
// have no ivars (thus not synthesized) then no need to synthesize this class.
|
2008-03-17 05:08:55 +08:00
|
|
|
if ((CDecl->isForwardDecl() || NumIvars == 0) &&
|
|
|
|
(!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
|
2009-04-15 07:22:57 +08:00
|
|
|
endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size());
|
2007-10-27 03:46:17 +08:00
|
|
|
return;
|
2007-11-27 03:52:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// FIXME: This has potential of causing problem. If
|
2008-01-08 03:49:32 +08:00
|
|
|
// SynthesizeObjCInternalStruct is ever called recursively.
|
2007-11-15 03:25:57 +08:00
|
|
|
Result += "\nstruct ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2008-03-11 07:16:54 +08:00
|
|
|
if (LangOpts.Microsoft)
|
|
|
|
Result += "_IMPL";
|
2008-03-12 08:25:36 +08:00
|
|
|
|
2007-11-01 01:29:28 +08:00
|
|
|
if (NumIvars > 0) {
|
2007-11-15 03:25:57 +08:00
|
|
|
const char *cursor = strchr(startBuf, '{');
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((cursor && endBuf)
|
2008-01-08 03:49:32 +08:00
|
|
|
&& "SynthesizeObjCInternalStruct - malformed @interface");
|
2008-05-31 22:15:04 +08:00
|
|
|
// If the buffer contains preprocessor directives, we do more fine-grained
|
|
|
|
// rewrites. This is intended to fix code that looks like (which occurs in
|
|
|
|
// NSURL.h, for example):
|
|
|
|
//
|
|
|
|
// #ifdef XYZ
|
|
|
|
// @interface Foo : NSObject
|
|
|
|
// #else
|
|
|
|
// @interface FooBar : NSObject
|
|
|
|
// #endif
|
|
|
|
// {
|
|
|
|
// int i;
|
|
|
|
// }
|
|
|
|
// @end
|
|
|
|
//
|
|
|
|
// This clause is segregated to avoid breaking the common case.
|
|
|
|
if (BufferContainsPPDirectives(startBuf, cursor)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() :
|
2008-05-31 22:15:04 +08:00
|
|
|
CDecl->getClassLoc();
|
|
|
|
const char *endHeader = SM->getCharacterData(L);
|
2009-04-15 07:22:57 +08:00
|
|
|
endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts);
|
2008-05-31 22:15:04 +08:00
|
|
|
|
2009-02-21 02:18:36 +08:00
|
|
|
if (CDecl->protocol_begin() != CDecl->protocol_end()) {
|
2008-05-31 22:15:04 +08:00
|
|
|
// advance to the end of the referenced protocols.
|
|
|
|
while (endHeader < cursor && *endHeader != '>') endHeader++;
|
|
|
|
endHeader++;
|
|
|
|
}
|
|
|
|
// rewrite the original header
|
|
|
|
ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size());
|
|
|
|
} else {
|
|
|
|
// rewrite the original header *without* disturbing the '{'
|
|
|
|
ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size());
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
|
2007-11-15 03:25:57 +08:00
|
|
|
Result = "\n struct ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += RCDecl->getNameAsString();
|
2008-03-13 05:09:20 +08:00
|
|
|
Result += "_IMPL ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += RCDecl->getNameAsString();
|
2008-03-13 05:22:52 +08:00
|
|
|
Result += "_IVARS;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 03:25:57 +08:00
|
|
|
// insert the super class structure definition.
|
2008-02-01 03:42:41 +08:00
|
|
|
SourceLocation OnePastCurly =
|
|
|
|
LocStart.getFileLocWithOffset(cursor-startBuf+1);
|
|
|
|
InsertText(OnePastCurly, Result.c_str(), Result.size());
|
2007-11-15 03:25:57 +08:00
|
|
|
}
|
|
|
|
cursor++; // past '{'
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-15 03:25:57 +08:00
|
|
|
// Now comment out any visibility specifiers.
|
|
|
|
while (cursor < endBuf) {
|
|
|
|
if (*cursor == '@') {
|
|
|
|
SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
|
2007-11-15 06:57:51 +08:00
|
|
|
// Skip whitespace.
|
|
|
|
for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
|
|
|
|
/*scan*/;
|
|
|
|
|
2007-11-01 01:29:28 +08:00
|
|
|
// FIXME: presence of @public, etc. inside comment results in
|
|
|
|
// this transformation as well, which is still correct c-code.
|
2007-11-15 03:25:57 +08:00
|
|
|
if (!strncmp(cursor, "public", strlen("public")) ||
|
|
|
|
!strncmp(cursor, "private", strlen("private")) ||
|
2008-04-05 06:34:24 +08:00
|
|
|
!strncmp(cursor, "package", strlen("package")) ||
|
2007-11-15 06:26:25 +08:00
|
|
|
!strncmp(cursor, "protected", strlen("protected")))
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(atLoc, "// ", 3);
|
2007-11-01 01:29:28 +08:00
|
|
|
}
|
2007-11-15 06:26:25 +08:00
|
|
|
// FIXME: If there are cases where '<' is used in ivar declaration part
|
|
|
|
// of user code, then scan the ivar list and use needToScanForQualifiers
|
|
|
|
// for type checking.
|
|
|
|
else if (*cursor == '<') {
|
|
|
|
SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(atLoc, "/* ", 3);
|
2007-11-15 06:26:25 +08:00
|
|
|
cursor = strchr(cursor, '>');
|
|
|
|
cursor++;
|
|
|
|
atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(atLoc, " */", 3);
|
2008-10-30 20:09:33 +08:00
|
|
|
} else if (*cursor == '^') { // rewrite block specifier.
|
|
|
|
SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
|
|
|
|
ReplaceText(caretLoc, 1, "*", 1);
|
2007-11-15 06:26:25 +08:00
|
|
|
}
|
2007-11-15 03:25:57 +08:00
|
|
|
cursor++;
|
2007-11-01 01:29:28 +08:00
|
|
|
}
|
2007-11-15 03:25:57 +08:00
|
|
|
// Don't forget to add a ';'!!
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(LocEnd.getFileLocWithOffset(1), ";", 1);
|
2007-11-15 03:25:57 +08:00
|
|
|
} else { // we don't have any instance variables - insert super struct.
|
2009-04-15 07:22:57 +08:00
|
|
|
endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
|
2007-11-15 03:25:57 +08:00
|
|
|
Result += " {\n struct ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += RCDecl->getNameAsString();
|
2008-03-13 05:09:20 +08:00
|
|
|
Result += "_IMPL ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += RCDecl->getNameAsString();
|
2008-03-13 05:22:52 +08:00
|
|
|
Result += "_IVARS;\n};\n";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size());
|
2007-10-27 03:46:17 +08:00
|
|
|
}
|
|
|
|
// Mark this struct as having been generated.
|
2008-01-08 03:49:32 +08:00
|
|
|
if (!ObjCSynthesizedStructs.insert(CDecl))
|
2008-05-07 02:26:51 +08:00
|
|
|
assert(false && "struct already synthesize- SynthesizeObjCInternalStruct");
|
2007-10-27 03:46:17 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
|
2007-10-25 03:23:36 +08:00
|
|
|
/// class methods.
|
2009-04-23 09:02:12 +08:00
|
|
|
template<typename MethodIterator>
|
|
|
|
void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
|
|
|
|
MethodIterator MethodEnd,
|
2007-10-25 08:14:44 +08:00
|
|
|
bool IsInstanceMethod,
|
2007-10-25 03:23:36 +08:00
|
|
|
const char *prefix,
|
2007-10-26 01:07:24 +08:00
|
|
|
const char *ClassName,
|
|
|
|
std::string &Result) {
|
2007-12-12 15:46:12 +08:00
|
|
|
if (MethodBegin == MethodEnd) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-23 05:41:37 +08:00
|
|
|
static bool objc_impl_method = false;
|
2007-12-12 15:46:12 +08:00
|
|
|
if (!objc_impl_method) {
|
2007-10-25 03:23:36 +08:00
|
|
|
/* struct _objc_method {
|
2007-10-23 05:41:37 +08:00
|
|
|
SEL _cmd;
|
|
|
|
char *method_types;
|
|
|
|
void *_imp;
|
|
|
|
}
|
2007-10-25 03:23:36 +08:00
|
|
|
*/
|
2007-10-26 01:07:24 +08:00
|
|
|
Result += "\nstruct _objc_method {\n";
|
|
|
|
Result += "\tSEL _cmd;\n";
|
|
|
|
Result += "\tchar *method_types;\n";
|
|
|
|
Result += "\tvoid *_imp;\n";
|
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
objc_impl_method = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
// Build _objc_method_list for class's methods if needed
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-11 08:12:29 +08:00
|
|
|
/* struct {
|
|
|
|
struct _objc_method_list *next_method;
|
|
|
|
int method_count;
|
|
|
|
struct _objc_method method_list[];
|
|
|
|
}
|
|
|
|
*/
|
2009-04-23 09:02:12 +08:00
|
|
|
unsigned NumMethods = std::distance(MethodBegin, MethodEnd);
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tstruct _objc_method_list *next_method;\n";
|
|
|
|
Result += "\tint method_count;\n";
|
|
|
|
Result += "\tstruct _objc_method method_list[";
|
2009-04-23 09:02:12 +08:00
|
|
|
Result += utostr(NumMethods);
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += "];\n} _OBJC_";
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += prefix;
|
|
|
|
Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
|
|
|
|
Result += "_METHODS_";
|
|
|
|
Result += ClassName;
|
2008-03-13 01:18:30 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __";
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += IsInstanceMethod ? "inst" : "cls";
|
|
|
|
Result += "_meth\")))= ";
|
2009-04-23 09:02:12 +08:00
|
|
|
Result += "{\n\t0, " + utostr(NumMethods) + "\n";
|
2007-12-12 15:46:12 +08:00
|
|
|
|
|
|
|
Result += "\t,{{(SEL)\"";
|
2008-11-24 11:33:13 +08:00
|
|
|
Result += (*MethodBegin)->getSelector().getAsString().c_str();
|
2007-12-12 15:46:12 +08:00
|
|
|
std::string MethodTypeString;
|
2008-01-08 03:49:32 +08:00
|
|
|
Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += "\", (void *)";
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += MethodInternalNames[*MethodBegin];
|
|
|
|
Result += "}\n";
|
|
|
|
for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
|
|
|
|
Result += "\t ,{(SEL)\"";
|
2008-11-24 11:33:13 +08:00
|
|
|
Result += (*MethodBegin)->getSelector().getAsString().c_str();
|
2007-10-30 06:57:28 +08:00
|
|
|
std::string MethodTypeString;
|
2008-01-08 03:49:32 +08:00
|
|
|
Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
|
2007-10-30 06:57:28 +08:00
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += "\", (void *)";
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += MethodInternalNames[*MethodBegin];
|
2007-11-14 05:02:00 +08:00
|
|
|
Result += "}\n";
|
2007-10-23 05:41:37 +08:00
|
|
|
}
|
2007-12-12 15:46:12 +08:00
|
|
|
Result += "\t }\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
/// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
|
2008-07-22 05:32:27 +08:00
|
|
|
void RewriteObjC::
|
2009-04-30 00:37:50 +08:00
|
|
|
RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix,
|
|
|
|
const char *ClassName, std::string &Result) {
|
2007-10-23 05:41:37 +08:00
|
|
|
static bool objc_protocol_methods = false;
|
2009-04-30 00:37:50 +08:00
|
|
|
|
|
|
|
// Output struct protocol_methods holder of method selector and type.
|
|
|
|
if (!objc_protocol_methods && !PDecl->isForwardDecl()) {
|
|
|
|
/* struct protocol_methods {
|
|
|
|
SEL _cmd;
|
|
|
|
char *method_types;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Result += "\nstruct _protocol_methods {\n";
|
|
|
|
Result += "\tstruct objc_selector *_cmd;\n";
|
|
|
|
Result += "\tchar *method_types;\n";
|
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
objc_protocol_methods = true;
|
|
|
|
}
|
|
|
|
// Do not synthesize the protocol more than once.
|
|
|
|
if (ObjCSynthesizedProtocols.count(PDecl))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
|
|
|
|
unsigned NumMethods = std::distance(PDecl->instmeth_begin(),
|
|
|
|
PDecl->instmeth_end());
|
2009-04-30 00:37:50 +08:00
|
|
|
/* struct _objc_protocol_method_list {
|
|
|
|
int protocol_method_count;
|
|
|
|
struct protocol_methods protocols[];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tint protocol_method_count;\n";
|
|
|
|
Result += "\tstruct _protocol_methods protocol_methods[";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_";
|
|
|
|
Result += PDecl->getNameAsString();
|
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= "
|
|
|
|
"{\n\t" + utostr(NumMethods) + "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// Output instance methods declared in this protocol.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::instmeth_iterator
|
|
|
|
I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
|
2009-04-30 00:37:50 +08:00
|
|
|
I != E; ++I) {
|
2009-06-30 10:36:12 +08:00
|
|
|
if (I == PDecl->instmeth_begin())
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\t ,{{(struct objc_selector *)\"";
|
|
|
|
else
|
|
|
|
Result += "\t ,{(struct objc_selector *)\"";
|
|
|
|
Result += (*I)->getSelector().getAsString().c_str();
|
|
|
|
std::string MethodTypeString;
|
|
|
|
Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
|
|
|
Result += "\"}\n";
|
2008-07-22 05:33:21 +08:00
|
|
|
}
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\t }\n};\n";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// Output class methods declared in this protocol.
|
2009-06-30 10:36:12 +08:00
|
|
|
unsigned NumMethods = std::distance(PDecl->classmeth_begin(),
|
|
|
|
PDecl->classmeth_end());
|
2009-04-30 00:37:50 +08:00
|
|
|
if (NumMethods > 0) {
|
|
|
|
/* struct _objc_protocol_method_list {
|
|
|
|
int protocol_method_count;
|
|
|
|
struct protocol_methods protocols[];
|
|
|
|
}
|
2008-07-22 05:33:21 +08:00
|
|
|
*/
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tint protocol_method_count;\n";
|
|
|
|
Result += "\tstruct _protocol_methods protocol_methods[";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += PDecl->getNameAsString();
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
|
|
|
|
"{\n\t";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// Output instance methods declared in this protocol.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
|
2009-04-30 00:37:50 +08:00
|
|
|
I != E; ++I) {
|
2009-06-30 10:36:12 +08:00
|
|
|
if (I == PDecl->classmeth_begin())
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\t ,{{(struct objc_selector *)\"";
|
|
|
|
else
|
|
|
|
Result += "\t ,{(struct objc_selector *)\"";
|
|
|
|
Result += (*I)->getSelector().getAsString().c_str();
|
|
|
|
std::string MethodTypeString;
|
|
|
|
Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
|
|
|
Result += "\"}\n";
|
2007-10-23 05:41:37 +08:00
|
|
|
}
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\t }\n};\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
/* struct _objc_protocol {
|
|
|
|
// Objective-C 1.0 extensions
|
|
|
|
struct _objc_protocol_extension *isa;
|
|
|
|
char *protocol_name;
|
|
|
|
struct _objc_protocol **protocol_list;
|
|
|
|
struct _objc_protocol_method_list *instance_methods;
|
|
|
|
struct _objc_protocol_method_list *class_methods;
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2009-04-30 00:37:50 +08:00
|
|
|
*/
|
|
|
|
static bool objc_protocol = false;
|
|
|
|
if (!objc_protocol) {
|
|
|
|
Result += "\nstruct _objc_protocol {\n";
|
|
|
|
Result += "\tstruct _objc_protocol_extension *isa;\n";
|
|
|
|
Result += "\tchar *protocol_name;\n";
|
|
|
|
Result += "\tstruct _objc_protocol **protocol_list;\n";
|
|
|
|
Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
|
|
|
|
Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
objc_protocol = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
|
|
|
|
Result += PDecl->getNameAsString();
|
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= "
|
|
|
|
"{\n\t0, \"";
|
|
|
|
Result += PDecl->getNameAsString();
|
|
|
|
Result += "\", 0, ";
|
2009-06-30 10:36:12 +08:00
|
|
|
if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
|
|
|
|
Result += PDecl->getNameAsString();
|
|
|
|
Result += ", ";
|
2008-07-22 05:33:21 +08:00
|
|
|
}
|
2009-04-30 00:37:50 +08:00
|
|
|
else
|
|
|
|
Result += "0, ";
|
2009-06-30 10:36:12 +08:00
|
|
|
if (PDecl->classmeth_begin() != PDecl->classmeth_end()) {
|
2009-04-30 00:37:50 +08:00
|
|
|
Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_";
|
|
|
|
Result += PDecl->getNameAsString();
|
|
|
|
Result += "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Result += "0\n";
|
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// Mark this protocol as having been generated.
|
|
|
|
if (!ObjCSynthesizedProtocols.insert(PDecl))
|
|
|
|
assert(false && "protocol already synthesized");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::
|
|
|
|
RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols,
|
|
|
|
const char *prefix, const char *ClassName,
|
|
|
|
std::string &Result) {
|
|
|
|
if (Protocols.empty()) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
for (unsigned i = 0; i != Protocols.size(); i++)
|
|
|
|
RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result);
|
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
// Output the top lovel protocol meta-data for the class.
|
|
|
|
/* struct _objc_protocol_list {
|
|
|
|
struct _objc_protocol_list *next;
|
|
|
|
int protocol_count;
|
|
|
|
struct _objc_protocol *class_protocols[];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tstruct _objc_protocol_list *next;\n";
|
|
|
|
Result += "\tint protocol_count;\n";
|
|
|
|
Result += "\tstruct _objc_protocol *class_protocols[";
|
|
|
|
Result += utostr(Protocols.size());
|
|
|
|
Result += "];\n} _OBJC_";
|
|
|
|
Result += prefix;
|
|
|
|
Result += "_PROTOCOLS_";
|
|
|
|
Result += ClassName;
|
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
|
|
|
|
"{\n\t0, ";
|
|
|
|
Result += utostr(Protocols.size());
|
|
|
|
Result += "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "\t,{&_OBJC_PROTOCOL_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += Protocols[0]->getNameAsString();
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += " \n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
for (unsigned i = 1; i != Protocols.size(); i++) {
|
|
|
|
Result += "\t ,&_OBJC_PROTOCOL_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += Protocols[i]->getNameAsString();
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "\n";
|
|
|
|
}
|
|
|
|
Result += "\t }\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
|
2007-10-25 03:23:36 +08:00
|
|
|
/// implementation.
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
|
2007-10-26 04:55:25 +08:00
|
|
|
std::string &Result) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
|
2007-10-25 03:23:36 +08:00
|
|
|
// Find category declaration for this implementation.
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCCategoryDecl *CDecl;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (CDecl = ClassDecl->getCategoryList(); CDecl;
|
2007-10-25 03:23:36 +08:00
|
|
|
CDecl = CDecl->getNextClassCategory())
|
|
|
|
if (CDecl->getIdentifier() == IDecl->getIdentifier())
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string FullCategoryName = ClassDecl->getNameAsString();
|
2007-12-23 09:40:15 +08:00
|
|
|
FullCategoryName += '_';
|
2008-11-24 13:29:24 +08:00
|
|
|
FullCategoryName += IDecl->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Build _objc_method_list for class's instance methods if needed
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::SmallVector<ObjCMethodDecl *, 32>
|
2009-06-30 10:36:12 +08:00
|
|
|
InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
|
2009-04-23 09:02:12 +08:00
|
|
|
|
|
|
|
// If any of our property implementations have associated getters or
|
|
|
|
// setters, produce metadata for them as well.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
|
|
|
|
PropEnd = IDecl->propimpl_end();
|
2009-04-23 09:02:12 +08:00
|
|
|
Prop != PropEnd; ++Prop) {
|
|
|
|
if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
|
|
|
if (!(*Prop)->getPropertyIvarDecl())
|
|
|
|
continue;
|
|
|
|
ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
|
|
|
|
if (!PD)
|
|
|
|
continue;
|
|
|
|
if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
|
|
|
|
InstanceMethods.push_back(Getter);
|
|
|
|
if (PD->isReadOnly())
|
|
|
|
continue;
|
|
|
|
if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
|
|
|
|
InstanceMethods.push_back(Setter);
|
|
|
|
}
|
|
|
|
RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
|
2007-12-23 09:40:15 +08:00
|
|
|
true, "CATEGORY_", FullCategoryName.c_str(),
|
|
|
|
Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Build _objc_method_list for class's class methods if needed
|
2009-06-30 10:36:12 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
|
2007-12-23 09:40:15 +08:00
|
|
|
false, "CATEGORY_", FullCategoryName.c_str(),
|
|
|
|
Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Protocols referenced in class declaration?
|
2007-11-14 06:09:49 +08:00
|
|
|
// Null CDecl is case of a category implementation with no category interface
|
|
|
|
if (CDecl)
|
2009-04-30 00:37:50 +08:00
|
|
|
RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY",
|
|
|
|
FullCategoryName.c_str(), Result);
|
2007-10-25 03:23:36 +08:00
|
|
|
/* struct _objc_category {
|
|
|
|
char *category_name;
|
|
|
|
char *class_name;
|
|
|
|
struct _objc_method_list *instance_methods;
|
|
|
|
struct _objc_method_list *class_methods;
|
|
|
|
struct _objc_protocol_list *protocols;
|
|
|
|
// Objective-C 1.0 extensions
|
|
|
|
uint32_t size; // sizeof (struct _objc_category)
|
2009-09-09 23:08:12 +08:00
|
|
|
struct _objc_property_list *instance_properties; // category's own
|
2007-10-25 03:23:36 +08:00
|
|
|
// @property decl.
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2007-10-25 03:23:36 +08:00
|
|
|
*/
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
static bool objc_category = false;
|
|
|
|
if (!objc_category) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstruct _objc_category {\n";
|
|
|
|
Result += "\tchar *category_name;\n";
|
|
|
|
Result += "\tchar *class_name;\n";
|
|
|
|
Result += "\tstruct _objc_method_list *instance_methods;\n";
|
|
|
|
Result += "\tstruct _objc_method_list *class_methods;\n";
|
|
|
|
Result += "\tstruct _objc_protocol_list *protocols;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\tunsigned int size;\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\tstruct _objc_property_list *instance_properties;\n";
|
|
|
|
Result += "};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
objc_category = true;
|
2007-10-23 05:41:37 +08:00
|
|
|
}
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
|
|
|
|
Result += FullCategoryName;
|
2008-03-13 01:18:30 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += IDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"\n\t, \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += ClassDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, (struct _objc_method_list *)"
|
|
|
|
"&_OBJC_CATEGORY_INSTANCE_METHODS_";
|
|
|
|
Result += FullCategoryName;
|
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-25 03:23:36 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, 0\n";
|
2009-06-30 10:36:12 +08:00
|
|
|
if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, (struct _objc_method_list *)"
|
|
|
|
"&_OBJC_CATEGORY_CLASS_METHODS_";
|
|
|
|
Result += FullCategoryName;
|
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-25 03:23:36 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, 0\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 02:18:36 +08:00
|
|
|
if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += FullCategoryName;
|
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-25 03:23:36 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, 0\n";
|
|
|
|
Result += "\t, sizeof(struct _objc_category), 0\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-27 03:46:17 +08:00
|
|
|
/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
|
|
|
|
/// ivar offset.
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
|
|
|
|
ObjCIvarDecl *ivar,
|
2007-10-27 03:46:17 +08:00
|
|
|
std::string &Result) {
|
2008-07-17 02:22:22 +08:00
|
|
|
if (ivar->isBitField()) {
|
|
|
|
// FIXME: The hack below doesn't work for bitfields. For now, we simply
|
|
|
|
// place all bitfields at offset 0.
|
|
|
|
Result += "0";
|
|
|
|
} else {
|
|
|
|
Result += "__OFFSETOFIVAR__(struct ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += IDecl->getNameAsString();
|
2008-07-17 02:22:22 +08:00
|
|
|
if (LangOpts.Microsoft)
|
|
|
|
Result += "_IMPL";
|
|
|
|
Result += ", ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += ivar->getNameAsString();
|
2008-07-17 02:22:22 +08:00
|
|
|
Result += ")";
|
|
|
|
}
|
2007-10-27 03:46:17 +08:00
|
|
|
}
|
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Meta Data Emission
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
|
2007-10-26 04:55:25 +08:00
|
|
|
std::string &Result) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-27 04:59:57 +08:00
|
|
|
// Explictly declared @interface's are already synthesized.
|
2009-04-21 04:09:33 +08:00
|
|
|
if (CDecl->isImplicitInterfaceDecl()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: Implementation of a class with no @interface (legacy) doese not
|
2007-11-27 04:59:57 +08:00
|
|
|
// produce correct synthesis as yet.
|
2008-01-08 03:49:32 +08:00
|
|
|
SynthesizeObjCInternalStruct(CDecl, Result);
|
2007-11-27 04:59:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:56:42 +08:00
|
|
|
// Build _objc_ivar_list metadata for classes ivars if needed
|
2009-06-30 10:36:12 +08:00
|
|
|
unsigned NumIvars = !IDecl->ivar_empty()
|
2009-09-09 23:08:12 +08:00
|
|
|
? IDecl->ivar_size()
|
2008-03-17 05:08:55 +08:00
|
|
|
: (CDecl ? CDecl->ivar_size() : 0);
|
2007-10-25 03:23:36 +08:00
|
|
|
if (NumIvars > 0) {
|
|
|
|
static bool objc_ivar = false;
|
|
|
|
if (!objc_ivar) {
|
|
|
|
/* struct _objc_ivar {
|
|
|
|
char *ivar_name;
|
|
|
|
char *ivar_type;
|
|
|
|
int ivar_offset;
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2007-10-25 03:23:36 +08:00
|
|
|
*/
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstruct _objc_ivar {\n";
|
|
|
|
Result += "\tchar *ivar_name;\n";
|
|
|
|
Result += "\tchar *ivar_type;\n";
|
|
|
|
Result += "\tint ivar_offset;\n";
|
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
objc_ivar = true;
|
|
|
|
}
|
|
|
|
|
2008-03-11 08:12:29 +08:00
|
|
|
/* struct {
|
|
|
|
int ivar_count;
|
|
|
|
struct _objc_ivar ivar_list[nIvars];
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2008-03-11 08:12:29 +08:00
|
|
|
*/
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\nstatic struct {\n";
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += "\tint ivar_count;\n";
|
|
|
|
Result += "\tstruct _objc_ivar ivar_list[";
|
|
|
|
Result += utostr(NumIvars);
|
|
|
|
Result += "];\n} _OBJC_INSTANCE_VARIABLES_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += IDecl->getNameAsString();
|
2008-03-13 01:18:30 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= "
|
2007-10-26 04:55:25 +08:00
|
|
|
"{\n\t";
|
|
|
|
Result += utostr(NumIvars);
|
|
|
|
Result += "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl::ivar_iterator IVI, IVE;
|
2009-04-23 11:23:08 +08:00
|
|
|
llvm::SmallVector<ObjCIvarDecl *, 8> IVars;
|
2009-06-30 10:36:12 +08:00
|
|
|
if (!IDecl->ivar_empty()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCImplementationDecl::ivar_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end();
|
2009-04-23 11:23:08 +08:00
|
|
|
IV != IVEnd; ++IV)
|
|
|
|
IVars.push_back(*IV);
|
|
|
|
IVI = IVars.begin();
|
|
|
|
IVE = IVars.end();
|
2007-12-12 15:56:42 +08:00
|
|
|
} else {
|
|
|
|
IVI = CDecl->ivar_begin();
|
|
|
|
IVE = CDecl->ivar_end();
|
|
|
|
}
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t,{{\"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += (*IVI)->getNameAsString();
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += "\", \"";
|
2009-04-30 00:37:50 +08:00
|
|
|
std::string TmpString, StrEncoding;
|
|
|
|
Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
|
|
|
|
QuoteDoublequotes(TmpString, StrEncoding);
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += StrEncoding;
|
|
|
|
Result += "\", ";
|
2007-12-12 15:56:42 +08:00
|
|
|
SynthesizeIvarOffsetComputation(IDecl, *IVI, Result);
|
2007-10-27 03:46:17 +08:00
|
|
|
Result += "}\n";
|
2007-12-12 15:56:42 +08:00
|
|
|
for (++IVI; IVI != IVE; ++IVI) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t ,{\"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += (*IVI)->getNameAsString();
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += "\", \"";
|
2009-04-30 00:37:50 +08:00
|
|
|
std::string TmpString, StrEncoding;
|
|
|
|
Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
|
|
|
|
QuoteDoublequotes(TmpString, StrEncoding);
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += StrEncoding;
|
|
|
|
Result += "\", ";
|
2007-12-12 15:56:42 +08:00
|
|
|
SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result);
|
2007-10-27 03:46:17 +08:00
|
|
|
Result += "}\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t }\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Build _objc_method_list for class's instance methods if needed
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::SmallVector<ObjCMethodDecl *, 32>
|
2009-06-30 10:36:12 +08:00
|
|
|
InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
|
2009-04-23 09:02:12 +08:00
|
|
|
|
|
|
|
// If any of our property implementations have associated getters or
|
|
|
|
// setters, produce metadata for them as well.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
|
|
|
|
PropEnd = IDecl->propimpl_end();
|
2009-04-23 09:02:12 +08:00
|
|
|
Prop != PropEnd; ++Prop) {
|
|
|
|
if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
|
|
|
if (!(*Prop)->getPropertyIvarDecl())
|
|
|
|
continue;
|
|
|
|
ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
|
|
|
|
if (!PD)
|
|
|
|
continue;
|
|
|
|
if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
|
|
|
|
InstanceMethods.push_back(Getter);
|
|
|
|
if (PD->isReadOnly())
|
|
|
|
continue;
|
|
|
|
if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
|
|
|
|
InstanceMethods.push_back(Setter);
|
|
|
|
}
|
|
|
|
RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
|
2008-11-24 11:54:41 +08:00
|
|
|
true, "", IDecl->getNameAsCString(), Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Build _objc_method_list for class's class methods if needed
|
2009-06-30 10:36:12 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
|
2008-11-24 11:54:41 +08:00
|
|
|
false, "", IDecl->getNameAsCString(), Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Protocols referenced in class declaration?
|
2009-04-30 00:37:50 +08:00
|
|
|
RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(),
|
|
|
|
"CLASS", CDecl->getNameAsCString(), Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 02:53:48 +08:00
|
|
|
// Declaration of class/meta-class metadata
|
|
|
|
/* struct _objc_class {
|
|
|
|
struct _objc_class *isa; // or const char *root_class_name when metadata
|
2007-10-23 08:02:02 +08:00
|
|
|
const char *super_class_name;
|
|
|
|
char *name;
|
|
|
|
long version;
|
|
|
|
long info;
|
|
|
|
long instance_size;
|
2007-10-24 02:53:48 +08:00
|
|
|
struct _objc_ivar_list *ivars;
|
|
|
|
struct _objc_method_list *methods;
|
2007-10-23 08:02:02 +08:00
|
|
|
struct objc_cache *cache;
|
|
|
|
struct objc_protocol_list *protocols;
|
|
|
|
const char *ivar_layout;
|
|
|
|
struct _objc_class_ext *ext;
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2007-10-23 08:02:02 +08:00
|
|
|
*/
|
2007-10-24 02:53:48 +08:00
|
|
|
static bool objc_class = false;
|
|
|
|
if (!objc_class) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstruct _objc_class {\n";
|
|
|
|
Result += "\tstruct _objc_class *isa;\n";
|
|
|
|
Result += "\tconst char *super_class_name;\n";
|
|
|
|
Result += "\tchar *name;\n";
|
|
|
|
Result += "\tlong version;\n";
|
|
|
|
Result += "\tlong info;\n";
|
|
|
|
Result += "\tlong instance_size;\n";
|
|
|
|
Result += "\tstruct _objc_ivar_list *ivars;\n";
|
|
|
|
Result += "\tstruct _objc_method_list *methods;\n";
|
|
|
|
Result += "\tstruct objc_cache *cache;\n";
|
|
|
|
Result += "\tstruct _objc_protocol_list *protocols;\n";
|
|
|
|
Result += "\tconst char *ivar_layout;\n";
|
|
|
|
Result += "\tstruct _objc_class_ext *ext;\n";
|
|
|
|
Result += "};\n";
|
2007-10-24 02:53:48 +08:00
|
|
|
objc_class = true;
|
2007-10-23 08:02:02 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-23 08:02:02 +08:00
|
|
|
// Meta-class metadata generation.
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *RootClass = 0;
|
|
|
|
ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
|
2007-10-23 08:02:02 +08:00
|
|
|
while (SuperClass) {
|
|
|
|
RootClass = SuperClass;
|
|
|
|
SuperClass = SuperClass->getSuperClass();
|
|
|
|
}
|
|
|
|
SuperClass = CDecl->getSuperClass();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2008-03-13 01:18:30 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= "
|
2007-10-26 04:55:25 +08:00
|
|
|
"{\n\t(struct _objc_class *)\"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString());
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"";
|
|
|
|
|
|
|
|
if (SuperClass) {
|
|
|
|
Result += ", \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += SuperClass->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\", \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Result += ", 0, \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"";
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
// Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
|
2007-10-23 08:02:02 +08:00
|
|
|
// 'info' field is initialized to CLS_META(2) for metaclass
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ", 0,2, sizeof(struct _objc_class), 0";
|
2009-06-30 10:36:12 +08:00
|
|
|
if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
|
2008-03-12 02:14:26 +08:00
|
|
|
Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += IDecl->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
}
|
2007-10-23 08:02:02 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ", 0\n";
|
2009-02-21 02:18:36 +08:00
|
|
|
if (CDecl->protocol_begin() != CDecl->protocol_end()) {
|
2008-03-12 09:06:30 +08:00
|
|
|
Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0,0\n";
|
|
|
|
}
|
2007-10-25 04:54:23 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t,0,0,0,0\n";
|
|
|
|
Result += "};\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-24 02:53:48 +08:00
|
|
|
// class metadata generation.
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstatic struct _objc_class _OBJC_CLASS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2008-03-13 01:18:30 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= "
|
2007-10-26 04:55:25 +08:00
|
|
|
"{\n\t&_OBJC_METACLASS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
if (SuperClass) {
|
|
|
|
Result += ", \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += SuperClass->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\", \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Result += ", 0, \"";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\"";
|
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
// 'info' field is initialized to CLS_CLASS(1) for class
|
2007-10-27 07:09:28 +08:00
|
|
|
Result += ", 0,1";
|
2008-01-08 03:49:32 +08:00
|
|
|
if (!ObjCSynthesizedStructs.count(CDecl))
|
2007-10-27 07:09:28 +08:00
|
|
|
Result += ",0";
|
|
|
|
else {
|
|
|
|
// class has size. Must synthesize its size.
|
2007-11-06 01:47:33 +08:00
|
|
|
Result += ",sizeof(struct ";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2008-03-11 07:33:22 +08:00
|
|
|
if (LangOpts.Microsoft)
|
|
|
|
Result += "_IMPL";
|
2007-10-27 07:09:28 +08:00
|
|
|
Result += ")";
|
|
|
|
}
|
2007-10-26 04:55:25 +08:00
|
|
|
if (NumIvars > 0) {
|
2008-03-12 01:37:02 +08:00
|
|
|
Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\n\t";
|
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0";
|
2009-06-30 10:36:12 +08:00
|
|
|
if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += ", 0\n\t";
|
2007-10-26 04:55:25 +08:00
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0,0";
|
2009-02-21 02:18:36 +08:00
|
|
|
if (CDecl->protocol_begin() != CDecl->protocol_end()) {
|
2008-03-12 09:06:30 +08:00
|
|
|
Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CDecl->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ", 0,0\n";
|
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0,0,0\n";
|
|
|
|
Result += "};\n";
|
2007-10-23 08:02:02 +08:00
|
|
|
}
|
2007-10-19 06:09:03 +08:00
|
|
|
|
2007-11-14 03:21:13 +08:00
|
|
|
/// RewriteImplementations - This routine rewrites all method implementations
|
|
|
|
/// and emits meta-data.
|
|
|
|
|
2008-11-14 04:07:04 +08:00
|
|
|
void RewriteObjC::RewriteImplementations() {
|
2007-10-19 03:23:00 +08:00
|
|
|
int ClsDefCount = ClassImplementation.size();
|
|
|
|
int CatDefCount = CategoryImplementation.size();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 03:21:13 +08:00
|
|
|
// Rewrite implemented methods
|
|
|
|
for (int i = 0; i < ClsDefCount; i++)
|
|
|
|
RewriteImplementationDecl(ClassImplementation[i]);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-14 04:04:28 +08:00
|
|
|
for (int i = 0; i < CatDefCount; i++)
|
|
|
|
RewriteImplementationDecl(CategoryImplementation[i]);
|
2008-11-14 04:07:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-14 04:07:04 +08:00
|
|
|
void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) {
|
|
|
|
int ClsDefCount = ClassImplementation.size();
|
|
|
|
int CatDefCount = CategoryImplementation.size();
|
|
|
|
|
2008-05-08 05:23:49 +08:00
|
|
|
// This is needed for determining instance variable offsets.
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
// For each implemented class, write out all its meta data.
|
2007-10-19 06:09:03 +08:00
|
|
|
for (int i = 0; i < ClsDefCount; i++)
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCClassMetaData(ClassImplementation[i], Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// For each implemented category, write out all its meta data.
|
|
|
|
for (int i = 0; i < CatDefCount; i++)
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
|
2009-04-30 00:37:50 +08:00
|
|
|
|
2007-10-19 03:23:00 +08:00
|
|
|
// Write objc_symtab metadata
|
|
|
|
/*
|
|
|
|
struct _objc_symtab
|
|
|
|
{
|
|
|
|
long sel_ref_cnt;
|
|
|
|
SEL *refs;
|
|
|
|
short cls_def_cnt;
|
|
|
|
short cat_def_cnt;
|
|
|
|
void *defs[cls_def_cnt + cat_def_cnt];
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2007-10-19 03:23:00 +08:00
|
|
|
*/
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstruct _objc_symtab {\n";
|
|
|
|
Result += "\tlong sel_ref_cnt;\n";
|
|
|
|
Result += "\tSEL *refs;\n";
|
|
|
|
Result += "\tshort cls_def_cnt;\n";
|
|
|
|
Result += "\tshort cat_def_cnt;\n";
|
|
|
|
Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
|
|
|
|
Result += "};\n\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "static struct _objc_symtab "
|
2008-03-13 01:18:30 +08:00
|
|
|
"_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\t0, 0, " + utostr(ClsDefCount)
|
2007-10-26 04:55:25 +08:00
|
|
|
+ ", " + utostr(CatDefCount) + "\n";
|
|
|
|
for (int i = 0; i < ClsDefCount; i++) {
|
|
|
|
Result += "\t,&_OBJC_CLASS_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += ClassImplementation[i]->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\n";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
for (int i = 0; i < CatDefCount; i++) {
|
|
|
|
Result += "\t,&_OBJC_CATEGORY_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CategoryImplementation[i]->getClassInterface()->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "_";
|
2008-11-24 13:29:24 +08:00
|
|
|
Result += CategoryImplementation[i]->getNameAsString();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\n";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "};\n\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-19 03:23:00 +08:00
|
|
|
// Write objc_module metadata
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-19 03:23:00 +08:00
|
|
|
/*
|
|
|
|
struct _objc_module {
|
|
|
|
long version;
|
|
|
|
long size;
|
|
|
|
const char *name;
|
|
|
|
struct _objc_symtab *symtab;
|
|
|
|
}
|
|
|
|
*/
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstruct _objc_module {\n";
|
|
|
|
Result += "\tlong version;\n";
|
|
|
|
Result += "\tlong size;\n";
|
|
|
|
Result += "\tconst char *name;\n";
|
|
|
|
Result += "\tstruct _objc_symtab *symtab;\n";
|
|
|
|
Result += "};\n\n";
|
|
|
|
Result += "static struct _objc_module "
|
2008-03-13 01:18:30 +08:00
|
|
|
"_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
Result += "\t" + utostr(OBJC_ABI_VERSION) +
|
2007-10-27 03:46:17 +08:00
|
|
|
", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "};\n\n";
|
2008-03-11 04:43:59 +08:00
|
|
|
|
|
|
|
if (LangOpts.Microsoft) {
|
2009-04-30 00:37:50 +08:00
|
|
|
if (ProtocolExprDecls.size()) {
|
|
|
|
Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n";
|
|
|
|
Result += "#pragma data_seg(push, \".objc_protocol$B\")\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
|
2009-04-30 00:37:50 +08:00
|
|
|
E = ProtocolExprDecls.end(); I != E; ++I) {
|
|
|
|
Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_";
|
|
|
|
Result += (*I)->getNameAsString();
|
|
|
|
Result += " = &_OBJC_PROTOCOL_";
|
|
|
|
Result += (*I)->getNameAsString();
|
|
|
|
Result += ";\n";
|
|
|
|
}
|
|
|
|
Result += "#pragma data_seg(pop)\n\n";
|
|
|
|
}
|
2008-03-11 04:43:59 +08:00
|
|
|
Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n";
|
2008-05-07 08:06:16 +08:00
|
|
|
Result += "#pragma data_seg(push, \".objc_module_info$B\")\n";
|
2008-03-11 04:43:59 +08:00
|
|
|
Result += "static struct _objc_module *_POINTER_OBJC_MODULES = ";
|
|
|
|
Result += "&_OBJC_MODULES;\n";
|
|
|
|
Result += "#pragma data_seg(pop)\n\n";
|
|
|
|
}
|
2007-10-19 03:23:00 +08:00
|
|
|
}
|
2007-10-17 06:36:42 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
|
|
|
|
const char *funcName,
|
|
|
|
std::string Tag) {
|
|
|
|
const FunctionType *AFT = CE->getFunctionType();
|
|
|
|
QualType RT = AFT->getResultType();
|
|
|
|
std::string StructRef = "struct " + Tag;
|
|
|
|
std::string S = "static " + RT.getAsString() + " __" +
|
|
|
|
funcName + "_" + "block_func_" + utostr(i);
|
|
|
|
|
|
|
|
BlockDecl *BD = CE->getBlockDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-27 07:50:07 +08:00
|
|
|
if (isa<FunctionNoProtoType>(AFT)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// No user-supplied arguments. Still need to pass in a pointer to the
|
2009-02-03 01:19:26 +08:00
|
|
|
// block (to reference imported block decl refs).
|
|
|
|
S += "(" + StructRef + " *__cself)";
|
2008-10-28 01:20:55 +08:00
|
|
|
} else if (BD->param_empty()) {
|
|
|
|
S += "(" + StructRef + " *__cself)";
|
|
|
|
} else {
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
|
2008-10-28 01:20:55 +08:00
|
|
|
assert(FT && "SynthesizeBlockFunc: No function proto");
|
|
|
|
S += '(';
|
|
|
|
// first add the implicit argument.
|
|
|
|
S += StructRef + " *__cself, ";
|
|
|
|
std::string ParamStr;
|
|
|
|
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-10-28 01:20:55 +08:00
|
|
|
S += ParamStr;
|
|
|
|
}
|
|
|
|
if (FT->isVariadic()) {
|
|
|
|
if (!BD->param_empty()) S += ", ";
|
|
|
|
S += "...";
|
|
|
|
}
|
|
|
|
S += ')';
|
|
|
|
}
|
|
|
|
S += " {\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(Name,
|
2009-05-30 04:38:28 +08:00
|
|
|
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-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
// void (^anotherClosure)(void);
|
|
|
|
// anotherClosure = ^(void) {
|
|
|
|
// myImportedClosure(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType((*I)->getType()))
|
2008-10-28 01:20:55 +08:00
|
|
|
S += "struct __block_impl *";
|
|
|
|
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-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
std::string RewrittenStr = RewrittenBlockExprs[CE];
|
|
|
|
const char *cstr = RewrittenStr.c_str();
|
|
|
|
while (*cstr++ != '{') ;
|
|
|
|
S += cstr;
|
|
|
|
S += "\n";
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RewriteObjC::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-28 01:20:55 +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-28 01:20:55 +08:00
|
|
|
E = ImportedBlockDecls.end(); I != E; ++I) {
|
2008-12-16 23:50:30 +08:00
|
|
|
S += "_Block_object_assign((void*)&dst->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-12-12 04:51:38 +08:00
|
|
|
S += ", (void*)src->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-12-16 23:50:30 +08:00
|
|
|
S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}";
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
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-28 01:20:55 +08:00
|
|
|
E = ImportedBlockDecls.end(); I != E; ++I) {
|
2008-12-16 23:50:30 +08:00
|
|
|
S += "_Block_object_dispose((void*)src->";
|
2008-11-24 13:29:24 +08:00
|
|
|
S += (*I)->getNameAsString();
|
2008-12-16 23:50:30 +08:00
|
|
|
S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);";
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
S += "}\n";
|
2008-10-28 01:20:55 +08:00
|
|
|
return S;
|
|
|
|
}
|
2008-04-17 22:40:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
|
|
|
|
bool hasCopyDisposeHelpers) {
|
2008-10-30 20:09:33 +08:00
|
|
|
std::string S = "\nstruct " + Tag;
|
2008-10-28 01:20:55 +08:00
|
|
|
std::string Constructor = " " + Tag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
S += " {\n struct __block_impl impl;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
S += " void *copy;\n void *dispose;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += "(void *fp";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += ", void *copyHelp, void *disposeHelp";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string FieldName = (*I)->getNameAsString();
|
2008-10-28 01:20:55 +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-28 01:20:55 +08:00
|
|
|
// void (^anotherBlock)(void);
|
|
|
|
// anotherBlock = ^(void) {
|
|
|
|
// myImportedBlock(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType((*I)->getType())) {
|
2008-10-28 01:20:55 +08:00
|
|
|
S += "struct __block_impl *";
|
|
|
|
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-28 01:20:55 +08:00
|
|
|
Constructor += ", " + ArgName;
|
|
|
|
}
|
|
|
|
S += FieldName + ";\n";
|
|
|
|
}
|
|
|
|
// Output all "by ref" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
|
2008-10-28 01:20:55 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
|
|
|
S += " ";
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string FieldName = (*I)->getNameAsString();
|
2008-10-28 01:20:55 +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-28 01:20:55 +08:00
|
|
|
// void (^anotherBlock)(void);
|
|
|
|
// anotherBlock = ^(void) {
|
|
|
|
// myImportedBlock(); // import and invoke the closure
|
|
|
|
// };
|
|
|
|
//
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType((*I)->getType())) {
|
2008-10-28 01:20:55 +08:00
|
|
|
S += "struct __block_impl *";
|
|
|
|
Constructor += ", void *" + ArgName;
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName,
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->PrintingPolicy);
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName,
|
2009-05-30 04:38:28 +08:00
|
|
|
Context->PrintingPolicy);
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += ", " + ArgName;
|
|
|
|
}
|
|
|
|
S += FieldName + "; // by ref\n";
|
|
|
|
}
|
|
|
|
// Finish writing the constructor.
|
|
|
|
Constructor += ", int flags=0) {\n";
|
2009-04-30 00:37:50 +08:00
|
|
|
if (GlobalVarDecl)
|
|
|
|
Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
|
|
|
|
else
|
|
|
|
Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
|
|
|
|
Constructor += " impl.Size = sizeof(";
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-28 01:20:55 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += " ";
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType((*I)->getType()))
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += Name + " = (struct __block_impl *)_";
|
|
|
|
else
|
|
|
|
Constructor += Name + " = _";
|
|
|
|
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-28 01:20:55 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string Name = (*I)->getNameAsString();
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += " ";
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType((*I)->getType()))
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += Name + " = (struct __block_impl *)_";
|
|
|
|
else
|
|
|
|
Constructor += Name + " = _";
|
|
|
|
Constructor += Name + ";\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Finish writing the constructor.
|
|
|
|
Constructor += ", int flags=0) {\n";
|
2009-04-30 00:37:50 +08:00
|
|
|
if (GlobalVarDecl)
|
|
|
|
Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
|
|
|
|
else
|
|
|
|
Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
|
|
|
|
Constructor += " impl.Size = sizeof(";
|
2008-10-28 01:20:55 +08:00
|
|
|
Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n";
|
|
|
|
if (hasCopyDisposeHelpers)
|
|
|
|
Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n";
|
|
|
|
}
|
|
|
|
Constructor += " ";
|
|
|
|
Constructor += "}\n";
|
|
|
|
S += Constructor;
|
|
|
|
S += "};\n";
|
|
|
|
return S;
|
|
|
|
}
|
2008-06-10 07:19:58 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
|
|
|
|
const char *FunName) {
|
|
|
|
// Insert closures that were part of the function.
|
|
|
|
for (unsigned i = 0; i < Blocks.size(); i++) {
|
|
|
|
|
|
|
|
CollectBlockDeclRefInfo(Blocks[i]);
|
|
|
|
|
|
|
|
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-28 01:20:55 +08:00
|
|
|
ImportedBlockDecls.size() > 0);
|
|
|
|
|
|
|
|
InsertText(FunLocStart, CI.c_str(), CI.size());
|
|
|
|
|
|
|
|
std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
InsertText(FunLocStart, CF.c_str(), CF.size());
|
|
|
|
|
|
|
|
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-10-28 01:20:55 +08:00
|
|
|
BlockDeclRefs.clear();
|
|
|
|
BlockByRefDecls.clear();
|
|
|
|
BlockByCopyDecls.clear();
|
|
|
|
BlockCallExprs.clear();
|
|
|
|
ImportedBlockDecls.clear();
|
|
|
|
}
|
|
|
|
Blocks.clear();
|
|
|
|
RewrittenBlockExprs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
|
|
|
|
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-10-28 01:20:55 +08:00
|
|
|
SynthesizeBlockLiterals(FunLocStart, FuncName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
|
2008-10-30 20:09:33 +08:00
|
|
|
//fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n");
|
|
|
|
//SourceLocation FunLocStart = MD->getLocStart();
|
|
|
|
// FIXME: This hack works around a bug in Rewrite.InsertText().
|
|
|
|
SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1);
|
2008-11-24 11:33:13 +08:00
|
|
|
std::string FuncName = MD->getSelector().getAsString();
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
SynthesizeBlockLiterals(FunLocStart, FuncName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
|
|
|
|
GetBlockDeclRefExprs(CBE->getBody());
|
|
|
|
else
|
|
|
|
GetBlockDeclRefExprs(*CI);
|
|
|
|
}
|
|
|
|
// Handle specific things.
|
|
|
|
if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S))
|
|
|
|
// FIXME: Handle enums.
|
|
|
|
if (!isa<FunctionDecl>(CDRE->getDecl()))
|
|
|
|
BlockDeclRefs.push_back(CDRE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::GetBlockCallExprs(Stmt *S) {
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI) {
|
|
|
|
if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
|
|
|
|
GetBlockCallExprs(CBE->getBody());
|
|
|
|
else
|
|
|
|
GetBlockCallExprs(*CI);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
|
|
|
if (CE->getCallee()->getType()->isBlockPointerType()) {
|
|
|
|
BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) {
|
2008-10-28 01:20:55 +08:00
|
|
|
// Navigate to relevant type information.
|
|
|
|
const char *closureName = 0;
|
|
|
|
const BlockPointerType *CPT = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +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-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
} else {
|
|
|
|
assert(1 && "RewriteBlockClass: Bad type");
|
|
|
|
}
|
|
|
|
assert(CPT && "RewriteBlockClass: Bad type");
|
|
|
|
const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType();
|
|
|
|
assert(FT && "RewriteBlockClass: Bad type");
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
|
2008-10-28 01:20:55 +08:00
|
|
|
// FTP will be null for closures that don't take arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl,
|
|
|
|
SourceLocation(),
|
|
|
|
&Context->Idents.get("__block_impl"));
|
|
|
|
QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
|
2008-10-28 01:20:55 +08:00
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
// Generate a funky cast.
|
|
|
|
llvm::SmallVector<QualType, 8> ArgTypes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
// Push the block argument type.
|
|
|
|
ArgTypes.push_back(PtrBlock);
|
2008-10-28 01:20:55 +08:00
|
|
|
if (FTP) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
|
2008-10-30 18:07:53 +08:00
|
|
|
E = FTP->arg_type_end(); I && (I != E); ++I) {
|
|
|
|
QualType t = *I;
|
|
|
|
// Make sure we convert "t (^)(...)" to "t (*)(...)".
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(t)) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const BlockPointerType *BPT = t->getAs<BlockPointerType>();
|
2008-10-30 18:07:53 +08:00
|
|
|
t = Context->getPointerType(BPT->getPointeeType());
|
|
|
|
}
|
|
|
|
ArgTypes.push_back(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now do the pointer to function cast.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(),
|
2008-10-30 18:07:53 +08:00
|
|
|
&ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock,
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr::CK_Unknown,
|
|
|
|
Exp->getCallee(),
|
2009-02-07 09:47:29 +08:00
|
|
|
PtrBlock, SourceLocation(),
|
|
|
|
SourceLocation());
|
2008-10-30 18:07:53 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2009-02-07 09:47:29 +08:00
|
|
|
ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
|
|
|
|
BlkCast);
|
2008-10-30 18:07:53 +08:00
|
|
|
//PE->dump();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 00:49:14 +08:00
|
|
|
FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(),
|
2009-09-09 23:08:12 +08:00
|
|
|
&Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0,
|
2009-01-20 09:17:11 +08:00
|
|
|
/*BitWidth=*/0, /*Mutable=*/true);
|
2009-02-07 09:47:29 +08:00
|
|
|
MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
|
|
|
|
FD->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-31 08:48:10 +08:00
|
|
|
CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType,
|
|
|
|
CastExpr::CK_Unknown, ME,
|
2009-02-07 09:47:29 +08:00
|
|
|
PtrToFuncCastType,
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation());
|
|
|
|
PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 18:07:53 +08:00
|
|
|
llvm::SmallVector<Expr*, 8> BlkExprs;
|
|
|
|
// Add the implicit argument.
|
|
|
|
BlkExprs.push_back(BlkCast);
|
|
|
|
// Add the user arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (CallExpr::arg_iterator I = Exp->arg_begin(),
|
2008-10-28 01:20:55 +08:00
|
|
|
E = Exp->arg_end(); I != E; ++I) {
|
2008-10-30 18:07:53 +08:00
|
|
|
BlkExprs.push_back(*I);
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
2009-02-10 04:51:47 +08:00
|
|
|
CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0],
|
|
|
|
BlkExprs.size(),
|
2009-02-07 09:47:29 +08:00
|
|
|
Exp->getType(), SourceLocation());
|
2008-10-30 18:07:53 +08:00
|
|
|
return CE;
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::RewriteBlockCall(CallExpr *Exp) {
|
2008-10-30 18:07:53 +08:00
|
|
|
Stmt *BlockCall = SynthesizeBlockCall(Exp);
|
|
|
|
ReplaceStmt(Exp, BlockCall);
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// We need to return the rewritten expression to handle cases where the
|
|
|
|
// BlockDeclRefExpr is embedded in another expression being rewritten.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// int main() {
|
|
|
|
// __block Foo *f;
|
|
|
|
// __block int i;
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-04-30 00:37:50 +08:00
|
|
|
// void (^myblock)() = ^() {
|
|
|
|
// [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten).
|
|
|
|
// i = 77;
|
|
|
|
// };
|
|
|
|
//}
|
|
|
|
Stmt *RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) {
|
2008-10-28 01:20:55 +08:00
|
|
|
// FIXME: Add more elaborate code generation required by the ABI.
|
2009-02-07 09:47:29 +08:00
|
|
|
Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref,
|
2009-02-03 01:19:26 +08:00
|
|
|
Context->getPointerType(BDRE->getType()),
|
|
|
|
SourceLocation());
|
|
|
|
// Need parens to enforce precedence.
|
2009-02-07 09:47:29 +08:00
|
|
|
ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr);
|
2009-02-03 01:19:26 +08:00
|
|
|
ReplaceStmt(BDRE, PE);
|
2009-04-30 00:37:50 +08:00
|
|
|
return PE;
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
|
2008-11-04 07:29:32 +08:00
|
|
|
void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) {
|
|
|
|
SourceLocation LocStart = CE->getLParenLoc();
|
|
|
|
SourceLocation LocEnd = CE->getRParenLoc();
|
2008-10-29 04:29:00 +08:00
|
|
|
|
|
|
|
// Need to avoid trying to rewrite synthesized casts.
|
|
|
|
if (LocStart.isInvalid())
|
|
|
|
return;
|
2008-11-03 19:20:24 +08:00
|
|
|
// Need to avoid trying to rewrite casts contained in macros.
|
|
|
|
if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
// advance the location to startArgList.
|
|
|
|
const char *argPtr = startBuf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
while (*argPtr++ && (argPtr < endBuf)) {
|
|
|
|
switch (*argPtr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
case '^':
|
2008-10-28 01:20:55 +08:00
|
|
|
// Replace the '^' with '*'.
|
|
|
|
LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf);
|
|
|
|
ReplaceText(LocStart, 1, "*", 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
|
|
|
|
SourceLocation DeclLoc = FD->getLocation();
|
|
|
|
unsigned parenCount = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +08:00
|
|
|
assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
parenCount++;
|
|
|
|
// advance the location to startArgList.
|
|
|
|
DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf);
|
|
|
|
assert((DeclLoc.isValid()) && "Invalid DeclLoc");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
const char *argPtr = startArgList;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +08:00
|
|
|
while (*argPtr++ && parenCount) {
|
|
|
|
switch (*argPtr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
case '^':
|
2008-10-28 01:20:55 +08:00
|
|
|
// Replace the '^' with '*'.
|
|
|
|
DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList);
|
|
|
|
ReplaceText(DeclLoc, 1, "*", 1);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case '(':
|
|
|
|
parenCount++;
|
2008-10-28 01:20:55 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case ')':
|
2008-10-28 01:20:55 +08:00
|
|
|
parenCount--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteObjC::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-28 01:20:55 +08:00
|
|
|
if (PT) {
|
2009-02-27 07:50:07 +08:00
|
|
|
FTP = PT->getPointeeType()->getAsFunctionProtoType();
|
2008-10-28 01:20:55 +08:00
|
|
|
} else {
|
2009-07-30 05:53:49 +08:00
|
|
|
const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
|
2008-10-28 01:20:55 +08:00
|
|
|
assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
|
2009-02-27 07:50:07 +08:00
|
|
|
FTP = BPT->getPointeeType()->getAsFunctionProtoType();
|
2008-10-28 01:20:55 +08:00
|
|
|
}
|
|
|
|
if (FTP) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
|
2008-10-28 01:20:55 +08:00
|
|
|
E = FTP->arg_type_end(); I != E; ++I)
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(*I))
|
2008-10-28 01:20:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-07 09:47:29 +08:00
|
|
|
void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen,
|
|
|
|
const char *&RParen) {
|
2008-10-28 01:20:55 +08:00
|
|
|
const char *argPtr = strchr(Name, '(');
|
|
|
|
assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-10-28 01:20:55 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
|
|
|
|
RewriteBlockPointerFunctionArgs(FD);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-28 01:20:55 +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();
|
|
|
|
else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
|
|
|
|
DeclT = FD->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2008-10-28 01:20:55 +08:00
|
|
|
assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 01:20:55 +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-28 01:20:55 +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)) {
|
|
|
|
// Replace the '^' with '*' for arguments.
|
|
|
|
DeclLoc = ND->getLocation();
|
|
|
|
startBuf = SM->getCharacterData(DeclLoc);
|
|
|
|
const char *argListBegin, *argListEnd;
|
|
|
|
GetExtentOfArgList(startBuf, argListBegin, argListEnd);
|
|
|
|
while (argListBegin < argListEnd) {
|
|
|
|
if (*argListBegin == '^') {
|
|
|
|
SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf);
|
|
|
|
ReplaceText(CaretLoc, 1, "*", 1);
|
|
|
|
}
|
|
|
|
argListBegin++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
|
2008-10-28 01:20:55 +08:00
|
|
|
// Add initializers for any closure decl refs.
|
|
|
|
GetBlockDeclRefExprs(Exp->getBody());
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
// Find any imported blocks...they will need special attention.
|
|
|
|
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
|
2008-12-12 04:51:38 +08:00
|
|
|
if (BlockDeclRefs[i]->getType()->isBlockPointerType()) {
|
2008-11-14 01:40:07 +08:00
|
|
|
GetBlockCallExprs(BlockDeclRefs[i]);
|
2008-10-28 01:20:55 +08:00
|
|
|
ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) {
|
|
|
|
IdentifierInfo *ID = &Context->Idents.get(name);
|
2009-02-27 07:50:07 +08:00
|
|
|
QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
return FunctionDecl::Create(*Context, TUDecl,SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
ID, FType, 0, FunctionDecl::Extern, false,
|
2009-02-26 00:33:18 +08:00
|
|
|
false);
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
|
|
|
|
2008-10-30 02:15:37 +08:00
|
|
|
Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) {
|
2008-10-29 04:29:00 +08:00
|
|
|
Blocks.push_back(Exp);
|
|
|
|
|
|
|
|
CollectBlockDeclRefInfo(Exp);
|
|
|
|
std::string FuncName;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (CurFunctionDef)
|
2008-11-24 11:33:13 +08:00
|
|
|
FuncName = CurFunctionDef->getNameAsString();
|
2008-10-29 04:29:00 +08:00
|
|
|
else if (CurMethodDef) {
|
2008-11-24 11:33:13 +08:00
|
|
|
FuncName = CurMethodDef->getSelector().getAsString();
|
2008-10-29 04:29:00 +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-30 02:15:37 +08:00
|
|
|
} else if (GlobalVarDecl)
|
2008-11-24 13:29:24 +08:00
|
|
|
FuncName = std::string(GlobalVarDecl->getNameAsString());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
std::string BlockNumber = utostr(Blocks.size()-1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
|
|
|
|
std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Get a pointer to the function type so we can cast appropriately.
|
|
|
|
QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0));
|
|
|
|
|
|
|
|
FunctionDecl *FD;
|
|
|
|
Expr *NewRep;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Simulate a contructor call...
|
|
|
|
FD = SynthBlockInitFunctionDecl(Tag.c_str());
|
2009-02-07 09:47:29 +08:00
|
|
|
DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 05:23:59 +08:00
|
|
|
// Initialize the block function.
|
2008-10-29 04:29:00 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl(Func.c_str());
|
2009-02-07 09:47:29 +08:00
|
|
|
DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(),
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy,
|
|
|
|
CastExpr::CK_Unknown, Arg,
|
2009-02-07 09:47:29 +08:00
|
|
|
Context->VoidPtrTy, SourceLocation(),
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.push_back(castExpr);
|
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ImportedBlockDecls.size()) {
|
|
|
|
std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber;
|
2008-10-30 05:23:59 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl(Buf.c_str());
|
2009-02-07 09:47:29 +08:00
|
|
|
Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy,
|
|
|
|
CastExpr::CK_Unknown, Arg,
|
2009-02-07 09:47:29 +08:00
|
|
|
Context->VoidPtrTy, SourceLocation(),
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.push_back(castExpr);
|
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
Buf = "__" + FuncName + "_block_dispose_" + BlockNumber;
|
2008-10-30 05:23:59 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl(Buf.c_str());
|
2009-02-07 09:47:29 +08:00
|
|
|
Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy,
|
|
|
|
CastExpr::CK_Unknown, Arg,
|
2009-02-07 09:47:29 +08:00
|
|
|
Context->VoidPtrTy, SourceLocation(),
|
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.push_back(castExpr);
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
|
|
|
// Add initializers for any closure decl refs.
|
|
|
|
if (BlockDeclRefs.size()) {
|
2008-10-30 05:23:59 +08:00
|
|
|
Expr *Exp;
|
2008-10-29 04:29:00 +08:00
|
|
|
// Output all "by copy" declarations.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
|
2008-10-29 04:29:00 +08:00
|
|
|
E = BlockByCopyDecls.end(); I != E; ++I) {
|
|
|
|
if (isObjCType((*I)->getType())) {
|
2008-10-30 05:23:59 +08:00
|
|
|
// FIXME: Conform to ABI ([[obj retain] autorelease]).
|
2008-11-24 11:54:41 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString());
|
2009-02-07 09:47:29 +08:00
|
|
|
Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
2008-12-12 05:05:33 +08:00
|
|
|
} else if (isTopLevelBlockPointerType((*I)->getType())) {
|
2008-11-24 11:54:41 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString());
|
2009-02-07 09:47:29 +08:00
|
|
|
Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy,
|
|
|
|
CastExpr::CK_Unknown, Arg,
|
|
|
|
Context->VoidPtrTy,
|
2009-07-31 08:48:10 +08:00
|
|
|
SourceLocation(),
|
2009-02-07 09:47:29 +08:00
|
|
|
SourceLocation());
|
2008-10-29 04:29:00 +08:00
|
|
|
} else {
|
2008-11-24 11:54:41 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString());
|
2009-02-07 09:47:29 +08:00
|
|
|
Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.push_back(Exp);
|
2008-10-29 04:29:00 +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-10-29 04:29:00 +08:00
|
|
|
E = BlockByRefDecls.end(); I != E; ++I) {
|
2008-11-24 11:54:41 +08:00
|
|
|
FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString());
|
2009-02-07 09:47:29 +08:00
|
|
|
Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
|
|
|
|
Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(Exp->getType()),
|
2008-10-30 05:23:59 +08:00
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
InitExprs.push_back(Exp);
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
|
|
|
}
|
2009-02-10 04:51:47 +08:00
|
|
|
NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(),
|
|
|
|
FType, SourceLocation());
|
2009-02-07 09:47:29 +08:00
|
|
|
NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf,
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->getPointerType(NewRep->getType()),
|
2008-10-29 04:29:00 +08:00
|
|
|
SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep,
|
2009-07-31 08:48:10 +08:00
|
|
|
FType, SourceLocation(),
|
2009-02-07 09:47:29 +08:00
|
|
|
SourceLocation());
|
2008-10-29 04:29:00 +08:00
|
|
|
BlockDeclRefs.clear();
|
|
|
|
BlockByRefDecls.clear();
|
|
|
|
BlockByCopyDecls.clear();
|
|
|
|
ImportedBlockDecls.clear();
|
|
|
|
return NewRep;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Body / Expression rewriting
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-12-05 00:24:46 +08:00
|
|
|
// This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer().
|
|
|
|
// The allows the main rewrite loop to associate all ObjCPropertyRefExprs with
|
|
|
|
// their respective BinaryOperator. Without this knowledge, we'd need to rewrite
|
|
|
|
// the ObjCPropertyRefExpr twice (once as a getter, and later as a setter).
|
|
|
|
// Since the rewriter isn't capable of rewriting rewritten code, it's important
|
|
|
|
// we get this right.
|
|
|
|
void RewriteObjC::CollectPropertySetters(Stmt *S) {
|
|
|
|
// Perform a bottom up traversal of all children.
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI)
|
|
|
|
CollectPropertySetters(*CI);
|
|
|
|
|
|
|
|
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
|
|
|
|
if (BinOp->isAssignmentOp()) {
|
|
|
|
if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()))
|
|
|
|
PropSetters[PRE] = BinOp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
|
2008-10-29 04:29:00 +08:00
|
|
|
isa<DoStmt>(S) || isa<ForStmt>(S))
|
|
|
|
Stmts.push_back(S);
|
|
|
|
else if (isa<ObjCForCollectionStmt>(S)) {
|
|
|
|
Stmts.push_back(S);
|
|
|
|
ObjCBcLabelNo.push_back(++BcLabelCount);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
SourceRange OrigStmtRange = S->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Perform a bottom up rewrite of all children.
|
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
|
|
|
if (*CI) {
|
|
|
|
Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (newStmt)
|
2008-10-29 04:29:00 +08:00
|
|
|
*CI = newStmt;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
|
|
|
|
// Rewrite the block body in place.
|
|
|
|
RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Now we snarf the rewritten text and stash it away for later use.
|
2008-10-30 02:15:37 +08:00
|
|
|
std::string Str = Rewrite.getRewritenText(BE->getSourceRange());
|
|
|
|
RewrittenBlockExprs[BE] = Str;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
Stmt *blockTranscribed = SynthBlockInitExpr(BE);
|
|
|
|
//blockTranscribed->dump();
|
2008-10-30 02:15:37 +08:00
|
|
|
ReplaceStmt(S, blockTranscribed);
|
2008-10-29 04:29:00 +08:00
|
|
|
return blockTranscribed;
|
|
|
|
}
|
|
|
|
// Handle specific things.
|
|
|
|
if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
|
|
|
|
return RewriteAtEncode(AtEncode);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S))
|
|
|
|
return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin());
|
|
|
|
|
2008-12-05 00:24:46 +08:00
|
|
|
if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) {
|
|
|
|
BinaryOperator *BinOp = PropSetters[PropRefExpr];
|
|
|
|
if (BinOp) {
|
|
|
|
// Because the rewriter doesn't allow us to rewrite rewritten code,
|
|
|
|
// we need to rewrite the right hand side prior to rewriting the setter.
|
2008-12-09 20:56:34 +08:00
|
|
|
DisableReplaceStmt = true;
|
|
|
|
// Save the source range. Even if we disable the replacement, the
|
|
|
|
// rewritten node will have been inserted into the tree. If the synthesized
|
|
|
|
// node is at the 'end', the rewriter will fail. Consider this:
|
2009-09-09 23:08:12 +08:00
|
|
|
// self.errorHandler = handler ? handler :
|
2008-12-09 20:56:34 +08:00
|
|
|
// ^(NSURL *errorURL, NSError *error) { return (BOOL)1; };
|
|
|
|
SourceRange SrcRange = BinOp->getSourceRange();
|
2008-12-05 00:24:46 +08:00
|
|
|
Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS());
|
2008-12-09 20:56:34 +08:00
|
|
|
DisableReplaceStmt = false;
|
2008-12-05 07:50:32 +08:00
|
|
|
//
|
|
|
|
// Unlike the main iterator, we explicily avoid changing 'BinOp'. If
|
|
|
|
// we changed the RHS of BinOp, the rewriter would fail (since it needs
|
|
|
|
// to see the original expression). Consider this example:
|
|
|
|
//
|
|
|
|
// Foo *obj1, *obj2;
|
|
|
|
//
|
|
|
|
// obj1.i = [obj2 rrrr];
|
|
|
|
//
|
|
|
|
// 'BinOp' for the previous expression looks like:
|
|
|
|
//
|
|
|
|
// (BinaryOperator 0x231ccf0 'int' '='
|
|
|
|
// (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i"
|
|
|
|
// (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0))
|
|
|
|
// (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr
|
|
|
|
// (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0)))
|
|
|
|
//
|
|
|
|
// 'newStmt' represents the rewritten message expression. For example:
|
|
|
|
//
|
|
|
|
// (CallExpr 0x231d300 'id':'struct objc_object *'
|
|
|
|
// (ParenExpr 0x231d2e0 'int (*)(id, SEL)'
|
|
|
|
// (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)'
|
|
|
|
// (CStyleCastExpr 0x231d220 'void *'
|
|
|
|
// (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0))))
|
|
|
|
//
|
|
|
|
// Note that 'newStmt' is passed to RewritePropertySetter so that it
|
|
|
|
// can be used as the setter argument. ReplaceStmt() will still 'see'
|
|
|
|
// the original RHS (since we haven't altered BinOp).
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// This implies the Rewrite* routines can no longer delete the original
|
2008-12-05 07:50:32 +08:00
|
|
|
// node. As a result, we now leak the original AST nodes.
|
|
|
|
//
|
2008-12-09 20:56:34 +08:00
|
|
|
return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange);
|
2008-12-05 00:24:46 +08:00
|
|
|
} else {
|
|
|
|
return RewritePropertyGetter(PropRefExpr);
|
2008-12-03 08:56:33 +08:00
|
|
|
}
|
|
|
|
}
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
|
|
|
|
return RewriteAtSelector(AtSelector);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
|
|
|
|
return RewriteObjCStringLiteral(AtString);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
|
2008-12-05 00:24:46 +08:00
|
|
|
#if 0
|
2008-10-29 04:29:00 +08:00
|
|
|
// Before we rewrite it, put the original message expression in a comment.
|
|
|
|
SourceLocation startLoc = MessExpr->getLocStart();
|
|
|
|
SourceLocation endLoc = MessExpr->getLocEnd();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
|
|
|
const char *endBuf = SM->getCharacterData(endLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
std::string messString;
|
|
|
|
messString += "// ";
|
|
|
|
messString.append(startBuf, endBuf-startBuf+1);
|
|
|
|
messString += "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// FIXME: Missing definition of
|
2008-10-29 04:29:00 +08:00
|
|
|
// InsertText(clang::SourceLocation, char const*, unsigned int).
|
|
|
|
// InsertText(startLoc, messString.c_str(), messString.size());
|
|
|
|
// Tried this, but it didn't work either...
|
|
|
|
// ReplaceText(startLoc, 0, messString.c_str(), messString.size());
|
2008-12-05 00:24:46 +08:00
|
|
|
#endif
|
2008-10-29 04:29:00 +08:00
|
|
|
return RewriteMessageExpr(MessExpr);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
|
|
|
|
return RewriteObjCTryStmt(StmtTry);
|
|
|
|
|
|
|
|
if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
|
|
|
|
return RewriteObjCSynchronizedStmt(StmtTry);
|
|
|
|
|
|
|
|
if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
|
|
|
|
return RewriteObjCThrowStmt(StmtThrow);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
|
|
|
|
return RewriteObjCProtocolExpr(ProtocolExp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (ObjCForCollectionStmt *StmtForCollection =
|
2008-10-29 04:29:00 +08:00
|
|
|
dyn_cast<ObjCForCollectionStmt>(S))
|
2009-09-09 23:08:12 +08:00
|
|
|
return RewriteObjCForCollectionStmt(StmtForCollection,
|
2008-10-29 04:29:00 +08:00
|
|
|
OrigStmtRange.getEnd());
|
|
|
|
if (BreakStmt *StmtBreakStmt =
|
|
|
|
dyn_cast<BreakStmt>(S))
|
|
|
|
return RewriteBreakStmt(StmtBreakStmt);
|
|
|
|
if (ContinueStmt *StmtContinueStmt =
|
|
|
|
dyn_cast<ContinueStmt>(S))
|
|
|
|
return RewriteContinueStmt(StmtContinueStmt);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Need to check for protocol refs (id <P>, Foo <P> *) in variable decls
|
2008-10-29 04:29:00 +08:00
|
|
|
// and cast exprs.
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
|
|
|
|
// FIXME: What we're doing here is modifying the type-specifier that
|
|
|
|
// precedes the first Decl. In the future the DeclGroup should have
|
2009-09-09 23:08:12 +08:00
|
|
|
// a separate type-specifier that we can rewrite.
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Blocks rewrite rules.
|
|
|
|
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
|
|
|
|
DI != DE; ++DI) {
|
2009-01-20 09:17:11 +08:00
|
|
|
Decl *SD = *DI;
|
2008-10-29 04:29:00 +08:00
|
|
|
if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(ND->getType()))
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteBlockPointerDecl(ND);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (ND->getType()->isFunctionPointerType())
|
2008-10-29 04:29:00 +08:00
|
|
|
CheckFunctionPointerDecl(ND->getType(), ND);
|
|
|
|
}
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteBlockPointerDecl(TD);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (TD->getUnderlyingType()->isFunctionPointerType())
|
2008-10-29 04:29:00 +08:00
|
|
|
CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S))
|
|
|
|
RewriteObjCQualifiedInterfaceTypes(CE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
|
2008-10-29 04:29:00 +08:00
|
|
|
isa<DoStmt>(S) || isa<ForStmt>(S)) {
|
|
|
|
assert(!Stmts.empty() && "Statement stack is empty");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
|
|
|
|
isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
|
2008-10-29 04:29:00 +08:00
|
|
|
&& "Statement stack mismatch");
|
|
|
|
Stmts.pop_back();
|
|
|
|
}
|
|
|
|
// Handle blocks rewriting.
|
|
|
|
if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
|
|
|
|
if (BDRE->isByRef())
|
2009-04-30 00:37:50 +08:00
|
|
|
return RewriteBlockDeclRefExpr(BDRE);
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
2008-10-30 18:07:53 +08:00
|
|
|
if (CE->getCallee()->getType()->isBlockPointerType()) {
|
|
|
|
Stmt *BlockCall = SynthesizeBlockCall(CE);
|
|
|
|
ReplaceStmt(S, BlockCall);
|
|
|
|
return BlockCall;
|
|
|
|
}
|
2008-10-29 04:29:00 +08:00
|
|
|
}
|
2008-11-04 07:29:32 +08:00
|
|
|
if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) {
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteCastExpr(CE);
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
|
2009-02-07 09:47:29 +08:00
|
|
|
CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
|
2008-10-29 04:29:00 +08:00
|
|
|
// Get the new text.
|
|
|
|
std::string SStr;
|
|
|
|
llvm::raw_string_ostream Buf(SStr);
|
2009-05-30 13:19:26 +08:00
|
|
|
Replacement->printPretty(Buf, *Context);
|
2008-10-29 04:29:00 +08:00
|
|
|
const std::string &Str = Buf.str();
|
|
|
|
|
|
|
|
printf("CAST = %s\n", &Str[0]);
|
|
|
|
InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
|
|
|
|
delete S;
|
|
|
|
return Replacement;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Return this stmt unmodified.
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleDeclInMainFile - This is called for each top-level decl defined in the
|
|
|
|
/// main file of the input.
|
|
|
|
void RewriteObjC::HandleDeclInMainFile(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2008-12-17 08:20:22 +08:00
|
|
|
if (FD->isOverloadedOperator())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// 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
|
|
|
RewriteBlocksInFunctionProtoType(FD->getType(), FD);
|
2008-10-29 04:29:00 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
// FIXME: If this should support Obj-C++, support CXXTryStmt
|
2009-06-30 10:35:26 +08:00
|
|
|
if (CompoundStmt *Body = FD->getCompoundBody()) {
|
2008-10-29 04:29:00 +08:00
|
|
|
CurFunctionDef = FD;
|
2008-12-05 00:24:46 +08:00
|
|
|
CollectPropertySetters(Body);
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = Body;
|
2009-03-13 02:33:24 +08:00
|
|
|
Body =
|
|
|
|
cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
|
|
|
|
FD->setBody(Body);
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = 0;
|
|
|
|
if (PropParentMap) {
|
|
|
|
delete PropParentMap;
|
|
|
|
PropParentMap = 0;
|
|
|
|
}
|
2008-10-29 04:29:00 +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-29 04:29:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2009-06-30 10:35:26 +08:00
|
|
|
if (CompoundStmt *Body = MD->getCompoundBody()) {
|
2008-10-29 04:29:00 +08:00
|
|
|
CurMethodDef = MD;
|
2008-12-05 00:24:46 +08:00
|
|
|
CollectPropertySetters(Body);
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = Body;
|
2009-03-13 02:33:24 +08:00
|
|
|
Body =
|
|
|
|
cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
|
|
|
|
MD->setBody(Body);
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = 0;
|
|
|
|
if (PropParentMap) {
|
|
|
|
delete PropParentMap;
|
|
|
|
PropParentMap = 0;
|
|
|
|
}
|
2008-10-29 04:29:00 +08:00
|
|
|
InsertBlockLiteralsWithinMethod(MD);
|
|
|
|
CurMethodDef = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
|
|
|
|
ClassImplementation.push_back(CI);
|
|
|
|
else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
|
|
|
|
CategoryImplementation.push_back(CI);
|
|
|
|
else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
|
|
|
|
RewriteForwardClassDecl(CD);
|
|
|
|
else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
RewriteObjCQualifiedInterfaceTypes(VD);
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(VD->getType()))
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteBlockPointerDecl(VD);
|
2008-10-30 02:15:37 +08:00
|
|
|
else if (VD->getType()->isFunctionPointerType()) {
|
2008-10-29 04:29:00 +08:00
|
|
|
CheckFunctionPointerDecl(VD->getType(), VD);
|
|
|
|
if (VD->getInit()) {
|
2008-11-04 07:29:32 +08:00
|
|
|
if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteCastExpr(CE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-30 02:15:37 +08:00
|
|
|
if (VD->getInit()) {
|
|
|
|
GlobalVarDecl = VD;
|
2008-12-05 00:24:46 +08:00
|
|
|
CollectPropertySetters(VD->getInit());
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = VD->getInit();
|
2008-10-30 02:15:37 +08:00
|
|
|
RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
|
2008-12-09 00:43:47 +08:00
|
|
|
CurrentBody = 0;
|
|
|
|
if (PropParentMap) {
|
|
|
|
delete PropParentMap;
|
|
|
|
PropParentMap = 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(),
|
2008-11-24 11:54:41 +08:00
|
|
|
VD->getNameAsCString());
|
2008-10-30 02:15:37 +08:00
|
|
|
GlobalVarDecl = 0;
|
|
|
|
|
|
|
|
// This is needed for blocks.
|
2008-11-04 07:29:32 +08:00
|
|
|
if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
|
2008-10-30 02:15:37 +08:00
|
|
|
RewriteCastExpr(CE);
|
|
|
|
}
|
|
|
|
}
|
2008-10-29 04:29:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteBlockPointerDecl(TD);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (TD->getUnderlyingType()->isFunctionPointerType())
|
2008-10-29 04:29:00 +08:00
|
|
|
CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
|
|
|
|
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-29 04:29:00 +08:00
|
|
|
FieldDecl *FD = *i;
|
2008-12-12 05:05:33 +08:00
|
|
|
if (isTopLevelBlockPointerType(FD->getType()))
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteBlockPointerDecl(FD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Nothing yet.
|
|
|
|
}
|
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
void RewriteObjC::HandleTranslationUnit(ASTContext &C) {
|
2008-10-29 04:29:00 +08:00
|
|
|
// Get the top-level buffer that this corresponds to.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Rewrite tabs if we care.
|
|
|
|
//RewriteTabs();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
RewriteInclude();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
// Here's a great place to add any extra declarations that may be needed.
|
|
|
|
// Write out meta data for each @protocol(<expr>).
|
2009-09-09 23:08:12 +08:00
|
|
|
for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
|
2009-04-30 00:37:50 +08:00
|
|
|
E = ProtocolExprDecls.end(); I != E; ++I)
|
|
|
|
RewriteObjCProtocolMetaData(*I, "", "", Preamble);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
InsertText(SM->getLocForStartOfFile(MainFileID),
|
2008-10-29 04:29:00 +08:00
|
|
|
Preamble.c_str(), Preamble.size(), false);
|
2008-11-14 22:10:01 +08:00
|
|
|
if (ClassImplementation.size() || CategoryImplementation.size())
|
|
|
|
RewriteImplementations();
|
2009-04-30 00:37:50 +08:00
|
|
|
|
2008-10-29 04:29:00 +08:00
|
|
|
// Get the buffer corresponding to MainFileID. If we haven't changed it, then
|
|
|
|
// we are done.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const RewriteBuffer *RewriteBuf =
|
2008-10-29 04:29:00 +08:00
|
|
|
Rewrite.getRewriteBufferFor(MainFileID)) {
|
|
|
|
//printf("Changed:\n");
|
|
|
|
*OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "No changes\n");
|
|
|
|
}
|
2008-11-14 04:07:04 +08:00
|
|
|
|
2009-04-30 00:37:50 +08:00
|
|
|
if (ClassImplementation.size() || CategoryImplementation.size() ||
|
|
|
|
ProtocolExprDecls.size()) {
|
2008-11-14 22:10:01 +08:00
|
|
|
// Rewrite Objective-c meta data*
|
|
|
|
std::string ResultStr;
|
|
|
|
SynthesizeMetaDataIntoBuffer(ResultStr);
|
|
|
|
// Emit metadata.
|
|
|
|
*OutFile << ResultStr;
|
|
|
|
}
|
2008-10-29 04:29:00 +08:00
|
|
|
OutFile->flush();
|
|
|
|
}
|
|
|
|
|