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