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 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/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" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.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" |
Dan Gohman | 63e2dcc | 2008-05-21 20:19:16 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 29 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 30 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 33 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 34 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 35 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 36 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 37 | unsigned RewriteFailedDiag; |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 38 | unsigned TryFinallyContainsReturnDiag; |
| 39 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 40 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 41 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 42 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 43 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 44 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 45 | SourceLocation LastIncLoc; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 47 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 48 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 49 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 50 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 51 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 52 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 53 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 54 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 55 | // Remember all the @protocol(<expr>) expressions. |
| 56 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 57 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 58 | unsigned NumObjCStringLiterals; |
| 59 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 60 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 61 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 62 | FunctionDecl *MsgSendStretFunctionDecl; |
| 63 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 64 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 65 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 66 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 67 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 68 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 69 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 70 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 71 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 72 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 73 | RecordDecl *NSStringRecord; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 74 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 75 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 76 | int BcLabelCount; |
| 77 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 78 | // Needed for super. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 79 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 80 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 81 | RecordDecl *ConstantStringDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 82 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 83 | TypeDecl *ProtocolTypeDecl; |
| 84 | QualType getProtocolType(); |
| 85 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 86 | // Needed for header files being rewritten |
| 87 | bool IsHeader; |
| 88 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 89 | std::string InFileName; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 90 | llvm::raw_ostream* OutFile; |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 91 | |
| 92 | bool SilenceRewriteMacroWarning; |
| 93 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 94 | std::string Preamble; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 95 | |
| 96 | // Block expressions. |
| 97 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 98 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 99 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 100 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 101 | // Block related declarations. |
| 102 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 104 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 105 | |
| 106 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 107 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 108 | // This maps a property to it's assignment statement. |
| 109 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 110 | // This maps a property to it's synthesied message expression. |
| 111 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 112 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
| 113 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 114 | // This maps an original source AST to it's rewritten form. This allows |
| 115 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 116 | // This is needed to support some of the exotic property rewriting. |
| 117 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 118 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 119 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 120 | VarDecl *GlobalVarDecl; |
| 121 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 122 | bool DisableReplaceStmt; |
| 123 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 124 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 125 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 126 | virtual void Initialize(ASTContext &context); |
| 127 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 128 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 129 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 130 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 131 | HandleTopLevelSingleDecl(*I); |
| 132 | } |
| 133 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 134 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 135 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 136 | Diagnostic &D, const LangOptions &LOpts, |
| 137 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 138 | |
| 139 | ~RewriteObjC() {} |
| 140 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 141 | virtual void HandleTranslationUnit(ASTContext &C); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 142 | |
| 143 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 144 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 145 | |
| 146 | if (ReplacingStmt) |
| 147 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 148 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 149 | if (DisableReplaceStmt) |
| 150 | return; // Used when rewriting the assignment of a property setter. |
| 151 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 152 | // If replacement succeeded or warning disabled return with no warning. |
| 153 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 154 | ReplacedNodes[Old] = New; |
| 155 | return; |
| 156 | } |
| 157 | if (SilenceRewriteMacroWarning) |
| 158 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 159 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 160 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 161 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 162 | |
| 163 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 164 | // Measaure the old text. |
| 165 | int Size = Rewrite.getRangeSize(SrcRange); |
| 166 | if (Size == -1) { |
| 167 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 168 | << Old->getSourceRange(); |
| 169 | return; |
| 170 | } |
| 171 | // Get the new text. |
| 172 | std::string SStr; |
| 173 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 174 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 175 | const std::string &Str = S.str(); |
| 176 | |
| 177 | // If replacement succeeded or warning disabled return with no warning. |
| 178 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, &Str[0], Str.size())) { |
| 179 | ReplacedNodes[Old] = New; |
| 180 | return; |
| 181 | } |
| 182 | if (SilenceRewriteMacroWarning) |
| 183 | return; |
| 184 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 185 | << Old->getSourceRange(); |
| 186 | } |
| 187 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 188 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 189 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 190 | // If insertion succeeded or warning disabled return with no warning. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 191 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 192 | SilenceRewriteMacroWarning) |
| 193 | return; |
| 194 | |
| 195 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 196 | } |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 197 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 198 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 199 | // If removal succeeded or warning disabled return with no warning. |
| 200 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 201 | return; |
| 202 | |
| 203 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 204 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 205 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 206 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 207 | const char *NewStr, unsigned NewLength) { |
| 208 | // If removal succeeded or warning disabled return with no warning. |
| 209 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 210 | SilenceRewriteMacroWarning) |
| 211 | return; |
| 212 | |
| 213 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 214 | } |
| 215 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 216 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 217 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 218 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 219 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 220 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 221 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 222 | ObjCImplementationDecl *IMD, |
| 223 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 224 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 225 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 226 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 227 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 228 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 229 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 230 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 231 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 232 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 233 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 234 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 235 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 236 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 237 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 238 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 239 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 240 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 241 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 242 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 243 | void CollectPropertySetters(Stmt *S); |
| 244 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 245 | Stmt *CurrentBody; |
| 246 | ParentMap *PropParentMap; // created lazily. |
| 247 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 248 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 249 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 250 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 251 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 252 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 253 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 254 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 255 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 256 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 257 | void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 259 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 261 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 262 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 263 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 264 | SourceLocation OrigEnd); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 265 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 266 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 267 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 268 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 269 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 270 | void SynthCountByEnumWithState(std::string &buf); |
| 271 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 272 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 273 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 274 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 275 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 276 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 277 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 278 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 279 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 280 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 281 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 282 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 283 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 284 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 286 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 287 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 289 | template<typename MethodIterator> |
| 290 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 291 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 292 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 293 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 294 | const char *ClassName, |
| 295 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 296 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 297 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 298 | const char *prefix, |
| 299 | const char *ClassName, |
| 300 | std::string &Result); |
| 301 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
| 302 | const char *prefix, |
| 303 | const char *ClassName, |
| 304 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 305 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 306 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 307 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 308 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 309 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 310 | void RewriteImplementations(); |
| 311 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 312 | |
| 313 | // Block rewriting. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 314 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 315 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 316 | |
| 317 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 318 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 319 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 320 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 321 | void RewriteBlockCall(CallExpr *Exp); |
| 322 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 323 | Stmt *RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 324 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 325 | |
| 326 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 327 | const char *funcName, std::string Tag); |
| 328 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 329 | const char *funcName, std::string Tag); |
| 330 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 331 | bool hasCopyDisposeHelpers); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 332 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 333 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 334 | const char *FunName); |
| 335 | |
| 336 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 337 | void GetBlockCallExprs(Stmt *S); |
| 338 | void GetBlockDeclRefExprs(Stmt *S); |
| 339 | |
| 340 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 341 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 342 | bool isTopLevelBlockPointerType(QualType T) { |
| 343 | return isa<BlockPointerType>(T); |
| 344 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 345 | |
| 346 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 347 | bool isObjCType(QualType T) { |
| 348 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 349 | return false; |
| 350 | |
| 351 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 352 | |
| 353 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 354 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 355 | return true; |
| 356 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 357 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 358 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 359 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 365 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 366 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 367 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 368 | |
| 369 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 370 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 371 | |
| 372 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 373 | for(unsigned i = 0; i < From.length(); i++) { |
| 374 | if (From[i] == '"') |
| 375 | To += "\\\""; |
| 376 | else |
| 377 | To += From[i]; |
| 378 | } |
| 379 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 380 | }; |
| 381 | } |
| 382 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 383 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 385 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
| 386 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 387 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 388 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 389 | // All the args are checked/rewritten. Don't call twice! |
| 390 | RewriteBlockPointerDecl(D); |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 397 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 398 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 399 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 402 | static bool IsHeaderFile(const std::string &Filename) { |
| 403 | std::string::size_type DotPos = Filename.rfind('.'); |
| 404 | |
| 405 | if (DotPos == std::string::npos) { |
| 406 | // no file extension |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 411 | // C header: .h |
| 412 | // C++ header: .hh or .H; |
| 413 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 414 | } |
| 415 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 416 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 417 | Diagnostic &D, const LangOptions &LOpts, |
| 418 | bool silenceMacroWarn) |
| 419 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 420 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 421 | IsHeader = IsHeaderFile(inFile); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 422 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 423 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 424 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 425 | "rewriter doesn't support user-specified control flow semantics " |
| 426 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 429 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 430 | llvm::raw_ostream* OS, |
| 431 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 432 | const LangOptions &LOpts, |
| 433 | bool SilenceRewriteMacroWarning) { |
| 434 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 435 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 436 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 437 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 438 | Context = &context; |
| 439 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 440 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 441 | MsgSendFunctionDecl = 0; |
| 442 | MsgSendSuperFunctionDecl = 0; |
| 443 | MsgSendStretFunctionDecl = 0; |
| 444 | MsgSendSuperStretFunctionDecl = 0; |
| 445 | MsgSendFpretFunctionDecl = 0; |
| 446 | GetClassFunctionDecl = 0; |
| 447 | GetMetaClassFunctionDecl = 0; |
| 448 | SelGetUidFunctionDecl = 0; |
| 449 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 450 | ConstantStringClassReference = 0; |
| 451 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 452 | CurMethodDef = 0; |
| 453 | CurFunctionDef = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 454 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 455 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 456 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 457 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 458 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 459 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 460 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 461 | PropParentMap = 0; |
| 462 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 463 | DisableReplaceStmt = false; |
| 464 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 465 | // Get the ID and start/end of the main file. |
| 466 | MainFileID = SM->getMainFileID(); |
| 467 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 468 | MainFileStart = MainBuf->getBufferStart(); |
| 469 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 471 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 473 | // declaring objc_selector outside the parameter list removes a silly |
| 474 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 475 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 476 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 477 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 478 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 479 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 480 | if (LangOpts.Microsoft) { |
| 481 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 482 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 483 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 484 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 485 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 486 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 487 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 488 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 489 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 490 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 491 | if (LangOpts.Microsoft) { |
| 492 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 493 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 494 | } else |
| 495 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 496 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 497 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 498 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 499 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 500 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 501 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 502 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 503 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 504 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 505 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 506 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 507 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 508 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 509 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 510 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 511 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 512 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 513 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 514 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 515 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 516 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 517 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 518 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 519 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 520 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 521 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 522 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 523 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 524 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 525 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 526 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 527 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 528 | Preamble += "#endif\n"; |
| 529 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 530 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 531 | Preamble += " int *isa;\n"; |
| 532 | Preamble += " int flags;\n"; |
| 533 | Preamble += " char *str;\n"; |
| 534 | Preamble += " long length;\n"; |
| 535 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 536 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 537 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 538 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 539 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 540 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 541 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 542 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 543 | // Blocks preamble. |
| 544 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 545 | Preamble += "#define BLOCK_IMPL\n"; |
| 546 | Preamble += "struct __block_impl {\n"; |
| 547 | Preamble += " void *isa;\n"; |
| 548 | Preamble += " int Flags;\n"; |
| 549 | Preamble += " int Size;\n"; |
| 550 | Preamble += " void *FuncPtr;\n"; |
| 551 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 552 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 553 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 554 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 555 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 556 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 557 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 558 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 559 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 560 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 561 | Preamble += "#define __attribute__(X)\n"; |
| 562 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 566 | //===----------------------------------------------------------------------===// |
| 567 | // Top Level Driver Code |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 570 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 571 | // Two cases: either the decl could be in the main file, or it could be in a |
| 572 | // #included file. If the former, rewrite it now. If the later, check to see |
| 573 | // if we rewrote the #include/#import. |
| 574 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 575 | Loc = SM->getInstantiationLoc(Loc); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 576 | |
| 577 | // If this is for a builtin, ignore it. |
| 578 | if (Loc.isInvalid()) return; |
| 579 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 580 | // Look for built-in declarations that we need to refer during the rewrite. |
| 581 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 582 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 583 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 584 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 585 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 586 | ConstantStringClassReference = FVD; |
| 587 | return; |
| 588 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 589 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 590 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 591 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 592 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 593 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 594 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 595 | } else if (ObjCForwardProtocolDecl *FP = |
| 596 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 597 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 598 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 599 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 600 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 601 | DIEnd = LSD->decls_end(); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 602 | DI != DIEnd; ++DI) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 603 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 605 | // 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] | 606 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 607 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 610 | //===----------------------------------------------------------------------===// |
| 611 | // Syntactic (non-AST) Rewriting Code |
| 612 | //===----------------------------------------------------------------------===// |
| 613 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 614 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 615 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 616 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 617 | const char *MainBufStart = MainBuf.first; |
| 618 | const char *MainBufEnd = MainBuf.second; |
| 619 | size_t ImportLen = strlen("import"); |
| 620 | size_t IncludeLen = strlen("include"); |
| 621 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 622 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 623 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 624 | if (*BufPtr == '#') { |
| 625 | if (++BufPtr == MainBufEnd) |
| 626 | return; |
| 627 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 628 | if (++BufPtr == MainBufEnd) |
| 629 | return; |
| 630 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 631 | // replace import with include |
| 632 | SourceLocation ImportLoc = |
| 633 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 634 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 635 | BufPtr += ImportLen; |
| 636 | } |
| 637 | } |
| 638 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 641 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 642 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 643 | const char *MainBufStart = MainBuf.first; |
| 644 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 645 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 646 | // Loop over the whole file, looking for tabs. |
| 647 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 648 | if (*BufPtr != '\t') |
| 649 | continue; |
| 650 | |
| 651 | // Okay, we found a tab. This tab will turn into at least one character, |
| 652 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 653 | unsigned VCol = 0; |
| 654 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 655 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 656 | ++VCol; |
| 657 | |
| 658 | // Okay, now that we know the virtual column, we know how many spaces to |
| 659 | // insert. We assume 8-character tab-stops. |
| 660 | unsigned Spaces = 8-(VCol & 7); |
| 661 | |
| 662 | // Get the location of the tab. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 663 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 664 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 665 | |
| 666 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 667 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 668 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 671 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 672 | ObjCIvarDecl *OID) { |
| 673 | std::string S; |
| 674 | S = "((struct "; |
| 675 | S += ClassDecl->getIdentifier()->getName(); |
| 676 | S += "_IMPL *)self)->"; |
| 677 | S += OID->getNameAsCString(); |
| 678 | return S; |
| 679 | } |
| 680 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 681 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 682 | ObjCImplementationDecl *IMD, |
| 683 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 684 | SourceLocation startLoc = PID->getLocStart(); |
| 685 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 686 | const char *startBuf = SM->getCharacterData(startLoc); |
| 687 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 688 | const char *semiBuf = strchr(startBuf, ';'); |
| 689 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 690 | SourceLocation onePastSemiLoc = |
| 691 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 692 | |
| 693 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 694 | return; // FIXME: is this correct? |
| 695 | |
| 696 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 697 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 698 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 699 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 700 | |
| 701 | if (!OID) |
| 702 | return; |
| 703 | |
| 704 | std::string Getr; |
| 705 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 706 | Getr += "{ "; |
| 707 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 708 | // FIXME: deal with code generation implications for various property |
| 709 | // attributes (copy, retain, nonatomic). |
| 710 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 711 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 712 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 713 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 714 | if (PD->isReadOnly()) |
| 715 | return; |
| 716 | |
| 717 | // Generate the 'setter' function. |
| 718 | std::string Setr; |
| 719 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 720 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 721 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 722 | // FIXME: deal with code generation implications for various property |
| 723 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 724 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 725 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 726 | Setr += PD->getNameAsCString(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 727 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 728 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 729 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 730 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 731 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 732 | // Get the start location and compute the semi location. |
| 733 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 734 | const char *startBuf = SM->getCharacterData(startLoc); |
| 735 | const char *semiPtr = strchr(startBuf, ';'); |
| 736 | |
| 737 | // Translate to typedef's that forward reference structs with the same name |
| 738 | // as the class. As a convenience, we include the original declaration |
| 739 | // as a comment. |
| 740 | std::string typedefString; |
| 741 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 742 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 743 | typedefString += "\n"; |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 744 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 745 | I != E; ++I) { |
| 746 | ObjCInterfaceDecl *ForwardDecl = *I; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 747 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 748 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 749 | typedefString += "\n"; |
| 750 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 751 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 752 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 753 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 754 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 755 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 759 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 760 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 763 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 764 | SourceLocation LocStart = Method->getLocStart(); |
| 765 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 767 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 768 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 769 | InsertText(LocStart, "#if 0\n", 6); |
| 770 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 771 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 772 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 776 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 777 | { |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 778 | SourceLocation Loc = prop->getLocation(); |
| 779 | |
| 780 | ReplaceText(Loc, 0, "// ", 3); |
| 781 | |
| 782 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 785 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 786 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 787 | |
| 788 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 789 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 791 | for (ObjCCategoryDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 792 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 793 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 794 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 795 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 796 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 797 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 798 | RewriteMethodDeclaration(*I); |
| 799 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 800 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 801 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 804 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 805 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 806 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 807 | SourceLocation LocStart = PDecl->getLocStart(); |
| 808 | |
| 809 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 810 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 812 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 813 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 814 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 815 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 816 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 817 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 818 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 819 | RewriteMethodDeclaration(*I); |
| 820 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 821 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 822 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 823 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 824 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 825 | // Must comment out @optional/@required |
| 826 | const char *startBuf = SM->getCharacterData(LocStart); |
| 827 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 828 | for (const char *p = startBuf; p < endBuf; p++) { |
| 829 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 830 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 831 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 832 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 833 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 834 | |
| 835 | } |
| 836 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 837 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 838 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 839 | ReplaceText(OptionalLoc, strlen("@required"), |
| 840 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 841 | |
| 842 | } |
| 843 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 846 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 847 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 848 | if (LocStart.isInvalid()) |
| 849 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 850 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 851 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 854 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 855 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 856 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 857 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 858 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 859 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 860 | ResultStr += "id"; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 861 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 862 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 863 | // needs special handling, since pointer-to-functions have special |
| 864 | // syntax (where a decaration models use). |
| 865 | QualType retType = OMD->getResultType(); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 866 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 867 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 868 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 869 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 870 | PointeeTy = BPT->getPointeeType(); |
| 871 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 872 | ResultStr += FPRetType->getResultType().getAsString(); |
| 873 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 874 | } |
| 875 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 876 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 877 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 878 | |
| 879 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 880 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 882 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 883 | NameStr += "_I_"; |
| 884 | else |
| 885 | NameStr += "_C_"; |
| 886 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 887 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 888 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 889 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 890 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 891 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 892 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 893 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 894 | } |
Steve Naroff | 563b828 | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 895 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 896 | { |
| 897 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 898 | int len = selString.size(); |
| 899 | for (int i = 0; i < len; i++) |
| 900 | if (selString[i] == ':') |
| 901 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 902 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 903 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 904 | // Remember this name for metadata emission |
| 905 | MethodInternalNames[OMD] = NameStr; |
| 906 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 907 | |
| 908 | // Rewrite arguments |
| 909 | ResultStr += "("; |
| 910 | |
| 911 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 912 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 913 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 914 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 915 | if (!LangOpts.Microsoft) { |
| 916 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 917 | ResultStr += "struct "; |
| 918 | } |
| 919 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 920 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 921 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 922 | } |
| 923 | else |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 924 | ResultStr += Context->getObjCClassType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 925 | |
| 926 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 927 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 928 | ResultStr += " _cmd"; |
| 929 | |
| 930 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 931 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 932 | E = OMD->param_end(); PI != E; ++PI) { |
| 933 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 934 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 935 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 936 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 937 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 938 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 939 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 940 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 941 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 942 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 943 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 944 | Context->PrintingPolicy); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 945 | } else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 946 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 947 | ResultStr += Name; |
| 948 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 949 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 950 | if (OMD->isVariadic()) |
| 951 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 952 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 953 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 954 | if (FPRetType) { |
| 955 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 956 | |
| 957 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 958 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 959 | ResultStr += "("; |
| 960 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 961 | if (i) ResultStr += ", "; |
| 962 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 963 | ResultStr += ParamStr; |
| 964 | } |
| 965 | if (FT->isVariadic()) { |
| 966 | if (FT->getNumArgs()) ResultStr += ", "; |
| 967 | ResultStr += "..."; |
| 968 | } |
| 969 | ResultStr += ")"; |
| 970 | } else { |
| 971 | ResultStr += "()"; |
| 972 | } |
| 973 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 975 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 976 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 977 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 978 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 979 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 980 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 981 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 982 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 984 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 985 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 986 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 987 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 988 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | ObjCMethodDecl *OMD = *I; |
| 990 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 991 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 992 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 993 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 994 | const char *startBuf = SM->getCharacterData(LocStart); |
| 995 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 996 | ReplaceText(LocStart, endBuf-startBuf, |
| 997 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1001 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1002 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1003 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1005 | ObjCMethodDecl *OMD = *I; |
| 1006 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1007 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1008 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1009 | |
| 1010 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1011 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1012 | ReplaceText(LocStart, endBuf-startBuf, |
| 1013 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1014 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1015 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1016 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1017 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1018 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1019 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1022 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1023 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1024 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1025 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1028 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1029 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1030 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1031 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1032 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1033 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1034 | ResultStr += "\n"; |
| 1035 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1036 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1037 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1038 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1039 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1040 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1041 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1042 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1043 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1044 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1045 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1046 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
| 1047 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1048 | RewriteProperty(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1049 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1050 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1051 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1052 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1053 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1054 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1055 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1056 | RewriteMethodDeclaration(*I); |
| 1057 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1058 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1059 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1062 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1063 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1064 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1065 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1066 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1067 | ObjCMessageExpr *MsgExpr; |
| 1068 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1069 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1070 | ExprVec.push_back(newStmt); |
| 1071 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1072 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1073 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1074 | if (PRE && PropGetters[PRE]) { |
| 1075 | // This allows us to handle chain/nested property getters. |
| 1076 | Receiver = PropGetters[PRE]; |
| 1077 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1078 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1079 | PDecl->getSetterName(), PDecl->getType(), |
| 1080 | PDecl->getSetterMethodDecl(), |
| 1081 | SourceLocation(), SourceLocation(), |
| 1082 | &ExprVec[0], 1); |
| 1083 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1084 | |
| 1085 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1086 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1087 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1088 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1089 | // to things that stay around. |
| 1090 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1091 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1094 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1095 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1096 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1097 | ObjCMessageExpr *MsgExpr; |
| 1098 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1099 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1100 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1101 | |
| 1102 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1103 | if (PRE && PropGetters[PRE]) { |
| 1104 | // This allows us to handle chain/nested property getters. |
| 1105 | Receiver = PropGetters[PRE]; |
| 1106 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1107 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1108 | PDecl->getGetterName(), PDecl->getType(), |
| 1109 | PDecl->getGetterMethodDecl(), |
| 1110 | SourceLocation(), SourceLocation(), |
| 1111 | 0, 0); |
| 1112 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1113 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (!PropParentMap) |
| 1116 | PropParentMap = new ParentMap(CurrentBody); |
| 1117 | |
| 1118 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1119 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1120 | // We stash away the ReplacingStmt since actually doing the |
| 1121 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1122 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1123 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1124 | // to things that stay around. |
| 1125 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1126 | return PropRefExpr; // return the original... |
| 1127 | } else { |
| 1128 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1129 | // delete PropRefExpr; elsewhere... |
| 1130 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1131 | // to things that stay around. |
| 1132 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1133 | return ReplacingStmt; |
| 1134 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1137 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1138 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1139 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1140 | if (CurMethodDef) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1141 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1142 | ObjCInterfaceType *iFaceDecl = |
| 1143 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1144 | // lookup which class implements the instance variable. |
| 1145 | ObjCInterfaceDecl *clsDeclared = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1146 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1147 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1148 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1149 | |
| 1150 | // Synthesize an explicit cast to gain access to the ivar. |
| 1151 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1152 | RecName += "_IMPL"; |
| 1153 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1154 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1155 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1156 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1157 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1158 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
| 1159 | CastExpr::CK_Unknown, |
| 1160 | IV->getBase(), |
| 1161 | castT,SourceLocation(), |
| 1162 | SourceLocation()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1163 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1164 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1165 | IV->getBase()->getLocEnd(), |
| 1166 | castExpr); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1167 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1168 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1169 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1170 | IV->getLocation(), |
| 1171 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1172 | ReplaceStmt(IV, ME); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1173 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1174 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1175 | } |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1176 | |
| 1177 | ReplaceStmt(IV->getBase(), PE); |
| 1178 | // Cannot delete IV->getBase(), since PE points to it. |
| 1179 | // Replace the old base with the cast. This is important when doing |
| 1180 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1181 | IV->setBase(PE); |
| 1182 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1183 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1184 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1185 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1186 | |
| 1187 | // Explicit ivar refs need to have a cast inserted. |
| 1188 | // FIXME: consider sharing some of this code with the code above. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1189 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1190 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1191 | // lookup which class implements the instance variable. |
| 1192 | ObjCInterfaceDecl *clsDeclared = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1193 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1194 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1195 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1196 | |
| 1197 | // Synthesize an explicit cast to gain access to the ivar. |
| 1198 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1199 | RecName += "_IMPL"; |
| 1200 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1201 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1202 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1203 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1204 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1205 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
| 1206 | CastExpr::CK_Unknown, |
| 1207 | IV->getBase(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1208 | castT, SourceLocation(), |
| 1209 | SourceLocation()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1210 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1211 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1212 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1213 | ReplaceStmt(IV->getBase(), PE); |
| 1214 | // Cannot delete IV->getBase(), since PE points to it. |
| 1215 | // Replace the old base with the cast. This is important when doing |
| 1216 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1217 | IV->setBase(PE); |
| 1218 | return IV; |
| 1219 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1220 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1221 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1224 | /// SynthCountByEnumWithState - To print: |
| 1225 | /// ((unsigned int (*) |
| 1226 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1227 | /// (void *)objc_msgSend)((id)l_collection, |
| 1228 | /// sel_registerName( |
| 1229 | /// "countByEnumeratingWithState:objects:count:"), |
| 1230 | /// &enumState, |
| 1231 | /// (id *)items, (unsigned int)16) |
| 1232 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1233 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1234 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1235 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1236 | buf += "\n\t\t"; |
| 1237 | buf += "((id)l_collection,\n\t\t"; |
| 1238 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1239 | buf += "\n\t\t"; |
| 1240 | buf += "&enumState, " |
| 1241 | "(id *)items, (unsigned int)16)"; |
| 1242 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1243 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1244 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1245 | /// statement to exit to its outer synthesized loop. |
| 1246 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1247 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1248 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1249 | return S; |
| 1250 | // replace break with goto __break_label |
| 1251 | std::string buf; |
| 1252 | |
| 1253 | SourceLocation startLoc = S->getLocStart(); |
| 1254 | buf = "goto __break_label_"; |
| 1255 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1256 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1262 | /// statement to continue with its inner synthesized loop. |
| 1263 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1264 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1265 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1266 | return S; |
| 1267 | // replace continue with goto __continue_label |
| 1268 | std::string buf; |
| 1269 | |
| 1270 | SourceLocation startLoc = S->getLocStart(); |
| 1271 | buf = "goto __continue_label_"; |
| 1272 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1273 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1274 | |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1278 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1279 | /// It rewrites: |
| 1280 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1281 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1282 | /// Into: |
| 1283 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1284 | /// type elem; |
| 1285 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1286 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1287 | /// id l_collection = (id)collection; |
| 1288 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1289 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1290 | /// if (limit) { |
| 1291 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1292 | /// do { |
| 1293 | /// unsigned long counter = 0; |
| 1294 | /// do { |
| 1295 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1296 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1297 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1298 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1299 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1300 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1301 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1302 | /// objects:items count:16]); |
| 1303 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1304 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1305 | /// } |
| 1306 | /// else |
| 1307 | /// elem = nil; |
| 1308 | /// } |
| 1309 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1310 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1311 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1312 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1313 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1314 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1315 | assert(!ObjCBcLabelNo.empty() && |
| 1316 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1317 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1318 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1319 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1320 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1321 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1322 | std::string buf; |
| 1323 | buf = "\n{\n\t"; |
| 1324 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1325 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1326 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1327 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1328 | elementTypeAsString = ElementType.getAsString(); |
| 1329 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1330 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1331 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1332 | buf += elementName; |
| 1333 | buf += ";\n\t"; |
| 1334 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1335 | else { |
| 1336 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1337 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1338 | elementTypeAsString |
| 1339 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1340 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1341 | |
| 1342 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1343 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1344 | // id items[16]; |
| 1345 | buf += "id items[16];\n\t"; |
| 1346 | // id l_collection = (id) |
| 1347 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1348 | // Find start location of 'collection' the hard way! |
| 1349 | const char *startCollectionBuf = startBuf; |
| 1350 | startCollectionBuf += 3; // skip 'for' |
| 1351 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1352 | startCollectionBuf++; // skip '(' |
| 1353 | // find 'in' and skip it. |
| 1354 | while (*startCollectionBuf != ' ' || |
| 1355 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1356 | (*(startCollectionBuf+3) != ' ' && |
| 1357 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1358 | startCollectionBuf++; |
| 1359 | startCollectionBuf += 3; |
| 1360 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1361 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1362 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1363 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1364 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1365 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1366 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1367 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1368 | buf = ";\n\t"; |
| 1369 | |
| 1370 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1371 | // objects:items count:16]; |
| 1372 | // which is synthesized into: |
| 1373 | // unsigned int limit = |
| 1374 | // ((unsigned int (*) |
| 1375 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1376 | // (void *)objc_msgSend)((id)l_collection, |
| 1377 | // sel_registerName( |
| 1378 | // "countByEnumeratingWithState:objects:count:"), |
| 1379 | // (struct __objcFastEnumerationState *)&state, |
| 1380 | // (id *)items, (unsigned int)16); |
| 1381 | buf += "unsigned long limit =\n\t\t"; |
| 1382 | SynthCountByEnumWithState(buf); |
| 1383 | buf += ";\n\t"; |
| 1384 | /// if (limit) { |
| 1385 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1386 | /// do { |
| 1387 | /// unsigned long counter = 0; |
| 1388 | /// do { |
| 1389 | /// if (startMutations != *enumState.mutationsPtr) |
| 1390 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1391 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1392 | buf += "if (limit) {\n\t"; |
| 1393 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1394 | buf += "do {\n\t\t"; |
| 1395 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1396 | buf += "do {\n\t\t\t"; |
| 1397 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1398 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1399 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1400 | buf += " = ("; |
| 1401 | buf += elementTypeAsString; |
| 1402 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1403 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1404 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1405 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1406 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1407 | /// } while (counter < limit); |
| 1408 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1409 | /// objects:items count:16]); |
| 1410 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1411 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | /// } |
| 1413 | /// else |
| 1414 | /// elem = nil; |
| 1415 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1416 | /// |
| 1417 | buf = ";\n\t"; |
| 1418 | buf += "__continue_label_"; |
| 1419 | buf += utostr(ObjCBcLabelNo.back()); |
| 1420 | buf += ": ;"; |
| 1421 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | buf += "} while (counter < limit);\n\t"; |
| 1423 | buf += "} while (limit = "; |
| 1424 | SynthCountByEnumWithState(buf); |
| 1425 | buf += ");\n\t"; |
| 1426 | buf += elementName; |
Steve Naroff | 5605fdf | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1427 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1428 | buf += "__break_label_"; |
| 1429 | buf += utostr(ObjCBcLabelNo.back()); |
| 1430 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1431 | buf += "}\n\t"; |
| 1432 | buf += "else\n\t\t"; |
| 1433 | buf += elementName; |
Steve Naroff | 5605fdf | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1434 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1435 | buf += "}\n"; |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1436 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1437 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1438 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1439 | if (isa<CompoundStmt>(S->getBody())) { |
| 1440 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1441 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1442 | } else { |
| 1443 | /* Need to treat single statements specially. For example: |
| 1444 | * |
| 1445 | * for (A *a in b) if (stuff()) break; |
| 1446 | * for (A *a in b) xxxyy; |
| 1447 | * |
| 1448 | * The following code simply scans ahead to the semi to find the actual end. |
| 1449 | */ |
| 1450 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1451 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1452 | assert(semiBuf && "Can't find ';'"); |
| 1453 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1454 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1455 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1456 | Stmts.pop_back(); |
| 1457 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1458 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1461 | /// RewriteObjCSynchronizedStmt - |
| 1462 | /// This routine rewrites @synchronized(expr) stmt; |
| 1463 | /// into: |
| 1464 | /// objc_sync_enter(expr); |
| 1465 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1466 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1467 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1468 | // Get the start location and compute the semi location. |
| 1469 | SourceLocation startLoc = S->getLocStart(); |
| 1470 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1471 | |
| 1472 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1473 | |
| 1474 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1475 | buf = "objc_sync_enter((id)"; |
| 1476 | const char *lparenBuf = startBuf; |
| 1477 | while (*lparenBuf != '(') lparenBuf++; |
| 1478 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1479 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1480 | // the sync expression is typically a message expression that's already |
| 1481 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1482 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1483 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1484 | while (*endBuf != ')') endBuf--; |
| 1485 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1486 | buf = ");\n"; |
| 1487 | // declare a new scope with two variables, _stack and _rethrow. |
| 1488 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1489 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1490 | buf += "char *pointers[4];} _stack;\n"; |
| 1491 | buf += "id volatile _rethrow = 0;\n"; |
| 1492 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1493 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1494 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1495 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1496 | startBuf = SM->getCharacterData(startLoc); |
| 1497 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1498 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1499 | SourceLocation lastCurlyLoc = startLoc; |
| 1500 | buf = "}\nelse {\n"; |
| 1501 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1502 | buf += "}\n"; |
| 1503 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1504 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1505 | buf += " objc_sync_exit("; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1506 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1507 | CastExpr::CK_Unknown, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1508 | S->getSynchExpr(), |
| 1509 | Context->getObjCIdType(), |
| 1510 | SourceLocation(), |
| 1511 | SourceLocation()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1512 | std::string syncExprBufS; |
| 1513 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1514 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1515 | PrintingPolicy(LangOpts)); |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1516 | buf += syncExprBuf.str(); |
| 1517 | buf += ");\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1518 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1519 | buf += "}\n"; |
| 1520 | buf += "}"; |
| 1521 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1522 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1523 | return 0; |
| 1524 | } |
| 1525 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1526 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1527 | // Perform a bottom up traversal of all children. |
| 1528 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1529 | CI != E; ++CI) |
| 1530 | if (*CI) |
| 1531 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1532 | |
| 1533 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1534 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1535 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1536 | TryFinallyContainsReturnDiag); |
| 1537 | } |
| 1538 | return; |
| 1539 | } |
| 1540 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1541 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1542 | // Get the start location and compute the semi location. |
| 1543 | SourceLocation startLoc = S->getLocStart(); |
| 1544 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1545 | |
| 1546 | assert((*startBuf == '@') && "bogus @try location"); |
| 1547 | |
| 1548 | std::string buf; |
| 1549 | // declare a new scope with two variables, _stack and _rethrow. |
| 1550 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1551 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1552 | buf += "char *pointers[4];} _stack;\n"; |
| 1553 | buf += "id volatile _rethrow = 0;\n"; |
| 1554 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1555 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1557 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1558 | |
| 1559 | startLoc = S->getTryBody()->getLocEnd(); |
| 1560 | startBuf = SM->getCharacterData(startLoc); |
| 1561 | |
| 1562 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1563 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1564 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1565 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1566 | if (catchList) { |
| 1567 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1568 | buf = " /* @catch begin */ else {\n"; |
| 1569 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1570 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1571 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1572 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1573 | buf += " else { /* @catch continue */"; |
| 1574 | |
| 1575 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1576 | } else { /* no catch list */ |
| 1577 | buf = "}\nelse {\n"; |
| 1578 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1579 | buf += "}"; |
| 1580 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1581 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1582 | bool sawIdTypedCatch = false; |
| 1583 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1584 | while (catchList) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1585 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (catchList == S->getCatchStmts()) |
| 1588 | buf = "if ("; // we are generating code for the first catch clause |
| 1589 | else |
| 1590 | buf = "else if ("; |
| 1591 | startLoc = catchList->getLocStart(); |
| 1592 | startBuf = SM->getCharacterData(startLoc); |
| 1593 | |
| 1594 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1595 | |
| 1596 | const char *lParenLoc = strchr(startBuf, '('); |
| 1597 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1598 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1599 | // Now rewrite the body... |
| 1600 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1601 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1602 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1603 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1604 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1605 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1606 | |
| 1607 | buf += "1) { id _tmp = _caught;"; |
| 1608 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1609 | buf.c_str(), buf.size()); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1610 | } else if (catchDecl) { |
| 1611 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1612 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1613 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1614 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1615 | sawIdTypedCatch = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1616 | } else if (const PointerType *pType = t->getAs<PointerType>()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1617 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1618 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1619 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1620 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1621 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1622 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1623 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1624 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | // Now rewrite the body... |
| 1628 | lastCatchBody = catchList->getCatchBody(); |
| 1629 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1630 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1631 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1632 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1633 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1634 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1635 | |
| 1636 | buf = " = _caught;"; |
| 1637 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1638 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1639 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1640 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1641 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1642 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1643 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1644 | catchList = catchList->getNextCatchStmt(); |
| 1645 | } |
| 1646 | // Complete the catch list... |
| 1647 | if (lastCatchBody) { |
| 1648 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1649 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1650 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1651 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1652 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1653 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1654 | buf = "} /* last catch end */\n"; |
| 1655 | buf += "else {\n"; |
| 1656 | buf += " _rethrow = _caught;\n"; |
| 1657 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1658 | buf += "} } /* @catch end */\n"; |
| 1659 | if (!S->getFinallyStmt()) |
| 1660 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1661 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1662 | |
| 1663 | // Set lastCurlyLoc |
| 1664 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1665 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1666 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1667 | startLoc = finalStmt->getLocStart(); |
| 1668 | startBuf = SM->getCharacterData(startLoc); |
| 1669 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1670 | |
| 1671 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1672 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1673 | |
| 1674 | Stmt *body = finalStmt->getFinallyBody(); |
| 1675 | SourceLocation startLoc = body->getLocStart(); |
| 1676 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1677 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1678 | "bogus @finally body location"); |
| 1679 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1680 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1681 | |
| 1682 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1683 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1684 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1685 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1686 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1687 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1688 | |
| 1689 | // Set lastCurlyLoc |
| 1690 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Now check for any return/continue/go statements within the @try. |
| 1693 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1694 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1695 | buf = "{ /* implicit finally clause */\n"; |
| 1696 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1697 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1698 | buf += "}"; |
| 1699 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1700 | } |
| 1701 | // Now emit the final closing curly brace... |
| 1702 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1703 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1704 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1705 | return 0; |
| 1706 | } |
| 1707 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1708 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1709 | return 0; |
| 1710 | } |
| 1711 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1712 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1713 | return 0; |
| 1714 | } |
| 1715 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1716 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1717 | // the throw expression is typically a message expression that's already |
| 1718 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1719 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1720 | // Get the start location and compute the semi location. |
| 1721 | SourceLocation startLoc = S->getLocStart(); |
| 1722 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1723 | |
| 1724 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1725 | |
| 1726 | std::string buf; |
| 1727 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1728 | if (S->getThrowExpr()) |
| 1729 | buf = "objc_exception_throw("; |
| 1730 | else // add an implicit argument |
| 1731 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1732 | |
| 1733 | // handle "@ throw" correctly. |
| 1734 | const char *wBuf = strchr(startBuf, 'w'); |
| 1735 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1736 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1737 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1738 | const char *semiBuf = strchr(startBuf, ';'); |
| 1739 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1740 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1741 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1742 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1743 | return 0; |
| 1744 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1745 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1746 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1747 | // Create a new string expression. |
| 1748 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1749 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1750 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1751 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1752 | StrEncoding.length(), false,StrType, |
| 1753 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1754 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1756 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1757 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1758 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1761 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1762 | if (!SelGetUidFunctionDecl) |
| 1763 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1764 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1765 | // Create a call to sel_registerName("selName"). |
| 1766 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1767 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1768 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1769 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1770 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1771 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1772 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1773 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1774 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1775 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1776 | return SelExp; |
| 1777 | } |
| 1778 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1779 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1780 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1781 | // 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] | 1782 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1783 | |
| 1784 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1785 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1786 | |
| 1787 | // 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] | 1788 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1789 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
| 1790 | CastExpr::CK_Unknown, |
| 1791 | DRE, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1792 | /*isLvalue=*/false); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1793 | |
| 1794 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1796 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1797 | SourceLocation()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1800 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1801 | const char *&startRef, const char *&endRef) { |
| 1802 | while (startBuf < endBuf) { |
| 1803 | if (*startBuf == '<') |
| 1804 | startRef = startBuf; // mark the start. |
| 1805 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1806 | if (startRef && *startRef == '<') { |
| 1807 | endRef = startBuf; // mark the end. |
| 1808 | return true; |
| 1809 | } |
| 1810 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1811 | } |
| 1812 | startBuf++; |
| 1813 | } |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1817 | static void scanToNextArgument(const char *&argRef) { |
| 1818 | int angle = 0; |
| 1819 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1820 | if (*argRef == '<') |
| 1821 | angle++; |
| 1822 | else if (*argRef == '>') |
| 1823 | angle--; |
| 1824 | argRef++; |
| 1825 | } |
| 1826 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1827 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1828 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1829 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1830 | return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType(); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1833 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1834 | QualType Type = E->getType(); |
| 1835 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1836 | SourceLocation Loc, EndLoc; |
| 1837 | |
| 1838 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1839 | Loc = ECE->getLParenLoc(); |
| 1840 | EndLoc = ECE->getRParenLoc(); |
| 1841 | } else { |
| 1842 | Loc = E->getLocStart(); |
| 1843 | EndLoc = E->getLocEnd(); |
| 1844 | } |
| 1845 | // This will defend against trying to rewrite synthesized expressions. |
| 1846 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1847 | return; |
| 1848 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1849 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1850 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1851 | const char *startRef = 0, *endRef = 0; |
| 1852 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1853 | // Get the locations of the startRef, endRef. |
| 1854 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1855 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1856 | // Comment out the protocol references. |
| 1857 | InsertText(LessLoc, "/*", 2); |
| 1858 | InsertText(GreaterLoc, "*/", 2); |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1863 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1864 | SourceLocation Loc; |
| 1865 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1866 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1867 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1868 | Loc = VD->getLocation(); |
| 1869 | Type = VD->getType(); |
| 1870 | } |
| 1871 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1872 | Loc = FD->getLocation(); |
| 1873 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1874 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1875 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1876 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1877 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1878 | if (!proto) |
| 1879 | return; |
| 1880 | Type = proto->getResultType(); |
| 1881 | } |
| 1882 | else |
| 1883 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1884 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1885 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1886 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1887 | |
| 1888 | const char *endBuf = SM->getCharacterData(Loc); |
| 1889 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1890 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1891 | startBuf--; // scan backward (from the decl location) for return type. |
| 1892 | const char *startRef = 0, *endRef = 0; |
| 1893 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1894 | // Get the locations of the startRef, endRef. |
| 1895 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1896 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1897 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1898 | InsertText(LessLoc, "/*", 2); |
| 1899 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1902 | if (!proto) |
| 1903 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1904 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1905 | const char *startBuf = SM->getCharacterData(Loc); |
| 1906 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1907 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1908 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1909 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1910 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1911 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1912 | // scan forward (from the decl location) for argument types. |
| 1913 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1914 | const char *startRef = 0, *endRef = 0; |
| 1915 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1916 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1917 | SourceLocation LessLoc = |
| 1918 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1919 | SourceLocation GreaterLoc = |
| 1920 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1921 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1922 | InsertText(LessLoc, "/*", 2); |
| 1923 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1924 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1925 | startBuf = ++endBuf; |
| 1926 | } |
| 1927 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1928 | // If the function name is derived from a macro expansion, then the |
| 1929 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1930 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1931 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1932 | startBuf++; |
| 1933 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1934 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1937 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1938 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1939 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1940 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1941 | ArgTys.push_back(Context->getPointerType( |
| 1942 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1943 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1944 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1945 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1946 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1947 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1948 | SelGetUidIdent, getFuncType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1949 | FunctionDecl::Extern, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1952 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1953 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1954 | if (FD->getIdentifier() && |
| 1955 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1956 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1957 | return; |
| 1958 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1959 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1962 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1963 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1964 | if (SuperContructorFunctionDecl) |
| 1965 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1966 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1967 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1968 | QualType argT = Context->getObjCIdType(); |
| 1969 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1970 | ArgTys.push_back(argT); |
| 1971 | ArgTys.push_back(argT); |
| 1972 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1973 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1974 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1975 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1976 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1977 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1978 | FunctionDecl::Extern, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1981 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1982 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1983 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1984 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1985 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1986 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1987 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1988 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1989 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1990 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1991 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1992 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1993 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1994 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1995 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1996 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1997 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2000 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2001 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2002 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2003 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2004 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2005 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2006 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2007 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2008 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2009 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2010 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2011 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2012 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2013 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2014 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2015 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2016 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2017 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2018 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2019 | FunctionDecl::Extern, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2022 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2023 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2024 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2025 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2026 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2027 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2028 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2029 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2030 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2031 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2032 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2033 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2034 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2035 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2036 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2037 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2038 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | // SynthMsgSendSuperStretFunctionDecl - |
| 2042 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2043 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2044 | IdentifierInfo *msgSendIdent = |
| 2045 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2046 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2047 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2048 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2049 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2050 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2051 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2052 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2053 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2054 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2055 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2056 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2057 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2058 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2059 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2060 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2061 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2062 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2065 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2066 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2067 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2068 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2069 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2070 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2071 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2072 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2073 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2074 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2075 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2076 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2077 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2078 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2079 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2080 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2081 | FunctionDecl::Extern, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2084 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2085 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2086 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2087 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2088 | ArgTys.push_back(Context->getPointerType( |
| 2089 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2090 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2091 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2092 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2093 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2094 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2095 | getClassIdent, getClassType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2096 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2099 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2100 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2101 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2102 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2103 | ArgTys.push_back(Context->getPointerType( |
| 2104 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2105 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2106 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2107 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2108 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2109 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2110 | getClassIdent, getClassType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2111 | FunctionDecl::Extern, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2114 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2115 | QualType strType = getConstantStringStructType(); |
| 2116 | |
| 2117 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2118 | |
| 2119 | std::string tmpName = InFileName; |
| 2120 | unsigned i; |
| 2121 | for (i=0; i < tmpName.length(); i++) { |
| 2122 | char c = tmpName.at(i); |
| 2123 | // replace any non alphanumeric characters with '_'. |
| 2124 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2125 | tmpName[i] = '_'; |
| 2126 | } |
| 2127 | S += tmpName; |
| 2128 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2129 | S += utostr(NumObjCStringLiterals++); |
| 2130 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2131 | Preamble += "static __NSConstantStringImpl " + S; |
| 2132 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2133 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2134 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2135 | std::string prettyBufS; |
| 2136 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2137 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2138 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2139 | Preamble += prettyBuf.str(); |
| 2140 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2141 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2142 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2143 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2144 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2145 | &Context->Idents.get(S.c_str()), strType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2146 | VarDecl::Static); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2147 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2148 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2149 | Context->getPointerType(DRE->getType()), |
| 2150 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2151 | // cast to NSConstantString * |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2152 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), |
| 2153 | CastExpr::CK_Unknown, |
| 2154 | Unop, Exp->getType(), |
| 2155 | SourceLocation(), |
| 2156 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2157 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2158 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2159 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2162 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2163 | // check if we are sending a message to 'super' |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2164 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2165 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2166 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2167 | const ObjCObjectPointerType *OPT = |
| 2168 | Super->getType()->getAsObjCObjectPointerType(); |
| 2169 | assert(OPT); |
| 2170 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2171 | return IT->getDecl(); |
| 2172 | } |
| 2173 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2177 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2178 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2179 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2180 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2181 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2182 | QualType FieldTypes[2]; |
| 2183 | |
| 2184 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2185 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2186 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2187 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2188 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2189 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2190 | for (unsigned i = 0; i < 2; ++i) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2191 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2192 | SourceLocation(), 0, |
| 2193 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2194 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2195 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2197 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2198 | } |
| 2199 | return Context->getTagDeclType(SuperStructDecl); |
| 2200 | } |
| 2201 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2202 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2203 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2204 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2205 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2206 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2207 | QualType FieldTypes[4]; |
| 2208 | |
| 2209 | // struct objc_object *receiver; |
| 2210 | FieldTypes[0] = Context->getObjCIdType(); |
| 2211 | // int flags; |
| 2212 | FieldTypes[1] = Context->IntTy; |
| 2213 | // char *str; |
| 2214 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2215 | // long length; |
| 2216 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2217 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2218 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2219 | for (unsigned i = 0; i < 4; ++i) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2220 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2221 | ConstantStringDecl, |
| 2222 | SourceLocation(), 0, |
| 2223 | FieldTypes[i], |
| 2224 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2225 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2229 | } |
| 2230 | return Context->getTagDeclType(ConstantStringDecl); |
| 2231 | } |
| 2232 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2233 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2234 | if (!SelGetUidFunctionDecl) |
| 2235 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2236 | if (!MsgSendFunctionDecl) |
| 2237 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2238 | if (!MsgSendSuperFunctionDecl) |
| 2239 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2240 | if (!MsgSendStretFunctionDecl) |
| 2241 | SynthMsgSendStretFunctionDecl(); |
| 2242 | if (!MsgSendSuperStretFunctionDecl) |
| 2243 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2244 | if (!MsgSendFpretFunctionDecl) |
| 2245 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2246 | if (!GetClassFunctionDecl) |
| 2247 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2248 | if (!GetMetaClassFunctionDecl) |
| 2249 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2250 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2251 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2252 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2253 | // May need to use objc_msgSend_stret() as well. |
| 2254 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2255 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2256 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2257 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2258 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2259 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2260 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2261 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2262 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2263 | // Synthesize a call to objc_msgSend(). |
| 2264 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2265 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2266 | |
| 2267 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2268 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2269 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2270 | // the 'super' idiom within a class method. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2271 | if (!strcmp(clsName->getName(), "super")) { |
| 2272 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2273 | if (MsgSendStretFlavor) |
| 2274 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2275 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2276 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2277 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2278 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2279 | |
| 2280 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2281 | |
| 2282 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2283 | InitExprs.push_back( |
| 2284 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2285 | CastExpr::CK_Unknown, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2286 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2287 | Context->getObjCIdType(), |
| 2288 | SourceLocation()), |
| 2289 | Context->getObjCIdType(), |
| 2290 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
| 2291 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2292 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2293 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2294 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2295 | SuperDecl->getIdentifier()->getName(), |
| 2296 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2297 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2298 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2299 | &ClsExprs[0], |
| 2300 | ClsExprs.size()); |
| 2301 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2302 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2303 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2304 | CastExpr::CK_Unknown, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2305 | Cls, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2306 | SourceLocation(), SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2307 | // struct objc_super |
| 2308 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2309 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2310 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2311 | if (LangOpts.Microsoft) { |
| 2312 | SynthSuperContructorFunctionDecl(); |
| 2313 | // Simulate a contructor call... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2314 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2315 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2316 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2317 | InitExprs.size(), |
| 2318 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2319 | // The code for super is a little tricky to prevent collision with |
| 2320 | // the structure definition in the header. The rewriter has it's own |
| 2321 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2322 | // we need the cast below. For example: |
| 2323 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2324 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2325 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2326 | Context->getPointerType(SuperRep->getType()), |
| 2327 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2328 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2329 | CastExpr::CK_Unknown, SuperRep, |
| 2330 | Context->getPointerType(superType), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2331 | SourceLocation(), SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2332 | } else { |
| 2333 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2334 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2335 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2336 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2337 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2338 | false); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2339 | // struct objc_super * |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2340 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2341 | Context->getPointerType(SuperRep->getType()), |
| 2342 | SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2343 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2344 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2345 | } else { |
| 2346 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2347 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2348 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2349 | clsName->getName(), |
| 2350 | clsName->getLength(), |
| 2351 | false, argType, |
| 2352 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2353 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2354 | &ClsExprs[0], |
| 2355 | ClsExprs.size()); |
| 2356 | MsgExprs.push_back(Cls); |
| 2357 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2358 | } else { // instance message. |
| 2359 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2361 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2362 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2363 | if (MsgSendStretFlavor) |
| 2364 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2365 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2366 | |
| 2367 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2368 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2369 | InitExprs.push_back( |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2370 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2371 | CastExpr::CK_Unknown, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2372 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2373 | Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2374 | SourceLocation()), |
| 2375 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2376 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2377 | |
| 2378 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2379 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2380 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2381 | SuperDecl->getIdentifier()->getName(), |
| 2382 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2383 | false, argType, SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2384 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2385 | &ClsExprs[0], |
| 2386 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2387 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2388 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2389 | // set 'super class', using objc_getClass(). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2390 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2391 | CastExpr::CK_Unknown, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2392 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | // struct objc_super |
| 2394 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2395 | Expr *SuperRep; |
| 2396 | |
| 2397 | if (LangOpts.Microsoft) { |
| 2398 | SynthSuperContructorFunctionDecl(); |
| 2399 | // Simulate a contructor call... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2400 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2401 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2402 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2403 | InitExprs.size(), |
| 2404 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2405 | // The code for super is a little tricky to prevent collision with |
| 2406 | // the structure definition in the header. The rewriter has it's own |
| 2407 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2408 | // we need the cast below. For example: |
| 2409 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2410 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2411 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2412 | Context->getPointerType(SuperRep->getType()), |
| 2413 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2414 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2415 | CastExpr::CK_Unknown, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2416 | SuperRep, Context->getPointerType(superType), |
| 2417 | SourceLocation(), SourceLocation()); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2418 | } else { |
| 2419 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2420 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2421 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2422 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2423 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2424 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2425 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2426 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2427 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2428 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2429 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2430 | recExpr = CE->getSubExpr(); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2431 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 2432 | CastExpr::CK_Unknown, recExpr, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2433 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2434 | SourceLocation(), SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2435 | MsgExprs.push_back(recExpr); |
| 2436 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2437 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2438 | // 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] | 2439 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2440 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2441 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2442 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2443 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2444 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2445 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2446 | &SelExprs[0], SelExprs.size()); |
| 2447 | MsgExprs.push_back(SelExp); |
| 2448 | |
| 2449 | // Now push any user supplied arguments. |
| 2450 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2451 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2452 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2453 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2454 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2455 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2456 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2457 | : ICE->getType(); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2458 | userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown, |
| 2459 | userExpr, type, SourceLocation(), |
| 2460 | SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2461 | } |
| 2462 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2463 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2464 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2465 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2466 | userExpr = CE->getSubExpr(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2467 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2468 | CastExpr::CK_Unknown, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2469 | userExpr, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2470 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2471 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2472 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2473 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2474 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2475 | // out the argument in the original expression (since we aren't deleting |
| 2476 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2477 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2478 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2479 | // Generate the funky cast. |
| 2480 | CastExpr *cast; |
| 2481 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2482 | QualType returnType; |
| 2483 | |
| 2484 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2485 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2486 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2487 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2488 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2489 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2490 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2491 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2492 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2493 | E = OMD->param_end(); PI != E; ++PI) { |
| 2494 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2495 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2496 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2497 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2498 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2499 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2500 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2501 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2502 | ArgTypes.push_back(t); |
| 2503 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2504 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2505 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2506 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2507 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2508 | } |
| 2509 | // 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] | 2510 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2511 | |
| 2512 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2513 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2514 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2515 | |
| 2516 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2517 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2518 | // xx.m:13: warning: function called through a non-compatible type |
| 2519 | // xx.m:13: note: if this code is reached, the program will abort |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2520 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2521 | CastExpr::CK_Unknown, DRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2522 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2523 | SourceLocation(), SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2524 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2525 | // Now do the "normal" pointer to function cast. |
| 2526 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2527 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2528 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2529 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2530 | castType = Context->getPointerType(castType); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2531 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast, |
| 2532 | castType, SourceLocation(), |
| 2533 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2534 | |
| 2535 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2536 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2537 | |
| 2538 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2539 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2540 | MsgExprs.size(), |
| 2541 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2542 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2543 | if (MsgSendStretFlavor) { |
| 2544 | // We have the method which returns a struct/union. Must also generate |
| 2545 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2546 | // expression which dictate which one to envoke depending on size of |
| 2547 | // method's return type. |
| 2548 | |
| 2549 | // Create a reference to the objc_msgSend_stret() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2550 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2551 | SourceLocation()); |
| 2552 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2553 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2554 | CastExpr::CK_Unknown, STDRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2555 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2556 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2557 | // Now do the "normal" pointer to function cast. |
| 2558 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2559 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2560 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2561 | castType = Context->getPointerType(castType); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2562 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, |
| 2563 | cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2564 | |
| 2565 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2566 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2567 | |
| 2568 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2569 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2570 | MsgExprs.size(), |
| 2571 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2572 | |
| 2573 | // Build sizeof(returnType) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2574 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
| 2575 | returnType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2576 | Context->getSizeType(), |
| 2577 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2579 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2580 | // For X86 it is more complicated and some kind of target specific routine |
| 2581 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2582 | unsigned IntSize = |
| 2583 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2584 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2585 | Context->IntTy, |
| 2586 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2587 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2588 | BinaryOperator::LE, |
| 2589 | Context->IntTy, |
| 2590 | SourceLocation()); |
| 2591 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2592 | ConditionalOperator *CondExpr = |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2593 | new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 2594 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2595 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2596 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2597 | return ReplacingStmt; |
| 2598 | } |
| 2599 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2600 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2601 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 80c2855 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2602 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2603 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2604 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2605 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2606 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2607 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2610 | // typedef struct objc_object Protocol; |
| 2611 | QualType RewriteObjC::getProtocolType() { |
| 2612 | if (!ProtocolTypeDecl) { |
| 2613 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 2614 | SourceLocation(), |
| 2615 | &Context->Idents.get("Protocol"), |
| 2616 | Context->getObjCIdType()); |
| 2617 | } |
| 2618 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2619 | } |
| 2620 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2621 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2622 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2623 | /// The forward references (and metadata) are generated in |
| 2624 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2625 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2626 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2627 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 2628 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2629 | ID, QualType()/*UNUSED*/, VarDecl::Extern); |
| 2630 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2631 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2632 | Context->getPointerType(DRE->getType()), |
| 2633 | SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2634 | CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(), |
| 2635 | CastExpr::CK_Unknown, |
| 2636 | DerefExpr, DerefExpr->getType(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2637 | SourceLocation(), SourceLocation()); |
| 2638 | ReplaceStmt(Exp, castExpr); |
| 2639 | ProtocolExprDecls.insert(Exp->getProtocol()); |
| 2640 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
| 2641 | return castExpr; |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2642 | |
| 2643 | } |
| 2644 | |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2645 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2646 | const char *endBuf) { |
| 2647 | while (startBuf < endBuf) { |
| 2648 | if (*startBuf == '#') { |
| 2649 | // Skip whitespace. |
| 2650 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2651 | ; |
| 2652 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2653 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2654 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2655 | !strncmp(startBuf, "define", strlen("define")) || |
| 2656 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2657 | !strncmp(startBuf, "else", strlen("else")) || |
| 2658 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2659 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2660 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2661 | !strncmp(startBuf, "include", strlen("include")) || |
| 2662 | !strncmp(startBuf, "import", strlen("import")) || |
| 2663 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2664 | return true; |
| 2665 | } |
| 2666 | startBuf++; |
| 2667 | } |
| 2668 | return false; |
| 2669 | } |
| 2670 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2671 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2672 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2673 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2674 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2675 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2676 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2677 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2678 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2679 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2680 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2681 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2682 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2683 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2684 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2685 | |
| 2686 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2687 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2688 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2689 | // If no ivars and no root or if its root, directly or indirectly, |
| 2690 | // 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] | 2691 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2692 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2693 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2694 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2695 | return; |
| 2696 | } |
| 2697 | |
| 2698 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2699 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2700 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2701 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2702 | if (LangOpts.Microsoft) |
| 2703 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2704 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2705 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2706 | const char *cursor = strchr(startBuf, '{'); |
| 2707 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2708 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2709 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2710 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2711 | // NSURL.h, for example): |
| 2712 | // |
| 2713 | // #ifdef XYZ |
| 2714 | // @interface Foo : NSObject |
| 2715 | // #else |
| 2716 | // @interface FooBar : NSObject |
| 2717 | // #endif |
| 2718 | // { |
| 2719 | // int i; |
| 2720 | // } |
| 2721 | // @end |
| 2722 | // |
| 2723 | // This clause is segregated to avoid breaking the common case. |
| 2724 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2725 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2726 | CDecl->getClassLoc(); |
| 2727 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2728 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2730 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2731 | // advance to the end of the referenced protocols. |
| 2732 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2733 | endHeader++; |
| 2734 | } |
| 2735 | // rewrite the original header |
| 2736 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2737 | } else { |
| 2738 | // rewrite the original header *without* disturbing the '{' |
| 2739 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2740 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2741 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2742 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2743 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2744 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2745 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2746 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2747 | |
| 2748 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2749 | SourceLocation OnePastCurly = |
| 2750 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2751 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2752 | } |
| 2753 | cursor++; // past '{' |
| 2754 | |
| 2755 | // Now comment out any visibility specifiers. |
| 2756 | while (cursor < endBuf) { |
| 2757 | if (*cursor == '@') { |
| 2758 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2759 | // Skip whitespace. |
| 2760 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2761 | /*scan*/; |
| 2762 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2763 | // FIXME: presence of @public, etc. inside comment results in |
| 2764 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2765 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2766 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2767 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2768 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2769 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2770 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2771 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2772 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2773 | // for type checking. |
| 2774 | else if (*cursor == '<') { |
| 2775 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2776 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2777 | cursor = strchr(cursor, '>'); |
| 2778 | cursor++; |
| 2779 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2780 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2781 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2782 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2783 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2784 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2785 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2786 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2787 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2788 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2789 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2790 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2791 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2792 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2793 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2794 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2795 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2796 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2797 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2798 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2799 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2800 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2803 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2804 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2805 | template<typename MethodIterator> |
| 2806 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2807 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2808 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2809 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2810 | const char *ClassName, |
| 2811 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2812 | if (MethodBegin == MethodEnd) return; |
| 2813 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2814 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2815 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2816 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2817 | SEL _cmd; |
| 2818 | char *method_types; |
| 2819 | void *_imp; |
| 2820 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2821 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2822 | Result += "\nstruct _objc_method {\n"; |
| 2823 | Result += "\tSEL _cmd;\n"; |
| 2824 | Result += "\tchar *method_types;\n"; |
| 2825 | Result += "\tvoid *_imp;\n"; |
| 2826 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2827 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2828 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2829 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2830 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2831 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2832 | |
| 2833 | /* struct { |
| 2834 | struct _objc_method_list *next_method; |
| 2835 | int method_count; |
| 2836 | struct _objc_method method_list[]; |
| 2837 | } |
| 2838 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2839 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2840 | Result += "\nstatic struct {\n"; |
| 2841 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2842 | Result += "\tint method_count;\n"; |
| 2843 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2844 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2845 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2846 | Result += prefix; |
| 2847 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2848 | Result += "_METHODS_"; |
| 2849 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2850 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2851 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2852 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2853 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2854 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2855 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2856 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2857 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2858 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2859 | Result += "\", \""; |
| 2860 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2861 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2862 | Result += MethodInternalNames[*MethodBegin]; |
| 2863 | Result += "}\n"; |
| 2864 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2865 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2866 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2867 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2868 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2869 | Result += "\", \""; |
| 2870 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2871 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2872 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2873 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2874 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2875 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2878 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2879 | void RewriteObjC:: |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2880 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 2881 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2882 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2883 | |
| 2884 | // Output struct protocol_methods holder of method selector and type. |
| 2885 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2886 | /* struct protocol_methods { |
| 2887 | SEL _cmd; |
| 2888 | char *method_types; |
| 2889 | } |
| 2890 | */ |
| 2891 | Result += "\nstruct _protocol_methods {\n"; |
| 2892 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 2893 | Result += "\tchar *method_types;\n"; |
| 2894 | Result += "};\n"; |
| 2895 | |
| 2896 | objc_protocol_methods = true; |
| 2897 | } |
| 2898 | // Do not synthesize the protocol more than once. |
| 2899 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2900 | return; |
| 2901 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2902 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2903 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 2904 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2905 | /* struct _objc_protocol_method_list { |
| 2906 | int protocol_method_count; |
| 2907 | struct protocol_methods protocols[]; |
| 2908 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2909 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2910 | Result += "\nstatic struct {\n"; |
| 2911 | Result += "\tint protocol_method_count;\n"; |
| 2912 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 2913 | Result += utostr(NumMethods); |
| 2914 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 2915 | Result += PDecl->getNameAsString(); |
| 2916 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2917 | "{\n\t" + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2918 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2919 | // Output instance methods declared in this protocol. |
| 2920 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2921 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2922 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2923 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2924 | Result += "\t ,{{(struct objc_selector *)\""; |
| 2925 | else |
| 2926 | Result += "\t ,{(struct objc_selector *)\""; |
| 2927 | Result += (*I)->getSelector().getAsString().c_str(); |
| 2928 | std::string MethodTypeString; |
| 2929 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2930 | Result += "\", \""; |
| 2931 | Result += MethodTypeString; |
| 2932 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2933 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2934 | Result += "\t }\n};\n"; |
| 2935 | } |
| 2936 | |
| 2937 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2938 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 2939 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2940 | if (NumMethods > 0) { |
| 2941 | /* struct _objc_protocol_method_list { |
| 2942 | int protocol_method_count; |
| 2943 | struct protocol_methods protocols[]; |
| 2944 | } |
| 2945 | */ |
| 2946 | Result += "\nstatic struct {\n"; |
| 2947 | Result += "\tint protocol_method_count;\n"; |
| 2948 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 2949 | Result += utostr(NumMethods); |
| 2950 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 2951 | Result += PDecl->getNameAsString(); |
| 2952 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2953 | "{\n\t"; |
| 2954 | Result += utostr(NumMethods); |
| 2955 | Result += "\n"; |
| 2956 | |
| 2957 | // Output instance methods declared in this protocol. |
| 2958 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2959 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2960 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2961 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2962 | Result += "\t ,{{(struct objc_selector *)\""; |
| 2963 | else |
| 2964 | Result += "\t ,{(struct objc_selector *)\""; |
| 2965 | Result += (*I)->getSelector().getAsString().c_str(); |
| 2966 | std::string MethodTypeString; |
| 2967 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2968 | Result += "\", \""; |
| 2969 | Result += MethodTypeString; |
| 2970 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2971 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2972 | Result += "\t }\n};\n"; |
| 2973 | } |
| 2974 | |
| 2975 | // Output: |
| 2976 | /* struct _objc_protocol { |
| 2977 | // Objective-C 1.0 extensions |
| 2978 | struct _objc_protocol_extension *isa; |
| 2979 | char *protocol_name; |
| 2980 | struct _objc_protocol **protocol_list; |
| 2981 | struct _objc_protocol_method_list *instance_methods; |
| 2982 | struct _objc_protocol_method_list *class_methods; |
| 2983 | }; |
| 2984 | */ |
| 2985 | static bool objc_protocol = false; |
| 2986 | if (!objc_protocol) { |
| 2987 | Result += "\nstruct _objc_protocol {\n"; |
| 2988 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2989 | Result += "\tchar *protocol_name;\n"; |
| 2990 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2991 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2992 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2993 | Result += "};\n"; |
| 2994 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2995 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2996 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2997 | |
| 2998 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 2999 | Result += PDecl->getNameAsString(); |
| 3000 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3001 | "{\n\t0, \""; |
| 3002 | Result += PDecl->getNameAsString(); |
| 3003 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3004 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3005 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3006 | Result += PDecl->getNameAsString(); |
| 3007 | Result += ", "; |
| 3008 | } |
| 3009 | else |
| 3010 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3011 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3012 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3013 | Result += PDecl->getNameAsString(); |
| 3014 | Result += "\n"; |
| 3015 | } |
| 3016 | else |
| 3017 | Result += "0\n"; |
| 3018 | Result += "};\n"; |
| 3019 | |
| 3020 | // Mark this protocol as having been generated. |
| 3021 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3022 | assert(false && "protocol already synthesized"); |
| 3023 | |
| 3024 | } |
| 3025 | |
| 3026 | void RewriteObjC:: |
| 3027 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3028 | const char *prefix, const char *ClassName, |
| 3029 | std::string &Result) { |
| 3030 | if (Protocols.empty()) return; |
| 3031 | |
| 3032 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3033 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3034 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3035 | // Output the top lovel protocol meta-data for the class. |
| 3036 | /* struct _objc_protocol_list { |
| 3037 | struct _objc_protocol_list *next; |
| 3038 | int protocol_count; |
| 3039 | struct _objc_protocol *class_protocols[]; |
| 3040 | } |
| 3041 | */ |
| 3042 | Result += "\nstatic struct {\n"; |
| 3043 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3044 | Result += "\tint protocol_count;\n"; |
| 3045 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3046 | Result += utostr(Protocols.size()); |
| 3047 | Result += "];\n} _OBJC_"; |
| 3048 | Result += prefix; |
| 3049 | Result += "_PROTOCOLS_"; |
| 3050 | Result += ClassName; |
| 3051 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3052 | "{\n\t0, "; |
| 3053 | Result += utostr(Protocols.size()); |
| 3054 | Result += "\n"; |
| 3055 | |
| 3056 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3057 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3058 | Result += " \n"; |
| 3059 | |
| 3060 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3061 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3062 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3063 | Result += "\n"; |
| 3064 | } |
| 3065 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3068 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3069 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3070 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3071 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3072 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3073 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3074 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3075 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3076 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 3077 | CDecl = CDecl->getNextClassCategory()) |
| 3078 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3079 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3081 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3082 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3083 | FullCategoryName += IDecl->getNameAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3084 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3085 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3086 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3087 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3088 | |
| 3089 | // If any of our property implementations have associated getters or |
| 3090 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3091 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3092 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3093 | Prop != PropEnd; ++Prop) { |
| 3094 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3095 | continue; |
| 3096 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3097 | continue; |
| 3098 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3099 | if (!PD) |
| 3100 | continue; |
| 3101 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3102 | InstanceMethods.push_back(Getter); |
| 3103 | if (PD->isReadOnly()) |
| 3104 | continue; |
| 3105 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3106 | InstanceMethods.push_back(Setter); |
| 3107 | } |
| 3108 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3109 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3110 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3111 | |
| 3112 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3113 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3114 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3115 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3116 | |
| 3117 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3118 | // Null CDecl is case of a category implementation with no category interface |
| 3119 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3120 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3121 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3122 | /* struct _objc_category { |
| 3123 | char *category_name; |
| 3124 | char *class_name; |
| 3125 | struct _objc_method_list *instance_methods; |
| 3126 | struct _objc_method_list *class_methods; |
| 3127 | struct _objc_protocol_list *protocols; |
| 3128 | // Objective-C 1.0 extensions |
| 3129 | uint32_t size; // sizeof (struct _objc_category) |
| 3130 | struct _objc_property_list *instance_properties; // category's own |
| 3131 | // @property decl. |
| 3132 | }; |
| 3133 | */ |
| 3134 | |
| 3135 | static bool objc_category = false; |
| 3136 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3137 | Result += "\nstruct _objc_category {\n"; |
| 3138 | Result += "\tchar *category_name;\n"; |
| 3139 | Result += "\tchar *class_name;\n"; |
| 3140 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3141 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3142 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3143 | Result += "\tunsigned int size;\n"; |
| 3144 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3145 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3146 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3147 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3148 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3149 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3150 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3151 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3152 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3153 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3154 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3155 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3156 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3157 | Result += "\t, (struct _objc_method_list *)" |
| 3158 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3159 | Result += FullCategoryName; |
| 3160 | Result += "\n"; |
| 3161 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3162 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3163 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3164 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3165 | Result += "\t, (struct _objc_method_list *)" |
| 3166 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3167 | Result += FullCategoryName; |
| 3168 | Result += "\n"; |
| 3169 | } |
| 3170 | else |
| 3171 | Result += "\t, 0\n"; |
| 3172 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3173 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3174 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3175 | Result += FullCategoryName; |
| 3176 | Result += "\n"; |
| 3177 | } |
| 3178 | else |
| 3179 | Result += "\t, 0\n"; |
| 3180 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3183 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3184 | /// ivar offset. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3185 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3186 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3187 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3188 | if (ivar->isBitField()) { |
| 3189 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3190 | // place all bitfields at offset 0. |
| 3191 | Result += "0"; |
| 3192 | } else { |
| 3193 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3194 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3195 | if (LangOpts.Microsoft) |
| 3196 | Result += "_IMPL"; |
| 3197 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3198 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3199 | Result += ")"; |
| 3200 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3203 | //===----------------------------------------------------------------------===// |
| 3204 | // Meta Data Emission |
| 3205 | //===----------------------------------------------------------------------===// |
| 3206 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3207 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3208 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3209 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3210 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3211 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3212 | if (CDecl->isImplicitInterfaceDecl()) { |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3213 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3214 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3215 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3216 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3217 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3218 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3219 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3220 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3221 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3222 | if (NumIvars > 0) { |
| 3223 | static bool objc_ivar = false; |
| 3224 | if (!objc_ivar) { |
| 3225 | /* struct _objc_ivar { |
| 3226 | char *ivar_name; |
| 3227 | char *ivar_type; |
| 3228 | int ivar_offset; |
| 3229 | }; |
| 3230 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3231 | Result += "\nstruct _objc_ivar {\n"; |
| 3232 | Result += "\tchar *ivar_name;\n"; |
| 3233 | Result += "\tchar *ivar_type;\n"; |
| 3234 | Result += "\tint ivar_offset;\n"; |
| 3235 | Result += "};\n"; |
| 3236 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3237 | objc_ivar = true; |
| 3238 | } |
| 3239 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3240 | /* struct { |
| 3241 | int ivar_count; |
| 3242 | struct _objc_ivar ivar_list[nIvars]; |
| 3243 | }; |
| 3244 | */ |
| 3245 | Result += "\nstatic struct {\n"; |
| 3246 | Result += "\tint ivar_count;\n"; |
| 3247 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3248 | Result += utostr(NumIvars); |
| 3249 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3250 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3251 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3252 | "{\n\t"; |
| 3253 | Result += utostr(NumIvars); |
| 3254 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3255 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3256 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3257 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3258 | if (!IDecl->ivar_empty()) { |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3259 | for (ObjCImplementationDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3260 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3261 | IV != IVEnd; ++IV) |
| 3262 | IVars.push_back(*IV); |
| 3263 | IVI = IVars.begin(); |
| 3264 | IVE = IVars.end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3265 | } else { |
| 3266 | IVI = CDecl->ivar_begin(); |
| 3267 | IVE = CDecl->ivar_end(); |
| 3268 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3269 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3270 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3271 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3272 | std::string TmpString, StrEncoding; |
| 3273 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3274 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3275 | Result += StrEncoding; |
| 3276 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3277 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3278 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3279 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3280 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3281 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3282 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3283 | std::string TmpString, StrEncoding; |
| 3284 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3285 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3286 | Result += StrEncoding; |
| 3287 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3288 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3289 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3296 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3297 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3298 | |
| 3299 | // If any of our property implementations have associated getters or |
| 3300 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3301 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3302 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3303 | Prop != PropEnd; ++Prop) { |
| 3304 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3305 | continue; |
| 3306 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3307 | continue; |
| 3308 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3309 | if (!PD) |
| 3310 | continue; |
| 3311 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3312 | InstanceMethods.push_back(Getter); |
| 3313 | if (PD->isReadOnly()) |
| 3314 | continue; |
| 3315 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3316 | InstanceMethods.push_back(Setter); |
| 3317 | } |
| 3318 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3319 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3320 | |
| 3321 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3322 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3323 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3324 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3325 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3326 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3327 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3328 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3329 | // Declaration of class/meta-class metadata |
| 3330 | /* struct _objc_class { |
| 3331 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3332 | const char *super_class_name; |
| 3333 | char *name; |
| 3334 | long version; |
| 3335 | long info; |
| 3336 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3337 | struct _objc_ivar_list *ivars; |
| 3338 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3339 | struct objc_cache *cache; |
| 3340 | struct objc_protocol_list *protocols; |
| 3341 | const char *ivar_layout; |
| 3342 | struct _objc_class_ext *ext; |
| 3343 | }; |
| 3344 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3345 | static bool objc_class = false; |
| 3346 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3347 | Result += "\nstruct _objc_class {\n"; |
| 3348 | Result += "\tstruct _objc_class *isa;\n"; |
| 3349 | Result += "\tconst char *super_class_name;\n"; |
| 3350 | Result += "\tchar *name;\n"; |
| 3351 | Result += "\tlong version;\n"; |
| 3352 | Result += "\tlong info;\n"; |
| 3353 | Result += "\tlong instance_size;\n"; |
| 3354 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3355 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3356 | Result += "\tstruct objc_cache *cache;\n"; |
| 3357 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3358 | Result += "\tconst char *ivar_layout;\n"; |
| 3359 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3360 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3361 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3365 | ObjCInterfaceDecl *RootClass = 0; |
| 3366 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3367 | while (SuperClass) { |
| 3368 | RootClass = SuperClass; |
| 3369 | SuperClass = SuperClass->getSuperClass(); |
| 3370 | } |
| 3371 | SuperClass = CDecl->getSuperClass(); |
| 3372 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3373 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3374 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3375 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3376 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3377 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3378 | Result += "\""; |
| 3379 | |
| 3380 | if (SuperClass) { |
| 3381 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3382 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3383 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3384 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3385 | Result += "\""; |
| 3386 | } |
| 3387 | else { |
| 3388 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3389 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3390 | Result += "\""; |
| 3391 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3392 | // 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] | 3393 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3394 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3395 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3396 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3397 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3398 | Result += "\n"; |
| 3399 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3400 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3401 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3402 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3403 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3404 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3405 | Result += ",0,0\n"; |
| 3406 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3407 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3408 | Result += "\t,0,0,0,0\n"; |
| 3409 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3410 | |
| 3411 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3412 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3413 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3414 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3415 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3416 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3417 | if (SuperClass) { |
| 3418 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3419 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3420 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3421 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3422 | Result += "\""; |
| 3423 | } |
| 3424 | else { |
| 3425 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3426 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3427 | Result += "\""; |
| 3428 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3429 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3430 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3431 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3432 | Result += ",0"; |
| 3433 | else { |
| 3434 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3435 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3436 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3437 | if (LangOpts.Microsoft) |
| 3438 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3439 | Result += ")"; |
| 3440 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3441 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3442 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3443 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3444 | Result += "\n\t"; |
| 3445 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3446 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3447 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3448 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3449 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3450 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3451 | Result += ", 0\n\t"; |
| 3452 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3453 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3454 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3455 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3456 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3457 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3458 | Result += ", 0,0\n"; |
| 3459 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3460 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3461 | Result += ",0,0,0\n"; |
| 3462 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3463 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3464 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3465 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3466 | /// and emits meta-data. |
| 3467 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3468 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3469 | int ClsDefCount = ClassImplementation.size(); |
| 3470 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3471 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3472 | // Rewrite implemented methods |
| 3473 | for (int i = 0; i < ClsDefCount; i++) |
| 3474 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3475 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3476 | for (int i = 0; i < CatDefCount; i++) |
| 3477 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3478 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3479 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3480 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3481 | int ClsDefCount = ClassImplementation.size(); |
| 3482 | int CatDefCount = CategoryImplementation.size(); |
| 3483 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3484 | // This is needed for determining instance variable offsets. |
Steve Naroff | a11440b | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3485 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3486 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3487 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3488 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3489 | |
| 3490 | // For each implemented category, write out all its meta data. |
| 3491 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3492 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3493 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3494 | // Write objc_symtab metadata |
| 3495 | /* |
| 3496 | struct _objc_symtab |
| 3497 | { |
| 3498 | long sel_ref_cnt; |
| 3499 | SEL *refs; |
| 3500 | short cls_def_cnt; |
| 3501 | short cat_def_cnt; |
| 3502 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3503 | }; |
| 3504 | */ |
| 3505 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3506 | Result += "\nstruct _objc_symtab {\n"; |
| 3507 | Result += "\tlong sel_ref_cnt;\n"; |
| 3508 | Result += "\tSEL *refs;\n"; |
| 3509 | Result += "\tshort cls_def_cnt;\n"; |
| 3510 | Result += "\tshort cat_def_cnt;\n"; |
| 3511 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3512 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3513 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3514 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3515 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3516 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3517 | + ", " + utostr(CatDefCount) + "\n"; |
| 3518 | for (int i = 0; i < ClsDefCount; i++) { |
| 3519 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3520 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3521 | Result += "\n"; |
| 3522 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3523 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3524 | for (int i = 0; i < CatDefCount; i++) { |
| 3525 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3526 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3527 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3528 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3529 | Result += "\n"; |
| 3530 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3531 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3532 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3533 | |
| 3534 | // Write objc_module metadata |
| 3535 | |
| 3536 | /* |
| 3537 | struct _objc_module { |
| 3538 | long version; |
| 3539 | long size; |
| 3540 | const char *name; |
| 3541 | struct _objc_symtab *symtab; |
| 3542 | } |
| 3543 | */ |
| 3544 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3545 | Result += "\nstruct _objc_module {\n"; |
| 3546 | Result += "\tlong version;\n"; |
| 3547 | Result += "\tlong size;\n"; |
| 3548 | Result += "\tconst char *name;\n"; |
| 3549 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3550 | Result += "};\n\n"; |
| 3551 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3552 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3553 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3554 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3555 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3556 | |
| 3557 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3558 | if (ProtocolExprDecls.size()) { |
| 3559 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3560 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 3561 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 3562 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3563 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3564 | Result += (*I)->getNameAsString(); |
| 3565 | Result += " = &_OBJC_PROTOCOL_"; |
| 3566 | Result += (*I)->getNameAsString(); |
| 3567 | Result += ";\n"; |
| 3568 | } |
| 3569 | Result += "#pragma data_seg(pop)\n\n"; |
| 3570 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3571 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3572 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3573 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3574 | Result += "&_OBJC_MODULES;\n"; |
| 3575 | Result += "#pragma data_seg(pop)\n\n"; |
| 3576 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3577 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3578 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3579 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3580 | const char *funcName, |
| 3581 | std::string Tag) { |
| 3582 | const FunctionType *AFT = CE->getFunctionType(); |
| 3583 | QualType RT = AFT->getResultType(); |
| 3584 | std::string StructRef = "struct " + Tag; |
| 3585 | std::string S = "static " + RT.getAsString() + " __" + |
| 3586 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3587 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3588 | BlockDecl *BD = CE->getBlockDecl(); |
| 3589 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3590 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3591 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3592 | // block (to reference imported block decl refs). |
| 3593 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3594 | } else if (BD->param_empty()) { |
| 3595 | S += "(" + StructRef + " *__cself)"; |
| 3596 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3597 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3598 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3599 | S += '('; |
| 3600 | // first add the implicit argument. |
| 3601 | S += StructRef + " *__cself, "; |
| 3602 | std::string ParamStr; |
| 3603 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3604 | E = BD->param_end(); AI != E; ++AI) { |
| 3605 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3606 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3607 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3608 | S += ParamStr; |
| 3609 | } |
| 3610 | if (FT->isVariadic()) { |
| 3611 | if (!BD->param_empty()) S += ", "; |
| 3612 | S += "..."; |
| 3613 | } |
| 3614 | S += ')'; |
| 3615 | } |
| 3616 | S += " {\n"; |
| 3617 | |
| 3618 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3619 | // First, emit a declaration for all "by ref" decls. |
| 3620 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3621 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3622 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3623 | std::string Name = (*I)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3624 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name, |
| 3625 | Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3626 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3627 | } |
| 3628 | // Next, emit a declaration for all "by copy" declarations. |
| 3629 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3630 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3631 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3632 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3633 | // Handle nested closure invocation. For example: |
| 3634 | // |
| 3635 | // void (^myImportedClosure)(void); |
| 3636 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3637 | // |
| 3638 | // void (^anotherClosure)(void); |
| 3639 | // anotherClosure = ^(void) { |
| 3640 | // myImportedClosure(); // import and invoke the closure |
| 3641 | // }; |
| 3642 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3643 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3644 | S += "struct __block_impl *"; |
| 3645 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3646 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3647 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3648 | } |
| 3649 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3650 | const char *cstr = RewrittenStr.c_str(); |
| 3651 | while (*cstr++ != '{') ; |
| 3652 | S += cstr; |
| 3653 | S += "\n"; |
| 3654 | return S; |
| 3655 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3656 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3657 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3658 | const char *funcName, |
| 3659 | std::string Tag) { |
| 3660 | std::string StructRef = "struct " + Tag; |
| 3661 | std::string S = "static void __"; |
| 3662 | |
| 3663 | S += funcName; |
| 3664 | S += "_block_copy_" + utostr(i); |
| 3665 | S += "(" + StructRef; |
| 3666 | S += "*dst, " + StructRef; |
| 3667 | S += "*src) {"; |
| 3668 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3669 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3670 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3671 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3672 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3673 | S += (*I)->getNameAsString(); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3674 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | } |
| 3676 | S += "\nstatic void __"; |
| 3677 | S += funcName; |
| 3678 | S += "_block_dispose_" + utostr(i); |
| 3679 | S += "(" + StructRef; |
| 3680 | S += "*src) {"; |
| 3681 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3682 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3683 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3684 | S += (*I)->getNameAsString(); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3685 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3686 | } |
| 3687 | S += "}\n"; |
| 3688 | return S; |
| 3689 | } |
| 3690 | |
| 3691 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3692 | bool hasCopyDisposeHelpers) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3693 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3694 | std::string Constructor = " " + Tag; |
| 3695 | |
| 3696 | S += " {\n struct __block_impl impl;\n"; |
| 3697 | |
| 3698 | if (hasCopyDisposeHelpers) |
| 3699 | S += " void *copy;\n void *dispose;\n"; |
| 3700 | |
| 3701 | Constructor += "(void *fp"; |
| 3702 | |
| 3703 | if (hasCopyDisposeHelpers) |
| 3704 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3705 | |
| 3706 | if (BlockDeclRefs.size()) { |
| 3707 | // Output all "by copy" declarations. |
| 3708 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3709 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3710 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3711 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3712 | std::string ArgName = "_" + FieldName; |
| 3713 | // Handle nested closure invocation. For example: |
| 3714 | // |
| 3715 | // void (^myImportedBlock)(void); |
| 3716 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3717 | // |
| 3718 | // void (^anotherBlock)(void); |
| 3719 | // anotherBlock = ^(void) { |
| 3720 | // myImportedBlock(); // import and invoke the closure |
| 3721 | // }; |
| 3722 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3723 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3724 | S += "struct __block_impl *"; |
| 3725 | Constructor += ", void *" + ArgName; |
| 3726 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3727 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3728 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3729 | Constructor += ", " + ArgName; |
| 3730 | } |
| 3731 | S += FieldName + ";\n"; |
| 3732 | } |
| 3733 | // Output all "by ref" declarations. |
| 3734 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3735 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3736 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3737 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3738 | std::string ArgName = "_" + FieldName; |
| 3739 | // Handle nested closure invocation. For example: |
| 3740 | // |
| 3741 | // void (^myImportedBlock)(void); |
| 3742 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3743 | // |
| 3744 | // void (^anotherBlock)(void); |
| 3745 | // anotherBlock = ^(void) { |
| 3746 | // myImportedBlock(); // import and invoke the closure |
| 3747 | // }; |
| 3748 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3749 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3750 | S += "struct __block_impl *"; |
| 3751 | Constructor += ", void *" + ArgName; |
| 3752 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3753 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName, |
| 3754 | Context->PrintingPolicy); |
| 3755 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName, |
| 3756 | Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3757 | Constructor += ", " + ArgName; |
| 3758 | } |
| 3759 | S += FieldName + "; // by ref\n"; |
| 3760 | } |
| 3761 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3762 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3763 | if (GlobalVarDecl) |
| 3764 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3765 | else |
| 3766 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3767 | Constructor += " impl.Size = sizeof("; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3768 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3769 | |
| 3770 | if (hasCopyDisposeHelpers) |
| 3771 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3772 | |
| 3773 | // Initialize all "by copy" arguments. |
| 3774 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3775 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3776 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3777 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3778 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3779 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3780 | else |
| 3781 | Constructor += Name + " = _"; |
| 3782 | Constructor += Name + ";\n"; |
| 3783 | } |
| 3784 | // Initialize all "by ref" arguments. |
| 3785 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3786 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3787 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3788 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3789 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3790 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3791 | else |
| 3792 | Constructor += Name + " = _"; |
| 3793 | Constructor += Name + ";\n"; |
| 3794 | } |
| 3795 | } else { |
| 3796 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3797 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3798 | if (GlobalVarDecl) |
| 3799 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3800 | else |
| 3801 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3802 | Constructor += " impl.Size = sizeof("; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3803 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3804 | if (hasCopyDisposeHelpers) |
| 3805 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3806 | } |
| 3807 | Constructor += " "; |
| 3808 | Constructor += "}\n"; |
| 3809 | S += Constructor; |
| 3810 | S += "};\n"; |
| 3811 | return S; |
| 3812 | } |
| 3813 | |
| 3814 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3815 | const char *FunName) { |
| 3816 | // Insert closures that were part of the function. |
| 3817 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3818 | |
| 3819 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3820 | |
| 3821 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3822 | |
| 3823 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3824 | ImportedBlockDecls.size() > 0); |
| 3825 | |
| 3826 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3827 | |
| 3828 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3829 | |
| 3830 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3831 | |
| 3832 | if (ImportedBlockDecls.size()) { |
| 3833 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3834 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3835 | } |
| 3836 | |
| 3837 | BlockDeclRefs.clear(); |
| 3838 | BlockByRefDecls.clear(); |
| 3839 | BlockByCopyDecls.clear(); |
| 3840 | BlockCallExprs.clear(); |
| 3841 | ImportedBlockDecls.clear(); |
| 3842 | } |
| 3843 | Blocks.clear(); |
| 3844 | RewrittenBlockExprs.clear(); |
| 3845 | } |
| 3846 | |
| 3847 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3848 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3849 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3850 | |
| 3851 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3852 | } |
| 3853 | |
| 3854 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3855 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3856 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3857 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3858 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3859 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3860 | // Convert colons to underscores. |
| 3861 | std::string::size_type loc = 0; |
| 3862 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3863 | FuncName.replace(loc, 1, "_"); |
| 3864 | |
| 3865 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3866 | } |
| 3867 | |
| 3868 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3869 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3870 | CI != E; ++CI) |
| 3871 | if (*CI) { |
| 3872 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3873 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3874 | else |
| 3875 | GetBlockDeclRefExprs(*CI); |
| 3876 | } |
| 3877 | // Handle specific things. |
| 3878 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3879 | // FIXME: Handle enums. |
| 3880 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3881 | BlockDeclRefs.push_back(CDRE); |
| 3882 | return; |
| 3883 | } |
| 3884 | |
| 3885 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3886 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3887 | CI != E; ++CI) |
| 3888 | if (*CI) { |
| 3889 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3890 | GetBlockCallExprs(CBE->getBody()); |
| 3891 | else |
| 3892 | GetBlockCallExprs(*CI); |
| 3893 | } |
| 3894 | |
| 3895 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3896 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3897 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3898 | } |
| 3899 | } |
| 3900 | return; |
| 3901 | } |
| 3902 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3903 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3904 | // Navigate to relevant type information. |
| 3905 | const char *closureName = 0; |
| 3906 | const BlockPointerType *CPT = 0; |
| 3907 | |
| 3908 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3909 | closureName = DRE->getDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3910 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3911 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3912 | closureName = CDRE->getDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3913 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3914 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3915 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3916 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3917 | } else { |
| 3918 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3919 | } |
| 3920 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3921 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3922 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3923 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3924 | // FTP will be null for closures that don't take arguments. |
| 3925 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3926 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3927 | SourceLocation(), |
| 3928 | &Context->Idents.get("__block_impl")); |
| 3929 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3930 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3931 | // Generate a funky cast. |
| 3932 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3933 | |
| 3934 | // Push the block argument type. |
| 3935 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3936 | if (FTP) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3937 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3938 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3939 | QualType t = *I; |
| 3940 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3941 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3942 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3943 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3944 | } |
| 3945 | ArgTypes.push_back(t); |
| 3946 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3947 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3948 | // Now do the pointer to function cast. |
| 3949 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3950 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3951 | |
| 3952 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3953 | |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 3954 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, |
| 3955 | CastExpr::CK_Unknown, |
| 3956 | Exp->getCallee(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3957 | PtrBlock, SourceLocation(), |
| 3958 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3959 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3960 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3961 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3962 | //PE->dump(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3964 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3965 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3966 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3967 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3968 | FD->getType()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3969 | |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 3970 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, |
| 3971 | CastExpr::CK_Unknown, ME, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3972 | PtrToFuncCastType, |
| 3973 | SourceLocation(), |
| 3974 | SourceLocation()); |
| 3975 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3976 | |
| 3977 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3978 | // Add the implicit argument. |
| 3979 | BlkExprs.push_back(BlkCast); |
| 3980 | // Add the user arguments. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3981 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3982 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3983 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3984 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3985 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3986 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3987 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3988 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3992 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3993 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3996 | // We need to return the rewritten expression to handle cases where the |
| 3997 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3998 | // For example: |
| 3999 | // |
| 4000 | // int main() { |
| 4001 | // __block Foo *f; |
| 4002 | // __block int i; |
| 4003 | // |
| 4004 | // void (^myblock)() = ^() { |
| 4005 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4006 | // i = 77; |
| 4007 | // }; |
| 4008 | //} |
| 4009 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4010 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4011 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4012 | Context->getPointerType(BDRE->getType()), |
| 4013 | SourceLocation()); |
| 4014 | // Need parens to enforce precedence. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4015 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4016 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4017 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4020 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4021 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4022 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4023 | |
| 4024 | // Need to avoid trying to rewrite synthesized casts. |
| 4025 | if (LocStart.isInvalid()) |
| 4026 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4027 | // Need to avoid trying to rewrite casts contained in macros. |
| 4028 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4029 | return; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4030 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4032 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 4033 | |
| 4034 | // advance the location to startArgList. |
| 4035 | const char *argPtr = startBuf; |
| 4036 | |
| 4037 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4038 | switch (*argPtr) { |
| 4039 | case '^': |
| 4040 | // Replace the '^' with '*'. |
| 4041 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4042 | ReplaceText(LocStart, 1, "*", 1); |
| 4043 | break; |
| 4044 | } |
| 4045 | } |
| 4046 | return; |
| 4047 | } |
| 4048 | |
| 4049 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4050 | SourceLocation DeclLoc = FD->getLocation(); |
| 4051 | unsigned parenCount = 0; |
| 4052 | |
| 4053 | // We have 1 or more arguments that have closure pointers. |
| 4054 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4055 | const char *startArgList = strchr(startBuf, '('); |
| 4056 | |
| 4057 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4058 | |
| 4059 | parenCount++; |
| 4060 | // advance the location to startArgList. |
| 4061 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4062 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4063 | |
| 4064 | const char *argPtr = startArgList; |
| 4065 | |
| 4066 | while (*argPtr++ && parenCount) { |
| 4067 | switch (*argPtr) { |
| 4068 | case '^': |
| 4069 | // Replace the '^' with '*'. |
| 4070 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4071 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4072 | break; |
| 4073 | case '(': |
| 4074 | parenCount++; |
| 4075 | break; |
| 4076 | case ')': |
| 4077 | parenCount--; |
| 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | return; |
| 4082 | } |
| 4083 | |
| 4084 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4085 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4086 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4087 | if (PT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4088 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4089 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4090 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4091 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4092 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4093 | } |
| 4094 | if (FTP) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4095 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4096 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4097 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4098 | return true; |
| 4099 | } |
| 4100 | return false; |
| 4101 | } |
| 4102 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4103 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4104 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4105 | const char *argPtr = strchr(Name, '('); |
| 4106 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4107 | |
| 4108 | LParen = argPtr; // output the start. |
| 4109 | argPtr++; // skip past the left paren. |
| 4110 | unsigned parenCount = 1; |
| 4111 | |
| 4112 | while (*argPtr && parenCount) { |
| 4113 | switch (*argPtr) { |
| 4114 | case '(': parenCount++; break; |
| 4115 | case ')': parenCount--; break; |
| 4116 | default: break; |
| 4117 | } |
| 4118 | if (parenCount) argPtr++; |
| 4119 | } |
| 4120 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4121 | RParen = argPtr; // output the end |
| 4122 | } |
| 4123 | |
| 4124 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4125 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4126 | RewriteBlockPointerFunctionArgs(FD); |
| 4127 | return; |
| 4128 | } |
| 4129 | // Handle Variables and Typedefs. |
| 4130 | SourceLocation DeclLoc = ND->getLocation(); |
| 4131 | QualType DeclT; |
| 4132 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4133 | DeclT = VD->getType(); |
| 4134 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4135 | DeclT = TDD->getUnderlyingType(); |
| 4136 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4137 | DeclT = FD->getType(); |
| 4138 | else |
| 4139 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4140 | |
| 4141 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4142 | const char *endBuf = startBuf; |
| 4143 | // scan backward (from the decl location) for the end of the previous decl. |
| 4144 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4145 | startBuf--; |
| 4146 | |
| 4147 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4148 | // may take block argument types (which will be handled below). |
| 4149 | if (*startBuf == '^') { |
| 4150 | // Replace the '^' with '*', computing a negative offset. |
| 4151 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4152 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4153 | } |
| 4154 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4155 | // Replace the '^' with '*' for arguments. |
| 4156 | DeclLoc = ND->getLocation(); |
| 4157 | startBuf = SM->getCharacterData(DeclLoc); |
| 4158 | const char *argListBegin, *argListEnd; |
| 4159 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4160 | while (argListBegin < argListEnd) { |
| 4161 | if (*argListBegin == '^') { |
| 4162 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4163 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4164 | } |
| 4165 | argListBegin++; |
| 4166 | } |
| 4167 | } |
| 4168 | return; |
| 4169 | } |
| 4170 | |
| 4171 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4172 | // Add initializers for any closure decl refs. |
| 4173 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4174 | if (BlockDeclRefs.size()) { |
| 4175 | // Unique all "by copy" declarations. |
| 4176 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4177 | if (!BlockDeclRefs[i]->isByRef()) |
| 4178 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4179 | // Unique all "by ref" declarations. |
| 4180 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4181 | if (BlockDeclRefs[i]->isByRef()) { |
| 4182 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4183 | } |
| 4184 | // Find any imported blocks...they will need special attention. |
| 4185 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4186 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4187 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4188 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4193 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4194 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4195 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4196 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4197 | ID, FType, FunctionDecl::Extern, false, |
| 4198 | false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4201 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4202 | Blocks.push_back(Exp); |
| 4203 | |
| 4204 | CollectBlockDeclRefInfo(Exp); |
| 4205 | std::string FuncName; |
| 4206 | |
| 4207 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4208 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4209 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4210 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4211 | // Convert colons to underscores. |
| 4212 | std::string::size_type loc = 0; |
| 4213 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4214 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4215 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4216 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4217 | |
| 4218 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4219 | |
| 4220 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4221 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4222 | |
| 4223 | // Get a pointer to the function type so we can cast appropriately. |
| 4224 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4225 | |
| 4226 | FunctionDecl *FD; |
| 4227 | Expr *NewRep; |
| 4228 | |
| 4229 | // Simulate a contructor call... |
| 4230 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4231 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4232 | |
| 4233 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4234 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4235 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4236 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4237 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4238 | SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4239 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4240 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4241 | Context->VoidPtrTy, SourceLocation(), |
| 4242 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4243 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4244 | |
| 4245 | if (ImportedBlockDecls.size()) { |
| 4246 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4247 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4248 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4249 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4250 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4251 | Context->VoidPtrTy, SourceLocation(), |
| 4252 | SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4253 | InitExprs.push_back(castExpr); |
| 4254 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4255 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4256 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4257 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4258 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4259 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4260 | Context->VoidPtrTy, SourceLocation(), |
| 4261 | SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4262 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4263 | } |
| 4264 | // Add initializers for any closure decl refs. |
| 4265 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4266 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4267 | // Output all "by copy" declarations. |
| 4268 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4269 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4270 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4271 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4272 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4273 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4274 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4275 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4276 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4277 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4278 | CastExpr::CK_Unknown, Arg, |
| 4279 | Context->VoidPtrTy, |
| 4280 | SourceLocation(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4281 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4282 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4283 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4284 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4285 | } |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4286 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4287 | } |
| 4288 | // Output all "by ref" declarations. |
| 4289 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4290 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4291 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4292 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4293 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4294 | Context->getPointerType(Exp->getType()), |
| 4295 | SourceLocation()); |
Steve Naroff | 707b0fe | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4296 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4299 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4300 | FType, SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4301 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4302 | Context->getPointerType(NewRep->getType()), |
| 4303 | SourceLocation()); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4304 | NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep, |
| 4305 | FType, SourceLocation(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4306 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4307 | BlockDeclRefs.clear(); |
| 4308 | BlockByRefDecls.clear(); |
| 4309 | BlockByCopyDecls.clear(); |
| 4310 | ImportedBlockDecls.clear(); |
| 4311 | return NewRep; |
| 4312 | } |
| 4313 | |
| 4314 | //===----------------------------------------------------------------------===// |
| 4315 | // Function Body / Expression rewriting |
| 4316 | //===----------------------------------------------------------------------===// |
| 4317 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4318 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4319 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4320 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4321 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4322 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4323 | // we get this right. |
| 4324 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4325 | // Perform a bottom up traversal of all children. |
| 4326 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4327 | CI != E; ++CI) |
| 4328 | if (*CI) |
| 4329 | CollectPropertySetters(*CI); |
| 4330 | |
| 4331 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4332 | if (BinOp->isAssignmentOp()) { |
| 4333 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4334 | PropSetters[PRE] = BinOp; |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4339 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4340 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4341 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4342 | Stmts.push_back(S); |
| 4343 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4344 | Stmts.push_back(S); |
| 4345 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4346 | } |
| 4347 | |
| 4348 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4349 | |
| 4350 | // Perform a bottom up rewrite of all children. |
| 4351 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4352 | CI != E; ++CI) |
| 4353 | if (*CI) { |
| 4354 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4355 | if (newStmt) |
| 4356 | *CI = newStmt; |
| 4357 | } |
| 4358 | |
| 4359 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4360 | // Rewrite the block body in place. |
| 4361 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4362 | |
| 4363 | // 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] | 4364 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4365 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4366 | |
| 4367 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4368 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4369 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4370 | return blockTranscribed; |
| 4371 | } |
| 4372 | // Handle specific things. |
| 4373 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4374 | return RewriteAtEncode(AtEncode); |
| 4375 | |
| 4376 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4377 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4378 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4379 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4380 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4381 | if (BinOp) { |
| 4382 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4383 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4384 | DisableReplaceStmt = true; |
| 4385 | // Save the source range. Even if we disable the replacement, the |
| 4386 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4387 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4388 | // self.errorHandler = handler ? handler : |
| 4389 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4390 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4391 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4392 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4393 | // |
| 4394 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4395 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4396 | // to see the original expression). Consider this example: |
| 4397 | // |
| 4398 | // Foo *obj1, *obj2; |
| 4399 | // |
| 4400 | // obj1.i = [obj2 rrrr]; |
| 4401 | // |
| 4402 | // 'BinOp' for the previous expression looks like: |
| 4403 | // |
| 4404 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4405 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4406 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4407 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4408 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4409 | // |
| 4410 | // 'newStmt' represents the rewritten message expression. For example: |
| 4411 | // |
| 4412 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4413 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4414 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4415 | // (CStyleCastExpr 0x231d220 'void *' |
| 4416 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4417 | // |
| 4418 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4419 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4420 | // the original RHS (since we haven't altered BinOp). |
| 4421 | // |
| 4422 | // This implies the Rewrite* routines can no longer delete the original |
| 4423 | // node. As a result, we now leak the original AST nodes. |
| 4424 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4425 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4426 | } else { |
| 4427 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4430 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4431 | return RewriteAtSelector(AtSelector); |
| 4432 | |
| 4433 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4434 | return RewriteObjCStringLiteral(AtString); |
| 4435 | |
| 4436 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4437 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4438 | // Before we rewrite it, put the original message expression in a comment. |
| 4439 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4440 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4441 | |
| 4442 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4443 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4444 | |
| 4445 | std::string messString; |
| 4446 | messString += "// "; |
| 4447 | messString.append(startBuf, endBuf-startBuf+1); |
| 4448 | messString += "\n"; |
| 4449 | |
| 4450 | // FIXME: Missing definition of |
| 4451 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4452 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4453 | // Tried this, but it didn't work either... |
| 4454 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4455 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4456 | return RewriteMessageExpr(MessExpr); |
| 4457 | } |
| 4458 | |
| 4459 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4460 | return RewriteObjCTryStmt(StmtTry); |
| 4461 | |
| 4462 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4463 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4464 | |
| 4465 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4466 | return RewriteObjCThrowStmt(StmtThrow); |
| 4467 | |
| 4468 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4469 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4470 | |
| 4471 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4472 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4473 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4474 | OrigStmtRange.getEnd()); |
| 4475 | if (BreakStmt *StmtBreakStmt = |
| 4476 | dyn_cast<BreakStmt>(S)) |
| 4477 | return RewriteBreakStmt(StmtBreakStmt); |
| 4478 | if (ContinueStmt *StmtContinueStmt = |
| 4479 | dyn_cast<ContinueStmt>(S)) |
| 4480 | return RewriteContinueStmt(StmtContinueStmt); |
| 4481 | |
| 4482 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4483 | // and cast exprs. |
| 4484 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4485 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4486 | // precedes the first Decl. In the future the DeclGroup should have |
| 4487 | // a separate type-specifier that we can rewrite. |
| 4488 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4489 | |
| 4490 | // Blocks rewrite rules. |
| 4491 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4492 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4493 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4494 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4495 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4496 | RewriteBlockPointerDecl(ND); |
| 4497 | else if (ND->getType()->isFunctionPointerType()) |
| 4498 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4499 | } |
| 4500 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4501 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4502 | RewriteBlockPointerDecl(TD); |
| 4503 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4504 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4505 | } |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4510 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4511 | |
| 4512 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4513 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4514 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4515 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4516 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4517 | && "Statement stack mismatch"); |
| 4518 | Stmts.pop_back(); |
| 4519 | } |
| 4520 | // Handle blocks rewriting. |
| 4521 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4522 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4523 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4524 | } |
| 4525 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4526 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4527 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4528 | ReplaceStmt(S, BlockCall); |
| 4529 | return BlockCall; |
| 4530 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4531 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4532 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4533 | RewriteCastExpr(CE); |
| 4534 | } |
| 4535 | #if 0 |
| 4536 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4537 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4538 | // Get the new text. |
| 4539 | std::string SStr; |
| 4540 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4541 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4542 | const std::string &Str = Buf.str(); |
| 4543 | |
| 4544 | printf("CAST = %s\n", &Str[0]); |
| 4545 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4546 | delete S; |
| 4547 | return Replacement; |
| 4548 | } |
| 4549 | #endif |
| 4550 | // Return this stmt unmodified. |
| 4551 | return S; |
| 4552 | } |
| 4553 | |
| 4554 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4555 | /// main file of the input. |
| 4556 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4557 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4558 | if (FD->isOverloadedOperator()) |
| 4559 | return; |
| 4560 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4561 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4562 | // prototype. This enables us to rewrite function declarations and |
| 4563 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4564 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4565 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 4566 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4567 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4568 | CurFunctionDef = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4569 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4570 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4571 | Body = |
| 4572 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4573 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4574 | CurrentBody = 0; |
| 4575 | if (PropParentMap) { |
| 4576 | delete PropParentMap; |
| 4577 | PropParentMap = 0; |
| 4578 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4579 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4580 | // and any copy/dispose helper functions. |
| 4581 | InsertBlockLiteralsWithinFunction(FD); |
| 4582 | CurFunctionDef = 0; |
| 4583 | } |
| 4584 | return; |
| 4585 | } |
| 4586 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4587 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4588 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4589 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4590 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4591 | Body = |
| 4592 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4593 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4594 | CurrentBody = 0; |
| 4595 | if (PropParentMap) { |
| 4596 | delete PropParentMap; |
| 4597 | PropParentMap = 0; |
| 4598 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4599 | InsertBlockLiteralsWithinMethod(MD); |
| 4600 | CurMethodDef = 0; |
| 4601 | } |
| 4602 | } |
| 4603 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4604 | ClassImplementation.push_back(CI); |
| 4605 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4606 | CategoryImplementation.push_back(CI); |
| 4607 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4608 | RewriteForwardClassDecl(CD); |
| 4609 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4610 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4611 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4612 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4613 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4614 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4615 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4616 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4617 | RewriteCastExpr(CE); |
| 4618 | } |
| 4619 | } |
| 4620 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4621 | if (VD->getInit()) { |
| 4622 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4623 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4624 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4625 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4626 | CurrentBody = 0; |
| 4627 | if (PropParentMap) { |
| 4628 | delete PropParentMap; |
| 4629 | PropParentMap = 0; |
| 4630 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4631 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4632 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4633 | GlobalVarDecl = 0; |
| 4634 | |
| 4635 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4636 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4637 | RewriteCastExpr(CE); |
| 4638 | } |
| 4639 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4640 | return; |
| 4641 | } |
| 4642 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4643 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4644 | RewriteBlockPointerDecl(TD); |
| 4645 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4646 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4647 | return; |
| 4648 | } |
| 4649 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4650 | if (RD->isDefinition()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4651 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4652 | e = RD->field_end(); i != e; ++i) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4653 | FieldDecl *FD = *i; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4654 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4655 | RewriteBlockPointerDecl(FD); |
| 4656 | } |
| 4657 | } |
| 4658 | return; |
| 4659 | } |
| 4660 | // Nothing yet. |
| 4661 | } |
| 4662 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4663 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4664 | // Get the top-level buffer that this corresponds to. |
| 4665 | |
| 4666 | // Rewrite tabs if we care. |
| 4667 | //RewriteTabs(); |
| 4668 | |
| 4669 | if (Diags.hasErrorOccurred()) |
| 4670 | return; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4671 | |
| 4672 | RewriteInclude(); |
| 4673 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4674 | // Here's a great place to add any extra declarations that may be needed. |
| 4675 | // Write out meta data for each @protocol(<expr>). |
| 4676 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 4677 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 4678 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 4679 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 4680 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4681 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4682 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4683 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4684 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4685 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4686 | // we are done. |
| 4687 | if (const RewriteBuffer *RewriteBuf = |
| 4688 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4689 | //printf("Changed:\n"); |
| 4690 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4691 | } else { |
| 4692 | fprintf(stderr, "No changes\n"); |
| 4693 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4694 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4695 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 4696 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4697 | // Rewrite Objective-c meta data* |
| 4698 | std::string ResultStr; |
| 4699 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4700 | // Emit metadata. |
| 4701 | *OutFile << ResultStr; |
| 4702 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4703 | OutFile->flush(); |
| 4704 | } |
| 4705 | |