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