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