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