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