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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "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-06-01 04:11:04 +08:00
|
|
|
#include "clang/AST/TranslationUnit.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"
|
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"
|
2007-12-02 09:13:47 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-01-29 05:34:52 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-05-22 04:19:16 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-03-22 08:08:40 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2008-04-15 06:53:48 +08:00
|
|
|
#include <sstream>
|
2008-03-22 08:08:40 +08:00
|
|
|
#include <fstream>
|
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
|
|
|
|
2008-01-29 05:34:52 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false),
|
|
|
|
llvm::cl::desc("Silence ObjC rewriting warnings"));
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2007-10-12 02:38:32 +08:00
|
|
|
unsigned MainFileID;
|
2007-12-02 09:13:47 +08:00
|
|
|
const char *MainFileStart, *MainFileEnd;
|
2007-10-17 05:07:07 +08:00
|
|
|
SourceLocation LastIncLoc;
|
2008-03-28 06:29:16 +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;
|
2008-01-23 06:44:46 +08:00
|
|
|
llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
|
2007-10-24 07:50:29 +08:00
|
|
|
|
2008-03-15 08:55:56 +08:00
|
|
|
unsigned NumObjCStringLiterals;
|
|
|
|
|
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;
|
2007-12-08 02:47:10 +08:00
|
|
|
FunctionDecl *GetProtocolFunctionDecl;
|
2008-03-12 01:37:02 +08:00
|
|
|
FunctionDecl *SuperContructorFunctionDecl;
|
2007-12-04 03:17:29 +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;
|
2007-11-05 06:37:50 +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;
|
|
|
|
|
2007-11-15 18:28:18 +08:00
|
|
|
// Needed for super.
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *CurMethodDecl;
|
2007-11-15 18:28:18 +08:00
|
|
|
RecordDecl *SuperStructDecl;
|
2008-03-15 08:55:56 +08:00
|
|
|
RecordDecl *ConstantStringDecl;
|
2007-11-15 18:28:18 +08:00
|
|
|
|
2008-01-18 09:15:54 +08:00
|
|
|
// Needed for header files being rewritten
|
|
|
|
bool IsHeader;
|
|
|
|
|
2008-03-29 06:26:09 +08:00
|
|
|
std::string InFileName;
|
|
|
|
std::string OutFileName;
|
|
|
|
|
2008-03-28 06:29:16 +08:00
|
|
|
std::string Preamble;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
virtual void InitializeTU(TranslationUnit &TU) {
|
|
|
|
TU.SetOwnsDecls(false);
|
|
|
|
Initialize(TU.getContext());
|
|
|
|
}
|
2008-02-01 03:38:44 +08:00
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Top Level Driver code.
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D);
|
2007-10-17 05:07:07 +08:00
|
|
|
void HandleDeclInMainFile(Decl *D);
|
2008-04-15 06:03:09 +08:00
|
|
|
RewriteObjC(std::string inFile, std::string outFile,
|
2008-03-29 06:26:09 +08:00
|
|
|
Diagnostic &D, const LangOptions &LOpts);
|
2008-08-08 12:15:52 +08:00
|
|
|
|
|
|
|
~RewriteObjC() {}
|
|
|
|
|
|
|
|
virtual void HandleTranslationUnit(TranslationUnit& TU);
|
2008-02-01 03:37:57 +08:00
|
|
|
|
|
|
|
void ReplaceStmt(Stmt *Old, Stmt *New) {
|
2008-02-01 03:42:41 +08:00
|
|
|
// If replacement succeeded or warning disabled return with no warning.
|
|
|
|
if (!Rewrite.ReplaceStmt(Old, New) || SilenceRewriteMacroWarning)
|
2008-02-01 03:37:57 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SourceRange Range = Old->getSourceRange();
|
|
|
|
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag,
|
|
|
|
0, 0, &Range, 1);
|
|
|
|
}
|
|
|
|
|
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.
|
2008-03-28 06:29:16 +08:00
|
|
|
if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) ||
|
2008-02-01 03:42:41 +08:00
|
|
|
SilenceRewriteMacroWarning)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
|
|
|
|
}
|
2008-02-01 03:37:57 +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;
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) ||
|
|
|
|
SilenceRewriteMacroWarning)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
|
2007-11-14 04:04:28 +08:00
|
|
|
void RewriteImplementationDecl(NamedDecl *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);
|
2008-03-17 05:23:50 +08:00
|
|
|
void RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties);
|
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);
|
2007-10-17 06:36:42 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Expression Rewriting.
|
2007-11-09 23:20:18 +08:00
|
|
|
Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
|
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);
|
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-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);
|
2007-10-25 06:48:43 +08:00
|
|
|
CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
|
|
|
|
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);
|
|
|
|
|
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();
|
2007-12-08 02:47:10 +08:00
|
|
|
void SynthGetProtocolFunctionDecl();
|
2008-03-12 01:37:02 +08:00
|
|
|
void SynthSuperContructorFunctionDecl();
|
2007-11-08 22:30:50 +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);
|
2007-10-25 03:23:36 +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);
|
2007-10-25 03:23:36 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator;
|
|
|
|
void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
|
2007-12-12 15:46:12 +08:00
|
|
|
instmeth_iterator 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-10-25 03:23:36 +08:00
|
|
|
|
2008-07-22 05:32:27 +08:00
|
|
|
void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl>
|
|
|
|
&Protocols,
|
2007-10-25 03:23:36 +08:00
|
|
|
const char *prefix,
|
2007-10-26 04:55:25 +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);
|
2008-01-08 03:49:32 +08:00
|
|
|
void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
|
|
|
|
ObjCIvarDecl *ivar,
|
2007-10-27 03:46:17 +08:00
|
|
|
std::string &Result);
|
2007-11-14 03:21:13 +08:00
|
|
|
void RewriteImplementations(std::string &Result);
|
2007-10-11 08:43:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-01-18 09:15:54 +08:00
|
|
|
static bool IsHeaderFile(const std::string &Filename) {
|
|
|
|
std::string::size_type DotPos = Filename.rfind('.');
|
|
|
|
|
|
|
|
if (DotPos == std::string::npos) {
|
|
|
|
// no file extension
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
RewriteObjC::RewriteObjC(std::string inFile, std::string outFile,
|
2008-03-29 06:26:09 +08:00
|
|
|
Diagnostic &D, const LangOptions &LOpts)
|
|
|
|
: Diags(D), LangOpts(LOpts) {
|
|
|
|
IsHeader = IsHeaderFile(inFile);
|
|
|
|
InFileName = inFile;
|
|
|
|
OutFileName = outFile;
|
|
|
|
RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
|
|
|
|
"rewriting sub-expression within a macro (may not be correct)");
|
|
|
|
}
|
|
|
|
|
2008-01-18 09:15:54 +08:00
|
|
|
ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile,
|
2008-03-22 08:08:40 +08:00
|
|
|
const std::string& OutFile,
|
2008-03-11 04:43:59 +08:00
|
|
|
Diagnostic &Diags,
|
|
|
|
const LangOptions &LOpts) {
|
2008-04-15 06:03:09 +08:00
|
|
|
return new RewriteObjC(InFile, OutFile, Diags, LOpts);
|
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;
|
|
|
|
GetProtocolFunctionDecl = 0;
|
|
|
|
ConstantStringClassReference = 0;
|
|
|
|
NSStringRecord = 0;
|
|
|
|
CurMethodDecl = 0;
|
|
|
|
SuperStructDecl = 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-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();
|
2008-03-28 06:29:16 +08:00
|
|
|
|
2008-02-01 03:38:44 +08:00
|
|
|
Rewrite.setSourceMgr(Context->getSourceManager());
|
2008-03-11 07:16:54 +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)
|
|
|
|
Preamble = "#pragma once\n";
|
|
|
|
Preamble += "struct objc_selector; struct objc_class;\n";
|
|
|
|
Preamble += "#ifndef OBJC_SUPER\n";
|
|
|
|
Preamble += "struct objc_super { struct objc_object *object; ";
|
|
|
|
Preamble += "struct objc_object *superClass; ";
|
2008-03-12 01:37:02 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
// Add a constructor for creating temporary objects.
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "objc_super(struct objc_object *o, struct objc_object *s) : ";
|
|
|
|
Preamble += "object(o), superClass(s) {} ";
|
2008-03-12 01:37:02 +08:00
|
|
|
}
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "};\n";
|
|
|
|
Preamble += "#define OBJC_SUPER\n";
|
|
|
|
Preamble += "#endif\n";
|
|
|
|
Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
|
|
|
|
Preamble += "typedef struct objc_object Protocol;\n";
|
|
|
|
Preamble += "#define _REWRITER_typedef_Protocol\n";
|
|
|
|
Preamble += "#endif\n";
|
2008-03-12 21:19:12 +08:00
|
|
|
if (LangOpts.Microsoft)
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n";
|
|
|
|
else
|
|
|
|
Preamble += "#define __OBJC_RW_EXTERN extern\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend_stret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper_stret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
|
2008-05-09 06:02:18 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN double objc_msgSend_fpret";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
|
2008-05-09 01:52:16 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getClass";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(const char *);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getMetaClass";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "(const char *);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN void objc_exception_throw(struct objc_object *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void objc_exception_try_enter(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void objc_exception_try_exit(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_exception_extract(void *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN 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.
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void objc_sync_enter(struct objc_object *);\n";
|
|
|
|
Preamble += "__OBJC_RW_EXTERN void objc_sync_exit(struct objc_object *);\n";
|
2008-05-07 06:45:19 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN 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-06-03 04:23:21 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN 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-05-16 05:12:10 +08:00
|
|
|
Preamble += "__OBJC_RW_EXTERN 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-05-16 05:12:10 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
Preamble += "#undef __OBJC_RW_EXTERN\n";
|
2008-03-28 06:29:16 +08:00
|
|
|
Preamble += "#define __attribute__(X)\n";
|
2008-05-16 05:12:10 +08:00
|
|
|
}
|
2008-02-01 03:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top Level Driver Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::HandleTopLevelDecl(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();
|
|
|
|
Loc = SM->getLogicalLoc(Loc);
|
|
|
|
|
|
|
|
// 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>
|
|
|
|
if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) {
|
|
|
|
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);
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCForwardProtocolDecl *FP =
|
|
|
|
dyn_cast<ObjCForwardProtocolDecl>(D)){
|
2007-11-14 08:42:16 +08:00
|
|
|
RewriteForwardProtocolDecl(FP);
|
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
|
|
|
/// HandleDeclInMainFile - This is called for each top-level decl defined in the
|
|
|
|
/// main file of the input.
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::HandleDeclInMainFile(Decl *D) {
|
2007-10-25 01:06:59 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
if (Stmt *Body = FD->getBody())
|
2007-11-09 23:20:18 +08:00
|
|
|
FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
|
2008-01-31 03:17:43 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2007-11-15 18:28:18 +08:00
|
|
|
if (Stmt *Body = MD->getBody()) {
|
|
|
|
//Body->dump();
|
|
|
|
CurMethodDecl = MD;
|
2007-11-14 07:01:27 +08:00
|
|
|
MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
|
2007-11-15 18:28:18 +08:00
|
|
|
CurMethodDecl = 0;
|
|
|
|
}
|
2007-11-14 07:01:27 +08:00
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
|
2007-10-25 01:06:59 +08:00
|
|
|
ClassImplementation.push_back(CI);
|
2008-01-08 03:49:32 +08:00
|
|
|
else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
|
2007-10-25 01:06:59 +08:00
|
|
|
CategoryImplementation.push_back(CI);
|
2008-01-08 03:49:32 +08:00
|
|
|
else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
|
2007-10-25 01:06:59 +08:00
|
|
|
RewriteForwardClassDecl(CD);
|
2007-11-09 23:20:18 +08:00
|
|
|
else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCQualifiedInterfaceTypes(VD);
|
2007-11-09 23:20:18 +08:00
|
|
|
if (VD->getInit())
|
|
|
|
RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
|
|
|
|
}
|
2007-10-25 01:06:59 +08:00
|
|
|
// Nothing yet.
|
|
|
|
}
|
|
|
|
|
2008-08-08 12:15:52 +08:00
|
|
|
void RewriteObjC::HandleTranslationUnit(TranslationUnit& TU) {
|
2007-10-25 01:06:59 +08:00
|
|
|
// Get the top-level buffer that this corresponds to.
|
2007-11-08 12:27:23 +08:00
|
|
|
|
|
|
|
// Rewrite tabs if we care.
|
|
|
|
//RewriteTabs();
|
2007-10-25 01:06:59 +08:00
|
|
|
|
2008-03-29 06:26:09 +08:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create the output file.
|
|
|
|
|
|
|
|
std::ostream *OutFile;
|
|
|
|
if (OutFileName == "-") {
|
|
|
|
OutFile = llvm::cout.stream();
|
|
|
|
} else if (!OutFileName.empty()) {
|
|
|
|
OutFile = new std::ofstream(OutFileName.c_str(),
|
|
|
|
std::ios_base::binary|std::ios_base::out);
|
|
|
|
} else if (InFileName == "-") {
|
|
|
|
OutFile = llvm::cout.stream();
|
|
|
|
} else {
|
|
|
|
llvm::sys::Path Path(InFileName);
|
|
|
|
Path.eraseSuffix();
|
|
|
|
Path.appendSuffix("cpp");
|
|
|
|
OutFile = new std::ofstream(Path.toString().c_str(),
|
|
|
|
std::ios_base::binary|std::ios_base::out);
|
|
|
|
}
|
|
|
|
|
2008-01-19 08:30:35 +08:00
|
|
|
RewriteInclude();
|
|
|
|
|
2008-03-28 06:29:16 +08:00
|
|
|
InsertText(SourceLocation::getFileLoc(MainFileID, 0),
|
|
|
|
Preamble.c_str(), Preamble.size(), false);
|
|
|
|
|
2007-11-06 01:47:33 +08:00
|
|
|
// Rewrite Objective-c meta data*
|
|
|
|
std::string ResultStr;
|
2007-11-14 03:21:13 +08:00
|
|
|
RewriteImplementations(ResultStr);
|
2007-11-06 01:47:33 +08:00
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
// Get the buffer corresponding to MainFileID. If we haven't changed it, then
|
|
|
|
// we are done.
|
|
|
|
if (const RewriteBuffer *RewriteBuf =
|
|
|
|
Rewrite.getRewriteBufferFor(MainFileID)) {
|
2007-11-03 19:27:19 +08:00
|
|
|
//printf("Changed:\n");
|
2008-03-29 06:26:09 +08:00
|
|
|
*OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
|
2007-10-25 01:06:59 +08:00
|
|
|
} else {
|
2008-03-22 08:08:40 +08:00
|
|
|
fprintf(stderr, "No changes\n");
|
2007-10-25 01:06:59 +08:00
|
|
|
}
|
2007-11-08 02:40:28 +08:00
|
|
|
// Emit metadata.
|
2008-03-29 06:26:09 +08:00
|
|
|
*OutFile << ResultStr;
|
2007-10-27 03:46:17 +08:00
|
|
|
}
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Syntactic (non-AST) Rewriting Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteInclude() {
|
2008-01-19 08:30:35 +08:00
|
|
|
SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0);
|
|
|
|
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");
|
|
|
|
|
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
|
|
|
|
SourceLocation ImportLoc =
|
|
|
|
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;
|
2007-10-19 03:23:00 +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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Get the location of the tab.
|
|
|
|
SourceLocation TabLoc =
|
|
|
|
SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
|
|
|
|
|
|
|
|
// 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-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
|
2007-10-25 01:06:59 +08:00
|
|
|
int numDecls = ClassDecl->getNumForwardDecls();
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
|
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, ';');
|
|
|
|
|
|
|
|
// 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";
|
|
|
|
for (int i = 0; i < numDecls; i++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i];
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += "#ifndef _REWRITER_typedef_";
|
|
|
|
typedefString += ForwardDecl->getName();
|
|
|
|
typedefString += "\n";
|
|
|
|
typedefString += "#define _REWRITER_typedef_";
|
|
|
|
typedefString += ForwardDecl->getName();
|
|
|
|
typedefString += "\n";
|
2007-11-05 22:36:37 +08:00
|
|
|
typedefString += "typedef struct objc_object ";
|
2007-10-25 06:48:43 +08:00
|
|
|
typedefString += ForwardDecl->getName();
|
2007-11-09 20:50:28 +08:00
|
|
|
typedefString += ";\n#endif\n";
|
2007-10-25 06:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the @class with typedefs corresponding to the classes.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, semiPtr-startBuf+1,
|
|
|
|
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();
|
2007-10-30 21:30:57 +08:00
|
|
|
|
2007-12-15 07:37:57 +08:00
|
|
|
if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) {
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(LocStart, "/* ", 3);
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocEnd, 1, ";*/ ", 4);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties)
|
2007-11-07 08:09:37 +08:00
|
|
|
{
|
2008-03-17 05:23:50 +08:00
|
|
|
for (unsigned i = 0; i < nProperties; i++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCPropertyDecl *Property = Properties[i];
|
2007-11-07 08:09:37 +08:00
|
|
|
SourceLocation Loc = Property->getLocation();
|
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(Loc, 0, "// ", 3);
|
2007-11-07 08:09:37 +08:00
|
|
|
|
|
|
|
// FIXME: handle properties that are declared across multiple lines.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// FIXME: handle category headers that are declared across multiple lines.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, 0, "// ", 3);
|
2007-10-30 21:30:57 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = CatDecl->instmeth_end(); I != E; ++I)
|
|
|
|
RewriteMethodDeclaration(*I);
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = CatDecl->classmeth_end(); I != E; ++I)
|
|
|
|
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);
|
|
|
|
|
2007-10-31 00:42:30 +08:00
|
|
|
SourceLocation LocStart = PDecl->getLocStart();
|
|
|
|
|
|
|
|
// FIXME: handle protocol headers that are declared across multiple lines.
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(LocStart, 0, "// ", 3);
|
2007-10-31 00:42:30 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = PDecl->instmeth_end(); I != E; ++I)
|
|
|
|
RewriteMethodDeclaration(*I);
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = PDecl->classmeth_end(); I != E; ++I)
|
|
|
|
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());
|
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());
|
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
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD,
|
2007-11-14 02:44:14 +08:00
|
|
|
std::string &ResultStr) {
|
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-07-16 22:40:40 +08:00
|
|
|
else if (OMD->getResultType()->isFunctionPointerType()) {
|
|
|
|
// needs special handling, since pointer-to-functions have special
|
|
|
|
// syntax (where a decaration models use).
|
|
|
|
QualType retType = OMD->getResultType();
|
|
|
|
if (const PointerType* PT = retType->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
|
|
|
if ((FPRetType = PointeeTy->getAsFunctionType())) {
|
|
|
|
ResultStr += FPRetType->getResultType().getAsString();
|
|
|
|
ResultStr += "(*";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2007-12-18 05:03:50 +08:00
|
|
|
ResultStr += OMD->getResultType().getAsString();
|
2008-01-10 09:39:52 +08:00
|
|
|
ResultStr += " ";
|
2007-11-14 02:44:14 +08:00
|
|
|
|
|
|
|
// Unique method name
|
2007-11-14 05:02:00 +08:00
|
|
|
std::string NameStr;
|
|
|
|
|
2007-11-14 02:44:14 +08:00
|
|
|
if (OMD->isInstance())
|
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_";
|
2007-11-14 02:44:14 +08:00
|
|
|
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += OMD->getClassInterface()->getName();
|
|
|
|
NameStr += "_";
|
2007-11-14 02:44:14 +08:00
|
|
|
|
|
|
|
NamedDecl *MethodContext = OMD->getMethodContext();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCCategoryImplDecl *CID =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(MethodContext)) {
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += CID->getName();
|
|
|
|
NameStr += "_";
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
2008-04-05 06:23:44 +08:00
|
|
|
// Append selector names, replacing ':' with '_'
|
|
|
|
if (OMD->getSelector().getName().find(':') == std::string::npos)
|
2007-11-14 05:02:00 +08:00
|
|
|
NameStr += OMD->getSelector().getName();
|
2007-11-14 02:44:14 +08:00
|
|
|
else {
|
|
|
|
std::string selString = OMD->getSelector().getName();
|
|
|
|
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;
|
2007-11-14 02:44:14 +08:00
|
|
|
|
|
|
|
// Rewrite arguments
|
|
|
|
ResultStr += "(";
|
|
|
|
|
|
|
|
// invisible arguments
|
|
|
|
if (OMD->isInstance()) {
|
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-03-11 07:16:54 +08:00
|
|
|
ResultStr += OMD->getClassInterface()->getName();
|
|
|
|
ResultStr += " *";
|
2007-11-14 02:44:14 +08:00
|
|
|
}
|
|
|
|
else
|
2008-01-08 03:49:32 +08:00
|
|
|
ResultStr += Context->getObjCIdType().getAsString();
|
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";
|
|
|
|
|
|
|
|
// Method arguments.
|
2008-03-16 09:07:14 +08:00
|
|
|
for (unsigned i = 0; i < OMD->getNumParams(); i++) {
|
2007-11-14 02:44:14 +08:00
|
|
|
ParmVarDecl *PDecl = OMD->getParamDecl(i);
|
|
|
|
ResultStr += ", ";
|
2008-04-19 05:13:19 +08:00
|
|
|
if (PDecl->getType()->isObjCQualifiedIdType()) {
|
|
|
|
ResultStr += "id ";
|
|
|
|
ResultStr += PDecl->getName();
|
|
|
|
} else {
|
|
|
|
std::string Name = PDecl->getName();
|
|
|
|
PDecl->getType().getAsStringInternal(Name);
|
|
|
|
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 += ") ";
|
2007-11-14 02:44:14 +08:00
|
|
|
|
2008-07-16 22:40:40 +08:00
|
|
|
if (FPRetType) {
|
|
|
|
ResultStr += ")"; // close the precedence "scope" for "*".
|
|
|
|
|
|
|
|
// Now, emit the argument types (if any).
|
|
|
|
if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(FPRetType)) {
|
|
|
|
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
|
|
|
}
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteImplementationDecl(NamedDecl *OID) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
|
|
|
|
ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
|
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);
|
2007-11-14 02:44:14 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryImplDecl::instmeth_iterator
|
2007-12-12 15:46:12 +08:00
|
|
|
I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
|
|
|
|
E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); 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();
|
|
|
|
SourceLocation LocEnd = OMD->getBody()->getLocStart();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryImplDecl::classmeth_iterator
|
2007-12-12 15:46:12 +08:00
|
|
|
I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
|
|
|
|
E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); 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();
|
|
|
|
SourceLocation LocEnd = OMD->getBody()->getLocStart();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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
|
2008-02-01 03:42:41 +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_";
|
2007-11-09 20:50:28 +08:00
|
|
|
ResultStr += ClassDecl->getName();
|
|
|
|
ResultStr += "\n";
|
|
|
|
ResultStr += "#define _REWRITER_typedef_";
|
|
|
|
ResultStr += ClassDecl->getName();
|
|
|
|
ResultStr += "\n";
|
2008-03-11 07:16:54 +08:00
|
|
|
ResultStr += "typedef struct objc_object ";
|
2007-11-01 11:35:41 +08:00
|
|
|
ResultStr += ClassDecl->getName();
|
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);
|
2007-10-30 10:23:23 +08:00
|
|
|
|
2007-11-07 08:09:37 +08:00
|
|
|
RewriteProperties(ClassDecl->getNumPropertyDecl(),
|
|
|
|
ClassDecl->getPropertyDecl());
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = ClassDecl->instmeth_end(); I != E; ++I)
|
|
|
|
RewriteMethodDeclaration(*I);
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
|
2007-12-15 07:37:57 +08:00
|
|
|
E = ClassDecl->classmeth_end(); I != E; ++I)
|
|
|
|
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-05-24 04:40:52 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV,
|
|
|
|
SourceLocation OrigStart) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIvarDecl *D = IV->getDecl();
|
2008-03-12 08:25:36 +08:00
|
|
|
if (CurMethodDecl) {
|
|
|
|
if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) {
|
2008-05-09 01:52:16 +08:00
|
|
|
ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
|
|
|
|
// lookup which class implements the instance variable.
|
|
|
|
ObjCInterfaceDecl *clsDeclared = 0;
|
|
|
|
iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared);
|
|
|
|
assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
|
|
|
|
|
|
|
|
// 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));
|
2008-08-19 07:01:59 +08:00
|
|
|
CastExpr *castExpr = new ExplicitCastExpr(castT, IV->getBase(),
|
|
|
|
SourceLocation());
|
2008-05-09 01:52:16 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2008-05-24 04:40:52 +08:00
|
|
|
ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(),
|
|
|
|
IV->getBase()->getLocEnd(),
|
|
|
|
castExpr);
|
2008-05-09 01:52:16 +08:00
|
|
|
if (IV->isFreeIvar() &&
|
|
|
|
CurMethodDecl->getClassInterface() == iFaceDecl->getDecl()) {
|
2008-05-24 04:40:52 +08:00
|
|
|
MemberExpr *ME = new MemberExpr(PE, true, D, IV->getLocation(),
|
|
|
|
D->getType());
|
2008-05-09 01:52:16 +08:00
|
|
|
ReplaceStmt(IV, ME);
|
|
|
|
delete IV;
|
|
|
|
return ME;
|
2007-11-15 19:33:00 +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].
|
|
|
|
IV->setBase(PE);
|
|
|
|
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");
|
|
|
|
|
|
|
|
// Explicit ivar refs need to have a cast inserted.
|
|
|
|
// FIXME: consider sharing some of this code with the code above.
|
|
|
|
if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) {
|
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;
|
2008-05-09 01:52:16 +08:00
|
|
|
iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared);
|
2008-05-07 07:20:07 +08:00
|
|
|
assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
|
|
|
|
|
|
|
|
// 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));
|
2008-08-19 07:01:59 +08:00
|
|
|
CastExpr *castExpr = new ExplicitCastExpr(castT, IV->getBase(),
|
|
|
|
SourceLocation());
|
2008-05-07 07:20:07 +08:00
|
|
|
// Don't forget the parens to enforce the proper binding.
|
2008-05-29 00:38:23 +08:00
|
|
|
ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(),
|
|
|
|
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].
|
|
|
|
IV->setBase(PE);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-10-25 01:06:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Body / Expression rewriting
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
|
2008-01-16 07:58:23 +08:00
|
|
|
if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
|
|
|
|
isa<DoStmt>(S) || isa<ForStmt>(S))
|
|
|
|
Stmts.push_back(S);
|
|
|
|
else if (isa<ObjCForCollectionStmt>(S)) {
|
|
|
|
Stmts.push_back(S);
|
|
|
|
ObjCBcLabelNo.push_back(++BcLabelCount);
|
|
|
|
}
|
|
|
|
|
2008-05-24 04:40:52 +08:00
|
|
|
SourceRange OrigStmtRange = S->getSourceRange();
|
2008-01-31 13:10:40 +08:00
|
|
|
|
|
|
|
// Start by rewriting all children.
|
2007-10-17 06:36:42 +08:00
|
|
|
for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
|
|
|
|
CI != E; ++CI)
|
2007-11-07 12:08:17 +08:00
|
|
|
if (*CI) {
|
2007-11-09 23:20:18 +08:00
|
|
|
Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI);
|
2007-11-07 12:08:17 +08:00
|
|
|
if (newStmt)
|
|
|
|
*CI = newStmt;
|
|
|
|
}
|
2007-10-24 07:50:29 +08:00
|
|
|
|
|
|
|
// Handle specific things.
|
|
|
|
if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
|
|
|
|
return RewriteAtEncode(AtEncode);
|
2007-11-15 10:58:25 +08:00
|
|
|
|
|
|
|
if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S))
|
2008-05-24 04:40:52 +08:00
|
|
|
return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin());
|
2007-11-05 22:50:49 +08:00
|
|
|
|
|
|
|
if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
|
|
|
|
return RewriteAtSelector(AtSelector);
|
2008-01-31 03:17:43 +08:00
|
|
|
|
2007-11-03 19:27:19 +08:00
|
|
|
if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
|
|
|
|
return RewriteObjCStringLiteral(AtString);
|
2007-10-24 07:50:29 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
|
|
|
|
// Before we rewrite it, put the original message expression in a comment.
|
|
|
|
SourceLocation startLoc = MessExpr->getLocStart();
|
|
|
|
SourceLocation endLoc = MessExpr->getLocEnd();
|
|
|
|
|
|
|
|
const char *startBuf = SM->getCharacterData(startLoc);
|
|
|
|
const char *endBuf = SM->getCharacterData(endLoc);
|
|
|
|
|
|
|
|
std::string messString;
|
|
|
|
messString += "// ";
|
|
|
|
messString.append(startBuf, endBuf-startBuf+1);
|
|
|
|
messString += "\n";
|
2007-10-27 04:53:56 +08:00
|
|
|
|
2008-02-01 03:42:41 +08:00
|
|
|
// FIXME: Missing definition of
|
|
|
|
// InsertText(clang::SourceLocation, char const*, unsigned int).
|
|
|
|
// InsertText(startLoc, messString.c_str(), messString.size());
|
2007-10-25 06:48:43 +08:00
|
|
|
// Tried this, but it didn't work either...
|
2008-02-01 03:42:41 +08:00
|
|
|
// ReplaceText(startLoc, 0, messString.c_str(), messString.size());
|
2007-10-24 07:50:29 +08:00
|
|
|
return RewriteMessageExpr(MessExpr);
|
2007-10-25 06:48:43 +08:00
|
|
|
}
|
2007-11-06 01:47:33 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
|
|
|
|
return RewriteObjCTryStmt(StmtTry);
|
2007-11-07 23:32:26 +08:00
|
|
|
|
2008-01-30 06:59:37 +08:00
|
|
|
if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
|
|
|
|
return RewriteObjCSynchronizedStmt(StmtTry);
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
|
|
|
|
return RewriteObjCThrowStmt(StmtThrow);
|
2007-12-08 02:47:10 +08:00
|
|
|
|
|
|
|
if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
|
|
|
|
return RewriteObjCProtocolExpr(ProtocolExp);
|
2008-01-08 05:40:22 +08:00
|
|
|
|
|
|
|
if (ObjCForCollectionStmt *StmtForCollection =
|
|
|
|
dyn_cast<ObjCForCollectionStmt>(S))
|
2008-05-24 04:40:52 +08:00
|
|
|
return RewriteObjCForCollectionStmt(StmtForCollection,
|
|
|
|
OrigStmtRange.getEnd());
|
2008-01-16 07:58:23 +08:00
|
|
|
if (BreakStmt *StmtBreakStmt =
|
|
|
|
dyn_cast<BreakStmt>(S))
|
|
|
|
return RewriteBreakStmt(StmtBreakStmt);
|
|
|
|
if (ContinueStmt *StmtContinueStmt =
|
|
|
|
dyn_cast<ContinueStmt>(S))
|
|
|
|
return RewriteContinueStmt(StmtContinueStmt);
|
2008-07-30 02:15:38 +08:00
|
|
|
|
|
|
|
// Need to check for protocol refs (id <P>, Foo <P> *) in variable decls and cast exprs.
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
|
|
|
|
RewriteObjCQualifiedInterfaceTypes(DS->getDecl());
|
2008-08-19 07:01:59 +08:00
|
|
|
if (ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(S))
|
2008-07-30 02:15:38 +08:00
|
|
|
RewriteObjCQualifiedInterfaceTypes(CE);
|
2008-01-08 05:40:22 +08:00
|
|
|
|
2008-01-16 07:58:23 +08:00
|
|
|
if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
|
|
|
|
isa<DoStmt>(S) || isa<ForStmt>(S)) {
|
|
|
|
assert(!Stmts.empty() && "Statement stack is empty");
|
|
|
|
assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
|
|
|
|
isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
|
|
|
|
&& "Statement stack mismatch");
|
|
|
|
Stmts.pop_back();
|
|
|
|
}
|
2007-11-15 18:28:18 +08:00
|
|
|
#if 0
|
|
|
|
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
|
|
|
|
CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
|
|
|
|
// Get the new text.
|
|
|
|
std::ostringstream Buf;
|
|
|
|
Replacement->printPretty(Buf);
|
|
|
|
const std::string &Str = Buf.str();
|
|
|
|
|
|
|
|
printf("CAST = %s\n", &Str[0]);
|
2008-02-01 03:42:41 +08:00
|
|
|
InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
|
2007-11-15 18:28:18 +08:00
|
|
|
delete S;
|
|
|
|
return Replacement;
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-25 00:57:36 +08:00
|
|
|
// Return this stmt unmodified.
|
|
|
|
return S;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
2008-01-08 05:40:22 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
/// SynthCountByEnumWithState - To print:
|
|
|
|
/// ((unsigned int (*)
|
|
|
|
/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
|
|
|
|
/// (void *)objc_msgSend)((id)l_collection,
|
|
|
|
/// sel_registerName(
|
|
|
|
/// "countByEnumeratingWithState:objects:count:"),
|
|
|
|
/// &enumState,
|
|
|
|
/// (id *)items, (unsigned int)16)
|
|
|
|
///
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthCountByEnumWithState(std::string &buf) {
|
2008-01-09 06:06:28 +08:00
|
|
|
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)";
|
|
|
|
}
|
2008-01-08 05:40:22 +08:00
|
|
|
|
2008-01-16 07:58:23 +08:00
|
|
|
/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
|
|
|
|
/// statement to exit to its outer synthesized loop.
|
|
|
|
///
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) {
|
2008-01-16 07:58:23 +08:00
|
|
|
if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
|
|
|
|
return S;
|
|
|
|
// replace break with goto __break_label
|
|
|
|
std::string buf;
|
|
|
|
|
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
buf = "goto __break_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size());
|
2008-01-16 07:58:23 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
|
|
|
|
/// statement to continue with its inner synthesized loop.
|
|
|
|
///
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) {
|
2008-01-16 07:58:23 +08:00
|
|
|
if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
|
|
|
|
return S;
|
|
|
|
// replace continue with goto __continue_label
|
|
|
|
std::string buf;
|
|
|
|
|
|
|
|
SourceLocation startLoc = S->getLocStart();
|
|
|
|
buf = "goto __continue_label_";
|
|
|
|
buf += utostr(ObjCBcLabelNo.back());
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size());
|
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; }
|
2007-10-19 06:09:03 +08:00
|
|
|
|
2008-01-08 05:40:22 +08:00
|
|
|
/// Into:
|
|
|
|
/// {
|
2008-01-09 06:06:28 +08:00
|
|
|
/// type elem;
|
|
|
|
/// 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;
|
|
|
|
/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
|
|
|
|
/// 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 {
|
|
|
|
/// 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);
|
2008-01-09 06:06:28 +08:00
|
|
|
/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
|
|
|
|
/// 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");
|
|
|
|
assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
|
|
|
|
"ObjCForCollectionStmt Statement stack mismatch");
|
|
|
|
assert(!ObjCBcLabelNo.empty() &&
|
|
|
|
"ObjCForCollectionStmt - Label No stack empty");
|
|
|
|
|
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;
|
|
|
|
QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType();
|
2008-01-10 02:15:42 +08:00
|
|
|
elementTypeAsString = ElementType.getAsString();
|
|
|
|
buf += elementTypeAsString;
|
2008-01-09 06:06:28 +08:00
|
|
|
buf += " ";
|
|
|
|
elementName = DS->getDecl()->getName();
|
|
|
|
buf += elementName;
|
|
|
|
buf += ";\n\t";
|
|
|
|
}
|
2008-04-08 13:52:18 +08:00
|
|
|
else {
|
|
|
|
DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
|
2008-01-09 06:06:28 +08:00
|
|
|
elementName = DR->getDecl()->getName();
|
2008-01-10 02:15:42 +08:00
|
|
|
elementTypeAsString = DR->getDecl()->getType().getAsString();
|
|
|
|
}
|
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;
|
|
|
|
|
2008-01-09 06:06:28 +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";
|
|
|
|
|
|
|
|
// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
|
|
|
|
// objects:items count:16];
|
|
|
|
// which is synthesized into:
|
|
|
|
// unsigned int limit =
|
|
|
|
// ((unsigned int (*)
|
|
|
|
// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
|
|
|
|
// (void *)objc_msgSend)((id)l_collection,
|
|
|
|
// sel_registerName(
|
|
|
|
// "countByEnumeratingWithState:objects:count:"),
|
|
|
|
// (struct __objcFastEnumerationState *)&state,
|
|
|
|
// (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 {
|
|
|
|
/// if (startMutations != *enumState.mutationsPtr)
|
|
|
|
/// 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());
|
2008-01-09 06:06:28 +08:00
|
|
|
|
2008-01-16 07:58:23 +08:00
|
|
|
/// __continue_label: ;
|
2008-01-09 06:06:28 +08:00
|
|
|
/// } while (counter < limit);
|
|
|
|
/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
|
|
|
|
/// 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;
|
|
|
|
/// }
|
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;
|
|
|
|
buf += " = nil;\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;
|
|
|
|
buf += " = nil;\n";
|
|
|
|
buf += "}\n";
|
2008-07-22 02:26:02 +08:00
|
|
|
|
2008-01-09 06:06:28 +08:00
|
|
|
// Insert all these *after* the statement body.
|
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
|
|
|
}
|
|
|
|
|
2008-01-30 06:59:37 +08:00
|
|
|
/// RewriteObjCSynchronizedStmt -
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
assert((*startBuf == '@') && "bogus @synchronized location");
|
|
|
|
|
|
|
|
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());
|
2008-08-19 21:04:19 +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
|
|
|
|
// 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);
|
|
|
|
|
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";
|
|
|
|
buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
|
2008-07-17 03:47:39 +08:00
|
|
|
buf += " objc_sync_exit(";
|
2008-08-21 21:03:03 +08:00
|
|
|
Expr *syncExpr = new ExplicitCastExpr(Context->getObjCIdType(),
|
|
|
|
S->getSynchExpr(), SourceLocation());
|
2008-07-17 03:47:39 +08:00
|
|
|
std::ostringstream syncExprBuf;
|
2008-08-21 21:03:03 +08:00
|
|
|
syncExpr->printPretty(syncExprBuf);
|
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 += "}";
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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());
|
2007-11-07 12:08:17 +08:00
|
|
|
|
|
|
|
startLoc = S->getTryBody()->getLocEnd();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
|
|
|
|
|
|
|
assert((*startBuf == '}') && "bogus @try block");
|
|
|
|
|
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 */";
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Stmt *catchStmt = catchList->getCatchParamStmt();
|
|
|
|
|
|
|
|
if (catchList == S->getCatchStmts())
|
|
|
|
buf = "if ("; // we are generating code for the first catch clause
|
|
|
|
else
|
|
|
|
buf = "else if (";
|
|
|
|
startLoc = catchList->getLocStart();
|
|
|
|
startBuf = SM->getCharacterData(startLoc);
|
|
|
|
|
|
|
|
assert((*startBuf == '@') && "bogus @catch location");
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
buf += "1) { id _tmp = _caught;";
|
|
|
|
Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1,
|
|
|
|
buf.c_str(), buf.size());
|
|
|
|
} else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) {
|
2007-11-07 12:08:17 +08:00
|
|
|
QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->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;
|
|
|
|
} else if (const PointerType *pType = t->getAsPointerType()) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceType *cls; // Should be a pointer to a class.
|
2007-11-07 12:08:17 +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(\"";
|
2007-11-07 12:08:17 +08:00
|
|
|
buf += cls->getDecl()->getName();
|
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");
|
|
|
|
|
|
|
|
buf = " = _caught;";
|
|
|
|
// Here we replace ") {" with "= _caught;" (which initializes and
|
|
|
|
// declares the @catch parameter).
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size());
|
2007-11-07 23:32:26 +08:00
|
|
|
} else if (!isa<NullStmt>(catchStmt)) {
|
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");
|
2007-11-07 12:08:17 +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());
|
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");
|
|
|
|
|
|
|
|
buf = "/* @finally */";
|
2008-02-01 03:51:04 +08:00
|
|
|
ReplaceText(startLoc, 8, buf.c_str(), buf.size());
|
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");
|
|
|
|
assert(*SM->getCharacterData(endLoc) == '}' &&
|
|
|
|
"bogus @finally body location");
|
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());
|
2007-11-07 12:08:17 +08:00
|
|
|
|
|
|
|
// Set lastCurlyLoc
|
|
|
|
lastCurlyLoc = body->getLocEnd();
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-01 03:37:57 +08:00
|
|
|
// This can't be done with ReplaceStmt(S, ThrowExpr), since
|
2007-11-07 23:32:26 +08:00
|
|
|
// the throw expression is typically a message expression that's already
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
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";
|
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());
|
|
|
|
|
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-01-23 06:44:46 +08:00
|
|
|
Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding,
|
|
|
|
EncodingRecordTypes);
|
2007-10-29 13:01:08 +08:00
|
|
|
Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
|
|
|
|
StrEncoding.length(), false, StrType,
|
2007-10-18 06:35:30 +08:00
|
|
|
SourceLocation(), SourceLocation());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, Replacement);
|
2007-12-01 06:25:36 +08:00
|
|
|
|
2007-12-01 06:53:43 +08:00
|
|
|
// Replace this subexpr in the parent.
|
2007-10-25 00:57:36 +08:00
|
|
|
delete Exp;
|
|
|
|
return Replacement;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
|
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);
|
|
|
|
SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
|
|
|
|
Exp->getSelector().getName().size(),
|
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
|
|
|
|
&SelExprs[0], SelExprs.size());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, SelExp);
|
2007-11-05 22:50:49 +08:00
|
|
|
delete Exp;
|
|
|
|
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();
|
2007-10-24 07:50:29 +08:00
|
|
|
|
|
|
|
// Create a reference to the objc_msgSend() declaration.
|
2007-10-25 06:48:43 +08:00
|
|
|
DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
|
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);
|
2007-10-24 07:50:29 +08:00
|
|
|
ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
|
|
|
|
|
|
|
|
const FunctionType *FT = msgSendType->getAsFunctionType();
|
2007-10-25 00:57:36 +08:00
|
|
|
|
2007-10-25 06:48:43 +08:00
|
|
|
return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
|
|
|
|
}
|
|
|
|
|
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) {
|
2007-12-18 05:03:50 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (T->isObjCQualifiedIdType())
|
2007-12-18 05:03:50 +08:00
|
|
|
return true;
|
|
|
|
|
2007-11-01 21:24:47 +08:00
|
|
|
if (const PointerType *pType = T->getAsPointerType()) {
|
2007-10-31 12:38:33 +08:00
|
|
|
Type *pointeeType = pType->getPointeeType().getTypePtr();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (isa<ObjCQualifiedInterfaceType>(pointeeType))
|
2007-10-31 12:38:33 +08:00
|
|
|
return true; // we have "Class <Protocol> *".
|
|
|
|
}
|
2007-11-01 21:24:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-30 02:15:38 +08:00
|
|
|
void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
|
|
|
|
QualType Type = E->getType();
|
|
|
|
if (needToScanForQualifiers(Type)) {
|
|
|
|
SourceLocation Loc = E->getLocStart();
|
|
|
|
const char *startBuf = SM->getCharacterData(Loc);
|
|
|
|
const char *endBuf = SM->getCharacterData(E->getLocEnd());
|
|
|
|
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;
|
|
|
|
const FunctionTypeProto *proto = 0;
|
|
|
|
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");
|
|
|
|
proto = dyn_cast<FunctionTypeProto>(funcType);
|
|
|
|
if (!proto)
|
|
|
|
return;
|
|
|
|
Type = proto->getResultType();
|
|
|
|
}
|
|
|
|
else
|
2007-12-12 03:56:36 +08:00
|
|
|
return;
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
2007-12-12 07:04:08 +08:00
|
|
|
SourceLocation LessLoc =
|
|
|
|
Loc.getFileLocWithOffset(startRef-startFuncBuf);
|
|
|
|
SourceLocation GreaterLoc =
|
|
|
|
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(),
|
|
|
|
false /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-12-05 05:47:40 +08:00
|
|
|
SelGetUidIdent, getFuncType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
2007-12-08 02:47:10 +08:00
|
|
|
// SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthGetProtocolFunctionDecl() {
|
2007-12-08 02:47:10 +08:00
|
|
|
IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol");
|
|
|
|
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->getObjCProtoType(),
|
2007-12-08 02:47:10 +08:00
|
|
|
&ArgTys[0], ArgTys.size(),
|
|
|
|
false /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-12-08 02:47:10 +08:00
|
|
|
SelGetProtoIdent, getFuncType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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>
|
2007-11-03 19:27:19 +08:00
|
|
|
if (strcmp(FD->getName(), "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;
|
|
|
|
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super");
|
|
|
|
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(),
|
|
|
|
false);
|
2008-04-17 22:40:12 +08:00
|
|
|
SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-03-12 01:37:02 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
true /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-10-31 07:14:51 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
true /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-11-15 18:28:18 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
true /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-12-04 03:17:29 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SynthMsgSendSuperStretFunctionDecl -
|
|
|
|
// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() {
|
2007-12-04 03:17:29 +08:00
|
|
|
IdentifierInfo *msgSendIdent =
|
|
|
|
&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(),
|
|
|
|
true /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-03-16 05:24:04 +08:00
|
|
|
SourceLocation(),
|
2007-12-04 03:17:29 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
true /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-12-04 05:26:48 +08:00
|
|
|
msgSendIdent, msgSendType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
false /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-10-31 07:14:51 +08:00
|
|
|
getClassIdent, getClassType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
false /*isVariadic*/);
|
2008-04-17 22:40:12 +08:00
|
|
|
GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2007-12-07 11:50:46 +08:00
|
|
|
getClassIdent, getClassType,
|
|
|
|
FunctionDecl::Extern, false, 0);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
std::ostringstream prettyBuf;
|
|
|
|
Exp->getString()->printPretty(prettyBuf);
|
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";
|
2008-03-15 08:55:56 +08:00
|
|
|
|
2008-04-17 22:40:12 +08:00
|
|
|
VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
|
2008-04-16 06:42:06 +08:00
|
|
|
&Context->Idents.get(S.c_str()), strType,
|
|
|
|
VarDecl::Static, NULL);
|
2008-03-15 08:55:56 +08:00
|
|
|
DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation());
|
|
|
|
Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf,
|
|
|
|
Context->getPointerType(DRE->getType()),
|
|
|
|
SourceLocation());
|
2007-11-08 22:30:50 +08:00
|
|
|
// cast to NSConstantString *
|
2008-08-19 07:01:59 +08:00
|
|
|
CastExpr *cast = new ExplicitCastExpr(Exp->getType(), Unop, SourceLocation());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, cast);
|
2007-11-08 22:30:50 +08:00
|
|
|
delete Exp;
|
|
|
|
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'
|
2008-03-15 14:12:44 +08:00
|
|
|
if (!CurMethodDecl || !CurMethodDecl->isInstance()) return 0;
|
|
|
|
|
2008-08-10 09:53:14 +08:00
|
|
|
if (PredefinedExpr *PDE = dyn_cast<PredefinedExpr>(recExpr))
|
|
|
|
if (PDE->getIdentType() == PredefinedExpr::ObjCSuper) {
|
2008-06-22 02:04:54 +08:00
|
|
|
const PointerType *PT = PDE->getType()->getAsPointerType();
|
|
|
|
assert(PT);
|
|
|
|
ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType());
|
|
|
|
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,
|
2008-03-16 05:32:50 +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];
|
|
|
|
|
|
|
|
// struct objc_object *receiver;
|
2008-01-08 03:49:32 +08:00
|
|
|
FieldTypes[0] = Context->getObjCIdType();
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_class *super;
|
2008-01-08 03:49:32 +08:00
|
|
|
FieldTypes[1] = Context->getObjCClassType();
|
2007-11-15 18:28:18 +08:00
|
|
|
// Create fields
|
|
|
|
FieldDecl *FieldDecls[2];
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
2008-04-06 12:47:34 +08:00
|
|
|
FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0,
|
2008-03-16 08:16:02 +08:00
|
|
|
FieldTypes[i]);
|
2007-11-15 18:28:18 +08:00
|
|
|
|
Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.
This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).
The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.
- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
addition of DeclGroups.
Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
referenced by the AST. For example:
typedef struct { ... } x;
The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
refer to it. This will be solved with DeclGroups.
- This patch also (temporarily) breaks CodeGen. More below.
High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it. When
a struct/union/class is first referenced, a RecordType and RecordDecl are
created for it, and the RecordType refers to that RecordDecl. Later, if
a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
updated to point to the RecordDecl that defines the struct/union/class.
- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
enum/struct/class/union. This is useful from going from a RecordDecl* that
defines a forward declaration to the RecordDecl* that provides the actual
definition. Note that this also works for EnumDecls, except that in this case
there is no distinction between forward declarations and definitions (yet).
- Clients should no longer assume that 'isDefinition()' returns true from a
RecordDecl if the corresponding struct/union/class has been defined.
isDefinition() only returns true if a particular RecordDecl is the defining
Decl. Use 'getDefinition()' instead to determine if a struct has been defined.
- The main changes to Sema happen in ActOnTag. To make the changes more
incremental, I split off the processing of enums and structs et al into two
code paths. Enums use the original code path (which is in ActOnTag) and
structs use the ActOnTagStruct. Eventually the two code paths will be merged,
but the idea was to preserve the original logic both for comparison and not to
change the logic for both enums and structs all at once.
- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
that correspond to the same type simply have a pointer to that type. If we
need to figure out what are all the RecordDecls for a given type we can build
a backmap.
- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
changes to RecordDecl. For some reason 'svn' marks the entire file as changed.
Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
RecordType*. This was true before because we only created one RecordDecl* for
a given RecordType*, but it is no longer true. I believe this shouldn't be too
hard to change, but the patch was big enough as it is.
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).
llvm-svn: 55839
2008-09-06 01:16:31 +08:00
|
|
|
SuperStructDecl->defineBody(*Context, FieldDecls, 4);
|
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,
|
2008-03-16 05:32:50 +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];
|
|
|
|
|
|
|
|
// struct objc_object *receiver;
|
|
|
|
FieldTypes[0] = Context->getObjCIdType();
|
|
|
|
// int flags;
|
|
|
|
FieldTypes[1] = Context->IntTy;
|
|
|
|
// char *str;
|
|
|
|
FieldTypes[2] = Context->getPointerType(Context->CharTy);
|
|
|
|
// long length;
|
|
|
|
FieldTypes[3] = Context->LongTy;
|
|
|
|
// Create fields
|
2008-03-28 06:59:54 +08:00
|
|
|
FieldDecl *FieldDecls[4];
|
2008-03-15 08:55:56 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < 4; ++i)
|
2008-04-06 12:47:34 +08:00
|
|
|
FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0,
|
2008-03-16 08:16:02 +08:00
|
|
|
FieldTypes[i]);
|
2008-03-15 08:55:56 +08:00
|
|
|
|
Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.
This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).
The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.
- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
addition of DeclGroups.
Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
referenced by the AST. For example:
typedef struct { ... } x;
The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
refer to it. This will be solved with DeclGroups.
- This patch also (temporarily) breaks CodeGen. More below.
High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it. When
a struct/union/class is first referenced, a RecordType and RecordDecl are
created for it, and the RecordType refers to that RecordDecl. Later, if
a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
updated to point to the RecordDecl that defines the struct/union/class.
- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
enum/struct/class/union. This is useful from going from a RecordDecl* that
defines a forward declaration to the RecordDecl* that provides the actual
definition. Note that this also works for EnumDecls, except that in this case
there is no distinction between forward declarations and definitions (yet).
- Clients should no longer assume that 'isDefinition()' returns true from a
RecordDecl if the corresponding struct/union/class has been defined.
isDefinition() only returns true if a particular RecordDecl is the defining
Decl. Use 'getDefinition()' instead to determine if a struct has been defined.
- The main changes to Sema happen in ActOnTag. To make the changes more
incremental, I split off the processing of enums and structs et al into two
code paths. Enums use the original code path (which is in ActOnTag) and
structs use the ActOnTagStruct. Eventually the two code paths will be merged,
but the idea was to preserve the original logic both for comparison and not to
change the logic for both enums and structs all at once.
- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
that correspond to the same type simply have a pointer to that type. If we
need to figure out what are all the RecordDecls for a given type we can build
a backmap.
- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
changes to RecordDecl. For some reason 'svn' marks the entire file as changed.
Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
RecordType*. This was true before because we only created one RecordDecl* for
a given RecordType*, but it is no longer true. I believe this shouldn't be too
hard to change, but the patch was big enough as it is.
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).
llvm-svn: 55839
2008-09-06 01:16:31 +08:00
|
|
|
ConstantStringDecl->defineBody(*Context, FieldDecls, 4);
|
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();
|
2007-12-04 03:17:29 +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;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
|
2007-12-04 03:17:29 +08:00
|
|
|
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
|
|
|
}
|
2007-11-15 18:28:18 +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();
|
|
|
|
|
|
|
|
// 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!");
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *SuperDecl =
|
2007-12-07 11:50:46 +08:00
|
|
|
CurMethodDecl->getClassInterface()->getSuperClass();
|
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
|
|
|
|
|
|
|
// set the receiver to self, the first argument to all methods.
|
2008-06-18 02:05:57 +08:00
|
|
|
InitExprs.push_back(new DeclRefExpr(
|
|
|
|
CurMethodDecl->getSelfDecl(),
|
|
|
|
Context->getObjCIdType(),
|
|
|
|
SourceLocation()));
|
2007-12-07 11:50:46 +08:00
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
|
|
|
ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
|
|
|
|
SuperDecl->getIdentifier()->getLength(),
|
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
|
|
|
|
&ClsExprs[0],
|
|
|
|
ClsExprs.size());
|
|
|
|
// To turn off a warning, type-cast to 'id'
|
|
|
|
InitExprs.push_back(
|
2008-08-19 07:01:59 +08:00
|
|
|
new ExplicitCastExpr(Context->getObjCIdType(),
|
2007-12-07 11:50:46 +08:00
|
|
|
Cls, SourceLocation())); // set 'super class', using objc_getClass().
|
|
|
|
// struct objc_super
|
|
|
|
QualType superType = getSuperStructType();
|
2008-03-12 02:14:26 +08:00
|
|
|
Expr *SuperRep;
|
2008-03-12 01:37:02 +08:00
|
|
|
|
2008-03-12 02:14:26 +08:00
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
SynthSuperContructorFunctionDecl();
|
|
|
|
// Simulate a contructor call...
|
|
|
|
DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl,
|
|
|
|
superType, SourceLocation());
|
|
|
|
SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(),
|
|
|
|
superType, SourceLocation());
|
|
|
|
} else {
|
|
|
|
// (struct objc_super) { <exprs from above> }
|
|
|
|
InitListExpr *ILE = new InitListExpr(SourceLocation(),
|
|
|
|
&InitExprs[0], InitExprs.size(),
|
|
|
|
SourceLocation());
|
|
|
|
SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false);
|
|
|
|
}
|
2007-12-07 11:50:46 +08:00
|
|
|
// struct objc_super *
|
|
|
|
Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
|
|
|
|
Context->getPointerType(SuperRep->getType()),
|
|
|
|
SourceLocation());
|
|
|
|
MsgExprs.push_back(Unop);
|
|
|
|
} else {
|
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
|
|
|
ClsExprs.push_back(new StringLiteral(clsName->getName(),
|
|
|
|
clsName->getLength(),
|
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
|
|
|
|
&ClsExprs[0],
|
|
|
|
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!");
|
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
|
|
|
|
2007-12-05 06:32:58 +08:00
|
|
|
InitExprs.push_back(
|
2008-08-19 07:01:59 +08:00
|
|
|
new ExplicitCastExpr(Context->getObjCIdType(),
|
2008-07-17 06:35:27 +08:00
|
|
|
new DeclRefExpr(CurMethodDecl->getSelfDecl(),
|
|
|
|
Context->getObjCIdType(),
|
|
|
|
SourceLocation()),
|
|
|
|
SourceLocation())); // set the 'receiver'.
|
2007-11-15 18:28:18 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 8> ClsExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
2007-12-07 11:50:46 +08:00
|
|
|
ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
|
|
|
|
SuperDecl->getIdentifier()->getLength(),
|
2007-11-15 18:28:18 +08:00
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
|
2007-12-05 06:32:58 +08:00
|
|
|
&ClsExprs[0],
|
|
|
|
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-08-19 07:01:59 +08:00
|
|
|
new ExplicitCastExpr(Context->getObjCIdType(),
|
2007-12-05 06:32:58 +08:00
|
|
|
Cls, SourceLocation())); // set 'super class', using objc_getClass().
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_super
|
|
|
|
QualType superType = getSuperStructType();
|
2008-03-12 01:37:02 +08:00
|
|
|
Expr *SuperRep;
|
|
|
|
|
|
|
|
if (LangOpts.Microsoft) {
|
|
|
|
SynthSuperContructorFunctionDecl();
|
|
|
|
// Simulate a contructor call...
|
|
|
|
DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl,
|
|
|
|
superType, SourceLocation());
|
|
|
|
SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(),
|
|
|
|
superType, SourceLocation());
|
|
|
|
} else {
|
|
|
|
// (struct objc_super) { <exprs from above> }
|
|
|
|
InitListExpr *ILE = new InitListExpr(SourceLocation(),
|
|
|
|
&InitExprs[0], InitExprs.size(),
|
|
|
|
SourceLocation());
|
|
|
|
SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false);
|
|
|
|
}
|
2007-11-15 18:28:18 +08:00
|
|
|
// struct objc_super *
|
|
|
|
Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
|
|
|
|
Context->getPointerType(SuperRep->getType()),
|
|
|
|
SourceLocation());
|
|
|
|
MsgExprs.push_back(Unop);
|
|
|
|
} 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-08-19 07:01:59 +08:00
|
|
|
while (ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(recExpr))
|
2007-12-08 05:21:21 +08:00
|
|
|
recExpr = CE->getSubExpr();
|
2008-08-19 07:01:59 +08:00
|
|
|
recExpr = new ExplicitCastExpr(Context->getObjCIdType(), recExpr,
|
|
|
|
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);
|
|
|
|
SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
|
|
|
|
Exp->getSelector().getName().size(),
|
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
|
|
|
|
&SelExprs[0], SelExprs.size());
|
|
|
|
MsgExprs.push_back(SelExp);
|
|
|
|
|
|
|
|
// 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-08-19 07:01:59 +08:00
|
|
|
userExpr = new ExplicitCastExpr(ICE->getType()->isObjCQualifiedIdType()
|
2008-01-08 03:49:32 +08:00
|
|
|
? Context->getObjCIdType()
|
2007-12-18 05:03:50 +08:00
|
|
|
: ICE->getType(), userExpr, SourceLocation());
|
2007-12-19 05:33:44 +08:00
|
|
|
}
|
|
|
|
// Make id<P...> cast into an 'id' cast.
|
2008-08-19 07:01:59 +08:00
|
|
|
else if (ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(userExpr)) {
|
2008-01-08 03:49:32 +08:00
|
|
|
if (CE->getType()->isObjCQualifiedIdType()) {
|
2008-08-19 07:01:59 +08:00
|
|
|
while ((CE = dyn_cast<ExplicitCastExpr>(userExpr)))
|
2007-12-19 05:33:44 +08:00
|
|
|
userExpr = CE->getSubExpr();
|
2008-08-19 07:01:59 +08:00
|
|
|
userExpr = new ExplicitCastExpr(Context->getObjCIdType(),
|
2007-12-19 05:33:44 +08:00
|
|
|
userExpr, SourceLocation());
|
|
|
|
}
|
2007-11-15 10:58:25 +08:00
|
|
|
}
|
2007-11-15 07:54:14 +08:00
|
|
|
MsgExprs.push_back(userExpr);
|
2007-10-25 06:48:43 +08:00
|
|
|
// We've transferred the ownership to MsgExprs. Null out the argument in
|
|
|
|
// the original expression, since we will delete it below.
|
|
|
|
Exp->setArg(i, 0);
|
|
|
|
}
|
2007-11-05 06:37:50 +08:00
|
|
|
// Generate the funky cast.
|
|
|
|
CastExpr *cast;
|
|
|
|
llvm::SmallVector<QualType, 8> ArgTypes;
|
|
|
|
QualType returnType;
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
|
2007-11-05 06:37:50 +08:00
|
|
|
// Push any user argument types.
|
2008-03-16 09:07:14 +08:00
|
|
|
for (unsigned i = 0; i < mDecl->getNumParams(); i++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType()
|
|
|
|
? Context->getObjCIdType()
|
2007-12-18 05:03:50 +08:00
|
|
|
: mDecl->getParamDecl(i)->getType();
|
2007-11-05 22:36:37 +08:00
|
|
|
ArgTypes.push_back(t);
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
returnType = mDecl->getResultType()->isObjCQualifiedIdType()
|
|
|
|
? Context->getObjCIdType() : mDecl->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();
|
2007-11-05 06:37:50 +08:00
|
|
|
|
|
|
|
// Create a reference to the objc_msgSend() declaration.
|
2007-12-04 03:17:29 +08:00
|
|
|
DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType,
|
|
|
|
SourceLocation());
|
2007-11-05 06:37:50 +08:00
|
|
|
|
|
|
|
// Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
|
|
|
|
// 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
|
2008-08-19 07:01:59 +08:00
|
|
|
cast = new ExplicitCastExpr(Context->getPointerType(Context->VoidTy), DRE,
|
2007-11-05 06:37:50 +08:00
|
|
|
SourceLocation());
|
2007-11-15 20:35:21 +08:00
|
|
|
|
2007-11-05 06:37:50 +08:00
|
|
|
// Now do the "normal" pointer to function cast.
|
|
|
|
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.
|
|
|
|
Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true);
|
2007-11-05 06:37:50 +08:00
|
|
|
castType = Context->getPointerType(castType);
|
2008-08-19 07:01:59 +08:00
|
|
|
cast = new ExplicitCastExpr(castType, cast, SourceLocation());
|
2007-11-05 06:37:50 +08:00
|
|
|
|
|
|
|
// Don't forget the parens to enforce the proper binding.
|
|
|
|
ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
|
|
|
|
|
|
|
|
const FunctionType *FT = msgSendType->getAsFunctionType();
|
|
|
|
CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
|
|
|
|
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.
|
|
|
|
|
|
|
|
// Create a reference to the objc_msgSend_stret() declaration.
|
|
|
|
DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType,
|
|
|
|
SourceLocation());
|
|
|
|
// Need to cast objc_msgSend_stret to "void *" (see above comment).
|
2008-08-19 07:01:59 +08:00
|
|
|
cast = new ExplicitCastExpr(Context->getPointerType(Context->VoidTy), STDRE,
|
2007-12-04 03:17:29 +08:00
|
|
|
SourceLocation());
|
|
|
|
// Now do the "normal" pointer to function cast.
|
|
|
|
castType = Context->getFunctionType(returnType,
|
2007-12-07 03:49:56 +08:00
|
|
|
&ArgTypes[0], ArgTypes.size(),
|
|
|
|
Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
|
2007-12-04 03:17:29 +08:00
|
|
|
castType = Context->getPointerType(castType);
|
2008-08-19 07:01:59 +08:00
|
|
|
cast = new ExplicitCastExpr(castType, cast, SourceLocation());
|
2007-12-04 03:17:29 +08:00
|
|
|
|
|
|
|
// Don't forget the parens to enforce the proper binding.
|
|
|
|
PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
|
|
|
|
|
|
|
|
FT = msgSendType->getAsFunctionType();
|
|
|
|
CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
|
|
|
|
FT->getResultType(), SourceLocation());
|
|
|
|
|
|
|
|
// Build sizeof(returnType)
|
|
|
|
SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true,
|
|
|
|
returnType, Context->getSizeType(),
|
|
|
|
SourceLocation(), SourceLocation());
|
|
|
|
// (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.
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned IntSize =
|
|
|
|
static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
|
2007-12-04 03:17:29 +08:00
|
|
|
IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8),
|
|
|
|
Context->IntTy,
|
|
|
|
SourceLocation());
|
|
|
|
BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit,
|
|
|
|
BinaryOperator::LE,
|
|
|
|
Context->IntTy,
|
|
|
|
SourceLocation());
|
|
|
|
// (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
|
|
|
|
ConditionalOperator *CondExpr =
|
|
|
|
new ConditionalOperator(lessThanExpr, CE, STCE, returnType);
|
2008-01-09 06:06:28 +08:00
|
|
|
ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
|
2007-12-04 03:17:29 +08:00
|
|
|
}
|
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);
|
2007-10-25 06:48:43 +08:00
|
|
|
// Now do the actual rewrite.
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, ReplacingStmt);
|
2007-10-25 06:48:43 +08:00
|
|
|
|
2007-10-25 00:57:36 +08:00
|
|
|
delete Exp;
|
2008-01-09 06:06:28 +08:00
|
|
|
return ReplacingStmt;
|
2007-10-24 07:50:29 +08:00
|
|
|
}
|
|
|
|
|
2007-12-08 02:47:10 +08:00
|
|
|
/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
|
|
|
|
/// call to objc_getProtocol("proto-name").
|
2008-04-15 06:03:09 +08:00
|
|
|
Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
|
2007-12-08 02:47:10 +08:00
|
|
|
if (!GetProtocolFunctionDecl)
|
|
|
|
SynthGetProtocolFunctionDecl();
|
|
|
|
// Create a call to objc_getProtocol("ProtocolName").
|
|
|
|
llvm::SmallVector<Expr*, 8> ProtoExprs;
|
|
|
|
QualType argType = Context->getPointerType(Context->CharTy);
|
|
|
|
ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(),
|
|
|
|
strlen(Exp->getProtocol()->getName()),
|
|
|
|
false, argType, SourceLocation(),
|
|
|
|
SourceLocation()));
|
|
|
|
CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl,
|
|
|
|
&ProtoExprs[0],
|
|
|
|
ProtoExprs.size());
|
2008-02-01 03:37:57 +08:00
|
|
|
ReplaceStmt(Exp, ProtoExp);
|
2007-12-08 02:47:10 +08:00
|
|
|
delete Exp;
|
|
|
|
return ProtoExp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:15:04 +08:00
|
|
|
bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf,
|
|
|
|
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");
|
|
|
|
assert(CDecl->getName() && "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();
|
|
|
|
|
|
|
|
const char *startBuf = SM->getCharacterData(LocStart);
|
|
|
|
const char *endBuf = SM->getCharacterData(LocEnd);
|
2008-05-31 22:15:04 +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))) {
|
2007-11-27 03:52:57 +08:00
|
|
|
endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
|
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
|
|
|
}
|
|
|
|
|
2007-11-15 06:26:25 +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 ";
|
|
|
|
Result += CDecl->getName();
|
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, '{');
|
|
|
|
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)) {
|
|
|
|
SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() :
|
|
|
|
CDecl->getClassLoc();
|
|
|
|
const char *endHeader = SM->getCharacterData(L);
|
|
|
|
endHeader += Lexer::MeasureTokenLength(L, *SM);
|
|
|
|
|
2008-07-22 02:19:38 +08:00
|
|
|
if (!CDecl->getReferencedProtocols().empty()) {
|
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 ";
|
|
|
|
Result += RCDecl->getName();
|
2008-03-13 05:09:20 +08:00
|
|
|
Result += "_IMPL ";
|
|
|
|
Result += RCDecl->getName();
|
2008-03-13 05:22:52 +08:00
|
|
|
Result += "_IVARS;\n";
|
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 '{'
|
|
|
|
|
|
|
|
// 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);
|
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.
|
|
|
|
endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
|
|
|
|
Result += " {\n struct ";
|
|
|
|
Result += RCDecl->getName();
|
2008-03-13 05:09:20 +08:00
|
|
|
Result += "_IMPL ";
|
|
|
|
Result += RCDecl->getName();
|
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.
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
|
2007-12-12 15:46:12 +08:00
|
|
|
instmeth_iterator 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;
|
|
|
|
|
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";
|
2007-10-26 04:55:25 +08:00
|
|
|
|
|
|
|
objc_impl_method = true;
|
|
|
|
}
|
2007-12-12 15:46:12 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
// Build _objc_method_list for class's methods if needed
|
2008-03-11 08:12:29 +08:00
|
|
|
|
|
|
|
/* struct {
|
|
|
|
struct _objc_method_list *next_method;
|
|
|
|
int method_count;
|
|
|
|
struct _objc_method method_list[];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tstruct _objc_method_list *next_method;\n";
|
|
|
|
Result += "\tint method_count;\n";
|
|
|
|
Result += "\tstruct _objc_method method_list[";
|
|
|
|
Result += utostr(MethodEnd-MethodBegin);
|
|
|
|
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\")))= ";
|
|
|
|
Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n";
|
|
|
|
|
|
|
|
Result += "\t,{{(SEL)\"";
|
|
|
|
Result += (*MethodBegin)->getSelector().getName().c_str();
|
|
|
|
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)\"";
|
|
|
|
Result += (*MethodBegin)->getSelector().getName().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
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data.
|
2008-07-22 05:32:27 +08:00
|
|
|
void RewriteObjC::
|
|
|
|
RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols,
|
|
|
|
const char *prefix,
|
|
|
|
const char *ClassName,
|
|
|
|
std::string &Result) {
|
2007-10-23 05:41:37 +08:00
|
|
|
static bool objc_protocol_methods = false;
|
2008-07-22 05:33:21 +08:00
|
|
|
if (Protocols.empty()) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != Protocols.size(); i++) {
|
|
|
|
ObjCProtocolDecl *PDecl = Protocols[i];
|
|
|
|
// Output struct protocol_methods holder of method selector and type.
|
|
|
|
if (!objc_protocol_methods && !PDecl->isForwardDecl()) {
|
|
|
|
/* struct protocol_methods {
|
|
|
|
SEL _cmd;
|
|
|
|
char *method_types;
|
|
|
|
}
|
2007-10-23 05:41:37 +08:00
|
|
|
*/
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "\nstruct protocol_methods {\n";
|
|
|
|
Result += "\tSEL _cmd;\n";
|
|
|
|
Result += "\tchar *method_types;\n";
|
|
|
|
Result += "};\n";
|
2007-10-23 05:41:37 +08:00
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
objc_protocol_methods = true;
|
|
|
|
}
|
|
|
|
// Do not synthesize the protocol more than once.
|
|
|
|
if (ObjCSynthesizedProtocols.count(PDecl))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
|
|
|
|
unsigned NumMethods = PDecl->getNumInstanceMethods();
|
|
|
|
/* 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 protocols[";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += PDecl->getName();
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= "
|
|
|
|
"{\n\t" + utostr(NumMethods) + "\n";
|
|
|
|
|
|
|
|
// Output instance methods declared in this protocol.
|
|
|
|
for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
|
|
|
|
E = PDecl->instmeth_end(); I != E; ++I) {
|
|
|
|
if (I == PDecl->instmeth_begin())
|
|
|
|
Result += "\t ,{{(SEL)\"";
|
|
|
|
else
|
|
|
|
Result += "\t ,{(SEL)\"";
|
|
|
|
Result += (*I)->getSelector().getName().c_str();
|
|
|
|
std::string MethodTypeString;
|
|
|
|
Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
|
|
|
Result += "\"}\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
}
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "\t }\n};\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output class methods declared in this protocol.
|
|
|
|
int NumMethods = PDecl->getNumClassMethods();
|
|
|
|
if (NumMethods > 0) {
|
|
|
|
/* 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 protocols[";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_";
|
|
|
|
Result += PDecl->getName();
|
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
|
|
|
|
"{\n\t";
|
|
|
|
Result += utostr(NumMethods);
|
|
|
|
Result += "\n";
|
|
|
|
|
|
|
|
// Output instance methods declared in this protocol.
|
|
|
|
for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
|
|
|
|
E = PDecl->classmeth_end(); I != E; ++I) {
|
|
|
|
if (I == PDecl->classmeth_begin())
|
|
|
|
Result += "\t ,{{(SEL)\"";
|
|
|
|
else
|
|
|
|
Result += "\t ,{(SEL)\"";
|
|
|
|
Result += (*I)->getSelector().getName().c_str();
|
|
|
|
std::string MethodTypeString;
|
|
|
|
Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += MethodTypeString;
|
|
|
|
Result += "\"}\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
}
|
2008-07-22 05:33:21 +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;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
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";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "};\n";
|
2008-05-07 02:26:51 +08:00
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
objc_protocol = true;
|
2007-10-23 05:41:37 +08:00
|
|
|
}
|
2007-10-26 04:55:25 +08:00
|
|
|
|
2008-07-22 05:33:21 +08:00
|
|
|
Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
|
|
|
|
Result += PDecl->getName();
|
|
|
|
Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= "
|
|
|
|
"{\n\t0, \"";
|
|
|
|
Result += PDecl->getName();
|
|
|
|
Result += "\", 0, ";
|
|
|
|
if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
|
|
|
|
Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
|
|
|
|
Result += PDecl->getName();
|
|
|
|
Result += ", ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Result += "0, ";
|
|
|
|
if (PDecl->getNumClassMethods() > 0) {
|
|
|
|
Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_";
|
|
|
|
Result += PDecl->getName();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\n";
|
2007-10-23 05:41:37 +08:00
|
|
|
}
|
2008-07-22 05:33:21 +08:00
|
|
|
else
|
|
|
|
Result += "0\n";
|
|
|
|
Result += "};\n";
|
|
|
|
|
|
|
|
// Mark this protocol as having been generated.
|
|
|
|
if (!ObjCSynthesizedProtocols.insert(PDecl))
|
|
|
|
assert(false && "protocol already synthesized");
|
|
|
|
}
|
|
|
|
// 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";
|
|
|
|
|
|
|
|
Result += "\t,{&_OBJC_PROTOCOL_";
|
|
|
|
Result += Protocols[0]->getName();
|
|
|
|
Result += " \n";
|
|
|
|
|
|
|
|
for (unsigned i = 1; i != Protocols.size(); i++) {
|
|
|
|
Result += "\t ,&_OBJC_PROTOCOL_";
|
|
|
|
Result += Protocols[i]->getName();
|
|
|
|
Result += "\n";
|
|
|
|
}
|
|
|
|
Result += "\t }\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +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;
|
2007-10-25 03:23:36 +08:00
|
|
|
for (CDecl = ClassDecl->getCategoryList(); CDecl;
|
|
|
|
CDecl = CDecl->getNextClassCategory())
|
|
|
|
if (CDecl->getIdentifier() == IDecl->getIdentifier())
|
|
|
|
break;
|
|
|
|
|
2007-12-23 09:40:15 +08:00
|
|
|
std::string FullCategoryName = ClassDecl->getName();
|
|
|
|
FullCategoryName += '_';
|
|
|
|
FullCategoryName += IDecl->getName();
|
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Build _objc_method_list for class's instance methods if needed
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
|
2007-12-23 09:40:15 +08:00
|
|
|
true, "CATEGORY_", FullCategoryName.c_str(),
|
|
|
|
Result);
|
2007-10-25 03:23:36 +08:00
|
|
|
|
|
|
|
// Build _objc_method_list for class's class methods if needed
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
|
2007-12-23 09:40:15 +08:00
|
|
|
false, "CATEGORY_", FullCategoryName.c_str(),
|
|
|
|
Result);
|
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)
|
2008-07-22 05:32:27 +08:00
|
|
|
RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY",
|
2007-12-23 09:40:15 +08:00
|
|
|
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)
|
|
|
|
struct _objc_property_list *instance_properties; // category's own
|
|
|
|
// @property decl.
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
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";
|
|
|
|
Result += "\tunsigned int size;\n";
|
|
|
|
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\"";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += IDecl->getName();
|
|
|
|
Result += "\"\n\t, \"";
|
|
|
|
Result += ClassDecl->getName();
|
|
|
|
Result += "\"\n";
|
|
|
|
|
|
|
|
if (IDecl->getNumInstanceMethods() > 0) {
|
|
|
|
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";
|
|
|
|
if (IDecl->getNumClassMethods() > 0) {
|
|
|
|
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";
|
2007-10-25 03:23:36 +08:00
|
|
|
|
2008-07-22 05:32:27 +08:00
|
|
|
if (CDecl && !CDecl->getReferencedProtocols().empty()) {
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
|
|
|
|
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.
|
2008-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
|
2008-01-08 03:49:32 +08:00
|
|
|
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 ";
|
|
|
|
Result += IDecl->getName();
|
|
|
|
if (LangOpts.Microsoft)
|
|
|
|
Result += "_IMPL";
|
|
|
|
Result += ", ";
|
|
|
|
Result += ivar->getName();
|
|
|
|
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();
|
2007-10-25 03:23:36 +08:00
|
|
|
|
2007-11-27 04:59:57 +08:00
|
|
|
// Explictly declared @interface's are already synthesized.
|
|
|
|
if (CDecl->ImplicitInterfaceDecl()) {
|
|
|
|
// FIXME: Implementation of a class with no @interface (legacy) doese not
|
|
|
|
// produce correct synthesis as yet.
|
2008-01-08 03:49:32 +08:00
|
|
|
SynthesizeObjCInternalStruct(CDecl, Result);
|
2007-11-27 04:59:57 +08:00
|
|
|
}
|
2007-10-27 07:09:28 +08:00
|
|
|
|
2007-12-12 15:56:42 +08:00
|
|
|
// Build _objc_ivar_list metadata for classes ivars if needed
|
2008-03-17 05:08:55 +08:00
|
|
|
unsigned NumIvars = !IDecl->ivar_empty()
|
|
|
|
? IDecl->ivar_size()
|
|
|
|
: (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;
|
|
|
|
};
|
|
|
|
*/
|
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";
|
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
Result += "\nstatic struct {\n";
|
|
|
|
Result += "\tint ivar_count;\n";
|
|
|
|
Result += "\tstruct _objc_ivar ivar_list[";
|
|
|
|
Result += utostr(NumIvars);
|
|
|
|
Result += "];\n} _OBJC_INSTANCE_VARIABLES_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += IDecl->getName();
|
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";
|
2007-12-12 15:56:42 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl::ivar_iterator IVI, IVE;
|
2008-03-17 05:08:55 +08:00
|
|
|
if (!IDecl->ivar_empty()) {
|
2007-12-12 15:56:42 +08:00
|
|
|
IVI = IDecl->ivar_begin();
|
|
|
|
IVE = IDecl->ivar_end();
|
|
|
|
} else {
|
|
|
|
IVI = CDecl->ivar_begin();
|
|
|
|
IVE = CDecl->ivar_end();
|
|
|
|
}
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t,{{\"";
|
2007-12-12 15:56:42 +08:00
|
|
|
Result += (*IVI)->getName();
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += "\", \"";
|
|
|
|
std::string StrEncoding;
|
2008-01-23 06:44:46 +08:00
|
|
|
Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
|
|
|
|
EncodingRecordTypes);
|
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 ,{\"";
|
2007-12-12 15:56:42 +08:00
|
|
|
Result += (*IVI)->getName();
|
2007-10-30 01:16:25 +08:00
|
|
|
Result += "\", \"";
|
|
|
|
std::string StrEncoding;
|
2008-01-23 06:44:46 +08:00
|
|
|
Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
|
|
|
|
EncodingRecordTypes);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Result += "\t }\n};\n";
|
2007-10-25 03:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build _objc_method_list for class's instance methods if needed
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
|
2007-12-12 15:46:12 +08:00
|
|
|
true, "", IDecl->getName(), Result);
|
2007-10-25 03:23:36 +08:00
|
|
|
|
|
|
|
// Build _objc_method_list for class's class methods if needed
|
2008-01-08 03:49:32 +08:00
|
|
|
RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
|
2007-12-12 15:46:12 +08:00
|
|
|
false, "", IDecl->getName(), Result);
|
2007-10-26 04:55:25 +08:00
|
|
|
|
2007-10-25 03:23:36 +08:00
|
|
|
// Protocols referenced in class declaration?
|
2008-07-22 05:32:27 +08:00
|
|
|
RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
|
2007-12-12 15:46:12 +08:00
|
|
|
"CLASS", CDecl->getName(), Result);
|
2007-10-25 03:23:36 +08:00
|
|
|
|
2007-10-23 08:02:02 +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;
|
|
|
|
};
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
|
|
|
|
Result += CDecl->getName();
|
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 *)\"";
|
|
|
|
Result += (RootClass ? RootClass->getName() : CDecl->getName());
|
|
|
|
Result += "\"";
|
|
|
|
|
|
|
|
if (SuperClass) {
|
|
|
|
Result += ", \"";
|
|
|
|
Result += SuperClass->getName();
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += CDecl->getName();
|
|
|
|
Result += "\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Result += ", 0, \"";
|
|
|
|
Result += CDecl->getName();
|
|
|
|
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";
|
2007-12-06 05:49:40 +08:00
|
|
|
if (IDecl->getNumClassMethods() > 0) {
|
2008-03-12 02:14:26 +08:00
|
|
|
Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_";
|
2007-12-06 05:49:40 +08:00
|
|
|
Result += IDecl->getName();
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-23 08:02:02 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ", 0\n";
|
2008-07-22 02:19:38 +08:00
|
|
|
if (!CDecl->getReferencedProtocols().empty()) {
|
2008-03-12 09:06:30 +08:00
|
|
|
Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += CDecl->getName();
|
|
|
|
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";
|
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_";
|
|
|
|
Result += CDecl->getName();
|
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_";
|
|
|
|
Result += CDecl->getName();
|
|
|
|
if (SuperClass) {
|
|
|
|
Result += ", \"";
|
|
|
|
Result += SuperClass->getName();
|
|
|
|
Result += "\", \"";
|
|
|
|
Result += CDecl->getName();
|
|
|
|
Result += "\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Result += ", 0, \"";
|
|
|
|
Result += CDecl->getName();
|
|
|
|
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 ";
|
2007-10-27 07:09:28 +08:00
|
|
|
Result += CDecl->getName();
|
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_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += CDecl->getName();
|
|
|
|
Result += "\n\t";
|
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0";
|
|
|
|
if (IDecl->getNumInstanceMethods() > 0) {
|
2008-03-11 08:12:29 +08:00
|
|
|
Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += CDecl->getName();
|
|
|
|
Result += ", 0\n\t";
|
|
|
|
}
|
2007-10-24 02:53:48 +08:00
|
|
|
else
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += ",0,0";
|
2008-07-22 02:19:38 +08:00
|
|
|
if (!CDecl->getReferencedProtocols().empty()) {
|
2008-03-12 09:06:30 +08:00
|
|
|
Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += CDecl->getName();
|
|
|
|
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-04-15 06:03:09 +08:00
|
|
|
void RewriteObjC::RewriteImplementations(std::string &Result) {
|
2007-10-19 03:23:00 +08:00
|
|
|
int ClsDefCount = ClassImplementation.size();
|
|
|
|
int CatDefCount = CategoryImplementation.size();
|
2007-11-14 03:21:13 +08:00
|
|
|
|
2007-10-19 03:23:00 +08:00
|
|
|
if (ClsDefCount == 0 && CatDefCount == 0)
|
|
|
|
return;
|
2007-11-14 03:21:13 +08:00
|
|
|
// Rewrite implemented methods
|
|
|
|
for (int i = 0; i < ClsDefCount; i++)
|
|
|
|
RewriteImplementationDecl(ClassImplementation[i]);
|
2007-10-19 06:09:03 +08:00
|
|
|
|
2007-11-14 04:04:28 +08:00
|
|
|
for (int i = 0; i < CatDefCount; i++)
|
|
|
|
RewriteImplementationDecl(CategoryImplementation[i]);
|
|
|
|
|
2008-05-08 05:23:49 +08:00
|
|
|
// This is needed for determining instance variable offsets.
|
2008-08-06 02:47:23 +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);
|
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);
|
2007-10-19 06:09:03 +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];
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
Result += "static struct _objc_symtab "
|
2008-03-13 01:18:30 +08:00
|
|
|
"_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n";
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "\t0, 0, " + utostr(ClsDefCount)
|
|
|
|
+ ", " + utostr(CatDefCount) + "\n";
|
|
|
|
for (int i = 0; i < ClsDefCount; i++) {
|
|
|
|
Result += "\t,&_OBJC_CLASS_";
|
|
|
|
Result += ClassImplementation[i]->getName();
|
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-19 03:23:00 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
for (int i = 0; i < CatDefCount; i++) {
|
|
|
|
Result += "\t,&_OBJC_CATEGORY_";
|
|
|
|
Result += CategoryImplementation[i]->getClassInterface()->getName();
|
|
|
|
Result += "_";
|
|
|
|
Result += CategoryImplementation[i]->getName();
|
|
|
|
Result += "\n";
|
|
|
|
}
|
2007-10-19 03:23:00 +08:00
|
|
|
|
2007-10-26 04:55:25 +08:00
|
|
|
Result += "};\n\n";
|
2007-10-19 03:23:00 +08:00
|
|
|
|
|
|
|
// Write objc_module metadata
|
|
|
|
|
|
|
|
/*
|
|
|
|
struct _objc_module {
|
|
|
|
long version;
|
|
|
|
long size;
|
|
|
|
const char *name;
|
|
|
|
struct _objc_symtab *symtab;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
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";
|
2007-10-27 03:46:17 +08:00
|
|
|
Result += "\t" + utostr(OBJC_ABI_VERSION) +
|
|
|
|
", 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) {
|
|
|
|
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-04-17 22:40:12 +08:00
|
|
|
|
2008-06-10 07:19:58 +08:00
|
|
|
|
|
|
|
|