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