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