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