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