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