Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 63e2dcc | 2008-05-21 20:19:16 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 32 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 34 | static llvm::cl::opt<bool> |
| 35 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 36 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 37 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 38 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 39 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 40 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 41 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 42 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 43 | unsigned RewriteFailedDiag; |
| 44 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 45 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 46 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 47 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 48 | unsigned MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 49 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 50 | SourceLocation LastIncLoc; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 52 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 53 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 54 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 55 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 56 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 57 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 58 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 59 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 60 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 61 | unsigned NumObjCStringLiterals; |
| 62 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 63 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 64 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 65 | FunctionDecl *MsgSendStretFunctionDecl; |
| 66 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 67 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 68 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 69 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 70 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 71 | FunctionDecl *CFStringFunctionDecl; |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 72 | FunctionDecl *GetProtocolFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 73 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 74 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 75 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 76 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 77 | RecordDecl *NSStringRecord; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 78 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 79 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 80 | int BcLabelCount; |
| 81 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 82 | // Needed for super. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 83 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 84 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 85 | RecordDecl *ConstantStringDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 87 | // Needed for header files being rewritten |
| 88 | bool IsHeader; |
| 89 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 90 | std::string InFileName; |
| 91 | std::string OutFileName; |
| 92 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 93 | std::string Preamble; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 94 | |
| 95 | // Block expressions. |
| 96 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 97 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 98 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 99 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 100 | // Block related declarations. |
| 101 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 102 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 104 | |
| 105 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 106 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 107 | // This maps a property to it's assignment statement. |
| 108 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 109 | // This maps an original source AST to it's rewritten form. This allows |
| 110 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 111 | // This is needed to support some of the exotic property rewriting. |
| 112 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 113 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 114 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 115 | VarDecl *GlobalVarDecl; |
| 116 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 117 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 118 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 119 | virtual void Initialize(ASTContext &context); |
| 120 | |
| 121 | virtual void InitializeTU(TranslationUnit &TU) { |
| 122 | TU.SetOwnsDecls(false); |
| 123 | Initialize(TU.getContext()); |
| 124 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 126 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 127 | // Top Level Driver code. |
| 128 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 129 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 130 | RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 131 | Diagnostic &D, const LangOptions &LOpts); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 132 | |
| 133 | ~RewriteObjC() {} |
| 134 | |
| 135 | virtual void HandleTranslationUnit(TranslationUnit& TU); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 136 | |
| 137 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 138 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 139 | |
| 140 | if (ReplacingStmt) |
| 141 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 142 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 143 | // If replacement succeeded or warning disabled return with no warning. |
| 144 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 145 | ReplacedNodes[Old] = New; |
| 146 | return; |
| 147 | } |
| 148 | if (SilenceRewriteMacroWarning) |
| 149 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 150 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 151 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 154 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 155 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 156 | // If insertion succeeded or warning disabled return with no warning. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 157 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 158 | SilenceRewriteMacroWarning) |
| 159 | return; |
| 160 | |
| 161 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 162 | } |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 163 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 164 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 165 | // If removal succeeded or warning disabled return with no warning. |
| 166 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 167 | return; |
| 168 | |
| 169 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 170 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 171 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 172 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 173 | const char *NewStr, unsigned NewLength) { |
| 174 | // If removal succeeded or warning disabled return with no warning. |
| 175 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 176 | SilenceRewriteMacroWarning) |
| 177 | return; |
| 178 | |
| 179 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 180 | } |
| 181 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 182 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 183 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 184 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 185 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 186 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 187 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 188 | ObjCImplementationDecl *IMD, |
| 189 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 190 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 191 | void RewriteImplementationDecl(NamedDecl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 192 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 193 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 194 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 195 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 196 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 197 | void RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 198 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 199 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 200 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 201 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 202 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 203 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 204 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 205 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 206 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 207 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 208 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 209 | void CollectPropertySetters(Stmt *S); |
| 210 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 211 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 212 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 213 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
| 214 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 215 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 216 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 217 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 218 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 219 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 220 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 222 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 223 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 224 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 225 | SourceLocation OrigEnd); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 226 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 227 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 228 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 229 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 230 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 231 | void SynthCountByEnumWithState(std::string &buf); |
| 232 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 233 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 234 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 235 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 236 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 237 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 238 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 239 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 240 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 241 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 242 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 243 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 244 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 245 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 246 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 249 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 251 | typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator; |
| 252 | void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 253 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 254 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 255 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 256 | const char *ClassName, |
| 257 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 259 | void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> |
| 260 | &Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 261 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 262 | const char *ClassName, |
| 263 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 264 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 265 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 266 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 267 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 268 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 269 | void RewriteImplementations(); |
| 270 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 271 | |
| 272 | // Block rewriting. |
| 273 | void RewriteBlocksInFunctionTypeProto(QualType funcType, NamedDecl *D); |
| 274 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 275 | |
| 276 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 277 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 278 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 279 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 280 | void RewriteBlockCall(CallExpr *Exp); |
| 281 | void RewriteBlockPointerDecl(NamedDecl *VD); |
| 282 | void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
| 283 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 284 | |
| 285 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 286 | const char *funcName, std::string Tag); |
| 287 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 288 | const char *funcName, std::string Tag); |
| 289 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 290 | bool hasCopyDisposeHelpers); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 291 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 292 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 293 | const char *FunName); |
| 294 | |
| 295 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 296 | void GetBlockCallExprs(Stmt *S); |
| 297 | void GetBlockDeclRefExprs(Stmt *S); |
| 298 | |
| 299 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 300 | // canonical type. We only care if the top-level type is a closure pointer. |
| 301 | bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); } |
| 302 | |
| 303 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 304 | bool isObjCType(QualType T) { |
| 305 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 306 | return false; |
| 307 | |
| 308 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 309 | |
| 310 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 311 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 312 | return true; |
| 313 | |
| 314 | if (const PointerType *PT = OCT->getAsPointerType()) { |
| 315 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 316 | isa<ObjCQualifiedIdType>(PT->getPointeeType())) |
| 317 | return true; |
| 318 | } |
| 319 | return false; |
| 320 | } |
| 321 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 322 | void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 323 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 324 | |
| 325 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 326 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 327 | }; |
| 328 | } |
| 329 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 330 | void RewriteObjC::RewriteBlocksInFunctionTypeProto(QualType funcType, |
| 331 | NamedDecl *D) { |
| 332 | if (FunctionTypeProto *fproto = dyn_cast<FunctionTypeProto>(funcType)) { |
| 333 | for (FunctionTypeProto::arg_type_iterator I = fproto->arg_type_begin(), |
| 334 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 335 | if (isBlockPointerType(*I)) { |
| 336 | // All the args are checked/rewritten. Don't call twice! |
| 337 | RewriteBlockPointerDecl(D); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 344 | const PointerType *PT = funcType->getAsPointerType(); |
| 345 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 346 | RewriteBlocksInFunctionTypeProto(PT->getPointeeType(), ND); |
| 347 | } |
| 348 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 349 | static bool IsHeaderFile(const std::string &Filename) { |
| 350 | std::string::size_type DotPos = Filename.rfind('.'); |
| 351 | |
| 352 | if (DotPos == std::string::npos) { |
| 353 | // no file extension |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 358 | // C header: .h |
| 359 | // C++ header: .hh or .H; |
| 360 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 361 | } |
| 362 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 363 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 364 | Diagnostic &D, const LangOptions &LOpts) |
| 365 | : Diags(D), LangOpts(LOpts) { |
| 366 | IsHeader = IsHeaderFile(inFile); |
| 367 | InFileName = inFile; |
| 368 | OutFileName = outFile; |
| 369 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 370 | "rewriting sub-expression within a macro (may not be correct)"); |
| 371 | } |
| 372 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 373 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 374 | const std::string& OutFile, |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 375 | Diagnostic &Diags, |
| 376 | const LangOptions &LOpts) { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 377 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 379 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 380 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 381 | Context = &context; |
| 382 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 383 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 384 | MsgSendFunctionDecl = 0; |
| 385 | MsgSendSuperFunctionDecl = 0; |
| 386 | MsgSendStretFunctionDecl = 0; |
| 387 | MsgSendSuperStretFunctionDecl = 0; |
| 388 | MsgSendFpretFunctionDecl = 0; |
| 389 | GetClassFunctionDecl = 0; |
| 390 | GetMetaClassFunctionDecl = 0; |
| 391 | SelGetUidFunctionDecl = 0; |
| 392 | CFStringFunctionDecl = 0; |
| 393 | GetProtocolFunctionDecl = 0; |
| 394 | ConstantStringClassReference = 0; |
| 395 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 396 | CurMethodDef = 0; |
| 397 | CurFunctionDef = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 398 | SuperStructDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 399 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 400 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 401 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 402 | NumObjCStringLiterals = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 403 | |
| 404 | // Get the ID and start/end of the main file. |
| 405 | MainFileID = SM->getMainFileID(); |
| 406 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 407 | MainFileStart = MainBuf->getBufferStart(); |
| 408 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 410 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 412 | // declaring objc_selector outside the parameter list removes a silly |
| 413 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 414 | if (IsHeader) |
| 415 | Preamble = "#pragma once\n"; |
| 416 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 417 | Preamble += "#ifndef OBJC_SUPER\n"; |
| 418 | Preamble += "struct objc_super { struct objc_object *object; "; |
| 419 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 420 | if (LangOpts.Microsoft) { |
| 421 | // Add a constructor for creating temporary objects. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 422 | Preamble += "objc_super(struct objc_object *o, struct objc_object *s) : "; |
| 423 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 424 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 425 | Preamble += "};\n"; |
| 426 | Preamble += "#define OBJC_SUPER\n"; |
| 427 | Preamble += "#endif\n"; |
| 428 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 429 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 430 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 431 | Preamble += "#endif\n"; |
Steve Naroff | 3c64d9e | 2008-03-12 13:19:12 +0000 | [diff] [blame] | 432 | if (LangOpts.Microsoft) |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 433 | Preamble += "#define __OBJC_RW_EXTERN extern \"C\" __declspec(dllimport)\n"; |
| 434 | else |
| 435 | Preamble += "#define __OBJC_RW_EXTERN extern\n"; |
| 436 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 437 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 438 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 439 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 440 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 441 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 442 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 443 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 444 | Preamble += "__OBJC_RW_EXTERN double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 445 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 446 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 447 | Preamble += "(const char *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 448 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 449 | Preamble += "(const char *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 450 | Preamble += "__OBJC_RW_EXTERN void objc_exception_throw(struct objc_object *);\n"; |
| 451 | Preamble += "__OBJC_RW_EXTERN void objc_exception_try_enter(void *);\n"; |
| 452 | Preamble += "__OBJC_RW_EXTERN void objc_exception_try_exit(void *);\n"; |
| 453 | Preamble += "__OBJC_RW_EXTERN struct objc_object *objc_exception_extract(void *);\n"; |
| 454 | Preamble += "__OBJC_RW_EXTERN int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 455 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 456 | // @synchronized hooks. |
| 457 | Preamble += "__OBJC_RW_EXTERN void objc_sync_enter(struct objc_object *);\n"; |
| 458 | Preamble += "__OBJC_RW_EXTERN void objc_sync_exit(struct objc_object *);\n"; |
Steve Naroff | 69c827f | 2008-05-06 22:45:19 +0000 | [diff] [blame] | 459 | Preamble += "__OBJC_RW_EXTERN Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 460 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 461 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 462 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 463 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 464 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 465 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 73ebd6d | 2008-06-02 20:23:21 +0000 | [diff] [blame] | 466 | Preamble += "__OBJC_RW_EXTERN void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 467 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 468 | Preamble += "#endif\n"; |
| 469 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 470 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 471 | Preamble += " int *isa;\n"; |
| 472 | Preamble += " int flags;\n"; |
| 473 | Preamble += " char *str;\n"; |
| 474 | Preamble += " long length;\n"; |
| 475 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 476 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 477 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 478 | Preamble += "#else\n"; |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 479 | Preamble += "__OBJC_RW_EXTERN int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 480 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 481 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 482 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 483 | // Blocks preamble. |
| 484 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 485 | Preamble += "#define BLOCK_IMPL\n"; |
| 486 | Preamble += "struct __block_impl {\n"; |
| 487 | Preamble += " void *isa;\n"; |
| 488 | Preamble += " int Flags;\n"; |
| 489 | Preamble += " int Size;\n"; |
| 490 | Preamble += " void *FuncPtr;\n"; |
| 491 | Preamble += "};\n"; |
| 492 | Preamble += "enum {\n"; |
| 493 | Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n"; |
| 494 | Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n"; |
| 495 | Preamble += "};\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 496 | Preamble += "// Runtime copy/destroy helper functions\n"; |
| 497 | Preamble += "__OBJC_RW_EXTERN void _Block_copy_assign(void *, void *);\n"; |
| 498 | Preamble += "__OBJC_RW_EXTERN void _Block_byref_assign_copy(void *, void *);\n"; |
| 499 | Preamble += "__OBJC_RW_EXTERN void _Block_destroy(void *);\n"; |
| 500 | Preamble += "__OBJC_RW_EXTERN void _Block_byref_release(void *);\n"; |
| 501 | Preamble += "__OBJC_RW_EXTERN void *_NSConcreteGlobalBlock;\n"; |
| 502 | Preamble += "__OBJC_RW_EXTERN void *_NSConcreteStackBlock;\n"; |
| 503 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 504 | if (LangOpts.Microsoft) { |
| 505 | Preamble += "#undef __OBJC_RW_EXTERN\n"; |
| 506 | Preamble += "#define __attribute__(X)\n"; |
| 507 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 511 | //===----------------------------------------------------------------------===// |
| 512 | // Top Level Driver Code |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 515 | void RewriteObjC::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 516 | // Two cases: either the decl could be in the main file, or it could be in a |
| 517 | // #included file. If the former, rewrite it now. If the later, check to see |
| 518 | // if we rewrote the #include/#import. |
| 519 | SourceLocation Loc = D->getLocation(); |
| 520 | Loc = SM->getLogicalLoc(Loc); |
| 521 | |
| 522 | // If this is for a builtin, ignore it. |
| 523 | if (Loc.isInvalid()) return; |
| 524 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 525 | // Look for built-in declarations that we need to refer during the rewrite. |
| 526 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 527 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 528 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 529 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 530 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 531 | ConstantStringClassReference = FVD; |
| 532 | return; |
| 533 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 534 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 535 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 536 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 537 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 538 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 539 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 540 | } else if (ObjCForwardProtocolDecl *FP = |
| 541 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 542 | RewriteForwardProtocolDecl(FP); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 544 | // 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] | 545 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 546 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 549 | //===----------------------------------------------------------------------===// |
| 550 | // Syntactic (non-AST) Rewriting Code |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 553 | void RewriteObjC::RewriteInclude() { |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 554 | SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0); |
| 555 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 556 | const char *MainBufStart = MainBuf.first; |
| 557 | const char *MainBufEnd = MainBuf.second; |
| 558 | size_t ImportLen = strlen("import"); |
| 559 | size_t IncludeLen = strlen("include"); |
| 560 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 561 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 562 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 563 | if (*BufPtr == '#') { |
| 564 | if (++BufPtr == MainBufEnd) |
| 565 | return; |
| 566 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 567 | if (++BufPtr == MainBufEnd) |
| 568 | return; |
| 569 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 570 | // replace import with include |
| 571 | SourceLocation ImportLoc = |
| 572 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 573 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 574 | BufPtr += ImportLen; |
| 575 | } |
| 576 | } |
| 577 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 580 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 581 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 582 | const char *MainBufStart = MainBuf.first; |
| 583 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 584 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 585 | // Loop over the whole file, looking for tabs. |
| 586 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 587 | if (*BufPtr != '\t') |
| 588 | continue; |
| 589 | |
| 590 | // Okay, we found a tab. This tab will turn into at least one character, |
| 591 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 592 | unsigned VCol = 0; |
| 593 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 594 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 595 | ++VCol; |
| 596 | |
| 597 | // Okay, now that we know the virtual column, we know how many spaces to |
| 598 | // insert. We assume 8-character tab-stops. |
| 599 | unsigned Spaces = 8-(VCol & 7); |
| 600 | |
| 601 | // Get the location of the tab. |
| 602 | SourceLocation TabLoc = |
| 603 | SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart); |
| 604 | |
| 605 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 606 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 610 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 611 | ObjCIvarDecl *OID) { |
| 612 | std::string S; |
| 613 | S = "((struct "; |
| 614 | S += ClassDecl->getIdentifier()->getName(); |
| 615 | S += "_IMPL *)self)->"; |
| 616 | S += OID->getNameAsCString(); |
| 617 | return S; |
| 618 | } |
| 619 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 620 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 621 | ObjCImplementationDecl *IMD, |
| 622 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 623 | SourceLocation startLoc = PID->getLocStart(); |
| 624 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 625 | const char *startBuf = SM->getCharacterData(startLoc); |
| 626 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 627 | const char *semiBuf = strchr(startBuf, ';'); |
| 628 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 629 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 630 | |
| 631 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 632 | return; // FIXME: is this correct? |
| 633 | |
| 634 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 635 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 636 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 637 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 638 | |
| 639 | if (!OID) |
| 640 | return; |
| 641 | |
| 642 | std::string Getr; |
| 643 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 644 | Getr += "{ "; |
| 645 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 646 | // FIXME: deal with code generation implications for various property |
| 647 | // attributes (copy, retain, nonatomic). |
| 648 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 649 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 650 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 651 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 652 | |
| 653 | // Add the rewritten getter to trigger meta data generation. An alternate, and |
| 654 | // possibly cleaner approach is to hack RewriteObjCMethodsMetaData() to deal |
| 655 | // with properties explicitly. The following addInstanceMethod() required far |
| 656 | // less code change (and actually models what the rewriter is doing). |
| 657 | if (IMD) |
| 658 | IMD->addInstanceMethod(PD->getGetterMethodDecl()); |
| 659 | else |
| 660 | CID->addInstanceMethod(PD->getGetterMethodDecl()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 661 | |
| 662 | if (PD->isReadOnly()) |
| 663 | return; |
| 664 | |
| 665 | // Generate the 'setter' function. |
| 666 | std::string Setr; |
| 667 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 668 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 669 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 670 | // FIXME: deal with code generation implications for various property |
| 671 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 672 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 673 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
| 674 | Setr += OID->getNameAsCString(); |
| 675 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 676 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 677 | |
| 678 | // Add the rewritten setter to trigger meta data generation. |
| 679 | if (IMD) |
| 680 | IMD->addInstanceMethod(PD->getSetterMethodDecl()); |
| 681 | else |
| 682 | CID->addInstanceMethod(PD->getSetterMethodDecl()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 683 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 684 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 685 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 686 | int numDecls = ClassDecl->getNumForwardDecls(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 687 | ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 688 | |
| 689 | // Get the start location and compute the semi location. |
| 690 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 691 | const char *startBuf = SM->getCharacterData(startLoc); |
| 692 | const char *semiPtr = strchr(startBuf, ';'); |
| 693 | |
| 694 | // Translate to typedef's that forward reference structs with the same name |
| 695 | // as the class. As a convenience, we include the original declaration |
| 696 | // as a comment. |
| 697 | std::string typedefString; |
| 698 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 699 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 700 | typedefString += "\n"; |
| 701 | for (int i = 0; i < numDecls; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 702 | ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i]; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 703 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 704 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 705 | typedefString += "\n"; |
| 706 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 707 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 708 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 709 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 710 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 711 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 715 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 716 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 719 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 720 | SourceLocation LocStart = Method->getLocStart(); |
| 721 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 722 | |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 723 | if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 724 | InsertText(LocStart, "#if 0\n", 6); |
| 725 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 726 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 727 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 731 | void RewriteObjC::RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 732 | { |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 733 | for (unsigned i = 0; i < nProperties; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 734 | ObjCPropertyDecl *Property = Properties[i]; |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 735 | SourceLocation Loc = Property->getLocation(); |
| 736 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 737 | ReplaceText(Loc, 0, "// ", 3); |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 738 | |
| 739 | // FIXME: handle properties that are declared across multiple lines. |
| 740 | } |
| 741 | } |
| 742 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 743 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 744 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 745 | |
| 746 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 747 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 749 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 750 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 751 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 752 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 753 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 754 | RewriteMethodDeclaration(*I); |
| 755 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 756 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 757 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 760 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 761 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 762 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 763 | SourceLocation LocStart = PDecl->getLocStart(); |
| 764 | |
| 765 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 766 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 767 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 768 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 769 | E = PDecl->instmeth_end(); I != E; ++I) |
| 770 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 771 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 772 | E = PDecl->classmeth_end(); I != E; ++I) |
| 773 | RewriteMethodDeclaration(*I); |
| 774 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 775 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 776 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 777 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 778 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 779 | // Must comment out @optional/@required |
| 780 | const char *startBuf = SM->getCharacterData(LocStart); |
| 781 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 782 | for (const char *p = startBuf; p < endBuf; p++) { |
| 783 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 784 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 785 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 786 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 787 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 788 | |
| 789 | } |
| 790 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 791 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 792 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 793 | ReplaceText(OptionalLoc, strlen("@required"), |
| 794 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 795 | |
| 796 | } |
| 797 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 800 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 801 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 802 | if (LocStart.isInvalid()) |
| 803 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 804 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 805 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 808 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 809 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 810 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 811 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 812 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 813 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 814 | ResultStr += "id"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 815 | else if (OMD->getResultType()->isFunctionPointerType()) { |
| 816 | // needs special handling, since pointer-to-functions have special |
| 817 | // syntax (where a decaration models use). |
| 818 | QualType retType = OMD->getResultType(); |
| 819 | if (const PointerType* PT = retType->getAsPointerType()) { |
| 820 | QualType PointeeTy = PT->getPointeeType(); |
| 821 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 822 | ResultStr += FPRetType->getResultType().getAsString(); |
| 823 | ResultStr += "(*"; |
| 824 | } |
| 825 | } |
| 826 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 827 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 828 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 829 | |
| 830 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 831 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 832 | |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 833 | if (OMD->isInstance()) |
| 834 | NameStr += "_I_"; |
| 835 | else |
| 836 | NameStr += "_C_"; |
| 837 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 838 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 839 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 840 | |
| 841 | NamedDecl *MethodContext = OMD->getMethodContext(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 842 | if (ObjCCategoryImplDecl *CID = |
| 843 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 844 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 845 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 846 | } |
Steve Naroff | 563b828 | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 847 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 848 | { |
| 849 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 850 | int len = selString.size(); |
| 851 | for (int i = 0; i < len; i++) |
| 852 | if (selString[i] == ':') |
| 853 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 854 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 855 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 856 | // Remember this name for metadata emission |
| 857 | MethodInternalNames[OMD] = NameStr; |
| 858 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 859 | |
| 860 | // Rewrite arguments |
| 861 | ResultStr += "("; |
| 862 | |
| 863 | // invisible arguments |
| 864 | if (OMD->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 865 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 866 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 867 | if (!LangOpts.Microsoft) { |
| 868 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 869 | ResultStr += "struct "; |
| 870 | } |
| 871 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 872 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 873 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 874 | } |
| 875 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 876 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 877 | |
| 878 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 879 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 880 | ResultStr += " _cmd"; |
| 881 | |
| 882 | // Method arguments. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 883 | for (unsigned i = 0; i < OMD->getNumParams(); i++) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 884 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 885 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 886 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 887 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 888 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 889 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 890 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 891 | if (isBlockPointerType(PDecl->getType())) { |
| 892 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 893 | const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType(); |
| 894 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name); |
| 895 | } else |
| 896 | PDecl->getType().getAsStringInternal(Name); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 897 | ResultStr += Name; |
| 898 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 899 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 900 | if (OMD->isVariadic()) |
| 901 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 902 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 903 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 904 | if (FPRetType) { |
| 905 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 906 | |
| 907 | // Now, emit the argument types (if any). |
| 908 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(FPRetType)) { |
| 909 | ResultStr += "("; |
| 910 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 911 | if (i) ResultStr += ", "; |
| 912 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 913 | ResultStr += ParamStr; |
| 914 | } |
| 915 | if (FT->isVariadic()) { |
| 916 | if (FT->getNumArgs()) ResultStr += ", "; |
| 917 | ResultStr += "..."; |
| 918 | } |
| 919 | ResultStr += ")"; |
| 920 | } else { |
| 921 | ResultStr += "()"; |
| 922 | } |
| 923 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 924 | } |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 925 | void RewriteObjC::RewriteImplementationDecl(NamedDecl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 926 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 927 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 928 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 929 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 930 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 931 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 932 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 933 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 934 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 935 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 936 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 937 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 938 | ObjCMethodDecl *OMD = *I; |
| 939 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 940 | SourceLocation LocStart = OMD->getLocStart(); |
| 941 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 942 | |
| 943 | const char *startBuf = SM->getCharacterData(LocStart); |
| 944 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 945 | ReplaceText(LocStart, endBuf-startBuf, |
| 946 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 949 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 950 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 951 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 952 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 953 | ObjCMethodDecl *OMD = *I; |
| 954 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 955 | SourceLocation LocStart = OMD->getLocStart(); |
| 956 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 957 | |
| 958 | const char *startBuf = SM->getCharacterData(LocStart); |
| 959 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 960 | ReplaceText(LocStart, endBuf-startBuf, |
| 961 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 962 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 963 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 964 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 965 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 966 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 969 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 970 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 971 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 972 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 975 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 976 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 977 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 978 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 979 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 980 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 981 | ResultStr += "\n"; |
| 982 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 983 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 984 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 985 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 986 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 987 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 988 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 990 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 991 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 992 | |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 993 | RewriteProperties(ClassDecl->getNumPropertyDecl(), |
| 994 | ClassDecl->getPropertyDecl()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 995 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 996 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 997 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 999 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 1000 | RewriteMethodDeclaration(*I); |
| 1001 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1002 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1003 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1006 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt) { |
| 1007 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1008 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1009 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1010 | ObjCMessageExpr *MsgExpr; |
| 1011 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1012 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1013 | ExprVec.push_back(newStmt); |
| 1014 | |
| 1015 | MsgExpr = new ObjCMessageExpr(PropRefExpr->getBase(), |
| 1016 | PDecl->getSetterName(), PDecl->getType(), |
| 1017 | PDecl->getSetterMethodDecl(), |
| 1018 | SourceLocation(), SourceLocation(), |
| 1019 | &ExprVec[0], 1); |
| 1020 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1021 | |
| 1022 | // Now do the actual rewrite. |
| 1023 | ReplaceStmt(BinOp, ReplacingStmt); |
| 1024 | delete BinOp; |
| 1025 | delete MsgExpr; |
| 1026 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1029 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1030 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1031 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1032 | ObjCMessageExpr *MsgExpr; |
| 1033 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1034 | |
| 1035 | MsgExpr = new ObjCMessageExpr(PropRefExpr->getBase(), |
| 1036 | PDecl->getGetterName(), PDecl->getType(), |
| 1037 | PDecl->getGetterMethodDecl(), |
| 1038 | SourceLocation(), SourceLocation(), |
| 1039 | 0, 0); |
| 1040 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1041 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1042 | |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1043 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1044 | |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1045 | // delete PropRefExpr; elsewhere... |
| 1046 | delete MsgExpr; |
| 1047 | return ReplacingStmt; |
| 1048 | } |
| 1049 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1050 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1051 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1052 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1053 | if (CurMethodDef) { |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1054 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1055 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
| 1056 | // lookup which class implements the instance variable. |
| 1057 | ObjCInterfaceDecl *clsDeclared = 0; |
| 1058 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
| 1059 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1060 | |
| 1061 | // Synthesize an explicit cast to gain access to the ivar. |
| 1062 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1063 | RecName += "_IMPL"; |
| 1064 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1065 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1066 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1067 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1068 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1069 | CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1070 | SourceLocation(), SourceLocation()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1071 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1072 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 1073 | IV->getBase()->getLocEnd(), |
| 1074 | castExpr); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1075 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1076 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1077 | MemberExpr *ME = new MemberExpr(PE, true, D, IV->getLocation(), |
| 1078 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1079 | ReplaceStmt(IV, ME); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1080 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1081 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1082 | } |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1083 | |
| 1084 | ReplaceStmt(IV->getBase(), PE); |
| 1085 | // Cannot delete IV->getBase(), since PE points to it. |
| 1086 | // Replace the old base with the cast. This is important when doing |
| 1087 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1088 | IV->setBase(PE); |
| 1089 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1090 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1091 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1092 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1093 | |
| 1094 | // Explicit ivar refs need to have a cast inserted. |
| 1095 | // FIXME: consider sharing some of this code with the code above. |
| 1096 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1097 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1098 | // lookup which class implements the instance variable. |
| 1099 | ObjCInterfaceDecl *clsDeclared = 0; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1100 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1101 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1102 | |
| 1103 | // Synthesize an explicit cast to gain access to the ivar. |
| 1104 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1105 | RecName += "_IMPL"; |
| 1106 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1107 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1108 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1109 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1110 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1111 | CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1112 | SourceLocation(), SourceLocation()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1113 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1114 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 1115 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1116 | ReplaceStmt(IV->getBase(), PE); |
| 1117 | // Cannot delete IV->getBase(), since PE points to it. |
| 1118 | // Replace the old base with the cast. This is important when doing |
| 1119 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1120 | IV->setBase(PE); |
| 1121 | return IV; |
| 1122 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1123 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1124 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1127 | /// SynthCountByEnumWithState - To print: |
| 1128 | /// ((unsigned int (*) |
| 1129 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1130 | /// (void *)objc_msgSend)((id)l_collection, |
| 1131 | /// sel_registerName( |
| 1132 | /// "countByEnumeratingWithState:objects:count:"), |
| 1133 | /// &enumState, |
| 1134 | /// (id *)items, (unsigned int)16) |
| 1135 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1136 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1137 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1138 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1139 | buf += "\n\t\t"; |
| 1140 | buf += "((id)l_collection,\n\t\t"; |
| 1141 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1142 | buf += "\n\t\t"; |
| 1143 | buf += "&enumState, " |
| 1144 | "(id *)items, (unsigned int)16)"; |
| 1145 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1146 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1147 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1148 | /// statement to exit to its outer synthesized loop. |
| 1149 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1150 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1151 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1152 | return S; |
| 1153 | // replace break with goto __break_label |
| 1154 | std::string buf; |
| 1155 | |
| 1156 | SourceLocation startLoc = S->getLocStart(); |
| 1157 | buf = "goto __break_label_"; |
| 1158 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1159 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1165 | /// statement to continue with its inner synthesized loop. |
| 1166 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1167 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1168 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1169 | return S; |
| 1170 | // replace continue with goto __continue_label |
| 1171 | std::string buf; |
| 1172 | |
| 1173 | SourceLocation startLoc = S->getLocStart(); |
| 1174 | buf = "goto __continue_label_"; |
| 1175 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1176 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1181 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1182 | /// It rewrites: |
| 1183 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1184 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1185 | /// Into: |
| 1186 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1187 | /// type elem; |
| 1188 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1189 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1190 | /// id l_collection = (id)collection; |
| 1191 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1192 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1193 | /// if (limit) { |
| 1194 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1195 | /// do { |
| 1196 | /// unsigned long counter = 0; |
| 1197 | /// do { |
| 1198 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1199 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1200 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1201 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1202 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1203 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1204 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1205 | /// objects:items count:16]); |
| 1206 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1207 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1208 | /// } |
| 1209 | /// else |
| 1210 | /// elem = nil; |
| 1211 | /// } |
| 1212 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1213 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1214 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1215 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1216 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1217 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1218 | assert(!ObjCBcLabelNo.empty() && |
| 1219 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1220 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1221 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1222 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1223 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1224 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1225 | std::string buf; |
| 1226 | buf = "\n{\n\t"; |
| 1227 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1228 | // type elem; |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1229 | ScopedDecl* D = DS->getSolitaryDecl(); |
| 1230 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1231 | elementTypeAsString = ElementType.getAsString(); |
| 1232 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1233 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1234 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1235 | buf += elementName; |
| 1236 | buf += ";\n\t"; |
| 1237 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1238 | else { |
| 1239 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1240 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1241 | elementTypeAsString |
| 1242 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1243 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1244 | |
| 1245 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1246 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1247 | // id items[16]; |
| 1248 | buf += "id items[16];\n\t"; |
| 1249 | // id l_collection = (id) |
| 1250 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1251 | // Find start location of 'collection' the hard way! |
| 1252 | const char *startCollectionBuf = startBuf; |
| 1253 | startCollectionBuf += 3; // skip 'for' |
| 1254 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1255 | startCollectionBuf++; // skip '(' |
| 1256 | // find 'in' and skip it. |
| 1257 | while (*startCollectionBuf != ' ' || |
| 1258 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1259 | (*(startCollectionBuf+3) != ' ' && |
| 1260 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1261 | startCollectionBuf++; |
| 1262 | startCollectionBuf += 3; |
| 1263 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1264 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1265 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1266 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1267 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1268 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1269 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1270 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1271 | buf = ";\n\t"; |
| 1272 | |
| 1273 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1274 | // objects:items count:16]; |
| 1275 | // which is synthesized into: |
| 1276 | // unsigned int limit = |
| 1277 | // ((unsigned int (*) |
| 1278 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1279 | // (void *)objc_msgSend)((id)l_collection, |
| 1280 | // sel_registerName( |
| 1281 | // "countByEnumeratingWithState:objects:count:"), |
| 1282 | // (struct __objcFastEnumerationState *)&state, |
| 1283 | // (id *)items, (unsigned int)16); |
| 1284 | buf += "unsigned long limit =\n\t\t"; |
| 1285 | SynthCountByEnumWithState(buf); |
| 1286 | buf += ";\n\t"; |
| 1287 | /// if (limit) { |
| 1288 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1289 | /// do { |
| 1290 | /// unsigned long counter = 0; |
| 1291 | /// do { |
| 1292 | /// if (startMutations != *enumState.mutationsPtr) |
| 1293 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1294 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1295 | buf += "if (limit) {\n\t"; |
| 1296 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1297 | buf += "do {\n\t\t"; |
| 1298 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1299 | buf += "do {\n\t\t\t"; |
| 1300 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1301 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1302 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1303 | buf += " = ("; |
| 1304 | buf += elementTypeAsString; |
| 1305 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1306 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1307 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1308 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1309 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1310 | /// } while (counter < limit); |
| 1311 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1312 | /// objects:items count:16]); |
| 1313 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1314 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1315 | /// } |
| 1316 | /// else |
| 1317 | /// elem = nil; |
| 1318 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1319 | /// |
| 1320 | buf = ";\n\t"; |
| 1321 | buf += "__continue_label_"; |
| 1322 | buf += utostr(ObjCBcLabelNo.back()); |
| 1323 | buf += ": ;"; |
| 1324 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1325 | buf += "} while (counter < limit);\n\t"; |
| 1326 | buf += "} while (limit = "; |
| 1327 | SynthCountByEnumWithState(buf); |
| 1328 | buf += ");\n\t"; |
| 1329 | buf += elementName; |
| 1330 | buf += " = nil;\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1331 | buf += "__break_label_"; |
| 1332 | buf += utostr(ObjCBcLabelNo.back()); |
| 1333 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1334 | buf += "}\n\t"; |
| 1335 | buf += "else\n\t\t"; |
| 1336 | buf += elementName; |
| 1337 | buf += " = nil;\n"; |
| 1338 | buf += "}\n"; |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1339 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1340 | // Insert all these *after* the statement body. |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1341 | if (isa<CompoundStmt>(S->getBody())) { |
| 1342 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1343 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1344 | } else { |
| 1345 | /* Need to treat single statements specially. For example: |
| 1346 | * |
| 1347 | * for (A *a in b) if (stuff()) break; |
| 1348 | * for (A *a in b) xxxyy; |
| 1349 | * |
| 1350 | * The following code simply scans ahead to the semi to find the actual end. |
| 1351 | */ |
| 1352 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1353 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1354 | assert(semiBuf && "Can't find ';'"); |
| 1355 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1356 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1357 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1358 | Stmts.pop_back(); |
| 1359 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1360 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1363 | /// RewriteObjCSynchronizedStmt - |
| 1364 | /// This routine rewrites @synchronized(expr) stmt; |
| 1365 | /// into: |
| 1366 | /// objc_sync_enter(expr); |
| 1367 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1368 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1369 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1370 | // Get the start location and compute the semi location. |
| 1371 | SourceLocation startLoc = S->getLocStart(); |
| 1372 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1373 | |
| 1374 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1375 | |
| 1376 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1377 | buf = "objc_sync_enter((id)"; |
| 1378 | const char *lparenBuf = startBuf; |
| 1379 | while (*lparenBuf != '(') lparenBuf++; |
| 1380 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1381 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1382 | // the sync expression is typically a message expression that's already |
| 1383 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1384 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1385 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1386 | while (*endBuf != ')') endBuf--; |
| 1387 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1388 | buf = ");\n"; |
| 1389 | // declare a new scope with two variables, _stack and _rethrow. |
| 1390 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1391 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1392 | buf += "char *pointers[4];} _stack;\n"; |
| 1393 | buf += "id volatile _rethrow = 0;\n"; |
| 1394 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1395 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1396 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1397 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1398 | startBuf = SM->getCharacterData(startLoc); |
| 1399 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1400 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1401 | SourceLocation lastCurlyLoc = startLoc; |
| 1402 | buf = "}\nelse {\n"; |
| 1403 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1404 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1405 | buf += " objc_sync_exit("; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1406 | Expr *syncExpr = new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1407 | S->getSynchExpr(), |
| 1408 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1409 | SourceLocation(), SourceLocation()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1410 | std::string syncExprBufS; |
| 1411 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1412 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1413 | buf += syncExprBuf.str(); |
| 1414 | buf += ");\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1415 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1416 | buf += "}\n"; |
| 1417 | buf += "}"; |
| 1418 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1419 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1423 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1424 | // Get the start location and compute the semi location. |
| 1425 | SourceLocation startLoc = S->getLocStart(); |
| 1426 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1427 | |
| 1428 | assert((*startBuf == '@') && "bogus @try location"); |
| 1429 | |
| 1430 | std::string buf; |
| 1431 | // declare a new scope with two variables, _stack and _rethrow. |
| 1432 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1433 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1434 | buf += "char *pointers[4];} _stack;\n"; |
| 1435 | buf += "id volatile _rethrow = 0;\n"; |
| 1436 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1437 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1439 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1440 | |
| 1441 | startLoc = S->getTryBody()->getLocEnd(); |
| 1442 | startBuf = SM->getCharacterData(startLoc); |
| 1443 | |
| 1444 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1445 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1446 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1447 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1448 | if (catchList) { |
| 1449 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1450 | buf = " /* @catch begin */ else {\n"; |
| 1451 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1452 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1453 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1454 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1455 | buf += " else { /* @catch continue */"; |
| 1456 | |
| 1457 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1458 | } else { /* no catch list */ |
| 1459 | buf = "}\nelse {\n"; |
| 1460 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1461 | buf += "}"; |
| 1462 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1463 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1464 | bool sawIdTypedCatch = false; |
| 1465 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1466 | while (catchList) { |
| 1467 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1468 | |
| 1469 | if (catchList == S->getCatchStmts()) |
| 1470 | buf = "if ("; // we are generating code for the first catch clause |
| 1471 | else |
| 1472 | buf = "else if ("; |
| 1473 | startLoc = catchList->getLocStart(); |
| 1474 | startBuf = SM->getCharacterData(startLoc); |
| 1475 | |
| 1476 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1477 | |
| 1478 | const char *lParenLoc = strchr(startBuf, '('); |
| 1479 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1480 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1481 | // Now rewrite the body... |
| 1482 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1483 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1484 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1485 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1486 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1487 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1488 | |
| 1489 | buf += "1) { id _tmp = _caught;"; |
| 1490 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1491 | buf.c_str(), buf.size()); |
| 1492 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Ted Kremenek | 50a25e2 | 2008-10-06 22:39:38 +0000 | [diff] [blame] | 1493 | QualType t = dyn_cast<ValueDecl>(declStmt->getSolitaryDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1494 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1495 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1496 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1497 | sawIdTypedCatch = true; |
| 1498 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1499 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1500 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1501 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1502 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1503 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1504 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1505 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1506 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | // Now rewrite the body... |
| 1510 | lastCatchBody = catchList->getCatchBody(); |
| 1511 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1512 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1513 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1514 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1515 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1516 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1517 | |
| 1518 | buf = " = _caught;"; |
| 1519 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1520 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1521 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1522 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1523 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1524 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1525 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1526 | catchList = catchList->getNextCatchStmt(); |
| 1527 | } |
| 1528 | // Complete the catch list... |
| 1529 | if (lastCatchBody) { |
| 1530 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1531 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1532 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1533 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1534 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1535 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1536 | buf = "} /* last catch end */\n"; |
| 1537 | buf += "else {\n"; |
| 1538 | buf += " _rethrow = _caught;\n"; |
| 1539 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1540 | buf += "} } /* @catch end */\n"; |
| 1541 | if (!S->getFinallyStmt()) |
| 1542 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1543 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Set lastCurlyLoc |
| 1546 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1547 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1548 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1549 | startLoc = finalStmt->getLocStart(); |
| 1550 | startBuf = SM->getCharacterData(startLoc); |
| 1551 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1552 | |
| 1553 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1554 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1555 | |
| 1556 | Stmt *body = finalStmt->getFinallyBody(); |
| 1557 | SourceLocation startLoc = body->getLocStart(); |
| 1558 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1559 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1560 | "bogus @finally body location"); |
| 1561 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1562 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1563 | |
| 1564 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1565 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1566 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1567 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1568 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1569 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Set lastCurlyLoc |
| 1572 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1573 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1574 | buf = "{ /* implicit finally clause */\n"; |
| 1575 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1576 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1577 | buf += "}"; |
| 1578 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1579 | } |
| 1580 | // Now emit the final closing curly brace... |
| 1581 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1582 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1583 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1584 | return 0; |
| 1585 | } |
| 1586 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1587 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1588 | return 0; |
| 1589 | } |
| 1590 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1591 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1592 | return 0; |
| 1593 | } |
| 1594 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1595 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1596 | // the throw expression is typically a message expression that's already |
| 1597 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1598 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1599 | // Get the start location and compute the semi location. |
| 1600 | SourceLocation startLoc = S->getLocStart(); |
| 1601 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1602 | |
| 1603 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1604 | |
| 1605 | std::string buf; |
| 1606 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1607 | if (S->getThrowExpr()) |
| 1608 | buf = "objc_exception_throw("; |
| 1609 | else // add an implicit argument |
| 1610 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1611 | |
| 1612 | // handle "@ throw" correctly. |
| 1613 | const char *wBuf = strchr(startBuf, 'w'); |
| 1614 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1615 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1616 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1617 | const char *semiBuf = strchr(startBuf, ';'); |
| 1618 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1619 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1620 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1621 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1624 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1625 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1626 | // Create a new string expression. |
| 1627 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1628 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1629 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1630 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 1631 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1632 | SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1633 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1635 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1636 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1637 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1640 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1641 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1642 | // Create a call to sel_registerName("selName"). |
| 1643 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1644 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1645 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getAsString().c_str(), |
| 1646 | Exp->getSelector().getAsString().size(), |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1647 | false, argType, SourceLocation(), |
| 1648 | SourceLocation())); |
| 1649 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1650 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1651 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1652 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1653 | return SelExp; |
| 1654 | } |
| 1655 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1656 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1657 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1658 | // 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] | 1659 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1660 | |
| 1661 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1662 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1663 | |
| 1664 | // 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] | 1665 | QualType pToFunc = Context->getPointerType(msgSendType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1666 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE, |
| 1667 | /*isLvalue=*/false); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1668 | |
| 1669 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1670 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1671 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 1672 | } |
| 1673 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1674 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1675 | const char *&startRef, const char *&endRef) { |
| 1676 | while (startBuf < endBuf) { |
| 1677 | if (*startBuf == '<') |
| 1678 | startRef = startBuf; // mark the start. |
| 1679 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1680 | if (startRef && *startRef == '<') { |
| 1681 | endRef = startBuf; // mark the end. |
| 1682 | return true; |
| 1683 | } |
| 1684 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1685 | } |
| 1686 | startBuf++; |
| 1687 | } |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1691 | static void scanToNextArgument(const char *&argRef) { |
| 1692 | int angle = 0; |
| 1693 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1694 | if (*argRef == '<') |
| 1695 | angle++; |
| 1696 | else if (*argRef == '>') |
| 1697 | angle--; |
| 1698 | argRef++; |
| 1699 | } |
| 1700 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1701 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1702 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1703 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1704 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1705 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1706 | return true; |
| 1707 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1708 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1709 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1710 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1711 | return true; // we have "Class <Protocol> *". |
| 1712 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1713 | return false; |
| 1714 | } |
| 1715 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1716 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1717 | QualType Type = E->getType(); |
| 1718 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1719 | SourceLocation Loc, EndLoc; |
| 1720 | |
| 1721 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1722 | Loc = ECE->getLParenLoc(); |
| 1723 | EndLoc = ECE->getRParenLoc(); |
| 1724 | } else { |
| 1725 | Loc = E->getLocStart(); |
| 1726 | EndLoc = E->getLocEnd(); |
| 1727 | } |
| 1728 | // This will defend against trying to rewrite synthesized expressions. |
| 1729 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1730 | return; |
| 1731 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1732 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1733 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1734 | const char *startRef = 0, *endRef = 0; |
| 1735 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1736 | // Get the locations of the startRef, endRef. |
| 1737 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1738 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1739 | // Comment out the protocol references. |
| 1740 | InsertText(LessLoc, "/*", 2); |
| 1741 | InsertText(GreaterLoc, "*/", 2); |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1746 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1747 | SourceLocation Loc; |
| 1748 | QualType Type; |
| 1749 | const FunctionTypeProto *proto = 0; |
| 1750 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1751 | Loc = VD->getLocation(); |
| 1752 | Type = VD->getType(); |
| 1753 | } |
| 1754 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1755 | Loc = FD->getLocation(); |
| 1756 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1757 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1758 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1759 | assert(funcType && "missing function type"); |
| 1760 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1761 | if (!proto) |
| 1762 | return; |
| 1763 | Type = proto->getResultType(); |
| 1764 | } |
| 1765 | else |
| 1766 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1767 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1768 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1769 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1770 | |
| 1771 | const char *endBuf = SM->getCharacterData(Loc); |
| 1772 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1773 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1774 | startBuf--; // scan backward (from the decl location) for return type. |
| 1775 | const char *startRef = 0, *endRef = 0; |
| 1776 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1777 | // Get the locations of the startRef, endRef. |
| 1778 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1779 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1780 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1781 | InsertText(LessLoc, "/*", 2); |
| 1782 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1785 | if (!proto) |
| 1786 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1787 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1788 | const char *startBuf = SM->getCharacterData(Loc); |
| 1789 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1790 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1791 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1792 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1793 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1794 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1795 | // scan forward (from the decl location) for argument types. |
| 1796 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1797 | const char *startRef = 0, *endRef = 0; |
| 1798 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1799 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1800 | SourceLocation LessLoc = |
| 1801 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1802 | SourceLocation GreaterLoc = |
| 1803 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1804 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1805 | InsertText(LessLoc, "/*", 2); |
| 1806 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1807 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1808 | startBuf = ++endBuf; |
| 1809 | } |
| 1810 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1811 | // If the function name is derived from a macro expansion, then the |
| 1812 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1813 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1814 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1815 | startBuf++; |
| 1816 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1817 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1820 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1821 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1822 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1823 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1824 | ArgTys.push_back(Context->getPointerType( |
| 1825 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1826 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1827 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1828 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1829 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1830 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1831 | SelGetUidIdent, getFuncType, |
| 1832 | FunctionDecl::Extern, false, 0); |
| 1833 | } |
| 1834 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1835 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1836 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1837 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1838 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1839 | ArgTys.push_back(Context->getPointerType( |
| 1840 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1841 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1842 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1843 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1844 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1845 | SourceLocation(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1846 | SelGetProtoIdent, getFuncType, |
| 1847 | FunctionDecl::Extern, false, 0); |
| 1848 | } |
| 1849 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1850 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1851 | // declared in <objc/objc.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1852 | if (strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1853 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1854 | return; |
| 1855 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1856 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1859 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1860 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1861 | if (SuperContructorFunctionDecl) |
| 1862 | return; |
| 1863 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super"); |
| 1864 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1865 | QualType argT = Context->getObjCIdType(); |
| 1866 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1867 | ArgTys.push_back(argT); |
| 1868 | ArgTys.push_back(argT); |
| 1869 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1870 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1871 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1872 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1873 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1874 | msgSendIdent, msgSendType, |
| 1875 | FunctionDecl::Extern, false, 0); |
| 1876 | } |
| 1877 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1878 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1879 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1880 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1881 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1882 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1883 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1884 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1885 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1886 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1887 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1888 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1889 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1890 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1891 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1892 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1893 | msgSendIdent, msgSendType, |
| 1894 | FunctionDecl::Extern, false, 0); |
| 1895 | } |
| 1896 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1897 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1898 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1899 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1900 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1901 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1902 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1903 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1904 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1905 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1906 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1907 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1908 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1909 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1910 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1911 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1912 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1913 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1914 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1915 | msgSendIdent, msgSendType, |
| 1916 | FunctionDecl::Extern, false, 0); |
| 1917 | } |
| 1918 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1919 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1920 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1921 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 1922 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1923 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1924 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1925 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1926 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1927 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1928 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1929 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1930 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1931 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1932 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1933 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1934 | msgSendIdent, msgSendType, |
| 1935 | FunctionDecl::Extern, false, 0); |
| 1936 | } |
| 1937 | |
| 1938 | // SynthMsgSendSuperStretFunctionDecl - |
| 1939 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1940 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1941 | IdentifierInfo *msgSendIdent = |
| 1942 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 1943 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1944 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1945 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1946 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1947 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1948 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1949 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1950 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1951 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1952 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1953 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1954 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1955 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1956 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 1957 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1958 | msgSendIdent, msgSendType, |
| 1959 | FunctionDecl::Extern, false, 0); |
| 1960 | } |
| 1961 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 1962 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1963 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1964 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 1965 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1966 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1967 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1968 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1969 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1970 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1971 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 1972 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1973 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1974 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1975 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1976 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1977 | msgSendIdent, msgSendType, |
| 1978 | FunctionDecl::Extern, false, 0); |
| 1979 | } |
| 1980 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1981 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1982 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1983 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 1984 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1985 | ArgTys.push_back(Context->getPointerType( |
| 1986 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1987 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1988 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1989 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1990 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1991 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1992 | getClassIdent, getClassType, |
| 1993 | FunctionDecl::Extern, false, 0); |
| 1994 | } |
| 1995 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1996 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1997 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1998 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 1999 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2000 | ArgTys.push_back(Context->getPointerType( |
| 2001 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2002 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2003 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2004 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2005 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2006 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2007 | getClassIdent, getClassType, |
| 2008 | FunctionDecl::Extern, false, 0); |
| 2009 | } |
| 2010 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2011 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2012 | QualType strType = getConstantStringStructType(); |
| 2013 | |
| 2014 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2015 | |
| 2016 | std::string tmpName = InFileName; |
| 2017 | unsigned i; |
| 2018 | for (i=0; i < tmpName.length(); i++) { |
| 2019 | char c = tmpName.at(i); |
| 2020 | // replace any non alphanumeric characters with '_'. |
| 2021 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2022 | tmpName[i] = '_'; |
| 2023 | } |
| 2024 | S += tmpName; |
| 2025 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2026 | S += utostr(NumObjCStringLiterals++); |
| 2027 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2028 | Preamble += "static __NSConstantStringImpl " + S; |
| 2029 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2030 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2031 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2032 | std::string prettyBufS; |
| 2033 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2034 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2035 | Preamble += prettyBuf.str(); |
| 2036 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2037 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2038 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2039 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2040 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2041 | &Context->Idents.get(S.c_str()), strType, |
| 2042 | VarDecl::Static, NULL); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2043 | DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2044 | Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2045 | Context->getPointerType(DRE->getType()), |
| 2046 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2047 | // cast to NSConstantString * |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2048 | CastExpr *cast = new CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2049 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2050 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2051 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2052 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2055 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2056 | // check if we are sending a message to 'super' |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2057 | if (!CurMethodDef || !CurMethodDef->isInstance()) return 0; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2059 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2060 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2061 | assert(PT); |
| 2062 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2063 | return IT->getDecl(); |
| 2064 | } |
| 2065 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2069 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2070 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2071 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2072 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2073 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2074 | QualType FieldTypes[2]; |
| 2075 | |
| 2076 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2077 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2078 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2079 | FieldTypes[1] = Context->getObjCClassType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2080 | // Create fields |
| 2081 | FieldDecl *FieldDecls[2]; |
| 2082 | |
| 2083 | for (unsigned i = 0; i < 2; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2084 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 2085 | FieldTypes[i]); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2086 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2087 | SuperStructDecl->defineBody(*Context, FieldDecls, 4); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2088 | } |
| 2089 | return Context->getTagDeclType(SuperStructDecl); |
| 2090 | } |
| 2091 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2092 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2093 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2094 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2095 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2096 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2097 | QualType FieldTypes[4]; |
| 2098 | |
| 2099 | // struct objc_object *receiver; |
| 2100 | FieldTypes[0] = Context->getObjCIdType(); |
| 2101 | // int flags; |
| 2102 | FieldTypes[1] = Context->IntTy; |
| 2103 | // char *str; |
| 2104 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2105 | // long length; |
| 2106 | FieldTypes[3] = Context->LongTy; |
| 2107 | // Create fields |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 2108 | FieldDecl *FieldDecls[4]; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2109 | |
| 2110 | for (unsigned i = 0; i < 4; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2111 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 2112 | FieldTypes[i]); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2113 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2114 | ConstantStringDecl->defineBody(*Context, FieldDecls, 4); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2115 | } |
| 2116 | return Context->getTagDeclType(ConstantStringDecl); |
| 2117 | } |
| 2118 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2119 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2120 | if (!SelGetUidFunctionDecl) |
| 2121 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2122 | if (!MsgSendFunctionDecl) |
| 2123 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2124 | if (!MsgSendSuperFunctionDecl) |
| 2125 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2126 | if (!MsgSendStretFunctionDecl) |
| 2127 | SynthMsgSendStretFunctionDecl(); |
| 2128 | if (!MsgSendSuperStretFunctionDecl) |
| 2129 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2130 | if (!MsgSendFpretFunctionDecl) |
| 2131 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2132 | if (!GetClassFunctionDecl) |
| 2133 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2134 | if (!GetMetaClassFunctionDecl) |
| 2135 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2136 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2137 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2138 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2139 | // May need to use objc_msgSend_stret() as well. |
| 2140 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2141 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2142 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2143 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2144 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2145 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2146 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2147 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2148 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2149 | // Synthesize a call to objc_msgSend(). |
| 2150 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2151 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2152 | |
| 2153 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2154 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2155 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2156 | // the 'super' idiom within a class method. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2157 | if (!strcmp(clsName->getName(), "super")) { |
| 2158 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2159 | if (MsgSendStretFlavor) |
| 2160 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2161 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2162 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2163 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2164 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2165 | |
| 2166 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2167 | |
| 2168 | // set the receiver to self, the first argument to all methods. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2169 | InitExprs.push_back(new DeclRefExpr( |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2170 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2171 | Context->getObjCIdType(), |
| 2172 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2173 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2174 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2175 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2176 | SuperDecl->getIdentifier()->getLength(), |
| 2177 | false, argType, SourceLocation(), |
| 2178 | SourceLocation())); |
| 2179 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2180 | &ClsExprs[0], |
| 2181 | ClsExprs.size()); |
| 2182 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2183 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2184 | new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2185 | Cls, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2186 | SourceLocation(), SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2187 | // struct objc_super |
| 2188 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2189 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2190 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2191 | if (LangOpts.Microsoft) { |
| 2192 | SynthSuperContructorFunctionDecl(); |
| 2193 | // Simulate a contructor call... |
| 2194 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2195 | superType, SourceLocation()); |
| 2196 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2197 | superType, SourceLocation()); |
| 2198 | } else { |
| 2199 | // (struct objc_super) { <exprs from above> } |
| 2200 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2201 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2202 | SourceLocation(), false); |
| 2203 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, |
| 2204 | false); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2205 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2206 | // struct objc_super * |
| 2207 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2208 | Context->getPointerType(SuperRep->getType()), |
| 2209 | SourceLocation()); |
| 2210 | MsgExprs.push_back(Unop); |
| 2211 | } else { |
| 2212 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2213 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2214 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 2215 | clsName->getLength(), |
| 2216 | false, argType, SourceLocation(), |
| 2217 | SourceLocation())); |
| 2218 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2219 | &ClsExprs[0], |
| 2220 | ClsExprs.size()); |
| 2221 | MsgExprs.push_back(Cls); |
| 2222 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2223 | } else { // instance message. |
| 2224 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2225 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2226 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2227 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2228 | if (MsgSendStretFlavor) |
| 2229 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2230 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2231 | |
| 2232 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2233 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2234 | InitExprs.push_back( |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2235 | new CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2236 | new DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2237 | Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2238 | SourceLocation()), |
| 2239 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2240 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2241 | |
| 2242 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2243 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2244 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2245 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2246 | false, argType, SourceLocation(), |
| 2247 | SourceLocation())); |
| 2248 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2249 | &ClsExprs[0], |
| 2250 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2251 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2252 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2253 | // set 'super class', using objc_getClass(). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2254 | new CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2255 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2256 | // struct objc_super |
| 2257 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2258 | Expr *SuperRep; |
| 2259 | |
| 2260 | if (LangOpts.Microsoft) { |
| 2261 | SynthSuperContructorFunctionDecl(); |
| 2262 | // Simulate a contructor call... |
| 2263 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2264 | superType, SourceLocation()); |
| 2265 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2266 | superType, SourceLocation()); |
| 2267 | } else { |
| 2268 | // (struct objc_super) { <exprs from above> } |
| 2269 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2270 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2271 | SourceLocation(), false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2272 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 2273 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2274 | // struct objc_super * |
| 2275 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2276 | Context->getPointerType(SuperRep->getType()), |
| 2277 | SourceLocation()); |
| 2278 | MsgExprs.push_back(Unop); |
| 2279 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2280 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2281 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2282 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2283 | recExpr = CE->getSubExpr(); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2284 | recExpr = new CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2285 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2286 | SourceLocation(), SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2287 | MsgExprs.push_back(recExpr); |
| 2288 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2289 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2290 | // 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] | 2291 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2292 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2293 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getAsString().c_str(), |
| 2294 | Exp->getSelector().getAsString().size(), |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2295 | false, argType, SourceLocation(), |
| 2296 | SourceLocation())); |
| 2297 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2298 | &SelExprs[0], SelExprs.size()); |
| 2299 | MsgExprs.push_back(SelExp); |
| 2300 | |
| 2301 | // Now push any user supplied arguments. |
| 2302 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2303 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2304 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2305 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2306 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2307 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2308 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2309 | : ICE->getType(); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2310 | userExpr = new CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2311 | } |
| 2312 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2313 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2314 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2315 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2316 | userExpr = CE->getSubExpr(); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2317 | userExpr = new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2318 | userExpr, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2319 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2320 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2321 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2322 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2323 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2324 | // the original expression, since we will delete it below. |
| 2325 | Exp->setArg(i, 0); |
| 2326 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2327 | // Generate the funky cast. |
| 2328 | CastExpr *cast; |
| 2329 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2330 | QualType returnType; |
| 2331 | |
| 2332 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2333 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2334 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2335 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2336 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2337 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2338 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2339 | // Push any user argument types. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 2340 | for (unsigned i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2341 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2342 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2343 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2344 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 2345 | if (isBlockPointerType(t)) { |
| 2346 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2347 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2348 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2349 | ArgTypes.push_back(t); |
| 2350 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2351 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2352 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2353 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2354 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2355 | } |
| 2356 | // 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] | 2357 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2358 | |
| 2359 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2360 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 2361 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2362 | |
| 2363 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2364 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2365 | // xx.m:13: warning: function called through a non-compatible type |
| 2366 | // xx.m:13: note: if this code is reached, the program will abort |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2367 | cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2368 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2369 | SourceLocation(), SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2370 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2371 | // Now do the "normal" pointer to function cast. |
| 2372 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2373 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2374 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2375 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2376 | castType = Context->getPointerType(castType); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2377 | cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2378 | |
| 2379 | // Don't forget the parens to enforce the proper binding. |
| 2380 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2381 | |
| 2382 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 2383 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2384 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2385 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2386 | if (MsgSendStretFlavor) { |
| 2387 | // We have the method which returns a struct/union. Must also generate |
| 2388 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2389 | // expression which dictate which one to envoke depending on size of |
| 2390 | // method's return type. |
| 2391 | |
| 2392 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2393 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 2394 | SourceLocation()); |
| 2395 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2396 | cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2397 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2398 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2399 | // Now do the "normal" pointer to function cast. |
| 2400 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2401 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2402 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2403 | castType = Context->getPointerType(castType); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2404 | cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2405 | |
| 2406 | // Don't forget the parens to enforce the proper binding. |
| 2407 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2408 | |
| 2409 | FT = msgSendType->getAsFunctionType(); |
| 2410 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2411 | FT->getResultType(), SourceLocation()); |
| 2412 | |
| 2413 | // Build sizeof(returnType) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2414 | SizeOfAlignOfExpr *sizeofExpr = new SizeOfAlignOfExpr(true, true, |
| 2415 | returnType.getAsOpaquePtr(), |
| 2416 | Context->getSizeType(), |
| 2417 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2418 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2419 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2420 | // For X86 it is more complicated and some kind of target specific routine |
| 2421 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2422 | unsigned IntSize = |
| 2423 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2424 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 2425 | Context->IntTy, |
| 2426 | SourceLocation()); |
| 2427 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 2428 | BinaryOperator::LE, |
| 2429 | Context->IntTy, |
| 2430 | SourceLocation()); |
| 2431 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2432 | ConditionalOperator *CondExpr = |
| 2433 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2434 | ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2435 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2436 | return ReplacingStmt; |
| 2437 | } |
| 2438 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2439 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2440 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 80c2855 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2441 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2442 | //ReplacingStmt->dump(); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2443 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2444 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2445 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2446 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2447 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2450 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2451 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2452 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2453 | if (!GetProtocolFunctionDecl) |
| 2454 | SynthGetProtocolFunctionDecl(); |
| 2455 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2456 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2457 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2458 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getNameAsCString(), |
| 2459 | strlen(Exp->getProtocol()->getNameAsCString()), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2460 | false, argType, SourceLocation(), |
| 2461 | SourceLocation())); |
| 2462 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2463 | &ProtoExprs[0], |
| 2464 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2465 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2466 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2467 | return ProtoExp; |
| 2468 | |
| 2469 | } |
| 2470 | |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2471 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2472 | const char *endBuf) { |
| 2473 | while (startBuf < endBuf) { |
| 2474 | if (*startBuf == '#') { |
| 2475 | // Skip whitespace. |
| 2476 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2477 | ; |
| 2478 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2479 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2480 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2481 | !strncmp(startBuf, "define", strlen("define")) || |
| 2482 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2483 | !strncmp(startBuf, "else", strlen("else")) || |
| 2484 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2485 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2486 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2487 | !strncmp(startBuf, "include", strlen("include")) || |
| 2488 | !strncmp(startBuf, "import", strlen("import")) || |
| 2489 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2490 | return true; |
| 2491 | } |
| 2492 | startBuf++; |
| 2493 | } |
| 2494 | return false; |
| 2495 | } |
| 2496 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2497 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2498 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2499 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2500 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2501 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2502 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2503 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2504 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2505 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2506 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2507 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2508 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2509 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2510 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2511 | |
| 2512 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2513 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2514 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2515 | // If no ivars and no root or if its root, directly or indirectly, |
| 2516 | // 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] | 2517 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2518 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2519 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2520 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2525 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2526 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2527 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2528 | if (LangOpts.Microsoft) |
| 2529 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2530 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2531 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2532 | const char *cursor = strchr(startBuf, '{'); |
| 2533 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2534 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2535 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2536 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2537 | // NSURL.h, for example): |
| 2538 | // |
| 2539 | // #ifdef XYZ |
| 2540 | // @interface Foo : NSObject |
| 2541 | // #else |
| 2542 | // @interface FooBar : NSObject |
| 2543 | // #endif |
| 2544 | // { |
| 2545 | // int i; |
| 2546 | // } |
| 2547 | // @end |
| 2548 | // |
| 2549 | // This clause is segregated to avoid breaking the common case. |
| 2550 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2551 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2552 | CDecl->getClassLoc(); |
| 2553 | const char *endHeader = SM->getCharacterData(L); |
| 2554 | endHeader += Lexer::MeasureTokenLength(L, *SM); |
| 2555 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 2556 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2557 | // advance to the end of the referenced protocols. |
| 2558 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2559 | endHeader++; |
| 2560 | } |
| 2561 | // rewrite the original header |
| 2562 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2563 | } else { |
| 2564 | // rewrite the original header *without* disturbing the '{' |
| 2565 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2566 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2567 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2568 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2569 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2570 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2571 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2572 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2573 | |
| 2574 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2575 | SourceLocation OnePastCurly = |
| 2576 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2577 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2578 | } |
| 2579 | cursor++; // past '{' |
| 2580 | |
| 2581 | // Now comment out any visibility specifiers. |
| 2582 | while (cursor < endBuf) { |
| 2583 | if (*cursor == '@') { |
| 2584 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2585 | // Skip whitespace. |
| 2586 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2587 | /*scan*/; |
| 2588 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2589 | // FIXME: presence of @public, etc. inside comment results in |
| 2590 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2591 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2592 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2593 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2594 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2595 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2596 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2597 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2598 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2599 | // for type checking. |
| 2600 | else if (*cursor == '<') { |
| 2601 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2602 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2603 | cursor = strchr(cursor, '>'); |
| 2604 | cursor++; |
| 2605 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2606 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2607 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2608 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2609 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2610 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2611 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2612 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2613 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2614 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2615 | } else { // we don't have any instance variables - insert super struct. |
| 2616 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2617 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2618 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2619 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2620 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2621 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2622 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2623 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2624 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2625 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2626 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2629 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2630 | /// class methods. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2631 | void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2632 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2633 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2634 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2635 | const char *ClassName, |
| 2636 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2637 | if (MethodBegin == MethodEnd) return; |
| 2638 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2639 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2640 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2641 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2642 | SEL _cmd; |
| 2643 | char *method_types; |
| 2644 | void *_imp; |
| 2645 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2646 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2647 | Result += "\nstruct _objc_method {\n"; |
| 2648 | Result += "\tSEL _cmd;\n"; |
| 2649 | Result += "\tchar *method_types;\n"; |
| 2650 | Result += "\tvoid *_imp;\n"; |
| 2651 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2652 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2653 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2654 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2655 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2656 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2657 | |
| 2658 | /* struct { |
| 2659 | struct _objc_method_list *next_method; |
| 2660 | int method_count; |
| 2661 | struct _objc_method method_list[]; |
| 2662 | } |
| 2663 | */ |
| 2664 | Result += "\nstatic struct {\n"; |
| 2665 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2666 | Result += "\tint method_count;\n"; |
| 2667 | Result += "\tstruct _objc_method method_list["; |
| 2668 | Result += utostr(MethodEnd-MethodBegin); |
| 2669 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2670 | Result += prefix; |
| 2671 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2672 | Result += "_METHODS_"; |
| 2673 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2674 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2675 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2676 | Result += "_meth\")))= "; |
| 2677 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2679 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2680 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2681 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2682 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2683 | Result += "\", \""; |
| 2684 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2685 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2686 | Result += MethodInternalNames[*MethodBegin]; |
| 2687 | Result += "}\n"; |
| 2688 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2689 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2690 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2691 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2692 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2693 | Result += "\", \""; |
| 2694 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2695 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2696 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2697 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2698 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2699 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2702 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2703 | void RewriteObjC:: |
| 2704 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2705 | const char *prefix, |
| 2706 | const char *ClassName, |
| 2707 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2708 | static bool objc_protocol_methods = false; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2709 | if (Protocols.empty()) return; |
| 2710 | |
| 2711 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2712 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2713 | // Output struct protocol_methods holder of method selector and type. |
| 2714 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2715 | /* struct protocol_methods { |
| 2716 | SEL _cmd; |
| 2717 | char *method_types; |
| 2718 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2719 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2720 | Result += "\nstruct protocol_methods {\n"; |
| 2721 | Result += "\tSEL _cmd;\n"; |
| 2722 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2723 | Result += "};\n"; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2724 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2725 | objc_protocol_methods = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2726 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2727 | // Do not synthesize the protocol more than once. |
| 2728 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2729 | continue; |
| 2730 | |
| 2731 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2732 | unsigned NumMethods = PDecl->getNumInstanceMethods(); |
| 2733 | /* struct _objc_protocol_method_list { |
| 2734 | int protocol_method_count; |
| 2735 | struct protocol_methods protocols[]; |
| 2736 | } |
| 2737 | */ |
| 2738 | Result += "\nstatic struct {\n"; |
| 2739 | Result += "\tint protocol_method_count;\n"; |
| 2740 | Result += "\tstruct protocol_methods protocols["; |
| 2741 | Result += utostr(NumMethods); |
| 2742 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2743 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2744 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2745 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2746 | |
| 2747 | // Output instance methods declared in this protocol. |
| 2748 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
| 2749 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2750 | if (I == PDecl->instmeth_begin()) |
| 2751 | Result += "\t ,{{(SEL)\""; |
| 2752 | else |
| 2753 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2754 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2755 | std::string MethodTypeString; |
| 2756 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2757 | Result += "\", \""; |
| 2758 | Result += MethodTypeString; |
| 2759 | Result += "\"}\n"; |
| 2760 | } |
| 2761 | Result += "\t }\n};\n"; |
| 2762 | } |
| 2763 | |
| 2764 | // Output class methods declared in this protocol. |
| 2765 | int NumMethods = PDecl->getNumClassMethods(); |
| 2766 | if (NumMethods > 0) { |
| 2767 | /* struct _objc_protocol_method_list { |
| 2768 | int protocol_method_count; |
| 2769 | struct protocol_methods protocols[]; |
| 2770 | } |
| 2771 | */ |
| 2772 | Result += "\nstatic struct {\n"; |
| 2773 | Result += "\tint protocol_method_count;\n"; |
| 2774 | Result += "\tstruct protocol_methods protocols["; |
| 2775 | Result += utostr(NumMethods); |
| 2776 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2777 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2778 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2779 | "{\n\t"; |
| 2780 | Result += utostr(NumMethods); |
| 2781 | Result += "\n"; |
| 2782 | |
| 2783 | // Output instance methods declared in this protocol. |
| 2784 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
| 2785 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2786 | if (I == PDecl->classmeth_begin()) |
| 2787 | Result += "\t ,{{(SEL)\""; |
| 2788 | else |
| 2789 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2790 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2791 | std::string MethodTypeString; |
| 2792 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2793 | Result += "\", \""; |
| 2794 | Result += MethodTypeString; |
| 2795 | Result += "\"}\n"; |
| 2796 | } |
| 2797 | Result += "\t }\n};\n"; |
| 2798 | } |
| 2799 | |
| 2800 | // Output: |
| 2801 | /* struct _objc_protocol { |
| 2802 | // Objective-C 1.0 extensions |
| 2803 | struct _objc_protocol_extension *isa; |
| 2804 | char *protocol_name; |
| 2805 | struct _objc_protocol **protocol_list; |
| 2806 | struct _objc_protocol_method_list *instance_methods; |
| 2807 | struct _objc_protocol_method_list *class_methods; |
| 2808 | }; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2809 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2810 | static bool objc_protocol = false; |
| 2811 | if (!objc_protocol) { |
| 2812 | Result += "\nstruct _objc_protocol {\n"; |
| 2813 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2814 | Result += "\tchar *protocol_name;\n"; |
| 2815 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2816 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2817 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2818 | Result += "};\n"; |
| 2819 | |
| 2820 | objc_protocol = true; |
| 2821 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2822 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2823 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2824 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2825 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2826 | "{\n\t0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2827 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2828 | Result += "\", 0, "; |
| 2829 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2830 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2831 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2832 | Result += ", "; |
| 2833 | } |
| 2834 | else |
| 2835 | Result += "0, "; |
| 2836 | if (PDecl->getNumClassMethods() > 0) { |
| 2837 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2838 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2839 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2840 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2841 | else |
| 2842 | Result += "0\n"; |
| 2843 | Result += "};\n"; |
| 2844 | |
| 2845 | // Mark this protocol as having been generated. |
| 2846 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 2847 | assert(false && "protocol already synthesized"); |
| 2848 | } |
| 2849 | // Output the top lovel protocol meta-data for the class. |
| 2850 | /* struct _objc_protocol_list { |
| 2851 | struct _objc_protocol_list *next; |
| 2852 | int protocol_count; |
| 2853 | struct _objc_protocol *class_protocols[]; |
| 2854 | } |
| 2855 | */ |
| 2856 | Result += "\nstatic struct {\n"; |
| 2857 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2858 | Result += "\tint protocol_count;\n"; |
| 2859 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 2860 | Result += utostr(Protocols.size()); |
| 2861 | Result += "];\n} _OBJC_"; |
| 2862 | Result += prefix; |
| 2863 | Result += "_PROTOCOLS_"; |
| 2864 | Result += ClassName; |
| 2865 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2866 | "{\n\t0, "; |
| 2867 | Result += utostr(Protocols.size()); |
| 2868 | Result += "\n"; |
| 2869 | |
| 2870 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2871 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2872 | Result += " \n"; |
| 2873 | |
| 2874 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 2875 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2876 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2877 | Result += "\n"; |
| 2878 | } |
| 2879 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2882 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2883 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2884 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2885 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2886 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2887 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2888 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2889 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2890 | CDecl = CDecl->getNextClassCategory()) |
| 2891 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2892 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2894 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2895 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2896 | FullCategoryName += IDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2897 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2898 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2899 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2900 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 2901 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2902 | |
| 2903 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2904 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2905 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 2906 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2907 | |
| 2908 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2909 | // Null CDecl is case of a category implementation with no category interface |
| 2910 | if (CDecl) |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2911 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2912 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2913 | |
| 2914 | /* struct _objc_category { |
| 2915 | char *category_name; |
| 2916 | char *class_name; |
| 2917 | struct _objc_method_list *instance_methods; |
| 2918 | struct _objc_method_list *class_methods; |
| 2919 | struct _objc_protocol_list *protocols; |
| 2920 | // Objective-C 1.0 extensions |
| 2921 | uint32_t size; // sizeof (struct _objc_category) |
| 2922 | struct _objc_property_list *instance_properties; // category's own |
| 2923 | // @property decl. |
| 2924 | }; |
| 2925 | */ |
| 2926 | |
| 2927 | static bool objc_category = false; |
| 2928 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2929 | Result += "\nstruct _objc_category {\n"; |
| 2930 | Result += "\tchar *category_name;\n"; |
| 2931 | Result += "\tchar *class_name;\n"; |
| 2932 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 2933 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 2934 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2935 | Result += "\tunsigned int size;\n"; |
| 2936 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 2937 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2938 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2939 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2940 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 2941 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2942 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2943 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2944 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2945 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2946 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2947 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2948 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2949 | Result += "\t, (struct _objc_method_list *)" |
| 2950 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 2951 | Result += FullCategoryName; |
| 2952 | Result += "\n"; |
| 2953 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2954 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2955 | Result += "\t, 0\n"; |
| 2956 | if (IDecl->getNumClassMethods() > 0) { |
| 2957 | Result += "\t, (struct _objc_method_list *)" |
| 2958 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 2959 | Result += FullCategoryName; |
| 2960 | Result += "\n"; |
| 2961 | } |
| 2962 | else |
| 2963 | Result += "\t, 0\n"; |
| 2964 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2965 | if (CDecl && !CDecl->getReferencedProtocols().empty()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2966 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 2967 | Result += FullCategoryName; |
| 2968 | Result += "\n"; |
| 2969 | } |
| 2970 | else |
| 2971 | Result += "\t, 0\n"; |
| 2972 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2975 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 2976 | /// ivar offset. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2977 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2978 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2979 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2980 | if (ivar->isBitField()) { |
| 2981 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 2982 | // place all bitfields at offset 0. |
| 2983 | Result += "0"; |
| 2984 | } else { |
| 2985 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2986 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2987 | if (LangOpts.Microsoft) |
| 2988 | Result += "_IMPL"; |
| 2989 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2990 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2991 | Result += ")"; |
| 2992 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2995 | //===----------------------------------------------------------------------===// |
| 2996 | // Meta Data Emission |
| 2997 | //===----------------------------------------------------------------------===// |
| 2998 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2999 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3000 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3001 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3002 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3003 | // Explictly declared @interface's are already synthesized. |
| 3004 | if (CDecl->ImplicitInterfaceDecl()) { |
| 3005 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3006 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3007 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3008 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3009 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3010 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3011 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3012 | ? IDecl->ivar_size() |
| 3013 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3014 | if (NumIvars > 0) { |
| 3015 | static bool objc_ivar = false; |
| 3016 | if (!objc_ivar) { |
| 3017 | /* struct _objc_ivar { |
| 3018 | char *ivar_name; |
| 3019 | char *ivar_type; |
| 3020 | int ivar_offset; |
| 3021 | }; |
| 3022 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3023 | Result += "\nstruct _objc_ivar {\n"; |
| 3024 | Result += "\tchar *ivar_name;\n"; |
| 3025 | Result += "\tchar *ivar_type;\n"; |
| 3026 | Result += "\tint ivar_offset;\n"; |
| 3027 | Result += "};\n"; |
| 3028 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3029 | objc_ivar = true; |
| 3030 | } |
| 3031 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3032 | /* struct { |
| 3033 | int ivar_count; |
| 3034 | struct _objc_ivar ivar_list[nIvars]; |
| 3035 | }; |
| 3036 | */ |
| 3037 | Result += "\nstatic struct {\n"; |
| 3038 | Result += "\tint ivar_count;\n"; |
| 3039 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3040 | Result += utostr(NumIvars); |
| 3041 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3042 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3043 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3044 | "{\n\t"; |
| 3045 | Result += utostr(NumIvars); |
| 3046 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3047 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3048 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3049 | if (!IDecl->ivar_empty()) { |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3050 | IVI = IDecl->ivar_begin(); |
| 3051 | IVE = IDecl->ivar_end(); |
| 3052 | } else { |
| 3053 | IVI = CDecl->ivar_begin(); |
| 3054 | IVE = CDecl->ivar_end(); |
| 3055 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3056 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3057 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3058 | Result += "\", \""; |
| 3059 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3060 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3061 | Result += StrEncoding; |
| 3062 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3063 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3064 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3065 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3066 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3067 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3068 | Result += "\", \""; |
| 3069 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3070 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3071 | Result += StrEncoding; |
| 3072 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3073 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3074 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3081 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3082 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3083 | |
| 3084 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3085 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3086 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3087 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3088 | // Protocols referenced in class declaration? |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3089 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3090 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3091 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3092 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3093 | // Declaration of class/meta-class metadata |
| 3094 | /* struct _objc_class { |
| 3095 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3096 | const char *super_class_name; |
| 3097 | char *name; |
| 3098 | long version; |
| 3099 | long info; |
| 3100 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3101 | struct _objc_ivar_list *ivars; |
| 3102 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3103 | struct objc_cache *cache; |
| 3104 | struct objc_protocol_list *protocols; |
| 3105 | const char *ivar_layout; |
| 3106 | struct _objc_class_ext *ext; |
| 3107 | }; |
| 3108 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3109 | static bool objc_class = false; |
| 3110 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3111 | Result += "\nstruct _objc_class {\n"; |
| 3112 | Result += "\tstruct _objc_class *isa;\n"; |
| 3113 | Result += "\tconst char *super_class_name;\n"; |
| 3114 | Result += "\tchar *name;\n"; |
| 3115 | Result += "\tlong version;\n"; |
| 3116 | Result += "\tlong info;\n"; |
| 3117 | Result += "\tlong instance_size;\n"; |
| 3118 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3119 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3120 | Result += "\tstruct objc_cache *cache;\n"; |
| 3121 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3122 | Result += "\tconst char *ivar_layout;\n"; |
| 3123 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3124 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3125 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3129 | ObjCInterfaceDecl *RootClass = 0; |
| 3130 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3131 | while (SuperClass) { |
| 3132 | RootClass = SuperClass; |
| 3133 | SuperClass = SuperClass->getSuperClass(); |
| 3134 | } |
| 3135 | SuperClass = CDecl->getSuperClass(); |
| 3136 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3137 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3138 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3139 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3140 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3141 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3142 | Result += "\""; |
| 3143 | |
| 3144 | if (SuperClass) { |
| 3145 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3146 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3147 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3148 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3149 | Result += "\""; |
| 3150 | } |
| 3151 | else { |
| 3152 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3153 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3154 | Result += "\""; |
| 3155 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3156 | // 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] | 3157 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3158 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 3159 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3160 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3161 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3162 | Result += "\n"; |
| 3163 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3164 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3165 | Result += ", 0\n"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3166 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3167 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3168 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3169 | Result += ",0,0\n"; |
| 3170 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3171 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3172 | Result += "\t,0,0,0,0\n"; |
| 3173 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3174 | |
| 3175 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3176 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3177 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3178 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3179 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3180 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3181 | if (SuperClass) { |
| 3182 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3183 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3184 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3185 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3186 | Result += "\""; |
| 3187 | } |
| 3188 | else { |
| 3189 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3190 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3191 | Result += "\""; |
| 3192 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3193 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3194 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3195 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3196 | Result += ",0"; |
| 3197 | else { |
| 3198 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3199 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3200 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3201 | if (LangOpts.Microsoft) |
| 3202 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3203 | Result += ")"; |
| 3204 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3205 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3206 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3207 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3208 | Result += "\n\t"; |
| 3209 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3210 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3211 | Result += ",0"; |
| 3212 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3213 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3214 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3215 | Result += ", 0\n\t"; |
| 3216 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3217 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3218 | Result += ",0,0"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3219 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3220 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3221 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3222 | Result += ", 0,0\n"; |
| 3223 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3224 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3225 | Result += ",0,0,0\n"; |
| 3226 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3227 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3228 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3229 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3230 | /// and emits meta-data. |
| 3231 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3232 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3233 | int ClsDefCount = ClassImplementation.size(); |
| 3234 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3235 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3236 | // Rewrite implemented methods |
| 3237 | for (int i = 0; i < ClsDefCount; i++) |
| 3238 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3239 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3240 | for (int i = 0; i < CatDefCount; i++) |
| 3241 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3242 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3243 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3244 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3245 | int ClsDefCount = ClassImplementation.size(); |
| 3246 | int CatDefCount = CategoryImplementation.size(); |
| 3247 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3248 | // This is needed for determining instance variable offsets. |
Steve Naroff | a11440b | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3249 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3250 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3251 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3252 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3253 | |
| 3254 | // For each implemented category, write out all its meta data. |
| 3255 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3256 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3257 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3258 | // Write objc_symtab metadata |
| 3259 | /* |
| 3260 | struct _objc_symtab |
| 3261 | { |
| 3262 | long sel_ref_cnt; |
| 3263 | SEL *refs; |
| 3264 | short cls_def_cnt; |
| 3265 | short cat_def_cnt; |
| 3266 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3267 | }; |
| 3268 | */ |
| 3269 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3270 | Result += "\nstruct _objc_symtab {\n"; |
| 3271 | Result += "\tlong sel_ref_cnt;\n"; |
| 3272 | Result += "\tSEL *refs;\n"; |
| 3273 | Result += "\tshort cls_def_cnt;\n"; |
| 3274 | Result += "\tshort cat_def_cnt;\n"; |
| 3275 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3276 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3277 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3278 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3279 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3280 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3281 | + ", " + utostr(CatDefCount) + "\n"; |
| 3282 | for (int i = 0; i < ClsDefCount; i++) { |
| 3283 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3284 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3285 | Result += "\n"; |
| 3286 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3287 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3288 | for (int i = 0; i < CatDefCount; i++) { |
| 3289 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3290 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3291 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3292 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3293 | Result += "\n"; |
| 3294 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3295 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3296 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Write objc_module metadata |
| 3299 | |
| 3300 | /* |
| 3301 | struct _objc_module { |
| 3302 | long version; |
| 3303 | long size; |
| 3304 | const char *name; |
| 3305 | struct _objc_symtab *symtab; |
| 3306 | } |
| 3307 | */ |
| 3308 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3309 | Result += "\nstruct _objc_module {\n"; |
| 3310 | Result += "\tlong version;\n"; |
| 3311 | Result += "\tlong size;\n"; |
| 3312 | Result += "\tconst char *name;\n"; |
| 3313 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3314 | Result += "};\n\n"; |
| 3315 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3316 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3317 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3318 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3319 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (LangOpts.Microsoft) { |
| 3322 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3323 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3324 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3325 | Result += "&_OBJC_MODULES;\n"; |
| 3326 | Result += "#pragma data_seg(pop)\n\n"; |
| 3327 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3328 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3329 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3330 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3331 | const char *funcName, |
| 3332 | std::string Tag) { |
| 3333 | const FunctionType *AFT = CE->getFunctionType(); |
| 3334 | QualType RT = AFT->getResultType(); |
| 3335 | std::string StructRef = "struct " + Tag; |
| 3336 | std::string S = "static " + RT.getAsString() + " __" + |
| 3337 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3338 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3339 | BlockDecl *BD = CE->getBlockDecl(); |
| 3340 | |
| 3341 | if (isa<FunctionTypeNoProto>(AFT)) { |
| 3342 | S += "()"; |
| 3343 | } else if (BD->param_empty()) { |
| 3344 | S += "(" + StructRef + " *__cself)"; |
| 3345 | } else { |
| 3346 | const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT); |
| 3347 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3348 | S += '('; |
| 3349 | // first add the implicit argument. |
| 3350 | S += StructRef + " *__cself, "; |
| 3351 | std::string ParamStr; |
| 3352 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3353 | E = BD->param_end(); AI != E; ++AI) { |
| 3354 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3355 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3356 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3357 | S += ParamStr; |
| 3358 | } |
| 3359 | if (FT->isVariadic()) { |
| 3360 | if (!BD->param_empty()) S += ", "; |
| 3361 | S += "..."; |
| 3362 | } |
| 3363 | S += ')'; |
| 3364 | } |
| 3365 | S += " {\n"; |
| 3366 | |
| 3367 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3368 | // First, emit a declaration for all "by ref" decls. |
| 3369 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3370 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3371 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3372 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3373 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3374 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3375 | } |
| 3376 | // Next, emit a declaration for all "by copy" declarations. |
| 3377 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3378 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3379 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3380 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3381 | // Handle nested closure invocation. For example: |
| 3382 | // |
| 3383 | // void (^myImportedClosure)(void); |
| 3384 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3385 | // |
| 3386 | // void (^anotherClosure)(void); |
| 3387 | // anotherClosure = ^(void) { |
| 3388 | // myImportedClosure(); // import and invoke the closure |
| 3389 | // }; |
| 3390 | // |
| 3391 | if (isBlockPointerType((*I)->getType())) |
| 3392 | S += "struct __block_impl *"; |
| 3393 | else |
| 3394 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3395 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3396 | } |
| 3397 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3398 | const char *cstr = RewrittenStr.c_str(); |
| 3399 | while (*cstr++ != '{') ; |
| 3400 | S += cstr; |
| 3401 | S += "\n"; |
| 3402 | return S; |
| 3403 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3404 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3405 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3406 | const char *funcName, |
| 3407 | std::string Tag) { |
| 3408 | std::string StructRef = "struct " + Tag; |
| 3409 | std::string S = "static void __"; |
| 3410 | |
| 3411 | S += funcName; |
| 3412 | S += "_block_copy_" + utostr(i); |
| 3413 | S += "(" + StructRef; |
| 3414 | S += "*dst, " + StructRef; |
| 3415 | S += "*src) {"; |
| 3416 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3417 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3418 | S += "_Block_copy_assign(&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3419 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3420 | S += ", src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3421 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3422 | S += ");}"; |
| 3423 | } |
| 3424 | S += "\nstatic void __"; |
| 3425 | S += funcName; |
| 3426 | S += "_block_dispose_" + utostr(i); |
| 3427 | S += "(" + StructRef; |
| 3428 | S += "*src) {"; |
| 3429 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3430 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3431 | S += "_Block_destroy(src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3432 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3433 | S += ");"; |
| 3434 | } |
| 3435 | S += "}\n"; |
| 3436 | return S; |
| 3437 | } |
| 3438 | |
| 3439 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3440 | bool hasCopyDisposeHelpers) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3441 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3442 | std::string Constructor = " " + Tag; |
| 3443 | |
| 3444 | S += " {\n struct __block_impl impl;\n"; |
| 3445 | |
| 3446 | if (hasCopyDisposeHelpers) |
| 3447 | S += " void *copy;\n void *dispose;\n"; |
| 3448 | |
| 3449 | Constructor += "(void *fp"; |
| 3450 | |
| 3451 | if (hasCopyDisposeHelpers) |
| 3452 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3453 | |
| 3454 | if (BlockDeclRefs.size()) { |
| 3455 | // Output all "by copy" declarations. |
| 3456 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3457 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3458 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3459 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3460 | std::string ArgName = "_" + FieldName; |
| 3461 | // Handle nested closure invocation. For example: |
| 3462 | // |
| 3463 | // void (^myImportedBlock)(void); |
| 3464 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3465 | // |
| 3466 | // void (^anotherBlock)(void); |
| 3467 | // anotherBlock = ^(void) { |
| 3468 | // myImportedBlock(); // import and invoke the closure |
| 3469 | // }; |
| 3470 | // |
| 3471 | if (isBlockPointerType((*I)->getType())) { |
| 3472 | S += "struct __block_impl *"; |
| 3473 | Constructor += ", void *" + ArgName; |
| 3474 | } else { |
| 3475 | (*I)->getType().getAsStringInternal(FieldName); |
| 3476 | (*I)->getType().getAsStringInternal(ArgName); |
| 3477 | Constructor += ", " + ArgName; |
| 3478 | } |
| 3479 | S += FieldName + ";\n"; |
| 3480 | } |
| 3481 | // Output all "by ref" declarations. |
| 3482 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3483 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3484 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3485 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3486 | std::string ArgName = "_" + FieldName; |
| 3487 | // Handle nested closure invocation. For example: |
| 3488 | // |
| 3489 | // void (^myImportedBlock)(void); |
| 3490 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3491 | // |
| 3492 | // void (^anotherBlock)(void); |
| 3493 | // anotherBlock = ^(void) { |
| 3494 | // myImportedBlock(); // import and invoke the closure |
| 3495 | // }; |
| 3496 | // |
| 3497 | if (isBlockPointerType((*I)->getType())) { |
| 3498 | S += "struct __block_impl *"; |
| 3499 | Constructor += ", void *" + ArgName; |
| 3500 | } else { |
| 3501 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3502 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3503 | Constructor += ", " + ArgName; |
| 3504 | } |
| 3505 | S += FieldName + "; // by ref\n"; |
| 3506 | } |
| 3507 | // Finish writing the constructor. |
| 3508 | // FIXME: handle NSConcreteGlobalBlock. |
| 3509 | Constructor += ", int flags=0) {\n"; |
| 3510 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3511 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3512 | |
| 3513 | if (hasCopyDisposeHelpers) |
| 3514 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3515 | |
| 3516 | // Initialize all "by copy" arguments. |
| 3517 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3518 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3519 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3520 | Constructor += " "; |
| 3521 | if (isBlockPointerType((*I)->getType())) |
| 3522 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3523 | else |
| 3524 | Constructor += Name + " = _"; |
| 3525 | Constructor += Name + ";\n"; |
| 3526 | } |
| 3527 | // Initialize all "by ref" arguments. |
| 3528 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3529 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3530 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3531 | Constructor += " "; |
| 3532 | if (isBlockPointerType((*I)->getType())) |
| 3533 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3534 | else |
| 3535 | Constructor += Name + " = _"; |
| 3536 | Constructor += Name + ";\n"; |
| 3537 | } |
| 3538 | } else { |
| 3539 | // Finish writing the constructor. |
| 3540 | // FIXME: handle NSConcreteGlobalBlock. |
| 3541 | Constructor += ", int flags=0) {\n"; |
| 3542 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3543 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3544 | if (hasCopyDisposeHelpers) |
| 3545 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3546 | } |
| 3547 | Constructor += " "; |
| 3548 | Constructor += "}\n"; |
| 3549 | S += Constructor; |
| 3550 | S += "};\n"; |
| 3551 | return S; |
| 3552 | } |
| 3553 | |
| 3554 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3555 | const char *FunName) { |
| 3556 | // Insert closures that were part of the function. |
| 3557 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3558 | |
| 3559 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3560 | |
| 3561 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3562 | |
| 3563 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3564 | ImportedBlockDecls.size() > 0); |
| 3565 | |
| 3566 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3567 | |
| 3568 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3569 | |
| 3570 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3571 | |
| 3572 | if (ImportedBlockDecls.size()) { |
| 3573 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3574 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3575 | } |
| 3576 | |
| 3577 | BlockDeclRefs.clear(); |
| 3578 | BlockByRefDecls.clear(); |
| 3579 | BlockByCopyDecls.clear(); |
| 3580 | BlockCallExprs.clear(); |
| 3581 | ImportedBlockDecls.clear(); |
| 3582 | } |
| 3583 | Blocks.clear(); |
| 3584 | RewrittenBlockExprs.clear(); |
| 3585 | } |
| 3586 | |
| 3587 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3588 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3589 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3590 | |
| 3591 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3592 | } |
| 3593 | |
| 3594 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3595 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3596 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3597 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3598 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3599 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3600 | // Convert colons to underscores. |
| 3601 | std::string::size_type loc = 0; |
| 3602 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3603 | FuncName.replace(loc, 1, "_"); |
| 3604 | |
| 3605 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3606 | } |
| 3607 | |
| 3608 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3609 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3610 | CI != E; ++CI) |
| 3611 | if (*CI) { |
| 3612 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3613 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3614 | else |
| 3615 | GetBlockDeclRefExprs(*CI); |
| 3616 | } |
| 3617 | // Handle specific things. |
| 3618 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3619 | // FIXME: Handle enums. |
| 3620 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3621 | BlockDeclRefs.push_back(CDRE); |
| 3622 | return; |
| 3623 | } |
| 3624 | |
| 3625 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3626 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3627 | CI != E; ++CI) |
| 3628 | if (*CI) { |
| 3629 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3630 | GetBlockCallExprs(CBE->getBody()); |
| 3631 | else |
| 3632 | GetBlockCallExprs(*CI); |
| 3633 | } |
| 3634 | |
| 3635 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3636 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3637 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3638 | } |
| 3639 | } |
| 3640 | return; |
| 3641 | } |
| 3642 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3643 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3644 | // Navigate to relevant type information. |
| 3645 | const char *closureName = 0; |
| 3646 | const BlockPointerType *CPT = 0; |
| 3647 | |
| 3648 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3649 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3650 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3651 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3652 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3653 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3654 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3655 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3656 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3657 | } else { |
| 3658 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3659 | } |
| 3660 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3661 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3662 | assert(FT && "RewriteBlockClass: Bad type"); |
| 3663 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FT); |
| 3664 | // FTP will be null for closures that don't take arguments. |
| 3665 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3666 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3667 | SourceLocation(), |
| 3668 | &Context->Idents.get("__block_impl")); |
| 3669 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3670 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3671 | // Generate a funky cast. |
| 3672 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3673 | |
| 3674 | // Push the block argument type. |
| 3675 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3676 | if (FTP) { |
| 3677 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3678 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3679 | QualType t = *I; |
| 3680 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3681 | if (isBlockPointerType(t)) { |
| 3682 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3683 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3684 | } |
| 3685 | ArgTypes.push_back(t); |
| 3686 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3687 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3688 | // Now do the pointer to function cast. |
| 3689 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3690 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3691 | |
| 3692 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3693 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3694 | CastExpr *BlkCast = new CStyleCastExpr(PtrBlock, Exp->getCallee(), PtrBlock, SourceLocation(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3695 | // Don't forget the parens to enforce the proper binding. |
| 3696 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), BlkCast); |
| 3697 | //PE->dump(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3698 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3699 | FieldDecl *FD = FieldDecl::Create(*Context, SourceLocation(), |
| 3700 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy); |
| 3701 | MemberExpr *ME = new MemberExpr(PE, true, FD, SourceLocation(), FD->getType()); |
| 3702 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3703 | CastExpr *FunkCast = new CStyleCastExpr(PtrToFuncCastType, ME, PtrToFuncCastType, SourceLocation(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3704 | PE = new ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 3705 | |
| 3706 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3707 | // Add the implicit argument. |
| 3708 | BlkExprs.push_back(BlkCast); |
| 3709 | // Add the user arguments. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3710 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3711 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3712 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3713 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3714 | CallExpr *CE = new CallExpr(PE, &BlkExprs[0], BlkExprs.size(), Exp->getType(), SourceLocation()); |
| 3715 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3719 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3720 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3724 | // FIXME: Add more elaborate code generation required by the ABI. |
| 3725 | InsertText(BDRE->getLocStart(), "*", 1); |
| 3726 | } |
| 3727 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3728 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3729 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3730 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3731 | |
| 3732 | // Need to avoid trying to rewrite synthesized casts. |
| 3733 | if (LocStart.isInvalid()) |
| 3734 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3735 | // Need to avoid trying to rewrite casts contained in macros. |
| 3736 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3737 | return; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3738 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3739 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3740 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3741 | |
| 3742 | // advance the location to startArgList. |
| 3743 | const char *argPtr = startBuf; |
| 3744 | |
| 3745 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3746 | switch (*argPtr) { |
| 3747 | case '^': |
| 3748 | // Replace the '^' with '*'. |
| 3749 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3750 | ReplaceText(LocStart, 1, "*", 1); |
| 3751 | break; |
| 3752 | } |
| 3753 | } |
| 3754 | return; |
| 3755 | } |
| 3756 | |
| 3757 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3758 | SourceLocation DeclLoc = FD->getLocation(); |
| 3759 | unsigned parenCount = 0; |
| 3760 | |
| 3761 | // We have 1 or more arguments that have closure pointers. |
| 3762 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3763 | const char *startArgList = strchr(startBuf, '('); |
| 3764 | |
| 3765 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3766 | |
| 3767 | parenCount++; |
| 3768 | // advance the location to startArgList. |
| 3769 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3770 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3771 | |
| 3772 | const char *argPtr = startArgList; |
| 3773 | |
| 3774 | while (*argPtr++ && parenCount) { |
| 3775 | switch (*argPtr) { |
| 3776 | case '^': |
| 3777 | // Replace the '^' with '*'. |
| 3778 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 3779 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3780 | break; |
| 3781 | case '(': |
| 3782 | parenCount++; |
| 3783 | break; |
| 3784 | case ')': |
| 3785 | parenCount--; |
| 3786 | break; |
| 3787 | } |
| 3788 | } |
| 3789 | return; |
| 3790 | } |
| 3791 | |
| 3792 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 3793 | const FunctionTypeProto *FTP; |
| 3794 | const PointerType *PT = QT->getAsPointerType(); |
| 3795 | if (PT) { |
| 3796 | FTP = PT->getPointeeType()->getAsFunctionTypeProto(); |
| 3797 | } else { |
| 3798 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 3799 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 3800 | FTP = BPT->getPointeeType()->getAsFunctionTypeProto(); |
| 3801 | } |
| 3802 | if (FTP) { |
| 3803 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
| 3804 | E = FTP->arg_type_end(); I != E; ++I) |
| 3805 | if (isBlockPointerType(*I)) |
| 3806 | return true; |
| 3807 | } |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
| 3811 | void RewriteObjC::GetExtentOfArgList(const char *Name, |
| 3812 | const char *&LParen, const char *&RParen) { |
| 3813 | const char *argPtr = strchr(Name, '('); |
| 3814 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 3815 | |
| 3816 | LParen = argPtr; // output the start. |
| 3817 | argPtr++; // skip past the left paren. |
| 3818 | unsigned parenCount = 1; |
| 3819 | |
| 3820 | while (*argPtr && parenCount) { |
| 3821 | switch (*argPtr) { |
| 3822 | case '(': parenCount++; break; |
| 3823 | case ')': parenCount--; break; |
| 3824 | default: break; |
| 3825 | } |
| 3826 | if (parenCount) argPtr++; |
| 3827 | } |
| 3828 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 3829 | RParen = argPtr; // output the end |
| 3830 | } |
| 3831 | |
| 3832 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 3833 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3834 | RewriteBlockPointerFunctionArgs(FD); |
| 3835 | return; |
| 3836 | } |
| 3837 | // Handle Variables and Typedefs. |
| 3838 | SourceLocation DeclLoc = ND->getLocation(); |
| 3839 | QualType DeclT; |
| 3840 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3841 | DeclT = VD->getType(); |
| 3842 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 3843 | DeclT = TDD->getUnderlyingType(); |
| 3844 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 3845 | DeclT = FD->getType(); |
| 3846 | else |
| 3847 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 3848 | |
| 3849 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3850 | const char *endBuf = startBuf; |
| 3851 | // scan backward (from the decl location) for the end of the previous decl. |
| 3852 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 3853 | startBuf--; |
| 3854 | |
| 3855 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 3856 | // may take block argument types (which will be handled below). |
| 3857 | if (*startBuf == '^') { |
| 3858 | // Replace the '^' with '*', computing a negative offset. |
| 3859 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 3860 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3861 | } |
| 3862 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 3863 | // Replace the '^' with '*' for arguments. |
| 3864 | DeclLoc = ND->getLocation(); |
| 3865 | startBuf = SM->getCharacterData(DeclLoc); |
| 3866 | const char *argListBegin, *argListEnd; |
| 3867 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 3868 | while (argListBegin < argListEnd) { |
| 3869 | if (*argListBegin == '^') { |
| 3870 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 3871 | ReplaceText(CaretLoc, 1, "*", 1); |
| 3872 | } |
| 3873 | argListBegin++; |
| 3874 | } |
| 3875 | } |
| 3876 | return; |
| 3877 | } |
| 3878 | |
| 3879 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 3880 | // Add initializers for any closure decl refs. |
| 3881 | GetBlockDeclRefExprs(Exp->getBody()); |
| 3882 | if (BlockDeclRefs.size()) { |
| 3883 | // Unique all "by copy" declarations. |
| 3884 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3885 | if (!BlockDeclRefs[i]->isByRef()) |
| 3886 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 3887 | // Unique all "by ref" declarations. |
| 3888 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3889 | if (BlockDeclRefs[i]->isByRef()) { |
| 3890 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 3891 | } |
| 3892 | // Find any imported blocks...they will need special attention. |
| 3893 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3894 | if (isBlockPointerType(BlockDeclRefs[i]->getType())) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 3895 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3896 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3901 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 3902 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 3903 | QualType FType = Context->getFunctionTypeNoProto(Context->VoidPtrTy); |
| 3904 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
| 3905 | ID, FType, FunctionDecl::Extern, false, 0); |
| 3906 | } |
| 3907 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 3908 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3909 | Blocks.push_back(Exp); |
| 3910 | |
| 3911 | CollectBlockDeclRefInfo(Exp); |
| 3912 | std::string FuncName; |
| 3913 | |
| 3914 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3915 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3916 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3917 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3918 | // Convert colons to underscores. |
| 3919 | std::string::size_type loc = 0; |
| 3920 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3921 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 3922 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3923 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3924 | |
| 3925 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 3926 | |
| 3927 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 3928 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 3929 | |
| 3930 | // Get a pointer to the function type so we can cast appropriately. |
| 3931 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 3932 | |
| 3933 | FunctionDecl *FD; |
| 3934 | Expr *NewRep; |
| 3935 | |
| 3936 | // Simulate a contructor call... |
| 3937 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
| 3938 | DeclRefExpr *DRE = new DeclRefExpr(FD, FType, SourceLocation()); |
| 3939 | |
| 3940 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 3941 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3942 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3943 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
| 3944 | DeclRefExpr *Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 3945 | CastExpr *castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3946 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3947 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3948 | |
| 3949 | if (ImportedBlockDecls.size()) { |
| 3950 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3951 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
| 3952 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 3953 | castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3954 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3955 | InitExprs.push_back(castExpr); |
| 3956 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3957 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3958 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
| 3959 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 3960 | castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3961 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3962 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3963 | } |
| 3964 | // Add initializers for any closure decl refs. |
| 3965 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3966 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3967 | // Output all "by copy" declarations. |
| 3968 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3969 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3970 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3971 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3972 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3973 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3974 | } else if (isBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3975 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3976 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 3977 | Exp = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3978 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3979 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3980 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3981 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3982 | } |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3983 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3984 | } |
| 3985 | // Output all "by ref" declarations. |
| 3986 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3987 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3988 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 3989 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 3990 | Exp = new UnaryOperator(Exp, UnaryOperator::AddrOf, |
| 3991 | Context->getPointerType(Exp->getType()), |
| 3992 | SourceLocation()); |
Steve Naroff | 707b0fe | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 3993 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3994 | } |
| 3995 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3996 | NewRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 3997 | FType, SourceLocation()); |
| 3998 | NewRep = new UnaryOperator(NewRep, UnaryOperator::AddrOf, |
| 3999 | Context->getPointerType(NewRep->getType()), |
| 4000 | SourceLocation()); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4001 | NewRep = new CStyleCastExpr(FType, NewRep, FType, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4002 | BlockDeclRefs.clear(); |
| 4003 | BlockByRefDecls.clear(); |
| 4004 | BlockByCopyDecls.clear(); |
| 4005 | ImportedBlockDecls.clear(); |
| 4006 | return NewRep; |
| 4007 | } |
| 4008 | |
| 4009 | //===----------------------------------------------------------------------===// |
| 4010 | // Function Body / Expression rewriting |
| 4011 | //===----------------------------------------------------------------------===// |
| 4012 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4013 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4014 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4015 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4016 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4017 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4018 | // we get this right. |
| 4019 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4020 | // Perform a bottom up traversal of all children. |
| 4021 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4022 | CI != E; ++CI) |
| 4023 | if (*CI) |
| 4024 | CollectPropertySetters(*CI); |
| 4025 | |
| 4026 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4027 | if (BinOp->isAssignmentOp()) { |
| 4028 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4029 | PropSetters[PRE] = BinOp; |
| 4030 | } |
| 4031 | } |
| 4032 | } |
| 4033 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4034 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4035 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4036 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4037 | Stmts.push_back(S); |
| 4038 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4039 | Stmts.push_back(S); |
| 4040 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4041 | } |
| 4042 | |
| 4043 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4044 | |
| 4045 | // Perform a bottom up rewrite of all children. |
| 4046 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4047 | CI != E; ++CI) |
| 4048 | if (*CI) { |
| 4049 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4050 | if (newStmt) |
| 4051 | *CI = newStmt; |
| 4052 | } |
| 4053 | |
| 4054 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4055 | // Rewrite the block body in place. |
| 4056 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4057 | |
| 4058 | // Now we snarf the rewritten text and stash it away for later use. |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4059 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4060 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4061 | |
| 4062 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4063 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4064 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4065 | return blockTranscribed; |
| 4066 | } |
| 4067 | // Handle specific things. |
| 4068 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4069 | return RewriteAtEncode(AtEncode); |
| 4070 | |
| 4071 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4072 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4073 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4074 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4075 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4076 | if (BinOp) { |
| 4077 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4078 | // we need to rewrite the right hand side prior to rewriting the setter. |
| 4079 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4080 | // |
| 4081 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4082 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4083 | // to see the original expression). Consider this example: |
| 4084 | // |
| 4085 | // Foo *obj1, *obj2; |
| 4086 | // |
| 4087 | // obj1.i = [obj2 rrrr]; |
| 4088 | // |
| 4089 | // 'BinOp' for the previous expression looks like: |
| 4090 | // |
| 4091 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4092 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4093 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4094 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4095 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4096 | // |
| 4097 | // 'newStmt' represents the rewritten message expression. For example: |
| 4098 | // |
| 4099 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4100 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4101 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4102 | // (CStyleCastExpr 0x231d220 'void *' |
| 4103 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4104 | // |
| 4105 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4106 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4107 | // the original RHS (since we haven't altered BinOp). |
| 4108 | // |
| 4109 | // This implies the Rewrite* routines can no longer delete the original |
| 4110 | // node. As a result, we now leak the original AST nodes. |
| 4111 | // |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4112 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt)); |
| 4113 | } else { |
| 4114 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4115 | } |
| 4116 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4117 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4118 | return RewriteAtSelector(AtSelector); |
| 4119 | |
| 4120 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4121 | return RewriteObjCStringLiteral(AtString); |
| 4122 | |
| 4123 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4124 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4125 | // Before we rewrite it, put the original message expression in a comment. |
| 4126 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4127 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4128 | |
| 4129 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4130 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4131 | |
| 4132 | std::string messString; |
| 4133 | messString += "// "; |
| 4134 | messString.append(startBuf, endBuf-startBuf+1); |
| 4135 | messString += "\n"; |
| 4136 | |
| 4137 | // FIXME: Missing definition of |
| 4138 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4139 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4140 | // Tried this, but it didn't work either... |
| 4141 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4142 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4143 | return RewriteMessageExpr(MessExpr); |
| 4144 | } |
| 4145 | |
| 4146 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4147 | return RewriteObjCTryStmt(StmtTry); |
| 4148 | |
| 4149 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4150 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4151 | |
| 4152 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4153 | return RewriteObjCThrowStmt(StmtThrow); |
| 4154 | |
| 4155 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4156 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4157 | |
| 4158 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4159 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4160 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4161 | OrigStmtRange.getEnd()); |
| 4162 | if (BreakStmt *StmtBreakStmt = |
| 4163 | dyn_cast<BreakStmt>(S)) |
| 4164 | return RewriteBreakStmt(StmtBreakStmt); |
| 4165 | if (ContinueStmt *StmtContinueStmt = |
| 4166 | dyn_cast<ContinueStmt>(S)) |
| 4167 | return RewriteContinueStmt(StmtContinueStmt); |
| 4168 | |
| 4169 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4170 | // and cast exprs. |
| 4171 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4172 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4173 | // precedes the first Decl. In the future the DeclGroup should have |
| 4174 | // a separate type-specifier that we can rewrite. |
| 4175 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4176 | |
| 4177 | // Blocks rewrite rules. |
| 4178 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4179 | DI != DE; ++DI) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4180 | ScopedDecl *SD = *DI; |
| 4181 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 4182 | if (isBlockPointerType(ND->getType())) |
| 4183 | RewriteBlockPointerDecl(ND); |
| 4184 | else if (ND->getType()->isFunctionPointerType()) |
| 4185 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4186 | } |
| 4187 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
| 4188 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 4189 | RewriteBlockPointerDecl(TD); |
| 4190 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4191 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4192 | } |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4197 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4198 | |
| 4199 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4200 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4201 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4202 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4203 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4204 | && "Statement stack mismatch"); |
| 4205 | Stmts.pop_back(); |
| 4206 | } |
| 4207 | // Handle blocks rewriting. |
| 4208 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4209 | if (BDRE->isByRef()) |
| 4210 | RewriteBlockDeclRefExpr(BDRE); |
| 4211 | } |
| 4212 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4213 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4214 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4215 | ReplaceStmt(S, BlockCall); |
| 4216 | return BlockCall; |
| 4217 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4218 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4219 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4220 | RewriteCastExpr(CE); |
| 4221 | } |
| 4222 | #if 0 |
| 4223 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 4224 | CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
| 4225 | // Get the new text. |
| 4226 | std::string SStr; |
| 4227 | llvm::raw_string_ostream Buf(SStr); |
| 4228 | Replacement->printPretty(Buf); |
| 4229 | const std::string &Str = Buf.str(); |
| 4230 | |
| 4231 | printf("CAST = %s\n", &Str[0]); |
| 4232 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4233 | delete S; |
| 4234 | return Replacement; |
| 4235 | } |
| 4236 | #endif |
| 4237 | // Return this stmt unmodified. |
| 4238 | return S; |
| 4239 | } |
| 4240 | |
| 4241 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4242 | /// main file of the input. |
| 4243 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4244 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4245 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4246 | // prototype. This enables us to rewrite function declarations and |
| 4247 | // definitions using the same code. |
| 4248 | RewriteBlocksInFunctionTypeProto(FD->getType(), FD); |
| 4249 | |
| 4250 | if (Stmt *Body = FD->getBody()) { |
| 4251 | CurFunctionDef = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4252 | CollectPropertySetters(Body); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4253 | FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4254 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4255 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4256 | // and any copy/dispose helper functions. |
| 4257 | InsertBlockLiteralsWithinFunction(FD); |
| 4258 | CurFunctionDef = 0; |
| 4259 | } |
| 4260 | return; |
| 4261 | } |
| 4262 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 4263 | if (Stmt *Body = MD->getBody()) { |
| 4264 | //Body->dump(); |
| 4265 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4266 | CollectPropertySetters(Body); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4267 | MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4268 | InsertBlockLiteralsWithinMethod(MD); |
| 4269 | CurMethodDef = 0; |
| 4270 | } |
| 4271 | } |
| 4272 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4273 | ClassImplementation.push_back(CI); |
| 4274 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4275 | CategoryImplementation.push_back(CI); |
| 4276 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4277 | RewriteForwardClassDecl(CD); |
| 4278 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4279 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4280 | if (isBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4281 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4282 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4283 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4284 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4285 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4286 | RewriteCastExpr(CE); |
| 4287 | } |
| 4288 | } |
| 4289 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4290 | if (VD->getInit()) { |
| 4291 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4292 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4293 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4294 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4295 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4296 | GlobalVarDecl = 0; |
| 4297 | |
| 4298 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4299 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4300 | RewriteCastExpr(CE); |
| 4301 | } |
| 4302 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4303 | return; |
| 4304 | } |
| 4305 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 4306 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 4307 | RewriteBlockPointerDecl(TD); |
| 4308 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4309 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4310 | return; |
| 4311 | } |
| 4312 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4313 | if (RD->isDefinition()) { |
| 4314 | for (RecordDecl::field_const_iterator i = RD->field_begin(), |
| 4315 | e = RD->field_end(); i != e; ++i) { |
| 4316 | FieldDecl *FD = *i; |
| 4317 | if (isBlockPointerType(FD->getType())) |
| 4318 | RewriteBlockPointerDecl(FD); |
| 4319 | } |
| 4320 | } |
| 4321 | return; |
| 4322 | } |
| 4323 | // Nothing yet. |
| 4324 | } |
| 4325 | |
| 4326 | void RewriteObjC::HandleTranslationUnit(TranslationUnit& TU) { |
| 4327 | // Get the top-level buffer that this corresponds to. |
| 4328 | |
| 4329 | // Rewrite tabs if we care. |
| 4330 | //RewriteTabs(); |
| 4331 | |
| 4332 | if (Diags.hasErrorOccurred()) |
| 4333 | return; |
| 4334 | |
| 4335 | // Create the output file. |
| 4336 | |
| 4337 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 4338 | llvm::raw_ostream *OutFile; |
| 4339 | if (OutFileName == "-") { |
| 4340 | OutFile = &llvm::outs(); |
| 4341 | } else if (!OutFileName.empty()) { |
| 4342 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 4343 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), false, Err); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4344 | OwnedStream.reset(OutFile); |
| 4345 | } else if (InFileName == "-") { |
| 4346 | OutFile = &llvm::outs(); |
| 4347 | } else { |
| 4348 | llvm::sys::Path Path(InFileName); |
| 4349 | Path.eraseSuffix(); |
| 4350 | Path.appendSuffix("cpp"); |
| 4351 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 4352 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), false, Err); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4353 | OwnedStream.reset(OutFile); |
| 4354 | } |
| 4355 | |
| 4356 | RewriteInclude(); |
| 4357 | |
| 4358 | InsertText(SourceLocation::getFileLoc(MainFileID, 0), |
| 4359 | Preamble.c_str(), Preamble.size(), false); |
| 4360 | |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4361 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4362 | RewriteImplementations(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4363 | |
| 4364 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4365 | // we are done. |
| 4366 | if (const RewriteBuffer *RewriteBuf = |
| 4367 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4368 | //printf("Changed:\n"); |
| 4369 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4370 | } else { |
| 4371 | fprintf(stderr, "No changes\n"); |
| 4372 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4373 | |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4374 | if (ClassImplementation.size() || CategoryImplementation.size()) { |
| 4375 | // Rewrite Objective-c meta data* |
| 4376 | std::string ResultStr; |
| 4377 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4378 | // Emit metadata. |
| 4379 | *OutFile << ResultStr; |
| 4380 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4381 | OutFile->flush(); |
| 4382 | } |
| 4383 | |