Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 63e2dcc | 2008-05-21 20:19:16 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 28 | #include "llvm/System/Path.h" |
Steve Naroff | 0d4e963 | 2008-04-14 22:53:48 +0000 | [diff] [blame] | 29 | #include <sstream> |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 30 | #include <fstream> |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 32 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 34 | static llvm::cl::opt<bool> |
| 35 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 36 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 37 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 38 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 39 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 40 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 41 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 42 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 43 | unsigned RewriteFailedDiag; |
| 44 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 45 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 46 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 47 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 48 | unsigned MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 49 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 50 | SourceLocation LastIncLoc; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 52 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 53 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 54 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 55 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 56 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 57 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 58 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 59 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 60 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 61 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 62 | unsigned NumObjCStringLiterals; |
| 63 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 64 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 65 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 66 | FunctionDecl *MsgSendStretFunctionDecl; |
| 67 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 68 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 69 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 70 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 71 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 72 | FunctionDecl *CFStringFunctionDecl; |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 73 | FunctionDecl *GetProtocolFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 74 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 75 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 76 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 77 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 78 | RecordDecl *NSStringRecord; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 79 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 80 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 81 | int BcLabelCount; |
| 82 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 83 | // Needed for super. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 84 | ObjCMethodDecl *CurMethodDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 85 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 86 | RecordDecl *ConstantStringDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 87 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 88 | // Needed for header files being rewritten |
| 89 | bool IsHeader; |
| 90 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 91 | std::string InFileName; |
| 92 | std::string OutFileName; |
| 93 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 94 | std::string Preamble; |
| 95 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 96 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 97 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 98 | virtual void Initialize(ASTContext &context); |
| 99 | |
| 100 | virtual void InitializeTU(TranslationUnit &TU) { |
| 101 | TU.SetOwnsDecls(false); |
| 102 | Initialize(TU.getContext()); |
| 103 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 105 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 106 | // Top Level Driver code. |
| 107 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 108 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 109 | RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 110 | Diagnostic &D, const LangOptions &LOpts); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 111 | ~RewriteObjC(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 112 | |
| 113 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 114 | // If replacement succeeded or warning disabled return with no warning. |
| 115 | if (!Rewrite.ReplaceStmt(Old, New) || SilenceRewriteMacroWarning) |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 116 | return; |
| 117 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 118 | SourceRange Range = Old->getSourceRange(); |
| 119 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag, |
| 120 | 0, 0, &Range, 1); |
| 121 | } |
| 122 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 123 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 124 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 125 | // If insertion succeeded or warning disabled return with no warning. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 126 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 127 | SilenceRewriteMacroWarning) |
| 128 | return; |
| 129 | |
| 130 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 131 | } |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 132 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 133 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 134 | // If removal succeeded or warning disabled return with no warning. |
| 135 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 136 | return; |
| 137 | |
| 138 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 139 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 140 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 141 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 142 | const char *NewStr, unsigned NewLength) { |
| 143 | // If removal succeeded or warning disabled return with no warning. |
| 144 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 145 | SilenceRewriteMacroWarning) |
| 146 | return; |
| 147 | |
| 148 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 149 | } |
| 150 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 151 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 152 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 153 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 154 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 155 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
| 156 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 157 | void RewriteImplementationDecl(NamedDecl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 158 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 159 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 160 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 161 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 162 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 163 | void RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 164 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 165 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 166 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 167 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 168 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 169 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 170 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 171 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 172 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 173 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 174 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 175 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 176 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 177 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 178 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 179 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 180 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 181 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 182 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 183 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 184 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 185 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 186 | SourceLocation OrigEnd); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 187 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 188 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 189 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 190 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 191 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 192 | void SynthCountByEnumWithState(std::string &buf); |
| 193 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 194 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 195 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 196 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 197 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 198 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 199 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 200 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 201 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 202 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 203 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 204 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 205 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 206 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 207 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 209 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 210 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 212 | typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator; |
| 213 | void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 214 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 215 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 216 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 217 | const char *ClassName, |
| 218 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 220 | void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> |
| 221 | &Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 222 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 223 | const char *ClassName, |
| 224 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 225 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 226 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 227 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 228 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 229 | std::string &Result); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 230 | void RewriteImplementations(std::string &Result); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 231 | }; |
| 232 | } |
| 233 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 234 | static bool IsHeaderFile(const std::string &Filename) { |
| 235 | std::string::size_type DotPos = Filename.rfind('.'); |
| 236 | |
| 237 | if (DotPos == std::string::npos) { |
| 238 | // no file extension |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 243 | // C header: .h |
| 244 | // C++ header: .hh or .H; |
| 245 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 246 | } |
| 247 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 248 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 249 | Diagnostic &D, const LangOptions &LOpts) |
| 250 | : Diags(D), LangOpts(LOpts) { |
| 251 | IsHeader = IsHeaderFile(inFile); |
| 252 | InFileName = inFile; |
| 253 | OutFileName = outFile; |
| 254 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 255 | "rewriting sub-expression within a macro (may not be correct)"); |
| 256 | } |
| 257 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 258 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 259 | const std::string& OutFile, |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 260 | Diagnostic &Diags, |
| 261 | const LangOptions &LOpts) { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 262 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 263 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 264 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 265 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 266 | Context = &context; |
| 267 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 268 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 269 | MsgSendFunctionDecl = 0; |
| 270 | MsgSendSuperFunctionDecl = 0; |
| 271 | MsgSendStretFunctionDecl = 0; |
| 272 | MsgSendSuperStretFunctionDecl = 0; |
| 273 | MsgSendFpretFunctionDecl = 0; |
| 274 | GetClassFunctionDecl = 0; |
| 275 | GetMetaClassFunctionDecl = 0; |
| 276 | SelGetUidFunctionDecl = 0; |
| 277 | CFStringFunctionDecl = 0; |
| 278 | GetProtocolFunctionDecl = 0; |
| 279 | ConstantStringClassReference = 0; |
| 280 | NSStringRecord = 0; |
| 281 | CurMethodDecl = 0; |
| 282 | SuperStructDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 283 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 284 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 285 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 286 | NumObjCStringLiterals = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 287 | |
| 288 | // Get the ID and start/end of the main file. |
| 289 | MainFileID = SM->getMainFileID(); |
| 290 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 291 | MainFileStart = MainBuf->getBufferStart(); |
| 292 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 294 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 296 | // declaring objc_selector outside the parameter list removes a silly |
| 297 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 298 | if (IsHeader) |
| 299 | Preamble = "#pragma once\n"; |
| 300 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 301 | Preamble += "#ifndef OBJC_SUPER\n"; |
| 302 | Preamble += "struct objc_super { struct objc_object *object; "; |
| 303 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 304 | if (LangOpts.Microsoft) { |
| 305 | // Add a constructor for creating temporary objects. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 306 | Preamble += "objc_super(struct objc_object *o, struct objc_object *s) : "; |
| 307 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 308 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 309 | Preamble += "};\n"; |
| 310 | Preamble += "#define OBJC_SUPER\n"; |
| 311 | Preamble += "#endif\n"; |
| 312 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 313 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 314 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 315 | Preamble += "#endif\n"; |
Steve Naroff | 3c64d9e | 2008-03-12 13:19:12 +0000 | [diff] [blame] | 316 | if (LangOpts.Microsoft) |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 317 | Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n"; |
| 318 | else |
| 319 | Preamble += "#define __OBJC_RW_EXTERN extern\n"; |
| 320 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 321 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 322 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 323 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 324 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 325 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 326 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 327 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 328 | Preamble += "__OBJC_RW_EXTERN double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 329 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 330 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 331 | Preamble += "(const char *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 332 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 333 | Preamble += "(const char *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 334 | Preamble += "__OBJC_RW_EXTERN void objc_exception_throw(struct objc_object *);\n"; |
| 335 | Preamble += "__OBJC_RW_EXTERN void objc_exception_try_enter(void *);\n"; |
| 336 | Preamble += "__OBJC_RW_EXTERN void objc_exception_try_exit(void *);\n"; |
| 337 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_exception_extract(void *);\n"; |
| 338 | Preamble += "__OBJC_RW_EXTERN int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 339 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 340 | // @synchronized hooks. |
| 341 | Preamble += "__OBJC_RW_EXTERN void objc_sync_enter(struct objc_object *);\n"; |
| 342 | Preamble += "__OBJC_RW_EXTERN void objc_sync_exit(struct objc_object *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 343 | Preamble += "__OBJC_RW_EXTERN Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 344 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 345 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 346 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 347 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 348 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 349 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 73ebd6d | 2008-06-02 20:23:21 +0000 | [diff] [blame] | 350 | Preamble += "__OBJC_RW_EXTERN void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 351 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 352 | Preamble += "#endif\n"; |
| 353 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 354 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 355 | Preamble += " int *isa;\n"; |
| 356 | Preamble += " int flags;\n"; |
| 357 | Preamble += " char *str;\n"; |
| 358 | Preamble += " long length;\n"; |
| 359 | Preamble += "};\n"; |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 360 | Preamble += "__OBJC_RW_EXTERN int __CFConstantStringClassReference[];\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 361 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 362 | Preamble += "#endif\n"; |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 363 | if (LangOpts.Microsoft) { |
| 364 | Preamble += "#undef __OBJC_RW_EXTERN\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 365 | Preamble += "#define __attribute__(X)\n"; |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 370 | //===----------------------------------------------------------------------===// |
| 371 | // Top Level Driver Code |
| 372 | //===----------------------------------------------------------------------===// |
| 373 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 374 | void RewriteObjC::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 375 | // Two cases: either the decl could be in the main file, or it could be in a |
| 376 | // #included file. If the former, rewrite it now. If the later, check to see |
| 377 | // if we rewrote the #include/#import. |
| 378 | SourceLocation Loc = D->getLocation(); |
| 379 | Loc = SM->getLogicalLoc(Loc); |
| 380 | |
| 381 | // If this is for a builtin, ignore it. |
| 382 | if (Loc.isInvalid()) return; |
| 383 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 384 | // Look for built-in declarations that we need to refer during the rewrite. |
| 385 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 386 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 387 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 388 | // declared in <Foundation/NSString.h> |
| 389 | if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) { |
| 390 | ConstantStringClassReference = FVD; |
| 391 | return; |
| 392 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 393 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 394 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 395 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 396 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 397 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 398 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 399 | } else if (ObjCForwardProtocolDecl *FP = |
| 400 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 401 | RewriteForwardProtocolDecl(FP); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 403 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | cf7e958 | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 404 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 405 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 408 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 409 | /// main file of the input. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 410 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 411 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 412 | if (Stmt *Body = FD->getBody()) |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 413 | FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 415 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 416 | if (Stmt *Body = MD->getBody()) { |
| 417 | //Body->dump(); |
| 418 | CurMethodDecl = MD; |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 419 | MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 420 | CurMethodDecl = 0; |
| 421 | } |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 422 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 423 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 424 | ClassImplementation.push_back(CI); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 425 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 426 | CategoryImplementation.push_back(CI); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 427 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 428 | RewriteForwardClassDecl(CD); |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 429 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 430 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 431 | if (VD->getInit()) |
| 432 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 433 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 434 | // Nothing yet. |
| 435 | } |
| 436 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 437 | RewriteObjC::~RewriteObjC() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 438 | // Get the top-level buffer that this corresponds to. |
Chris Lattner | 74a0c77 | 2007-11-08 04:27:23 +0000 | [diff] [blame] | 439 | |
| 440 | // Rewrite tabs if we care. |
| 441 | //RewriteTabs(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 442 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 443 | if (Diags.hasErrorOccurred()) |
| 444 | return; |
| 445 | |
| 446 | // Create the output file. |
| 447 | |
| 448 | std::ostream *OutFile; |
| 449 | if (OutFileName == "-") { |
| 450 | OutFile = llvm::cout.stream(); |
| 451 | } else if (!OutFileName.empty()) { |
| 452 | OutFile = new std::ofstream(OutFileName.c_str(), |
| 453 | std::ios_base::binary|std::ios_base::out); |
| 454 | } else if (InFileName == "-") { |
| 455 | OutFile = llvm::cout.stream(); |
| 456 | } else { |
| 457 | llvm::sys::Path Path(InFileName); |
| 458 | Path.eraseSuffix(); |
| 459 | Path.appendSuffix("cpp"); |
| 460 | OutFile = new std::ofstream(Path.toString().c_str(), |
| 461 | std::ios_base::binary|std::ios_base::out); |
| 462 | } |
| 463 | |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 464 | RewriteInclude(); |
| 465 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 466 | InsertText(SourceLocation::getFileLoc(MainFileID, 0), |
| 467 | Preamble.c_str(), Preamble.size(), false); |
| 468 | |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 469 | // Rewrite Objective-c meta data* |
| 470 | std::string ResultStr; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 471 | RewriteImplementations(ResultStr); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 472 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 473 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 474 | // we are done. |
| 475 | if (const RewriteBuffer *RewriteBuf = |
| 476 | Rewrite.getRewriteBufferFor(MainFileID)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 477 | //printf("Changed:\n"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 478 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 479 | } else { |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 480 | fprintf(stderr, "No changes\n"); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 481 | } |
Fariborz Jahanian | 4402d81 | 2007-11-07 18:40:28 +0000 | [diff] [blame] | 482 | // Emit metadata. |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 483 | *OutFile << ResultStr; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 486 | //===----------------------------------------------------------------------===// |
| 487 | // Syntactic (non-AST) Rewriting Code |
| 488 | //===----------------------------------------------------------------------===// |
| 489 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 490 | void RewriteObjC::RewriteInclude() { |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 491 | SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0); |
| 492 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 493 | const char *MainBufStart = MainBuf.first; |
| 494 | const char *MainBufEnd = MainBuf.second; |
| 495 | size_t ImportLen = strlen("import"); |
| 496 | size_t IncludeLen = strlen("include"); |
| 497 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 498 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 499 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 500 | if (*BufPtr == '#') { |
| 501 | if (++BufPtr == MainBufEnd) |
| 502 | return; |
| 503 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 504 | if (++BufPtr == MainBufEnd) |
| 505 | return; |
| 506 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 507 | // replace import with include |
| 508 | SourceLocation ImportLoc = |
| 509 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 510 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 511 | BufPtr += ImportLen; |
| 512 | } |
| 513 | } |
| 514 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 517 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 518 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 519 | const char *MainBufStart = MainBuf.first; |
| 520 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 521 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 522 | // Loop over the whole file, looking for tabs. |
| 523 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 524 | if (*BufPtr != '\t') |
| 525 | continue; |
| 526 | |
| 527 | // Okay, we found a tab. This tab will turn into at least one character, |
| 528 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 529 | unsigned VCol = 0; |
| 530 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 531 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 532 | ++VCol; |
| 533 | |
| 534 | // Okay, now that we know the virtual column, we know how many spaces to |
| 535 | // insert. We assume 8-character tab-stops. |
| 536 | unsigned Spaces = 8-(VCol & 7); |
| 537 | |
| 538 | // Get the location of the tab. |
| 539 | SourceLocation TabLoc = |
| 540 | SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart); |
| 541 | |
| 542 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 543 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 548 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 549 | int numDecls = ClassDecl->getNumForwardDecls(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 550 | ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 551 | |
| 552 | // Get the start location and compute the semi location. |
| 553 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 554 | const char *startBuf = SM->getCharacterData(startLoc); |
| 555 | const char *semiPtr = strchr(startBuf, ';'); |
| 556 | |
| 557 | // Translate to typedef's that forward reference structs with the same name |
| 558 | // as the class. As a convenience, we include the original declaration |
| 559 | // as a comment. |
| 560 | std::string typedefString; |
| 561 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 562 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 563 | typedefString += "\n"; |
| 564 | for (int i = 0; i < numDecls; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 565 | ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i]; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 566 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 567 | typedefString += ForwardDecl->getName(); |
| 568 | typedefString += "\n"; |
| 569 | typedefString += "#define _REWRITER_typedef_"; |
| 570 | typedefString += ForwardDecl->getName(); |
| 571 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 572 | typedefString += "typedef struct objc_object "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 573 | typedefString += ForwardDecl->getName(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 574 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 578 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 579 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 582 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 583 | SourceLocation LocStart = Method->getLocStart(); |
| 584 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 585 | |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 586 | if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 587 | InsertText(LocStart, "/* ", 3); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 588 | ReplaceText(LocEnd, 1, ";*/ ", 4); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 589 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 590 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 594 | void RewriteObjC::RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 595 | { |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 596 | for (unsigned i = 0; i < nProperties; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 597 | ObjCPropertyDecl *Property = Properties[i]; |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 598 | SourceLocation Loc = Property->getLocation(); |
| 599 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 600 | ReplaceText(Loc, 0, "// ", 3); |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 601 | |
| 602 | // FIXME: handle properties that are declared across multiple lines. |
| 603 | } |
| 604 | } |
| 605 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 606 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 607 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 608 | |
| 609 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 610 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 611 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 612 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 613 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 614 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 615 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 616 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 617 | RewriteMethodDeclaration(*I); |
| 618 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 619 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 620 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 623 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 624 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 625 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 626 | SourceLocation LocStart = PDecl->getLocStart(); |
| 627 | |
| 628 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 629 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 630 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 631 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 632 | E = PDecl->instmeth_end(); I != E; ++I) |
| 633 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 634 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 635 | E = PDecl->classmeth_end(); I != E; ++I) |
| 636 | RewriteMethodDeclaration(*I); |
| 637 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 638 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 639 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 640 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 641 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 642 | // Must comment out @optional/@required |
| 643 | const char *startBuf = SM->getCharacterData(LocStart); |
| 644 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 645 | for (const char *p = startBuf; p < endBuf; p++) { |
| 646 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 647 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 648 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 649 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 650 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 651 | |
| 652 | } |
| 653 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 654 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 655 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 656 | ReplaceText(OptionalLoc, strlen("@required"), |
| 657 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 658 | |
| 659 | } |
| 660 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 663 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 664 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 665 | if (LocStart.isInvalid()) |
| 666 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 667 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 668 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 671 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 672 | std::string &ResultStr) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 673 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 674 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 675 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 676 | ResultStr += "id"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 677 | else if (OMD->getResultType()->isFunctionPointerType()) { |
| 678 | // needs special handling, since pointer-to-functions have special |
| 679 | // syntax (where a decaration models use). |
| 680 | QualType retType = OMD->getResultType(); |
| 681 | if (const PointerType* PT = retType->getAsPointerType()) { |
| 682 | QualType PointeeTy = PT->getPointeeType(); |
| 683 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 684 | ResultStr += FPRetType->getResultType().getAsString(); |
| 685 | ResultStr += "(*"; |
| 686 | } |
| 687 | } |
| 688 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 689 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 690 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 691 | |
| 692 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 693 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 694 | |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 695 | if (OMD->isInstance()) |
| 696 | NameStr += "_I_"; |
| 697 | else |
| 698 | NameStr += "_C_"; |
| 699 | |
| 700 | NameStr += OMD->getClassInterface()->getName(); |
| 701 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 702 | |
| 703 | NamedDecl *MethodContext = OMD->getMethodContext(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 704 | if (ObjCCategoryImplDecl *CID = |
| 705 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) { |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 706 | NameStr += CID->getName(); |
| 707 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 708 | } |
Steve Naroff | 563b828 | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 709 | // Append selector names, replacing ':' with '_' |
| 710 | if (OMD->getSelector().getName().find(':') == std::string::npos) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 711 | NameStr += OMD->getSelector().getName(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 712 | else { |
| 713 | std::string selString = OMD->getSelector().getName(); |
| 714 | int len = selString.size(); |
| 715 | for (int i = 0; i < len; i++) |
| 716 | if (selString[i] == ':') |
| 717 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 718 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 719 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 720 | // Remember this name for metadata emission |
| 721 | MethodInternalNames[OMD] = NameStr; |
| 722 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 723 | |
| 724 | // Rewrite arguments |
| 725 | ResultStr += "("; |
| 726 | |
| 727 | // invisible arguments |
| 728 | if (OMD->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 729 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 730 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 731 | if (!LangOpts.Microsoft) { |
| 732 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 733 | ResultStr += "struct "; |
| 734 | } |
| 735 | // When rewriting for Microsoft, explicitly omit the structure name. |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 736 | ResultStr += OMD->getClassInterface()->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 737 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 738 | } |
| 739 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 740 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 741 | |
| 742 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 743 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 744 | ResultStr += " _cmd"; |
| 745 | |
| 746 | // Method arguments. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 747 | for (unsigned i = 0; i < OMD->getNumParams(); i++) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 748 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 749 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 750 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 751 | ResultStr += "id "; |
| 752 | ResultStr += PDecl->getName(); |
| 753 | } else { |
| 754 | std::string Name = PDecl->getName(); |
| 755 | PDecl->getType().getAsStringInternal(Name); |
| 756 | ResultStr += Name; |
| 757 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 758 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 759 | if (OMD->isVariadic()) |
| 760 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 761 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 762 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 763 | if (FPRetType) { |
| 764 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 765 | |
| 766 | // Now, emit the argument types (if any). |
| 767 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(FPRetType)) { |
| 768 | ResultStr += "("; |
| 769 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 770 | if (i) ResultStr += ", "; |
| 771 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 772 | ResultStr += ParamStr; |
| 773 | } |
| 774 | if (FT->isVariadic()) { |
| 775 | if (FT->getNumArgs()) ResultStr += ", "; |
| 776 | ResultStr += "..."; |
| 777 | } |
| 778 | ResultStr += ")"; |
| 779 | } else { |
| 780 | ResultStr += "()"; |
| 781 | } |
| 782 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 783 | } |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 784 | void RewriteObjC::RewriteImplementationDecl(NamedDecl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 785 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 786 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 787 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 788 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 789 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 790 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 791 | InsertText(CID->getLocStart(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 792 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 793 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 794 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 795 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 796 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 797 | ObjCMethodDecl *OMD = *I; |
| 798 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 799 | SourceLocation LocStart = OMD->getLocStart(); |
| 800 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 801 | |
| 802 | const char *startBuf = SM->getCharacterData(LocStart); |
| 803 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 804 | ReplaceText(LocStart, endBuf-startBuf, |
| 805 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 808 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 809 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 810 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 811 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 812 | ObjCMethodDecl *OMD = *I; |
| 813 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 814 | SourceLocation LocStart = OMD->getLocStart(); |
| 815 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 816 | |
| 817 | const char *startBuf = SM->getCharacterData(LocStart); |
| 818 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 819 | ReplaceText(LocStart, endBuf-startBuf, |
| 820 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 821 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 822 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 823 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 824 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 825 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 828 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 829 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 830 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 831 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 832 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 833 | ResultStr += ClassDecl->getName(); |
| 834 | ResultStr += "\n"; |
| 835 | ResultStr += "#define _REWRITER_typedef_"; |
| 836 | ResultStr += ClassDecl->getName(); |
| 837 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 838 | ResultStr += "typedef struct objc_object "; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 839 | ResultStr += ClassDecl->getName(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 840 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 841 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 842 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 843 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 844 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 845 | |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 846 | RewriteProperties(ClassDecl->getNumPropertyDecl(), |
| 847 | ClassDecl->getPropertyDecl()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 848 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 849 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 850 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 851 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 852 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 853 | RewriteMethodDeclaration(*I); |
| 854 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 855 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 856 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 859 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 860 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 861 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 862 | if (CurMethodDecl) { |
| 863 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 864 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
| 865 | // lookup which class implements the instance variable. |
| 866 | ObjCInterfaceDecl *clsDeclared = 0; |
| 867 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
| 868 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 869 | |
| 870 | // Synthesize an explicit cast to gain access to the ivar. |
| 871 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 872 | RecName += "_IMPL"; |
| 873 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 874 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 875 | SourceLocation(), II, 0); |
| 876 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 877 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 878 | CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); |
| 879 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 880 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 881 | IV->getBase()->getLocEnd(), |
| 882 | castExpr); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 883 | if (IV->isFreeIvar() && |
| 884 | CurMethodDecl->getClassInterface() == iFaceDecl->getDecl()) { |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 885 | MemberExpr *ME = new MemberExpr(PE, true, D, IV->getLocation(), |
| 886 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 887 | ReplaceStmt(IV, ME); |
| 888 | delete IV; |
| 889 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 890 | } |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 891 | |
| 892 | ReplaceStmt(IV->getBase(), PE); |
| 893 | // Cannot delete IV->getBase(), since PE points to it. |
| 894 | // Replace the old base with the cast. This is important when doing |
| 895 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 896 | IV->setBase(PE); |
| 897 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 898 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 899 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 900 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 901 | |
| 902 | // Explicit ivar refs need to have a cast inserted. |
| 903 | // FIXME: consider sharing some of this code with the code above. |
| 904 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 905 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 906 | // lookup which class implements the instance variable. |
| 907 | ObjCInterfaceDecl *clsDeclared = 0; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 908 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 909 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 910 | |
| 911 | // Synthesize an explicit cast to gain access to the ivar. |
| 912 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 913 | RecName += "_IMPL"; |
| 914 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 915 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 916 | SourceLocation(), II, 0); |
| 917 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 918 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 919 | CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); |
| 920 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 921 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 922 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 923 | ReplaceStmt(IV->getBase(), PE); |
| 924 | // Cannot delete IV->getBase(), since PE points to it. |
| 925 | // Replace the old base with the cast. This is important when doing |
| 926 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 927 | IV->setBase(PE); |
| 928 | return IV; |
| 929 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 930 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 931 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 934 | //===----------------------------------------------------------------------===// |
| 935 | // Function Body / Expression rewriting |
| 936 | //===----------------------------------------------------------------------===// |
| 937 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 938 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 939 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 940 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 941 | Stmts.push_back(S); |
| 942 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 943 | Stmts.push_back(S); |
| 944 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 945 | } |
| 946 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 947 | SourceRange OrigStmtRange = S->getSourceRange(); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 948 | |
| 949 | // Start by rewriting all children. |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 950 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 951 | CI != E; ++CI) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 952 | if (*CI) { |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 953 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 954 | if (newStmt) |
| 955 | *CI = newStmt; |
| 956 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 957 | |
| 958 | // Handle specific things. |
| 959 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 960 | return RewriteAtEncode(AtEncode); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 961 | |
| 962 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 963 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 964 | |
| 965 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 966 | return RewriteAtSelector(AtSelector); |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 967 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 968 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 969 | return RewriteObjCStringLiteral(AtString); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 970 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 971 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 972 | // Before we rewrite it, put the original message expression in a comment. |
| 973 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 974 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 975 | |
| 976 | const char *startBuf = SM->getCharacterData(startLoc); |
| 977 | const char *endBuf = SM->getCharacterData(endLoc); |
| 978 | |
| 979 | std::string messString; |
| 980 | messString += "// "; |
| 981 | messString.append(startBuf, endBuf-startBuf+1); |
| 982 | messString += "\n"; |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 983 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 984 | // FIXME: Missing definition of |
| 985 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 986 | // InsertText(startLoc, messString.c_str(), messString.size()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 987 | // Tried this, but it didn't work either... |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 988 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 989 | return RewriteMessageExpr(MessExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 990 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 991 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 992 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 993 | return RewriteObjCTryStmt(StmtTry); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 994 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 995 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 996 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 997 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 999 | return RewriteObjCThrowStmt(StmtThrow); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1000 | |
| 1001 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 1002 | return RewriteObjCProtocolExpr(ProtocolExp); |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (ObjCForCollectionStmt *StmtForCollection = |
| 1005 | dyn_cast<ObjCForCollectionStmt>(S)) |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1006 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 1007 | OrigStmtRange.getEnd()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1008 | if (BreakStmt *StmtBreakStmt = |
| 1009 | dyn_cast<BreakStmt>(S)) |
| 1010 | return RewriteBreakStmt(StmtBreakStmt); |
| 1011 | if (ContinueStmt *StmtContinueStmt = |
| 1012 | dyn_cast<ContinueStmt>(S)) |
| 1013 | return RewriteContinueStmt(StmtContinueStmt); |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1014 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1015 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 1016 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 1017 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 1018 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 1019 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 1020 | && "Statement stack mismatch"); |
| 1021 | Stmts.pop_back(); |
| 1022 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1023 | #if 0 |
| 1024 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 1025 | CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
| 1026 | // Get the new text. |
| 1027 | std::ostringstream Buf; |
| 1028 | Replacement->printPretty(Buf); |
| 1029 | const std::string &Str = Buf.str(); |
| 1030 | |
| 1031 | printf("CAST = %s\n", &Str[0]); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1032 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1033 | delete S; |
| 1034 | return Replacement; |
| 1035 | } |
| 1036 | #endif |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1037 | // Return this stmt unmodified. |
| 1038 | return S; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1039 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1041 | /// SynthCountByEnumWithState - To print: |
| 1042 | /// ((unsigned int (*) |
| 1043 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1044 | /// (void *)objc_msgSend)((id)l_collection, |
| 1045 | /// sel_registerName( |
| 1046 | /// "countByEnumeratingWithState:objects:count:"), |
| 1047 | /// &enumState, |
| 1048 | /// (id *)items, (unsigned int)16) |
| 1049 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1050 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1051 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1052 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1053 | buf += "\n\t\t"; |
| 1054 | buf += "((id)l_collection,\n\t\t"; |
| 1055 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1056 | buf += "\n\t\t"; |
| 1057 | buf += "&enumState, " |
| 1058 | "(id *)items, (unsigned int)16)"; |
| 1059 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1060 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1061 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1062 | /// statement to exit to its outer synthesized loop. |
| 1063 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1064 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1065 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1066 | return S; |
| 1067 | // replace break with goto __break_label |
| 1068 | std::string buf; |
| 1069 | |
| 1070 | SourceLocation startLoc = S->getLocStart(); |
| 1071 | buf = "goto __break_label_"; |
| 1072 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1073 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1079 | /// statement to continue with its inner synthesized loop. |
| 1080 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1081 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1082 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1083 | return S; |
| 1084 | // replace continue with goto __continue_label |
| 1085 | std::string buf; |
| 1086 | |
| 1087 | SourceLocation startLoc = S->getLocStart(); |
| 1088 | buf = "goto __continue_label_"; |
| 1089 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1090 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1095 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1096 | /// It rewrites: |
| 1097 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1098 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1099 | /// Into: |
| 1100 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1101 | /// type elem; |
| 1102 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1103 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1104 | /// id l_collection = (id)collection; |
| 1105 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1106 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1107 | /// if (limit) { |
| 1108 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1109 | /// do { |
| 1110 | /// unsigned long counter = 0; |
| 1111 | /// do { |
| 1112 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1113 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1114 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1115 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1116 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1117 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1118 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1119 | /// objects:items count:16]); |
| 1120 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1121 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1122 | /// } |
| 1123 | /// else |
| 1124 | /// elem = nil; |
| 1125 | /// } |
| 1126 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1127 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1128 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1129 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1130 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1131 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1132 | assert(!ObjCBcLabelNo.empty() && |
| 1133 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1134 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1135 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1136 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1137 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1138 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1139 | std::string buf; |
| 1140 | buf = "\n{\n\t"; |
| 1141 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1142 | // type elem; |
| 1143 | QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1144 | elementTypeAsString = ElementType.getAsString(); |
| 1145 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1146 | buf += " "; |
| 1147 | elementName = DS->getDecl()->getName(); |
| 1148 | buf += elementName; |
| 1149 | buf += ";\n\t"; |
| 1150 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1151 | else { |
| 1152 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1153 | elementName = DR->getDecl()->getName(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1154 | elementTypeAsString = DR->getDecl()->getType().getAsString(); |
| 1155 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1156 | |
| 1157 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1158 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1159 | // id items[16]; |
| 1160 | buf += "id items[16];\n\t"; |
| 1161 | // id l_collection = (id) |
| 1162 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1163 | // Find start location of 'collection' the hard way! |
| 1164 | const char *startCollectionBuf = startBuf; |
| 1165 | startCollectionBuf += 3; // skip 'for' |
| 1166 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1167 | startCollectionBuf++; // skip '(' |
| 1168 | // find 'in' and skip it. |
| 1169 | while (*startCollectionBuf != ' ' || |
| 1170 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1171 | (*(startCollectionBuf+3) != ' ' && |
| 1172 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1173 | startCollectionBuf++; |
| 1174 | startCollectionBuf += 3; |
| 1175 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1176 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1177 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1178 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1179 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1180 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1181 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1182 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1183 | buf = ";\n\t"; |
| 1184 | |
| 1185 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1186 | // objects:items count:16]; |
| 1187 | // which is synthesized into: |
| 1188 | // unsigned int limit = |
| 1189 | // ((unsigned int (*) |
| 1190 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1191 | // (void *)objc_msgSend)((id)l_collection, |
| 1192 | // sel_registerName( |
| 1193 | // "countByEnumeratingWithState:objects:count:"), |
| 1194 | // (struct __objcFastEnumerationState *)&state, |
| 1195 | // (id *)items, (unsigned int)16); |
| 1196 | buf += "unsigned long limit =\n\t\t"; |
| 1197 | SynthCountByEnumWithState(buf); |
| 1198 | buf += ";\n\t"; |
| 1199 | /// if (limit) { |
| 1200 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1201 | /// do { |
| 1202 | /// unsigned long counter = 0; |
| 1203 | /// do { |
| 1204 | /// if (startMutations != *enumState.mutationsPtr) |
| 1205 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1206 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1207 | buf += "if (limit) {\n\t"; |
| 1208 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1209 | buf += "do {\n\t\t"; |
| 1210 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1211 | buf += "do {\n\t\t\t"; |
| 1212 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1213 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1214 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1215 | buf += " = ("; |
| 1216 | buf += elementTypeAsString; |
| 1217 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1218 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1219 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1220 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1221 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1222 | /// } while (counter < limit); |
| 1223 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1224 | /// objects:items count:16]); |
| 1225 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1226 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1227 | /// } |
| 1228 | /// else |
| 1229 | /// elem = nil; |
| 1230 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1231 | /// |
| 1232 | buf = ";\n\t"; |
| 1233 | buf += "__continue_label_"; |
| 1234 | buf += utostr(ObjCBcLabelNo.back()); |
| 1235 | buf += ": ;"; |
| 1236 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1237 | buf += "} while (counter < limit);\n\t"; |
| 1238 | buf += "} while (limit = "; |
| 1239 | SynthCountByEnumWithState(buf); |
| 1240 | buf += ");\n\t"; |
| 1241 | buf += elementName; |
| 1242 | buf += " = nil;\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1243 | buf += "__break_label_"; |
| 1244 | buf += utostr(ObjCBcLabelNo.back()); |
| 1245 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1246 | buf += "}\n\t"; |
| 1247 | buf += "else\n\t\t"; |
| 1248 | buf += elementName; |
| 1249 | buf += " = nil;\n"; |
| 1250 | buf += "}\n"; |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1251 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1252 | // Insert all these *after* the statement body. |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1253 | if (isa<CompoundStmt>(S->getBody())) { |
| 1254 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1255 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1256 | } else { |
| 1257 | /* Need to treat single statements specially. For example: |
| 1258 | * |
| 1259 | * for (A *a in b) if (stuff()) break; |
| 1260 | * for (A *a in b) xxxyy; |
| 1261 | * |
| 1262 | * The following code simply scans ahead to the semi to find the actual end. |
| 1263 | */ |
| 1264 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1265 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1266 | assert(semiBuf && "Can't find ';'"); |
| 1267 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1268 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1269 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1270 | Stmts.pop_back(); |
| 1271 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1272 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1275 | /// RewriteObjCSynchronizedStmt - |
| 1276 | /// This routine rewrites @synchronized(expr) stmt; |
| 1277 | /// into: |
| 1278 | /// objc_sync_enter(expr); |
| 1279 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1280 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1281 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1282 | // Get the start location and compute the semi location. |
| 1283 | SourceLocation startLoc = S->getLocStart(); |
| 1284 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1285 | |
| 1286 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1287 | |
| 1288 | std::string buf; |
| 1289 | buf = "objc_sync_enter"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1290 | ReplaceText(startLoc, 13, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1291 | SourceLocation endLoc = S->getSynchExpr()->getLocEnd(); |
| 1292 | const char *endBuf = SM->getCharacterData(endLoc); |
| 1293 | endBuf++; |
| 1294 | const char *rparenBuf = strchr(endBuf, ')'); |
| 1295 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
| 1296 | buf = ");\n"; |
| 1297 | // declare a new scope with two variables, _stack and _rethrow. |
| 1298 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1299 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1300 | buf += "char *pointers[4];} _stack;\n"; |
| 1301 | buf += "id volatile _rethrow = 0;\n"; |
| 1302 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1303 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1304 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1305 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1306 | startBuf = SM->getCharacterData(startLoc); |
| 1307 | |
| 1308 | assert((*startBuf == '}') && "bogus @try block"); |
| 1309 | SourceLocation lastCurlyLoc = startLoc; |
| 1310 | buf = "}\nelse {\n"; |
| 1311 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1312 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1313 | buf += " objc_sync_exit("; |
| 1314 | std::ostringstream syncExprBuf; |
| 1315 | S->getSynchExpr()->printPretty(syncExprBuf); |
| 1316 | buf += syncExprBuf.str(); |
| 1317 | buf += ");\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1318 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1319 | buf += "}\n"; |
| 1320 | buf += "}"; |
| 1321 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1322 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1326 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1327 | // Get the start location and compute the semi location. |
| 1328 | SourceLocation startLoc = S->getLocStart(); |
| 1329 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1330 | |
| 1331 | assert((*startBuf == '@') && "bogus @try location"); |
| 1332 | |
| 1333 | std::string buf; |
| 1334 | // declare a new scope with two variables, _stack and _rethrow. |
| 1335 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1336 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1337 | buf += "char *pointers[4];} _stack;\n"; |
| 1338 | buf += "id volatile _rethrow = 0;\n"; |
| 1339 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1340 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1342 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1343 | |
| 1344 | startLoc = S->getTryBody()->getLocEnd(); |
| 1345 | startBuf = SM->getCharacterData(startLoc); |
| 1346 | |
| 1347 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1348 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1349 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1350 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1351 | if (catchList) { |
| 1352 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1353 | buf = " /* @catch begin */ else {\n"; |
| 1354 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1355 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1356 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1357 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1358 | buf += " else { /* @catch continue */"; |
| 1359 | |
| 1360 | InsertText(startLoc, buf.c_str(), buf.size()); |
| 1361 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1362 | bool sawIdTypedCatch = false; |
| 1363 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1364 | while (catchList) { |
| 1365 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1366 | |
| 1367 | if (catchList == S->getCatchStmts()) |
| 1368 | buf = "if ("; // we are generating code for the first catch clause |
| 1369 | else |
| 1370 | buf = "else if ("; |
| 1371 | startLoc = catchList->getLocStart(); |
| 1372 | startBuf = SM->getCharacterData(startLoc); |
| 1373 | |
| 1374 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1375 | |
| 1376 | const char *lParenLoc = strchr(startBuf, '('); |
| 1377 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1378 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1379 | // Now rewrite the body... |
| 1380 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1381 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1382 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1383 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1384 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1385 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1386 | |
| 1387 | buf += "1) { id _tmp = _caught;"; |
| 1388 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1389 | buf.c_str(), buf.size()); |
| 1390 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1391 | QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1392 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1393 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1394 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1395 | sawIdTypedCatch = true; |
| 1396 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1397 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1398 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1399 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1400 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1401 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1402 | buf += cls->getDecl()->getName(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1403 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1404 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | // Now rewrite the body... |
| 1408 | lastCatchBody = catchList->getCatchBody(); |
| 1409 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1410 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1411 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1412 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1413 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1414 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1415 | |
| 1416 | buf = " = _caught;"; |
| 1417 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1418 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1419 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1420 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1421 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1422 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1423 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1424 | catchList = catchList->getNextCatchStmt(); |
| 1425 | } |
| 1426 | // Complete the catch list... |
| 1427 | if (lastCatchBody) { |
| 1428 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1429 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1430 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1431 | bodyLoc = bodyLoc.getFileLocWithOffset(1); |
| 1432 | buf = " } } /* @catch end */\n"; |
| 1433 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1434 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Set lastCurlyLoc |
| 1437 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1438 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1439 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1440 | startLoc = finalStmt->getLocStart(); |
| 1441 | startBuf = SM->getCharacterData(startLoc); |
| 1442 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1443 | |
| 1444 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1445 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1446 | |
| 1447 | Stmt *body = finalStmt->getFinallyBody(); |
| 1448 | SourceLocation startLoc = body->getLocStart(); |
| 1449 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1450 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1451 | "bogus @finally body location"); |
| 1452 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1453 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1454 | |
| 1455 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1456 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1457 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1458 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1459 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1460 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Set lastCurlyLoc |
| 1463 | lastCurlyLoc = body->getLocEnd(); |
| 1464 | } |
| 1465 | // Now emit the final closing curly brace... |
| 1466 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1467 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1468 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1469 | return 0; |
| 1470 | } |
| 1471 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1472 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1476 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1480 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1481 | // the throw expression is typically a message expression that's already |
| 1482 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1483 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1484 | // Get the start location and compute the semi location. |
| 1485 | SourceLocation startLoc = S->getLocStart(); |
| 1486 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1487 | |
| 1488 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1489 | |
| 1490 | std::string buf; |
| 1491 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1492 | if (S->getThrowExpr()) |
| 1493 | buf = "objc_exception_throw("; |
| 1494 | else // add an implicit argument |
| 1495 | buf = "objc_exception_throw(_caught"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1496 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1497 | const char *semiBuf = strchr(startBuf, ';'); |
| 1498 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1499 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1500 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1501 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1502 | return 0; |
| 1503 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1504 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1505 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1506 | // Create a new string expression. |
| 1507 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1508 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1509 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding, |
| 1510 | EncodingRecordTypes); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1511 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 1512 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1513 | SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1514 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1516 | // Replace this subexpr in the parent. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1517 | delete Exp; |
| 1518 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1521 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1522 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1523 | // Create a call to sel_registerName("selName"). |
| 1524 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1525 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1526 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 1527 | Exp->getSelector().getName().size(), |
| 1528 | false, argType, SourceLocation(), |
| 1529 | SourceLocation())); |
| 1530 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1531 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1532 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1533 | delete Exp; |
| 1534 | return SelExp; |
| 1535 | } |
| 1536 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1537 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1538 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1539 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1540 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1543 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 1546 | QualType pToFunc = Context->getPointerType(msgSendType); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1547 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE); |
| 1548 | |
| 1549 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1550 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1551 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 1552 | } |
| 1553 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1554 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1555 | const char *&startRef, const char *&endRef) { |
| 1556 | while (startBuf < endBuf) { |
| 1557 | if (*startBuf == '<') |
| 1558 | startRef = startBuf; // mark the start. |
| 1559 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1560 | if (startRef && *startRef == '<') { |
| 1561 | endRef = startBuf; // mark the end. |
| 1562 | return true; |
| 1563 | } |
| 1564 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1565 | } |
| 1566 | startBuf++; |
| 1567 | } |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1571 | static void scanToNextArgument(const char *&argRef) { |
| 1572 | int angle = 0; |
| 1573 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1574 | if (*argRef == '<') |
| 1575 | angle++; |
| 1576 | else if (*argRef == '>') |
| 1577 | angle--; |
| 1578 | argRef++; |
| 1579 | } |
| 1580 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1581 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1582 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1583 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1584 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1585 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1586 | return true; |
| 1587 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1588 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1589 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1590 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1591 | return true; // we have "Class <Protocol> *". |
| 1592 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1593 | return false; |
| 1594 | } |
| 1595 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1596 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1597 | SourceLocation Loc; |
| 1598 | QualType Type; |
| 1599 | const FunctionTypeProto *proto = 0; |
| 1600 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1601 | Loc = VD->getLocation(); |
| 1602 | Type = VD->getType(); |
| 1603 | } |
| 1604 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1605 | Loc = FD->getLocation(); |
| 1606 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1607 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1608 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1609 | assert(funcType && "missing function type"); |
| 1610 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1611 | if (!proto) |
| 1612 | return; |
| 1613 | Type = proto->getResultType(); |
| 1614 | } |
| 1615 | else |
| 1616 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1617 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1618 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1619 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1620 | |
| 1621 | const char *endBuf = SM->getCharacterData(Loc); |
| 1622 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1623 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1624 | startBuf--; // scan backward (from the decl location) for return type. |
| 1625 | const char *startRef = 0, *endRef = 0; |
| 1626 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1627 | // Get the locations of the startRef, endRef. |
| 1628 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1629 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1630 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1631 | InsertText(LessLoc, "/*", 2); |
| 1632 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1635 | if (!proto) |
| 1636 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1637 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1638 | const char *startBuf = SM->getCharacterData(Loc); |
| 1639 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1640 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1641 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1642 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1643 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1644 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1645 | // scan forward (from the decl location) for argument types. |
| 1646 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1647 | const char *startRef = 0, *endRef = 0; |
| 1648 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1649 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1650 | SourceLocation LessLoc = |
| 1651 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1652 | SourceLocation GreaterLoc = |
| 1653 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1654 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1655 | InsertText(LessLoc, "/*", 2); |
| 1656 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1657 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1658 | startBuf = ++endBuf; |
| 1659 | } |
| 1660 | else { |
| 1661 | while (*startBuf != ')' && *startBuf != ',') |
| 1662 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1663 | startBuf++; |
| 1664 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1665 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1668 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1669 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1670 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1671 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1672 | ArgTys.push_back(Context->getPointerType( |
| 1673 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1674 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1675 | &ArgTys[0], ArgTys.size(), |
| 1676 | false /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1677 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1678 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1679 | SelGetUidIdent, getFuncType, |
| 1680 | FunctionDecl::Extern, false, 0); |
| 1681 | } |
| 1682 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1683 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1684 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1685 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1686 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1687 | ArgTys.push_back(Context->getPointerType( |
| 1688 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1689 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1690 | &ArgTys[0], ArgTys.size(), |
| 1691 | false /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1692 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1693 | SourceLocation(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1694 | SelGetProtoIdent, getFuncType, |
| 1695 | FunctionDecl::Extern, false, 0); |
| 1696 | } |
| 1697 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1698 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1699 | // declared in <objc/objc.h> |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1700 | if (strcmp(FD->getName(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1701 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1702 | return; |
| 1703 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1704 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1707 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1708 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1709 | if (SuperContructorFunctionDecl) |
| 1710 | return; |
| 1711 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super"); |
| 1712 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1713 | QualType argT = Context->getObjCIdType(); |
| 1714 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1715 | ArgTys.push_back(argT); |
| 1716 | ArgTys.push_back(argT); |
| 1717 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1718 | &ArgTys[0], ArgTys.size(), |
| 1719 | false); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1720 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1721 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1722 | msgSendIdent, msgSendType, |
| 1723 | FunctionDecl::Extern, false, 0); |
| 1724 | } |
| 1725 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1726 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1727 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1728 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1729 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1730 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1731 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1732 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1733 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1734 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1735 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1736 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1737 | &ArgTys[0], ArgTys.size(), |
| 1738 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1739 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1740 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1741 | msgSendIdent, msgSendType, |
| 1742 | FunctionDecl::Extern, false, 0); |
| 1743 | } |
| 1744 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1745 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1746 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1747 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1748 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1749 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1750 | SourceLocation(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1751 | &Context->Idents.get("objc_super"), 0); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1752 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1753 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1754 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1755 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1756 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1757 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1758 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1759 | &ArgTys[0], ArgTys.size(), |
| 1760 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1761 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1762 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1763 | msgSendIdent, msgSendType, |
| 1764 | FunctionDecl::Extern, false, 0); |
| 1765 | } |
| 1766 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1767 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1768 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1769 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 1770 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1771 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1772 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1773 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1774 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1775 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1776 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1777 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1778 | &ArgTys[0], ArgTys.size(), |
| 1779 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1780 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1781 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1782 | msgSendIdent, msgSendType, |
| 1783 | FunctionDecl::Extern, false, 0); |
| 1784 | } |
| 1785 | |
| 1786 | // SynthMsgSendSuperStretFunctionDecl - |
| 1787 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1788 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1789 | IdentifierInfo *msgSendIdent = |
| 1790 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 1791 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1792 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1793 | SourceLocation(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1794 | &Context->Idents.get("objc_super"), 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1795 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1796 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1797 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1798 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1799 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1800 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1801 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1802 | &ArgTys[0], ArgTys.size(), |
| 1803 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1804 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 1805 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1806 | msgSendIdent, msgSendType, |
| 1807 | FunctionDecl::Extern, false, 0); |
| 1808 | } |
| 1809 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 1810 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1811 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1812 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 1813 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1814 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1815 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1816 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1817 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1818 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1819 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 1820 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1821 | &ArgTys[0], ArgTys.size(), |
| 1822 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1823 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1824 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1825 | msgSendIdent, msgSendType, |
| 1826 | FunctionDecl::Extern, false, 0); |
| 1827 | } |
| 1828 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1829 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1830 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1831 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 1832 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1833 | ArgTys.push_back(Context->getPointerType( |
| 1834 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1835 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1836 | &ArgTys[0], ArgTys.size(), |
| 1837 | false /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1838 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1839 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1840 | getClassIdent, getClassType, |
| 1841 | FunctionDecl::Extern, false, 0); |
| 1842 | } |
| 1843 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1844 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1845 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1846 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 1847 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1848 | ArgTys.push_back(Context->getPointerType( |
| 1849 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1850 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1851 | &ArgTys[0], ArgTys.size(), |
| 1852 | false /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1853 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1854 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1855 | getClassIdent, getClassType, |
| 1856 | FunctionDecl::Extern, false, 0); |
| 1857 | } |
| 1858 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1859 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1860 | QualType strType = getConstantStringStructType(); |
| 1861 | |
| 1862 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 1863 | |
| 1864 | std::string tmpName = InFileName; |
| 1865 | unsigned i; |
| 1866 | for (i=0; i < tmpName.length(); i++) { |
| 1867 | char c = tmpName.at(i); |
| 1868 | // replace any non alphanumeric characters with '_'. |
| 1869 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 1870 | tmpName[i] = '_'; |
| 1871 | } |
| 1872 | S += tmpName; |
| 1873 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1874 | S += utostr(NumObjCStringLiterals++); |
| 1875 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1876 | Preamble += "static __NSConstantStringImpl " + S; |
| 1877 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 1878 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1879 | // The pretty printer for StringLiteral handles escape characters properly. |
| 1880 | std::ostringstream prettyBuf; |
| 1881 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1882 | Preamble += prettyBuf.str(); |
| 1883 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 1884 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1885 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1886 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1887 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1888 | &Context->Idents.get(S.c_str()), strType, |
| 1889 | VarDecl::Static, NULL); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1890 | DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); |
| 1891 | Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 1892 | Context->getPointerType(DRE->getType()), |
| 1893 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1894 | // cast to NSConstantString * |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1895 | CastExpr *cast = new CastExpr(Exp->getType(), Unop, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1896 | ReplaceStmt(Exp, cast); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1897 | delete Exp; |
| 1898 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1901 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1902 | // check if we are sending a message to 'super' |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1903 | if (!CurMethodDecl || !CurMethodDecl->isInstance()) return 0; |
| 1904 | |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 1905 | if (PreDefinedExpr *PDE = dyn_cast<PreDefinedExpr>(recExpr)) |
| 1906 | if (PDE->getIdentType() == PreDefinedExpr::ObjCSuper) { |
| 1907 | const PointerType *PT = PDE->getType()->getAsPointerType(); |
| 1908 | assert(PT); |
| 1909 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 1910 | return IT->getDecl(); |
| 1911 | } |
| 1912 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1916 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1917 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1918 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1919 | SourceLocation(), |
| 1920 | &Context->Idents.get("objc_super"), 0); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1921 | QualType FieldTypes[2]; |
| 1922 | |
| 1923 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1924 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1925 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1926 | FieldTypes[1] = Context->getObjCClassType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1927 | // Create fields |
| 1928 | FieldDecl *FieldDecls[2]; |
| 1929 | |
| 1930 | for (unsigned i = 0; i < 2; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1931 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1932 | FieldTypes[i]); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1933 | |
| 1934 | SuperStructDecl->defineBody(FieldDecls, 4); |
| 1935 | } |
| 1936 | return Context->getTagDeclType(SuperStructDecl); |
| 1937 | } |
| 1938 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1939 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1940 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1941 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1942 | SourceLocation(), |
| 1943 | &Context->Idents.get("__NSConstantStringImpl"), 0); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1944 | QualType FieldTypes[4]; |
| 1945 | |
| 1946 | // struct objc_object *receiver; |
| 1947 | FieldTypes[0] = Context->getObjCIdType(); |
| 1948 | // int flags; |
| 1949 | FieldTypes[1] = Context->IntTy; |
| 1950 | // char *str; |
| 1951 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 1952 | // long length; |
| 1953 | FieldTypes[3] = Context->LongTy; |
| 1954 | // Create fields |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 1955 | FieldDecl *FieldDecls[4]; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1956 | |
| 1957 | for (unsigned i = 0; i < 4; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1958 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1959 | FieldTypes[i]); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1960 | |
| 1961 | ConstantStringDecl->defineBody(FieldDecls, 4); |
| 1962 | } |
| 1963 | return Context->getTagDeclType(ConstantStringDecl); |
| 1964 | } |
| 1965 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1966 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1967 | if (!SelGetUidFunctionDecl) |
| 1968 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1969 | if (!MsgSendFunctionDecl) |
| 1970 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1971 | if (!MsgSendSuperFunctionDecl) |
| 1972 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1973 | if (!MsgSendStretFunctionDecl) |
| 1974 | SynthMsgSendStretFunctionDecl(); |
| 1975 | if (!MsgSendSuperStretFunctionDecl) |
| 1976 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1977 | if (!MsgSendFpretFunctionDecl) |
| 1978 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1979 | if (!GetClassFunctionDecl) |
| 1980 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1981 | if (!GetMetaClassFunctionDecl) |
| 1982 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1983 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1984 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1985 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 1986 | // May need to use objc_msgSend_stret() as well. |
| 1987 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1988 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1989 | QualType resultType = mDecl->getResultType(); |
| 1990 | if (resultType.getCanonicalType()->isStructureType() |
| 1991 | || resultType.getCanonicalType()->isUnionType()) |
| 1992 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1993 | else if (resultType.getCanonicalType()->isRealFloatingType()) |
| 1994 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1995 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1996 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1997 | // Synthesize a call to objc_msgSend(). |
| 1998 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 1999 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2000 | |
| 2001 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2002 | if (clsName) { // class message. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2003 | if (!strcmp(clsName->getName(), "super")) { |
| 2004 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2005 | if (MsgSendStretFlavor) |
| 2006 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2007 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2008 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2009 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2010 | CurMethodDecl->getClassInterface()->getSuperClass(); |
| 2011 | |
| 2012 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2013 | |
| 2014 | // set the receiver to self, the first argument to all methods. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2015 | InitExprs.push_back(new DeclRefExpr( |
| 2016 | CurMethodDecl->getSelfDecl(), |
| 2017 | Context->getObjCIdType(), |
| 2018 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2019 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2020 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2021 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2022 | SuperDecl->getIdentifier()->getLength(), |
| 2023 | false, argType, SourceLocation(), |
| 2024 | SourceLocation())); |
| 2025 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2026 | &ClsExprs[0], |
| 2027 | ClsExprs.size()); |
| 2028 | // To turn off a warning, type-cast to 'id' |
| 2029 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2030 | new CastExpr(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2031 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
| 2032 | // struct objc_super |
| 2033 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2034 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2035 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2036 | if (LangOpts.Microsoft) { |
| 2037 | SynthSuperContructorFunctionDecl(); |
| 2038 | // Simulate a contructor call... |
| 2039 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2040 | superType, SourceLocation()); |
| 2041 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2042 | superType, SourceLocation()); |
| 2043 | } else { |
| 2044 | // (struct objc_super) { <exprs from above> } |
| 2045 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2046 | &InitExprs[0], InitExprs.size(), |
| 2047 | SourceLocation()); |
| 2048 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 2049 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2050 | // struct objc_super * |
| 2051 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2052 | Context->getPointerType(SuperRep->getType()), |
| 2053 | SourceLocation()); |
| 2054 | MsgExprs.push_back(Unop); |
| 2055 | } else { |
| 2056 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2057 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2058 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 2059 | clsName->getLength(), |
| 2060 | false, argType, SourceLocation(), |
| 2061 | SourceLocation())); |
| 2062 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2063 | &ClsExprs[0], |
| 2064 | ClsExprs.size()); |
| 2065 | MsgExprs.push_back(Cls); |
| 2066 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2067 | } else { // instance message. |
| 2068 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2069 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2070 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2071 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2072 | if (MsgSendStretFlavor) |
| 2073 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2074 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2075 | |
| 2076 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2077 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2078 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2079 | new CastExpr(Context->getObjCIdType(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2080 | new DeclRefExpr(CurMethodDecl->getSelfDecl(), |
| 2081 | Context->getObjCIdType(), |
| 2082 | SourceLocation()), |
| 2083 | SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2084 | |
| 2085 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2086 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2087 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2088 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2089 | false, argType, SourceLocation(), |
| 2090 | SourceLocation())); |
| 2091 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2092 | &ClsExprs[0], |
| 2093 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2094 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2095 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2096 | new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2097 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2098 | // struct objc_super |
| 2099 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2100 | Expr *SuperRep; |
| 2101 | |
| 2102 | if (LangOpts.Microsoft) { |
| 2103 | SynthSuperContructorFunctionDecl(); |
| 2104 | // Simulate a contructor call... |
| 2105 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2106 | superType, SourceLocation()); |
| 2107 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2108 | superType, SourceLocation()); |
| 2109 | } else { |
| 2110 | // (struct objc_super) { <exprs from above> } |
| 2111 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2112 | &InitExprs[0], InitExprs.size(), |
| 2113 | SourceLocation()); |
| 2114 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 2115 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2116 | // struct objc_super * |
| 2117 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2118 | Context->getPointerType(SuperRep->getType()), |
| 2119 | SourceLocation()); |
| 2120 | MsgExprs.push_back(Unop); |
| 2121 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2122 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2123 | // Foo<Proto> *. |
| 2124 | while (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) |
| 2125 | recExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2126 | recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2127 | MsgExprs.push_back(recExpr); |
| 2128 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2129 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2130 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2131 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2132 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2133 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 2134 | Exp->getSelector().getName().size(), |
| 2135 | false, argType, SourceLocation(), |
| 2136 | SourceLocation())); |
| 2137 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2138 | &SelExprs[0], SelExprs.size()); |
| 2139 | MsgExprs.push_back(SelExp); |
| 2140 | |
| 2141 | // Now push any user supplied arguments. |
| 2142 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2143 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2144 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2145 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2146 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2147 | userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType() |
| 2148 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2149 | : ICE->getType(), userExpr, SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2150 | } |
| 2151 | // Make id<P...> cast into an 'id' cast. |
| 2152 | else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2153 | if (CE->getType()->isObjCQualifiedIdType()) { |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2154 | while ((CE = dyn_cast<CastExpr>(userExpr))) |
| 2155 | userExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2156 | userExpr = new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2157 | userExpr, SourceLocation()); |
| 2158 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2159 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2160 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2161 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2162 | // the original expression, since we will delete it below. |
| 2163 | Exp->setArg(i, 0); |
| 2164 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2165 | // Generate the funky cast. |
| 2166 | CastExpr *cast; |
| 2167 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2168 | QualType returnType; |
| 2169 | |
| 2170 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2171 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2172 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2173 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2174 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2175 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2176 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2177 | // Push any user argument types. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 2178 | for (unsigned i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2179 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2180 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2181 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2182 | ArgTypes.push_back(t); |
| 2183 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2184 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2185 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2186 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2187 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2188 | } |
| 2189 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2190 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2191 | |
| 2192 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2193 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 2194 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2195 | |
| 2196 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2197 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2198 | // xx.m:13: warning: function called through a non-compatible type |
| 2199 | // xx.m:13: note: if this code is reached, the program will abort |
| 2200 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE, |
| 2201 | SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2202 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2203 | // Now do the "normal" pointer to function cast. |
| 2204 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2205 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2206 | // If we don't have a method decl, force a variadic cast. |
| 2207 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2208 | castType = Context->getPointerType(castType); |
| 2209 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2210 | |
| 2211 | // Don't forget the parens to enforce the proper binding. |
| 2212 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2213 | |
| 2214 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 2215 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2216 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2217 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2218 | if (MsgSendStretFlavor) { |
| 2219 | // We have the method which returns a struct/union. Must also generate |
| 2220 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2221 | // expression which dictate which one to envoke depending on size of |
| 2222 | // method's return type. |
| 2223 | |
| 2224 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2225 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 2226 | SourceLocation()); |
| 2227 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2228 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
| 2229 | SourceLocation()); |
| 2230 | // Now do the "normal" pointer to function cast. |
| 2231 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2232 | &ArgTypes[0], ArgTypes.size(), |
| 2233 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2234 | castType = Context->getPointerType(castType); |
| 2235 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2236 | |
| 2237 | // Don't forget the parens to enforce the proper binding. |
| 2238 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2239 | |
| 2240 | FT = msgSendType->getAsFunctionType(); |
| 2241 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2242 | FT->getResultType(), SourceLocation()); |
| 2243 | |
| 2244 | // Build sizeof(returnType) |
| 2245 | SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true, |
| 2246 | returnType, Context->getSizeType(), |
| 2247 | SourceLocation(), SourceLocation()); |
| 2248 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2249 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2250 | // For X86 it is more complicated and some kind of target specific routine |
| 2251 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2252 | unsigned IntSize = |
| 2253 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2254 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 2255 | Context->IntTy, |
| 2256 | SourceLocation()); |
| 2257 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 2258 | BinaryOperator::LE, |
| 2259 | Context->IntTy, |
| 2260 | SourceLocation()); |
| 2261 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2262 | ConditionalOperator *CondExpr = |
| 2263 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2264 | ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2265 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2266 | return ReplacingStmt; |
| 2267 | } |
| 2268 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2269 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2270 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2271 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2272 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2273 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2274 | delete Exp; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2275 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2278 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2279 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2280 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2281 | if (!GetProtocolFunctionDecl) |
| 2282 | SynthGetProtocolFunctionDecl(); |
| 2283 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2284 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2285 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2286 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(), |
| 2287 | strlen(Exp->getProtocol()->getName()), |
| 2288 | false, argType, SourceLocation(), |
| 2289 | SourceLocation())); |
| 2290 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2291 | &ProtoExprs[0], |
| 2292 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2293 | ReplaceStmt(Exp, ProtoExp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2294 | delete Exp; |
| 2295 | return ProtoExp; |
| 2296 | |
| 2297 | } |
| 2298 | |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2299 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2300 | const char *endBuf) { |
| 2301 | while (startBuf < endBuf) { |
| 2302 | if (*startBuf == '#') { |
| 2303 | // Skip whitespace. |
| 2304 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2305 | ; |
| 2306 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2307 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2308 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2309 | !strncmp(startBuf, "define", strlen("define")) || |
| 2310 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2311 | !strncmp(startBuf, "else", strlen("else")) || |
| 2312 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2313 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2314 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2315 | !strncmp(startBuf, "include", strlen("include")) || |
| 2316 | !strncmp(startBuf, "import", strlen("import")) || |
| 2317 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2318 | return true; |
| 2319 | } |
| 2320 | startBuf++; |
| 2321 | } |
| 2322 | return false; |
| 2323 | } |
| 2324 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2325 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2326 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2327 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2328 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2329 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 2330 | assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2331 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2332 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2333 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2334 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2335 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2336 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2337 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2338 | |
| 2339 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2340 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2341 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2342 | // If no ivars and no root or if its root, directly or indirectly, |
| 2343 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2344 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2345 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2346 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2347 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2352 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2353 | Result += "\nstruct "; |
| 2354 | Result += CDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2355 | if (LangOpts.Microsoft) |
| 2356 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2357 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2358 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2359 | const char *cursor = strchr(startBuf, '{'); |
| 2360 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2361 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2362 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2363 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2364 | // NSURL.h, for example): |
| 2365 | // |
| 2366 | // #ifdef XYZ |
| 2367 | // @interface Foo : NSObject |
| 2368 | // #else |
| 2369 | // @interface FooBar : NSObject |
| 2370 | // #endif |
| 2371 | // { |
| 2372 | // int i; |
| 2373 | // } |
| 2374 | // @end |
| 2375 | // |
| 2376 | // This clause is segregated to avoid breaking the common case. |
| 2377 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2378 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2379 | CDecl->getClassLoc(); |
| 2380 | const char *endHeader = SM->getCharacterData(L); |
| 2381 | endHeader += Lexer::MeasureTokenLength(L, *SM); |
| 2382 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 2383 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2384 | // advance to the end of the referenced protocols. |
| 2385 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2386 | endHeader++; |
| 2387 | } |
| 2388 | // rewrite the original header |
| 2389 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2390 | } else { |
| 2391 | // rewrite the original header *without* disturbing the '{' |
| 2392 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2393 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2394 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2395 | Result = "\n struct "; |
| 2396 | Result += RCDecl->getName(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2397 | Result += "_IMPL "; |
| 2398 | Result += RCDecl->getName(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2399 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2400 | |
| 2401 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2402 | SourceLocation OnePastCurly = |
| 2403 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2404 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2405 | } |
| 2406 | cursor++; // past '{' |
| 2407 | |
| 2408 | // Now comment out any visibility specifiers. |
| 2409 | while (cursor < endBuf) { |
| 2410 | if (*cursor == '@') { |
| 2411 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2412 | // Skip whitespace. |
| 2413 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2414 | /*scan*/; |
| 2415 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2416 | // FIXME: presence of @public, etc. inside comment results in |
| 2417 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2418 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2419 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2420 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2421 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2422 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2423 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2424 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2425 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2426 | // for type checking. |
| 2427 | else if (*cursor == '<') { |
| 2428 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2429 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2430 | cursor = strchr(cursor, '>'); |
| 2431 | cursor++; |
| 2432 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2433 | InsertText(atLoc, " */", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2434 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2435 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2436 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2437 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2438 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2439 | } else { // we don't have any instance variables - insert super struct. |
| 2440 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2441 | Result += " {\n struct "; |
| 2442 | Result += RCDecl->getName(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2443 | Result += "_IMPL "; |
| 2444 | Result += RCDecl->getName(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2445 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2446 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2447 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2448 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2449 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2450 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2453 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2454 | /// class methods. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2455 | void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2456 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2457 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2458 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2459 | const char *ClassName, |
| 2460 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2461 | if (MethodBegin == MethodEnd) return; |
| 2462 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2463 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2464 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2465 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2466 | SEL _cmd; |
| 2467 | char *method_types; |
| 2468 | void *_imp; |
| 2469 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2470 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2471 | Result += "\nstruct _objc_method {\n"; |
| 2472 | Result += "\tSEL _cmd;\n"; |
| 2473 | Result += "\tchar *method_types;\n"; |
| 2474 | Result += "\tvoid *_imp;\n"; |
| 2475 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2476 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2477 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2478 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2479 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2480 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2481 | |
| 2482 | /* struct { |
| 2483 | struct _objc_method_list *next_method; |
| 2484 | int method_count; |
| 2485 | struct _objc_method method_list[]; |
| 2486 | } |
| 2487 | */ |
| 2488 | Result += "\nstatic struct {\n"; |
| 2489 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2490 | Result += "\tint method_count;\n"; |
| 2491 | Result += "\tstruct _objc_method method_list["; |
| 2492 | Result += utostr(MethodEnd-MethodBegin); |
| 2493 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2494 | Result += prefix; |
| 2495 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2496 | Result += "_METHODS_"; |
| 2497 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2498 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2499 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2500 | Result += "_meth\")))= "; |
| 2501 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2502 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2503 | Result += "\t,{{(SEL)\""; |
| 2504 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
| 2505 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2506 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2507 | Result += "\", \""; |
| 2508 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2509 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2510 | Result += MethodInternalNames[*MethodBegin]; |
| 2511 | Result += "}\n"; |
| 2512 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2513 | Result += "\t ,{(SEL)\""; |
| 2514 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2515 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2516 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2517 | Result += "\", \""; |
| 2518 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2519 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2520 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2521 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2522 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2523 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2526 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2527 | void RewriteObjC:: |
| 2528 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2529 | const char *prefix, |
| 2530 | const char *ClassName, |
| 2531 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2532 | static bool objc_protocol_methods = false; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2533 | if (!Protocols.empty()) { |
| 2534 | for (unsigned i = 0; i != Protocols.size(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2535 | ObjCProtocolDecl *PDecl = Protocols[i]; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2536 | // Output struct protocol_methods holder of method selector and type. |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2537 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2538 | /* struct protocol_methods { |
| 2539 | SEL _cmd; |
| 2540 | char *method_types; |
| 2541 | } |
| 2542 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2543 | Result += "\nstruct protocol_methods {\n"; |
| 2544 | Result += "\tSEL _cmd;\n"; |
| 2545 | Result += "\tchar *method_types;\n"; |
| 2546 | Result += "};\n"; |
| 2547 | |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2548 | objc_protocol_methods = true; |
| 2549 | } |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2550 | // Do not synthesize the protocol more than once. |
| 2551 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2552 | continue; |
| 2553 | |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2554 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2555 | unsigned NumMethods = PDecl->getNumInstanceMethods(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2556 | /* struct _objc_protocol_method_list { |
| 2557 | int protocol_method_count; |
| 2558 | struct protocol_methods protocols[]; |
| 2559 | } |
| 2560 | */ |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2561 | Result += "\nstatic struct {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2562 | Result += "\tint protocol_method_count;\n"; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2563 | Result += "\tstruct protocol_methods protocols["; |
| 2564 | Result += utostr(NumMethods); |
| 2565 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2566 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2567 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2568 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2569 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2570 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2571 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2572 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2573 | if (I == PDecl->instmeth_begin()) |
| 2574 | Result += "\t ,{{(SEL)\""; |
| 2575 | else |
| 2576 | Result += "\t ,{(SEL)\""; |
| 2577 | Result += (*I)->getSelector().getName().c_str(); |
| 2578 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2579 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2580 | Result += "\", \""; |
| 2581 | Result += MethodTypeString; |
| 2582 | Result += "\"}\n"; |
| 2583 | } |
| 2584 | Result += "\t }\n};\n"; |
| 2585 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2586 | |
| 2587 | // Output class methods declared in this protocol. |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2588 | int NumMethods = PDecl->getNumClassMethods(); |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2589 | if (NumMethods > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2590 | /* struct _objc_protocol_method_list { |
| 2591 | int protocol_method_count; |
| 2592 | struct protocol_methods protocols[]; |
| 2593 | } |
| 2594 | */ |
| 2595 | Result += "\nstatic struct {\n"; |
| 2596 | Result += "\tint protocol_method_count;\n"; |
| 2597 | Result += "\tstruct protocol_methods protocols["; |
| 2598 | Result += utostr(NumMethods); |
| 2599 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2600 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2601 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2602 | "{\n\t"; |
| 2603 | Result += utostr(NumMethods); |
| 2604 | Result += "\n"; |
| 2605 | |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2606 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2607 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2608 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2609 | if (I == PDecl->classmeth_begin()) |
| 2610 | Result += "\t ,{{(SEL)\""; |
| 2611 | else |
| 2612 | Result += "\t ,{(SEL)\""; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2613 | Result += (*I)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2614 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2615 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2616 | Result += "\", \""; |
| 2617 | Result += MethodTypeString; |
| 2618 | Result += "\"}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2619 | } |
| 2620 | Result += "\t }\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2621 | } |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2622 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2623 | // Output: |
| 2624 | /* struct _objc_protocol { |
| 2625 | // Objective-C 1.0 extensions |
| 2626 | struct _objc_protocol_extension *isa; |
| 2627 | char *protocol_name; |
| 2628 | struct _objc_protocol **protocol_list; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2629 | struct _objc_protocol_method_list *instance_methods; |
| 2630 | struct _objc_protocol_method_list *class_methods; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2631 | }; |
| 2632 | */ |
| 2633 | static bool objc_protocol = false; |
| 2634 | if (!objc_protocol) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2635 | Result += "\nstruct _objc_protocol {\n"; |
| 2636 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2637 | Result += "\tchar *protocol_name;\n"; |
| 2638 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2639 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2640 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2641 | Result += "};\n"; |
| 2642 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2643 | objc_protocol = true; |
| 2644 | } |
| 2645 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2646 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 2647 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2648 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2649 | "{\n\t0, \""; |
| 2650 | Result += PDecl->getName(); |
| 2651 | Result += "\", 0, "; |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2652 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2653 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2654 | Result += PDecl->getName(); |
| 2655 | Result += ", "; |
| 2656 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2657 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2658 | Result += "0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2659 | if (PDecl->getNumClassMethods() > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2660 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2661 | Result += PDecl->getName(); |
| 2662 | Result += "\n"; |
| 2663 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2664 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2665 | Result += "0\n"; |
| 2666 | Result += "};\n"; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2667 | |
| 2668 | // Mark this protocol as having been generated. |
| 2669 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 2670 | assert(false && "protocol already synthesized"); |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2671 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2672 | // Output the top lovel protocol meta-data for the class. |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2673 | /* struct _objc_protocol_list { |
| 2674 | struct _objc_protocol_list *next; |
| 2675 | int protocol_count; |
| 2676 | struct _objc_protocol *class_protocols[]; |
| 2677 | } |
| 2678 | */ |
| 2679 | Result += "\nstatic struct {\n"; |
| 2680 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2681 | Result += "\tint protocol_count;\n"; |
| 2682 | Result += "\tstruct _objc_protocol *class_protocols["; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2683 | Result += utostr(Protocols.size()); |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2684 | Result += "];\n} _OBJC_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2685 | Result += prefix; |
| 2686 | Result += "_PROTOCOLS_"; |
| 2687 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2688 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2689 | "{\n\t0, "; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2690 | Result += utostr(Protocols.size()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2691 | Result += "\n"; |
| 2692 | |
| 2693 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 2694 | Result += Protocols[0]->getName(); |
| 2695 | Result += " \n"; |
| 2696 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2697 | for (unsigned i = 1; i != Protocols.size(); i++) { |
Steve Naroff | fa8ec62 | 2008-04-18 22:15:15 +0000 | [diff] [blame] | 2698 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2699 | Result += Protocols[i]->getName(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2700 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2701 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2702 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2706 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2707 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2708 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2709 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2710 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2711 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2712 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2713 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2714 | CDecl = CDecl->getNextClassCategory()) |
| 2715 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2716 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2717 | |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2718 | std::string FullCategoryName = ClassDecl->getName(); |
| 2719 | FullCategoryName += '_'; |
| 2720 | FullCategoryName += IDecl->getName(); |
| 2721 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2722 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2723 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2724 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 2725 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2726 | |
| 2727 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2728 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2729 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 2730 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2731 | |
| 2732 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2733 | // Null CDecl is case of a category implementation with no category interface |
| 2734 | if (CDecl) |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2735 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2736 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2737 | |
| 2738 | /* struct _objc_category { |
| 2739 | char *category_name; |
| 2740 | char *class_name; |
| 2741 | struct _objc_method_list *instance_methods; |
| 2742 | struct _objc_method_list *class_methods; |
| 2743 | struct _objc_protocol_list *protocols; |
| 2744 | // Objective-C 1.0 extensions |
| 2745 | uint32_t size; // sizeof (struct _objc_category) |
| 2746 | struct _objc_property_list *instance_properties; // category's own |
| 2747 | // @property decl. |
| 2748 | }; |
| 2749 | */ |
| 2750 | |
| 2751 | static bool objc_category = false; |
| 2752 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2753 | Result += "\nstruct _objc_category {\n"; |
| 2754 | Result += "\tchar *category_name;\n"; |
| 2755 | Result += "\tchar *class_name;\n"; |
| 2756 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 2757 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 2758 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2759 | Result += "\tunsigned int size;\n"; |
| 2760 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 2761 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2762 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2763 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2764 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 2765 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2766 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2767 | Result += IDecl->getName(); |
| 2768 | Result += "\"\n\t, \""; |
| 2769 | Result += ClassDecl->getName(); |
| 2770 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2771 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2772 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2773 | Result += "\t, (struct _objc_method_list *)" |
| 2774 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 2775 | Result += FullCategoryName; |
| 2776 | Result += "\n"; |
| 2777 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2778 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2779 | Result += "\t, 0\n"; |
| 2780 | if (IDecl->getNumClassMethods() > 0) { |
| 2781 | Result += "\t, (struct _objc_method_list *)" |
| 2782 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 2783 | Result += FullCategoryName; |
| 2784 | Result += "\n"; |
| 2785 | } |
| 2786 | else |
| 2787 | Result += "\t, 0\n"; |
| 2788 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2789 | if (CDecl && !CDecl->getReferencedProtocols().empty()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2790 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 2791 | Result += FullCategoryName; |
| 2792 | Result += "\n"; |
| 2793 | } |
| 2794 | else |
| 2795 | Result += "\t, 0\n"; |
| 2796 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2799 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 2800 | /// ivar offset. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2801 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2802 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2803 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2804 | if (ivar->isBitField()) { |
| 2805 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 2806 | // place all bitfields at offset 0. |
| 2807 | Result += "0"; |
| 2808 | } else { |
| 2809 | Result += "__OFFSETOFIVAR__(struct "; |
| 2810 | Result += IDecl->getName(); |
| 2811 | if (LangOpts.Microsoft) |
| 2812 | Result += "_IMPL"; |
| 2813 | Result += ", "; |
| 2814 | Result += ivar->getName(); |
| 2815 | Result += ")"; |
| 2816 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2819 | //===----------------------------------------------------------------------===// |
| 2820 | // Meta Data Emission |
| 2821 | //===----------------------------------------------------------------------===// |
| 2822 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2823 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2824 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2825 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2826 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2827 | // Explictly declared @interface's are already synthesized. |
| 2828 | if (CDecl->ImplicitInterfaceDecl()) { |
| 2829 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 2830 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2831 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2832 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2834 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2835 | unsigned NumIvars = !IDecl->ivar_empty() |
| 2836 | ? IDecl->ivar_size() |
| 2837 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2838 | if (NumIvars > 0) { |
| 2839 | static bool objc_ivar = false; |
| 2840 | if (!objc_ivar) { |
| 2841 | /* struct _objc_ivar { |
| 2842 | char *ivar_name; |
| 2843 | char *ivar_type; |
| 2844 | int ivar_offset; |
| 2845 | }; |
| 2846 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2847 | Result += "\nstruct _objc_ivar {\n"; |
| 2848 | Result += "\tchar *ivar_name;\n"; |
| 2849 | Result += "\tchar *ivar_type;\n"; |
| 2850 | Result += "\tint ivar_offset;\n"; |
| 2851 | Result += "};\n"; |
| 2852 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2853 | objc_ivar = true; |
| 2854 | } |
| 2855 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2856 | /* struct { |
| 2857 | int ivar_count; |
| 2858 | struct _objc_ivar ivar_list[nIvars]; |
| 2859 | }; |
| 2860 | */ |
| 2861 | Result += "\nstatic struct {\n"; |
| 2862 | Result += "\tint ivar_count;\n"; |
| 2863 | Result += "\tstruct _objc_ivar ivar_list["; |
| 2864 | Result += utostr(NumIvars); |
| 2865 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2866 | Result += IDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2867 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2868 | "{\n\t"; |
| 2869 | Result += utostr(NumIvars); |
| 2870 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2871 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2872 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2873 | if (!IDecl->ivar_empty()) { |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2874 | IVI = IDecl->ivar_begin(); |
| 2875 | IVE = IDecl->ivar_end(); |
| 2876 | } else { |
| 2877 | IVI = CDecl->ivar_begin(); |
| 2878 | IVE = CDecl->ivar_end(); |
| 2879 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2880 | Result += "\t,{{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2881 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2882 | Result += "\", \""; |
| 2883 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2884 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2885 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2886 | Result += StrEncoding; |
| 2887 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2888 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2889 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2890 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2891 | Result += "\t ,{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2892 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2893 | Result += "\", \""; |
| 2894 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2895 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2896 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2897 | Result += StrEncoding; |
| 2898 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2899 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2900 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2907 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2908 | true, "", IDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2909 | |
| 2910 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2911 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2912 | false, "", IDecl->getName(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2913 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2914 | // Protocols referenced in class declaration? |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame^] | 2915 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2916 | "CLASS", CDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2917 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2918 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2919 | // Declaration of class/meta-class metadata |
| 2920 | /* struct _objc_class { |
| 2921 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2922 | const char *super_class_name; |
| 2923 | char *name; |
| 2924 | long version; |
| 2925 | long info; |
| 2926 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2927 | struct _objc_ivar_list *ivars; |
| 2928 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2929 | struct objc_cache *cache; |
| 2930 | struct objc_protocol_list *protocols; |
| 2931 | const char *ivar_layout; |
| 2932 | struct _objc_class_ext *ext; |
| 2933 | }; |
| 2934 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2935 | static bool objc_class = false; |
| 2936 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2937 | Result += "\nstruct _objc_class {\n"; |
| 2938 | Result += "\tstruct _objc_class *isa;\n"; |
| 2939 | Result += "\tconst char *super_class_name;\n"; |
| 2940 | Result += "\tchar *name;\n"; |
| 2941 | Result += "\tlong version;\n"; |
| 2942 | Result += "\tlong info;\n"; |
| 2943 | Result += "\tlong instance_size;\n"; |
| 2944 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 2945 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 2946 | Result += "\tstruct objc_cache *cache;\n"; |
| 2947 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2948 | Result += "\tconst char *ivar_layout;\n"; |
| 2949 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 2950 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2951 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2955 | ObjCInterfaceDecl *RootClass = 0; |
| 2956 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2957 | while (SuperClass) { |
| 2958 | RootClass = SuperClass; |
| 2959 | SuperClass = SuperClass->getSuperClass(); |
| 2960 | } |
| 2961 | SuperClass = CDecl->getSuperClass(); |
| 2962 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2963 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 2964 | Result += CDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2965 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2966 | "{\n\t(struct _objc_class *)\""; |
| 2967 | Result += (RootClass ? RootClass->getName() : CDecl->getName()); |
| 2968 | Result += "\""; |
| 2969 | |
| 2970 | if (SuperClass) { |
| 2971 | Result += ", \""; |
| 2972 | Result += SuperClass->getName(); |
| 2973 | Result += "\", \""; |
| 2974 | Result += CDecl->getName(); |
| 2975 | Result += "\""; |
| 2976 | } |
| 2977 | else { |
| 2978 | Result += ", 0, \""; |
| 2979 | Result += CDecl->getName(); |
| 2980 | Result += "\""; |
| 2981 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2982 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2983 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2984 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2985 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2986 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2987 | Result += IDecl->getName(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2988 | Result += "\n"; |
| 2989 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2990 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2991 | Result += ", 0\n"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 2992 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2993 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2994 | Result += CDecl->getName(); |
| 2995 | Result += ",0,0\n"; |
| 2996 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 2997 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2998 | Result += "\t,0,0,0,0\n"; |
| 2999 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3000 | |
| 3001 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3002 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 3003 | Result += CDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3004 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3005 | "{\n\t&_OBJC_METACLASS_"; |
| 3006 | Result += CDecl->getName(); |
| 3007 | if (SuperClass) { |
| 3008 | Result += ", \""; |
| 3009 | Result += SuperClass->getName(); |
| 3010 | Result += "\", \""; |
| 3011 | Result += CDecl->getName(); |
| 3012 | Result += "\""; |
| 3013 | } |
| 3014 | else { |
| 3015 | Result += ", 0, \""; |
| 3016 | Result += CDecl->getName(); |
| 3017 | Result += "\""; |
| 3018 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3019 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3020 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3021 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3022 | Result += ",0"; |
| 3023 | else { |
| 3024 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3025 | Result += ",sizeof(struct "; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3026 | Result += CDecl->getName(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3027 | if (LangOpts.Microsoft) |
| 3028 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3029 | Result += ")"; |
| 3030 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3031 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3032 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3033 | Result += CDecl->getName(); |
| 3034 | Result += "\n\t"; |
| 3035 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3036 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3037 | Result += ",0"; |
| 3038 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3039 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3040 | Result += CDecl->getName(); |
| 3041 | Result += ", 0\n\t"; |
| 3042 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3043 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3044 | Result += ",0,0"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3045 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3046 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3047 | Result += CDecl->getName(); |
| 3048 | Result += ", 0,0\n"; |
| 3049 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3050 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3051 | Result += ",0,0,0\n"; |
| 3052 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3053 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3054 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3055 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3056 | /// and emits meta-data. |
| 3057 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3058 | void RewriteObjC::RewriteImplementations(std::string &Result) { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3059 | int ClsDefCount = ClassImplementation.size(); |
| 3060 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3061 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3062 | if (ClsDefCount == 0 && CatDefCount == 0) |
| 3063 | return; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3064 | // Rewrite implemented methods |
| 3065 | for (int i = 0; i < ClsDefCount; i++) |
| 3066 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3067 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3068 | for (int i = 0; i < CatDefCount; i++) |
| 3069 | RewriteImplementationDecl(CategoryImplementation[i]); |
| 3070 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3071 | // This is needed for determining instance variable offsets. |
| 3072 | Result += "#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3073 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3074 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3075 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3076 | |
| 3077 | // For each implemented category, write out all its meta data. |
| 3078 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3079 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3080 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3081 | // Write objc_symtab metadata |
| 3082 | /* |
| 3083 | struct _objc_symtab |
| 3084 | { |
| 3085 | long sel_ref_cnt; |
| 3086 | SEL *refs; |
| 3087 | short cls_def_cnt; |
| 3088 | short cat_def_cnt; |
| 3089 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3090 | }; |
| 3091 | */ |
| 3092 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3093 | Result += "\nstruct _objc_symtab {\n"; |
| 3094 | Result += "\tlong sel_ref_cnt;\n"; |
| 3095 | Result += "\tSEL *refs;\n"; |
| 3096 | Result += "\tshort cls_def_cnt;\n"; |
| 3097 | Result += "\tshort cat_def_cnt;\n"; |
| 3098 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3099 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3100 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3101 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3102 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3103 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3104 | + ", " + utostr(CatDefCount) + "\n"; |
| 3105 | for (int i = 0; i < ClsDefCount; i++) { |
| 3106 | Result += "\t,&_OBJC_CLASS_"; |
| 3107 | Result += ClassImplementation[i]->getName(); |
| 3108 | Result += "\n"; |
| 3109 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3110 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3111 | for (int i = 0; i < CatDefCount; i++) { |
| 3112 | Result += "\t,&_OBJC_CATEGORY_"; |
| 3113 | Result += CategoryImplementation[i]->getClassInterface()->getName(); |
| 3114 | Result += "_"; |
| 3115 | Result += CategoryImplementation[i]->getName(); |
| 3116 | Result += "\n"; |
| 3117 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3118 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3119 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3120 | |
| 3121 | // Write objc_module metadata |
| 3122 | |
| 3123 | /* |
| 3124 | struct _objc_module { |
| 3125 | long version; |
| 3126 | long size; |
| 3127 | const char *name; |
| 3128 | struct _objc_symtab *symtab; |
| 3129 | } |
| 3130 | */ |
| 3131 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3132 | Result += "\nstruct _objc_module {\n"; |
| 3133 | Result += "\tlong version;\n"; |
| 3134 | Result += "\tlong size;\n"; |
| 3135 | Result += "\tconst char *name;\n"; |
| 3136 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3137 | Result += "};\n\n"; |
| 3138 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3139 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3140 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3141 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3142 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3143 | |
| 3144 | if (LangOpts.Microsoft) { |
| 3145 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3146 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3147 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3148 | Result += "&_OBJC_MODULES;\n"; |
| 3149 | Result += "#pragma data_seg(pop)\n\n"; |
| 3150 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3151 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3152 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3153 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3154 | |
| 3155 | |