Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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 | e99c832 | 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 | 9f30fc3 | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 28 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 29 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 32 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 33 | Rewriter Rewrite; |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 34 | Diagnostic &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 35 | const LangOptions &LangOpts; |
Steve Naroff | 7b3579b | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 36 | unsigned RewriteFailedDiag; |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 37 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 39 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 40 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 41 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 42 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 43 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 44 | SourceLocation LastIncLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 46 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 47 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 48 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 49 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 50 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 51 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 52 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 53 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 54 | // Remember all the @protocol(<expr>) expressions. |
| 55 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 57 | unsigned NumObjCStringLiterals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 59 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 60 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 61 | FunctionDecl *MsgSendStretFunctionDecl; |
| 62 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 63 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 64 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 65 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 66 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 67 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 68 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 70 | // ObjC string constant support. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 71 | VarDecl *ConstantStringClassReference; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 72 | RecordDecl *NSStringRecord; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Fariborz Jahanian | 19d42bf | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 74 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 75 | int BcLabelCount; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 77 | // Needed for super. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 78 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 79 | RecordDecl *SuperStructDecl; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 80 | RecordDecl *ConstantStringDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 82 | TypeDecl *ProtocolTypeDecl; |
| 83 | QualType getProtocolType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 85 | // Needed for header files being rewritten |
| 86 | bool IsHeader; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 88 | std::string InFileName; |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 89 | llvm::raw_ostream* OutFile; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 90 | |
| 91 | bool SilenceRewriteMacroWarning; |
| 92 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 93 | std::string Preamble; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 94 | |
| 95 | // Block expressions. |
| 96 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 97 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 98 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 100 | // Block related declarations. |
| 101 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 102 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 104 | |
| 105 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 106 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 107 | // This maps a property to it's assignment statement. |
| 108 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 109 | // This maps a property to it's synthesied message expression. |
| 110 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 111 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 113 | // This maps an original source AST to it's rewritten form. This allows |
| 114 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 115 | // This is needed to support some of the exotic property rewriting. |
| 116 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 117 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 118 | FunctionDecl *CurFunctionDef; |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 119 | VarDecl *GlobalVarDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 121 | bool DisableReplaceStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 123 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 124 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 125 | virtual void Initialize(ASTContext &context); |
| 126 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 127 | // Top Level Driver code. |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 128 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 129 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 130 | HandleTopLevelSingleDecl(*I); |
| 131 | } |
| 132 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 133 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 134 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 135 | Diagnostic &D, const LangOptions &LOpts, |
| 136 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 137 | |
| 138 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 140 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 142 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 143 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 145 | if (ReplacingStmt) |
| 146 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 147 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 148 | if (DisableReplaceStmt) |
| 149 | return; // Used when rewriting the assignment of a property setter. |
| 150 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 151 | // If replacement succeeded or warning disabled return with no warning. |
| 152 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 153 | ReplacedNodes[Old] = New; |
| 154 | return; |
| 155 | } |
| 156 | if (SilenceRewriteMacroWarning) |
| 157 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 158 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 159 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 160 | } |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 161 | |
| 162 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 163 | // Measaure the old text. |
| 164 | int Size = Rewrite.getRangeSize(SrcRange); |
| 165 | if (Size == -1) { |
| 166 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 167 | << Old->getSourceRange(); |
| 168 | return; |
| 169 | } |
| 170 | // Get the new text. |
| 171 | std::string SStr; |
| 172 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 173 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 174 | const std::string &Str = S.str(); |
| 175 | |
| 176 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 177 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 178 | ReplacedNodes[Old] = New; |
| 179 | return; |
| 180 | } |
| 181 | if (SilenceRewriteMacroWarning) |
| 182 | return; |
| 183 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 184 | << Old->getSourceRange(); |
| 185 | } |
| 186 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 187 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 188 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 189 | // If insertion succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 190 | if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen), |
| 191 | InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 192 | SilenceRewriteMacroWarning) |
| 193 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 195 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 9cc55f5 | 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; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 203 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 204 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 9cc55f5 | 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. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 209 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 210 | llvm::StringRef(NewStr, NewLength)) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 211 | SilenceRewriteMacroWarning) |
| 212 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 214 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 217 | // Syntactic Rewriting. |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 218 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 219 | void RewriteInclude(); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 220 | void RewriteTabs(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 222 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 223 | ObjCImplementationDecl *IMD, |
| 224 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 225 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 226 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 227 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 228 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 229 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 230 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 231 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 232 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 233 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 234 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 235 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 236 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 237 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 238 | QualType getSuperStructType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 239 | QualType getConstantStringStructType(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 240 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 242 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 243 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 244 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 246 | Stmt *CurrentBody; |
| 247 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 249 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 250 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 251 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 253 | SourceRange SrcRange); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 254 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 255 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 256 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 257 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 258 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 259 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 260 | void RewriteTryReturnStmts(Stmt *S); |
| 261 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 262 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 263 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 264 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 265 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 266 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 267 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 268 | SourceLocation OrigEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 270 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 271 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 272 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 273 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 274 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 276 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 277 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 278 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 279 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 280 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 281 | void SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 282 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 283 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 284 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 286 | // Metadata emission. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 287 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 288 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 290 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 291 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 293 | template<typename MethodIterator> |
| 294 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 295 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 296 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 297 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 298 | const char *ClassName, |
| 299 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 301 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 302 | const char *prefix, |
| 303 | const char *ClassName, |
| 304 | std::string &Result); |
| 305 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | const char *prefix, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 307 | const char *ClassName, |
| 308 | std::string &Result); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 309 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 310 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 312 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 313 | std::string &Result); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 314 | void RewriteImplementations(); |
| 315 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 317 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 319 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 321 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 322 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
| 324 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 325 | void RewriteBlockCall(CallExpr *Exp); |
| 326 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 327 | void RewriteByRefVar(NamedDecl *VD); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 328 | Stmt *RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 329 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
| 331 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 332 | const char *funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 334 | const char *funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 335 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 336 | std::string Tag, std::string Desc); |
| 337 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 338 | std::string ImplTag, |
| 339 | int i, const char *funcName, |
| 340 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 341 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 342 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 343 | const char *FunName); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 344 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 346 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 347 | void GetBlockCallExprs(Stmt *S); |
| 348 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 350 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 351 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 352 | bool isTopLevelBlockPointerType(QualType T) { |
| 353 | return isa<BlockPointerType>(T); |
| 354 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 356 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 357 | bool isObjCType(QualType T) { |
| 358 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 359 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 363 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 364 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 365 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 367 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 369 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 375 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 376 | const char *&RParen); |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 377 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 379 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 380 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 382 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 384 | if (From[i] == '"') |
| 385 | To += "\\\""; |
| 386 | else |
| 387 | To += From[i]; |
| 388 | } |
| 389 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 390 | }; |
| 391 | } |
| 392 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 394 | NamedDecl *D) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 395 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 397 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 398 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 399 | // All the args are checked/rewritten. Don't call twice! |
| 400 | RewriteBlockPointerDecl(D); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 407 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 408 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 409 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 412 | static bool IsHeaderFile(const std::string &Filename) { |
| 413 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 415 | if (DotPos == std::string::npos) { |
| 416 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 420 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 421 | // C header: .h |
| 422 | // C++ header: .hh or .H; |
| 423 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 425 | |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 426 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 427 | Diagnostic &D, const LangOptions &LOpts, |
| 428 | bool silenceMacroWarn) |
| 429 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 430 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 431 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 433 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 435 | "rewriter doesn't support user-specified control flow semantics " |
| 436 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 439 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 440 | llvm::raw_ostream* OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | Diagnostic &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 442 | const LangOptions &LOpts, |
| 443 | bool SilenceRewriteMacroWarning) { |
| 444 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 445 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 446 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 447 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 448 | Context = &context; |
| 449 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 450 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 451 | MsgSendFunctionDecl = 0; |
| 452 | MsgSendSuperFunctionDecl = 0; |
| 453 | MsgSendStretFunctionDecl = 0; |
| 454 | MsgSendSuperStretFunctionDecl = 0; |
| 455 | MsgSendFpretFunctionDecl = 0; |
| 456 | GetClassFunctionDecl = 0; |
| 457 | GetMetaClassFunctionDecl = 0; |
| 458 | SelGetUidFunctionDecl = 0; |
| 459 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 460 | ConstantStringClassReference = 0; |
| 461 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 462 | CurMethodDef = 0; |
| 463 | CurFunctionDef = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 464 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 465 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 466 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 467 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 468 | BcLabelCount = 0; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 469 | SuperContructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 470 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 471 | PropParentMap = 0; |
| 472 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 473 | DisableReplaceStmt = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 475 | // Get the ID and start/end of the main file. |
| 476 | MainFileID = SM->getMainFileID(); |
| 477 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 478 | MainFileStart = MainBuf->getBufferStart(); |
| 479 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 481 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 483 | // declaring objc_selector outside the parameter list removes a silly |
| 484 | // scope related warning... |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 485 | if (IsHeader) |
Steve Naroff | fcc6fd5 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 486 | Preamble = "#pragma once\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 487 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 488 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 489 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 490 | if (LangOpts.Microsoft) { |
| 491 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 492 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 493 | ": "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 494 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 495 | } |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 496 | Preamble += "};\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 497 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 498 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 499 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 500 | Preamble += "#endif\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 501 | if (LangOpts.Microsoft) { |
| 502 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 503 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 504 | } else |
| 505 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 506 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 507 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 508 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 509 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 510 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 511 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 512 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 513 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 514 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 515 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 516 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 517 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 518 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 519 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 520 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 521 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 522 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 523 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 524 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | d30f8c5 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 525 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 8dd1525 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 526 | // @synchronized hooks. |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 527 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 528 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 529 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 530 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 531 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 532 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | 4dbab8a | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 533 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 534 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 535 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 536 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 537 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 538 | Preamble += "#endif\n"; |
| 539 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 540 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 541 | Preamble += " int *isa;\n"; |
| 542 | Preamble += " int flags;\n"; |
| 543 | Preamble += " char *str;\n"; |
| 544 | Preamble += " long length;\n"; |
| 545 | Preamble += "};\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 546 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 547 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 548 | Preamble += "#else\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 549 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 550 | Preamble += "#endif\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 551 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 552 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 553 | // Blocks preamble. |
| 554 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 555 | Preamble += "#define BLOCK_IMPL\n"; |
| 556 | Preamble += "struct __block_impl {\n"; |
| 557 | Preamble += " void *isa;\n"; |
| 558 | Preamble += " int Flags;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 559 | Preamble += " int Reserved;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 560 | Preamble += " void *FuncPtr;\n"; |
| 561 | Preamble += "};\n"; |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 562 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | 287a2bf | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 563 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 564 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_assign(void *, const void *, const int);\n"; |
| 565 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 566 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 567 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 568 | Preamble += "#else\n"; |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 569 | Preamble += "__OBJC_RW_DLLIMPORT \"C\" void _Block_object_assign(void *, const void *, const int);\n"; |
| 570 | Preamble += "__OBJC_RW_DLLIMPORT \"C\" void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 571 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 572 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 573 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 574 | Preamble += "#endif\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 575 | if (LangOpts.Microsoft) { |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 576 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 577 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 578 | Preamble += "#define __attribute__(X)\n"; |
| 579 | } |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 583 | //===----------------------------------------------------------------------===// |
| 584 | // Top Level Driver Code |
| 585 | //===----------------------------------------------------------------------===// |
| 586 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 587 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 588 | // Two cases: either the decl could be in the main file, or it could be in a |
| 589 | // #included file. If the former, rewrite it now. If the later, check to see |
| 590 | // if we rewrote the #include/#import. |
| 591 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 592 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 594 | // If this is for a builtin, ignore it. |
| 595 | if (Loc.isInvalid()) return; |
| 596 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 597 | // Look for built-in declarations that we need to refer during the rewrite. |
| 598 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 599 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 600 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 601 | // declared in <Foundation/NSString.h> |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 602 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 603 | ConstantStringClassReference = FVD; |
| 604 | return; |
| 605 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 606 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 607 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 608 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 609 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 610 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 611 | RewriteProtocolDecl(PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 613 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 614 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 615 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 616 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 617 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 618 | DIEnd = LSD->decls_end(); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 619 | DI != DIEnd; ++DI) |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 620 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 621 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 622 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | d61ed3b | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 623 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 624 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 627 | //===----------------------------------------------------------------------===// |
| 628 | // Syntactic (non-AST) Rewriting Code |
| 629 | //===----------------------------------------------------------------------===// |
| 630 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 631 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 632 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 633 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 634 | const char *MainBufStart = MainBuf.first; |
| 635 | const char *MainBufEnd = MainBuf.second; |
| 636 | size_t ImportLen = strlen("import"); |
| 637 | size_t IncludeLen = strlen("include"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 639 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 640 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 641 | if (*BufPtr == '#') { |
| 642 | if (++BufPtr == MainBufEnd) |
| 643 | return; |
| 644 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 645 | if (++BufPtr == MainBufEnd) |
| 646 | return; |
| 647 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 648 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | SourceLocation ImportLoc = |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 650 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 651 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 652 | BufPtr += ImportLen; |
| 653 | } |
| 654 | } |
| 655 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 658 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 659 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 660 | const char *MainBufStart = MainBuf.first; |
| 661 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 663 | // Loop over the whole file, looking for tabs. |
| 664 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 665 | if (*BufPtr != '\t') |
| 666 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 668 | // Okay, we found a tab. This tab will turn into at least one character, |
| 669 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 670 | unsigned VCol = 0; |
| 671 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 672 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 673 | ++VCol; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 675 | // Okay, now that we know the virtual column, we know how many spaces to |
| 676 | // insert. We assume 8-character tab-stops. |
| 677 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 679 | // Get the location of the tab. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 680 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 681 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 683 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 684 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 685 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 688 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 689 | ObjCIvarDecl *OID) { |
| 690 | std::string S; |
| 691 | S = "((struct "; |
| 692 | S += ClassDecl->getIdentifier()->getName(); |
| 693 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 694 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 695 | return S; |
| 696 | } |
| 697 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 698 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 699 | ObjCImplementationDecl *IMD, |
| 700 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 701 | SourceLocation startLoc = PID->getLocStart(); |
| 702 | InsertText(startLoc, "// ", 3); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 703 | const char *startBuf = SM->getCharacterData(startLoc); |
| 704 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 705 | const char *semiBuf = strchr(startBuf, ';'); |
| 706 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 707 | SourceLocation onePastSemiLoc = |
| 708 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 709 | |
| 710 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 711 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 713 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 714 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 715 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 716 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 718 | if (!OID) |
| 719 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 721 | std::string Getr; |
| 722 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 723 | Getr += "{ "; |
| 724 | // Synthesize an explicit cast to gain access to the ivar. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | // FIXME: deal with code generation implications for various property |
| 726 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 0629704 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 727 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 728 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 729 | Getr += "; }"; |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 730 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 731 | if (PD->isReadOnly()) |
| 732 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 734 | // Generate the 'setter' function. |
| 735 | std::string Setr; |
| 736 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 737 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 738 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | // FIXME: deal with code generation implications for various property |
| 740 | // attributes (copy, retain, nonatomic). |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 741 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 742 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 743 | Setr += PD->getNameAsCString(); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 744 | Setr += "; }"; |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 745 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 746 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 747 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 748 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 749 | // Get the start location and compute the semi location. |
| 750 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 751 | const char *startBuf = SM->getCharacterData(startLoc); |
| 752 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 754 | // Translate to typedef's that forward reference structs with the same name |
| 755 | // as the class. As a convenience, we include the original declaration |
| 756 | // as a comment. |
| 757 | std::string typedefString; |
| 758 | typedefString += "// "; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 759 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 760 | typedefString += "\n"; |
Chris Lattner | 9ee23b7 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 761 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 762 | I != E; ++I) { |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 763 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 764 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 765 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 766 | typedefString += "\n"; |
| 767 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 768 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 769 | typedefString += "\n"; |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 770 | typedefString += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 771 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 772 | typedefString += ";\n#endif\n"; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 773 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 775 | // Replace the @class with typedefs corresponding to the classes. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | ReplaceText(startLoc, semiPtr-startBuf+1, |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 777 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 780 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 781 | SourceLocation LocStart = Method->getLocStart(); |
| 782 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 784 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 785 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | e020fa1 | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 786 | InsertText(LocStart, "#if 0\n", 6); |
| 787 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 788 | } else { |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 789 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 794 | SourceLocation Loc = prop->getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 796 | ReplaceText(Loc, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 798 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 801 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 802 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 804 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 805 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
| 807 | for (ObjCCategoryDecl::instmeth_iterator |
| 808 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 809 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 810 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 812 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 813 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 814 | RewriteMethodDeclaration(*I); |
| 815 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 816 | // Lastly, comment out the @end. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 817 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 820 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 821 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 823 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 825 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 826 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
| 828 | for (ObjCProtocolDecl::instmeth_iterator |
| 829 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 830 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 831 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 832 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 833 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 834 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 835 | RewriteMethodDeclaration(*I); |
| 836 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 837 | // Lastly, comment out the @end. |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 838 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 839 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 840 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 841 | // Must comment out @optional/@required |
| 842 | const char *startBuf = SM->getCharacterData(LocStart); |
| 843 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 844 | for (const char *p = startBuf; p < endBuf; p++) { |
| 845 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 846 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 847 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 848 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 849 | CommentedOptional.c_str(), CommentedOptional.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 851 | } |
| 852 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 853 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 854 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 855 | ReplaceText(OptionalLoc, strlen("@required"), |
| 856 | CommentedRequired.c_str(), CommentedRequired.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 862 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 863 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 864 | if (LocStart.isInvalid()) |
| 865 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 866 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 867 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 871 | std::string &ResultStr) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 872 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 873 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 874 | ResultStr += "\nstatic "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 875 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 876 | ResultStr += "id"; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 877 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 878 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 879 | // needs special handling, since pointer-to-functions have special |
| 880 | // syntax (where a decaration models use). |
| 881 | QualType retType = OMD->getResultType(); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 882 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 883 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 884 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 885 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 886 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 887 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 888 | ResultStr += FPRetType->getResultType().getAsString(); |
| 889 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 890 | } |
| 891 | } else |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 892 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 893 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 895 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 896 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 898 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 899 | NameStr += "_I_"; |
| 900 | else |
| 901 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 903 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 904 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
| 906 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 907 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 908 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 909 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 910 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 912 | { |
| 913 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 914 | int len = selString.size(); |
| 915 | for (int i = 0; i < len; i++) |
| 916 | if (selString[i] == ':') |
| 917 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 918 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 919 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 920 | // Remember this name for metadata emission |
| 921 | MethodInternalNames[OMD] = NameStr; |
| 922 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 924 | // Rewrite arguments |
| 925 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 927 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 928 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 929 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 930 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 931 | if (!LangOpts.Microsoft) { |
| 932 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 933 | ResultStr += "struct "; |
| 934 | } |
| 935 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 936 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 937 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 938 | } |
| 939 | else |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 940 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 942 | ResultStr += " self, "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 943 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 944 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 946 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 947 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 948 | E = OMD->param_end(); PI != E; ++PI) { |
| 949 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 950 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 951 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 952 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 953 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 954 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 955 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 956 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 957 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 958 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 959 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 960 | Context->PrintingPolicy); |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 961 | } else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 962 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 963 | ResultStr += Name; |
| 964 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 965 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 966 | if (OMD->isVariadic()) |
| 967 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 968 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 970 | if (FPRetType) { |
| 971 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 973 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 974 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 975 | ResultStr += "("; |
| 976 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 977 | if (i) ResultStr += ", "; |
| 978 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 979 | ResultStr += ParamStr; |
| 980 | } |
| 981 | if (FT->isVariadic()) { |
| 982 | if (FT->getNumArgs()) ResultStr += ", "; |
| 983 | ResultStr += "..."; |
| 984 | } |
| 985 | ResultStr += ")"; |
| 986 | } else { |
| 987 | ResultStr += "()"; |
| 988 | } |
| 989 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 990 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 991 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 992 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 993 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 995 | if (IMD) |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 996 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 997 | else |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 998 | InsertText(CID->getLocStart(), "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1001 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1002 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1003 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1005 | ObjCMethodDecl *OMD = *I; |
| 1006 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1007 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1008 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1009 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1010 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1011 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1012 | ReplaceText(LocStart, endBuf-startBuf, |
| 1013 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1014 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1016 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1017 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1018 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1019 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1020 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1021 | ObjCMethodDecl *OMD = *I; |
| 1022 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1023 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1024 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1026 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1027 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1028 | ReplaceText(LocStart, endBuf-startBuf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1030 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1031 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1032 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1034 | I != E; ++I) { |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1035 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1038 | if (IMD) |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1039 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1040 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1044 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1045 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1046 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1047 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1048 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1049 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1050 | ResultStr += "\n"; |
| 1051 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1052 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1053 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1054 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1055 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1056 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1057 | // Mark this typedef as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1058 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1059 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1060 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
| 1062 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1063 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1064 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1066 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1067 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1068 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1070 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1071 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1072 | RewriteMethodDeclaration(*I); |
| 1073 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1074 | // Lastly, comment out the @end. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1075 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1078 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1079 | SourceRange SrcRange) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1080 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1081 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1082 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1083 | ObjCMessageExpr *MsgExpr; |
| 1084 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1085 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1086 | ExprVec.push_back(newStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1088 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1089 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1090 | if (PRE && PropGetters[PRE]) { |
| 1091 | // This allows us to handle chain/nested property getters. |
| 1092 | Receiver = PropGetters[PRE]; |
| 1093 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1095 | PDecl->getSetterName(), PDecl->getType(), |
| 1096 | PDecl->getSetterMethodDecl(), |
| 1097 | SourceLocation(), SourceLocation(), |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1098 | &ExprVec[0], 1); |
| 1099 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1101 | // Now do the actual rewrite. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1102 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | df70577 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1103 | //delete BinOp; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1104 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1105 | // to things that stay around. |
| 1106 | Context->Deallocate(MsgExpr); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1107 | return ReplacingStmt; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1110 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1111 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1112 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1113 | ObjCMessageExpr *MsgExpr; |
| 1114 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1116 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1118 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1119 | if (PRE && PropGetters[PRE]) { |
| 1120 | // This allows us to handle chain/nested property getters. |
| 1121 | Receiver = PropGetters[PRE]; |
| 1122 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1124 | PDecl->getGetterName(), PDecl->getType(), |
| 1125 | PDecl->getGetterMethodDecl(), |
| 1126 | SourceLocation(), SourceLocation(), |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1127 | 0, 0); |
| 1128 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1129 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1130 | |
| 1131 | if (!PropParentMap) |
| 1132 | PropParentMap = new ParentMap(CurrentBody); |
| 1133 | |
| 1134 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1135 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1136 | // We stash away the ReplacingStmt since actually doing the |
| 1137 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1138 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1139 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1140 | // to things that stay around. |
| 1141 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1142 | return PropRefExpr; // return the original... |
| 1143 | } else { |
| 1144 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1146 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1147 | // to things that stay around. |
| 1148 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1149 | return ReplacingStmt; |
| 1150 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1154 | SourceLocation OrigStart) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1155 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1156 | if (CurMethodDef) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1157 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1158 | ObjCInterfaceType *iFaceDecl = |
| 1159 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1160 | // lookup which class implements the instance variable. |
| 1161 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1163 | clsDeclared); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1164 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1166 | // Synthesize an explicit cast to gain access to the ivar. |
| 1167 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1168 | RecName += "_IMPL"; |
| 1169 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1170 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1171 | SourceLocation(), II); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1172 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1173 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1175 | CastExpr::CK_Unknown, |
| 1176 | IV->getBase(), |
| 1177 | castT,SourceLocation(), |
| 1178 | SourceLocation()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1179 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1180 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1181 | IV->getBase()->getLocEnd(), |
| 1182 | castExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | if (IV->isFreeIvar() && |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1184 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1185 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1186 | IV->getLocation(), |
| 1187 | D->getType()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1188 | ReplaceStmt(IV, ME); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1189 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1190 | return ME; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1193 | ReplaceStmt(IV->getBase(), PE); |
| 1194 | // Cannot delete IV->getBase(), since PE points to it. |
| 1195 | // Replace the old base with the cast. This is important when doing |
| 1196 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | IV->setBase(PE); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1198 | return IV; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1199 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1200 | } else { // we are outside a method. |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1201 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1203 | // Explicit ivar refs need to have a cast inserted. |
| 1204 | // FIXME: consider sharing some of this code with the code above. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1205 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1206 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1207 | // lookup which class implements the instance variable. |
| 1208 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1210 | clsDeclared); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1211 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1213 | // Synthesize an explicit cast to gain access to the ivar. |
| 1214 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1215 | RecName += "_IMPL"; |
| 1216 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1217 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1218 | SourceLocation(), II); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1219 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1220 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1222 | CastExpr::CK_Unknown, |
| 1223 | IV->getBase(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1224 | castT, SourceLocation(), |
| 1225 | SourceLocation()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1226 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1227 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 34873d2 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1228 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1229 | ReplaceStmt(IV->getBase(), PE); |
| 1230 | // Cannot delete IV->getBase(), since PE points to it. |
| 1231 | // Replace the old base with the cast. This is important when doing |
| 1232 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | IV->setBase(PE); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1234 | return IV; |
| 1235 | } |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1236 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1237 | return IV; |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1240 | /// SynthCountByEnumWithState - To print: |
| 1241 | /// ((unsigned int (*) |
| 1242 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1244 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | /// "countByEnumeratingWithState:objects:count:"), |
| 1246 | /// &enumState, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1247 | /// (id *)items, (unsigned int)16) |
| 1248 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1249 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1250 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1251 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1252 | buf += "\n\t\t"; |
| 1253 | buf += "((id)l_collection,\n\t\t"; |
| 1254 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1255 | buf += "\n\t\t"; |
| 1256 | buf += "&enumState, " |
| 1257 | "(id *)items, (unsigned int)16)"; |
| 1258 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1259 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1260 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1261 | /// statement to exit to its outer synthesized loop. |
| 1262 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1263 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1264 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1265 | return S; |
| 1266 | // replace break with goto __break_label |
| 1267 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1269 | SourceLocation startLoc = S->getLocStart(); |
| 1270 | buf = "goto __break_label_"; |
| 1271 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1272 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1278 | /// statement to continue with its inner synthesized loop. |
| 1279 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1280 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1281 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1282 | return S; |
| 1283 | // replace continue with goto __continue_label |
| 1284 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1286 | SourceLocation startLoc = S->getLocStart(); |
| 1287 | buf = "goto __continue_label_"; |
| 1288 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1289 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
| 1293 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1294 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1295 | /// It rewrites: |
| 1296 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1298 | /// Into: |
| 1299 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1301 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1302 | /// id items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1303 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1305 | /// objects:items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1306 | /// if (limit) { |
| 1307 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1308 | /// do { |
| 1309 | /// unsigned long counter = 0; |
| 1310 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1312 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1313 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1314 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1315 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1316 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1318 | /// objects:items count:16]); |
| 1319 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1320 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1321 | /// } |
| 1322 | /// else |
| 1323 | /// elem = nil; |
| 1324 | /// } |
| 1325 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1326 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1327 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1328 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1330 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1332 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1334 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1335 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1336 | const char *elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1337 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1338 | std::string buf; |
| 1339 | buf = "\n{\n\t"; |
| 1340 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1341 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1342 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1343 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1344 | if (ElementType->isObjCQualifiedIdType() || |
| 1345 | ElementType->isObjCQualifiedInterfaceType()) |
| 1346 | // Simply use 'id' for all qualified types. |
| 1347 | elementTypeAsString = "id"; |
| 1348 | else |
| 1349 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1350 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1351 | buf += " "; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1352 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1353 | buf += elementName; |
| 1354 | buf += ";\n\t"; |
| 1355 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1356 | else { |
| 1357 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1358 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1359 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1360 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1361 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1362 | // Simply use 'id' for all qualified types. |
| 1363 | elementTypeAsString = "id"; |
| 1364 | else |
| 1365 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1368 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1369 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1370 | // id items[16]; |
| 1371 | buf += "id items[16];\n\t"; |
| 1372 | // id l_collection = (id) |
| 1373 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1374 | // Find start location of 'collection' the hard way! |
| 1375 | const char *startCollectionBuf = startBuf; |
| 1376 | startCollectionBuf += 3; // skip 'for' |
| 1377 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1378 | startCollectionBuf++; // skip '(' |
| 1379 | // find 'in' and skip it. |
| 1380 | while (*startCollectionBuf != ' ' || |
| 1381 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1382 | (*(startCollectionBuf+3) != ' ' && |
| 1383 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1384 | startCollectionBuf++; |
| 1385 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1388 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1389 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1390 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1391 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1392 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1393 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1394 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1396 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1397 | // objects:items count:16]; |
| 1398 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1400 | // ((unsigned int (*) |
| 1401 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1403 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | // "countByEnumeratingWithState:objects:count:"), |
| 1405 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1406 | // (id *)items, (unsigned int)16); |
| 1407 | buf += "unsigned long limit =\n\t\t"; |
| 1408 | SynthCountByEnumWithState(buf); |
| 1409 | buf += ";\n\t"; |
| 1410 | /// if (limit) { |
| 1411 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1412 | /// do { |
| 1413 | /// unsigned long counter = 0; |
| 1414 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1416 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1417 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1418 | buf += "if (limit) {\n\t"; |
| 1419 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1420 | buf += "do {\n\t\t"; |
| 1421 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1422 | buf += "do {\n\t\t\t"; |
| 1423 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1424 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1425 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1426 | buf += " = ("; |
| 1427 | buf += elementTypeAsString; |
| 1428 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1429 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1430 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1432 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1433 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1435 | /// objects:items count:16]); |
| 1436 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1437 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1438 | /// } |
| 1439 | /// else |
| 1440 | /// elem = nil; |
| 1441 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1443 | buf = ";\n\t"; |
| 1444 | buf += "__continue_label_"; |
| 1445 | buf += utostr(ObjCBcLabelNo.back()); |
| 1446 | buf += ": ;"; |
| 1447 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1448 | buf += "} while (counter < limit);\n\t"; |
| 1449 | buf += "} while (limit = "; |
| 1450 | SynthCountByEnumWithState(buf); |
| 1451 | buf += ");\n\t"; |
| 1452 | buf += elementName; |
Steve Naroff | d1a3679 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1453 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1454 | buf += "__break_label_"; |
| 1455 | buf += utostr(ObjCBcLabelNo.back()); |
| 1456 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1457 | buf += "}\n\t"; |
| 1458 | buf += "else\n\t\t"; |
| 1459 | buf += elementName; |
Steve Naroff | d1a3679 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1460 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1461 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1463 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1464 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1465 | if (isa<CompoundStmt>(S->getBody())) { |
| 1466 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1467 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1468 | } else { |
| 1469 | /* Need to treat single statements specially. For example: |
| 1470 | * |
| 1471 | * for (A *a in b) if (stuff()) break; |
| 1472 | * for (A *a in b) xxxyy; |
| 1473 | * |
| 1474 | * The following code simply scans ahead to the semi to find the actual end. |
| 1475 | */ |
| 1476 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1477 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1478 | assert(semiBuf && "Can't find ';'"); |
| 1479 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1480 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1481 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1482 | Stmts.pop_back(); |
| 1483 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1484 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1488 | /// This routine rewrites @synchronized(expr) stmt; |
| 1489 | /// into: |
| 1490 | /// objc_sync_enter(expr); |
| 1491 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1492 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1493 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1494 | // Get the start location and compute the semi location. |
| 1495 | SourceLocation startLoc = S->getLocStart(); |
| 1496 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1498 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
| 1500 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1501 | buf = "objc_sync_enter((id)"; |
| 1502 | const char *lparenBuf = startBuf; |
| 1503 | while (*lparenBuf != '(') lparenBuf++; |
| 1504 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1506 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1507 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1508 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1509 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1510 | while (*endBuf != ')') endBuf--; |
| 1511 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1512 | buf = ");\n"; |
| 1513 | // declare a new scope with two variables, _stack and _rethrow. |
| 1514 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1515 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1516 | buf += "char *pointers[4];} _stack;\n"; |
| 1517 | buf += "id volatile _rethrow = 0;\n"; |
| 1518 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1519 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1520 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1521 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1522 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1524 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1525 | SourceLocation lastCurlyLoc = startLoc; |
| 1526 | buf = "}\nelse {\n"; |
| 1527 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1528 | buf += "}\n"; |
| 1529 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1530 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1531 | |
| 1532 | std::string syncBuf; |
| 1533 | syncBuf += " objc_sync_exit("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1535 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | S->getSynchExpr(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1537 | Context->getObjCIdType(), |
| 1538 | SourceLocation(), |
| 1539 | SourceLocation()); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1540 | std::string syncExprBufS; |
| 1541 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1542 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1543 | PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1544 | syncBuf += syncExprBuf.str(); |
| 1545 | syncBuf += ");"; |
| 1546 | |
| 1547 | buf += syncBuf; |
| 1548 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1549 | buf += "}\n"; |
| 1550 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1552 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1553 | |
| 1554 | bool hasReturns = false; |
| 1555 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1556 | if (hasReturns) |
| 1557 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1558 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1562 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1563 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1564 | // Perform a bottom up traversal of all children. |
| 1565 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1566 | CI != E; ++CI) |
| 1567 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1568 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1569 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1570 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1572 | TryFinallyContainsReturnDiag); |
| 1573 | } |
| 1574 | return; |
| 1575 | } |
| 1576 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1577 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1578 | { |
| 1579 | // Perform a bottom up traversal of all children. |
| 1580 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1581 | CI != E; ++CI) |
| 1582 | if (*CI) |
| 1583 | HasReturnStmts(*CI, hasReturns); |
| 1584 | |
| 1585 | if (isa<ReturnStmt>(S)) |
| 1586 | hasReturns = true; |
| 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1591 | // Perform a bottom up traversal of all children. |
| 1592 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1593 | CI != E; ++CI) |
| 1594 | if (*CI) { |
| 1595 | RewriteTryReturnStmts(*CI); |
| 1596 | } |
| 1597 | if (isa<ReturnStmt>(S)) { |
| 1598 | SourceLocation startLoc = S->getLocStart(); |
| 1599 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1600 | |
| 1601 | const char *semiBuf = strchr(startBuf, ';'); |
| 1602 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1603 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1604 | |
| 1605 | std::string buf; |
| 1606 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1607 | |
| 1608 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1609 | InsertText(onePastSemiLoc, "}", 1); |
| 1610 | } |
| 1611 | return; |
| 1612 | } |
| 1613 | |
| 1614 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1615 | // Perform a bottom up traversal of all children. |
| 1616 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1617 | CI != E; ++CI) |
| 1618 | if (*CI) { |
| 1619 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1620 | } |
| 1621 | if (isa<ReturnStmt>(S)) { |
| 1622 | SourceLocation startLoc = S->getLocStart(); |
| 1623 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1624 | |
| 1625 | const char *semiBuf = strchr(startBuf, ';'); |
| 1626 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1627 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1628 | |
| 1629 | std::string buf; |
| 1630 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1631 | buf += syncExitBuf; |
| 1632 | buf += " return"; |
| 1633 | |
| 1634 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1635 | InsertText(onePastSemiLoc, "}", 1); |
| 1636 | } |
| 1637 | return; |
| 1638 | } |
| 1639 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1640 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1641 | // Get the start location and compute the semi location. |
| 1642 | SourceLocation startLoc = S->getLocStart(); |
| 1643 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1645 | assert((*startBuf == '@') && "bogus @try location"); |
| 1646 | |
| 1647 | std::string buf; |
| 1648 | // declare a new scope with two variables, _stack and _rethrow. |
| 1649 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1650 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1651 | buf += "char *pointers[4];} _stack;\n"; |
| 1652 | buf += "id volatile _rethrow = 0;\n"; |
| 1653 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1654 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1656 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1658 | startLoc = S->getTryBody()->getLocEnd(); |
| 1659 | startBuf = SM->getCharacterData(startLoc); |
| 1660 | |
| 1661 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1663 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1664 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1665 | if (catchList) { |
| 1666 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1667 | buf = " /* @catch begin */ else {\n"; |
| 1668 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1669 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1670 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1671 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1672 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1674 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1675 | } else { /* no catch list */ |
| 1676 | buf = "}\nelse {\n"; |
| 1677 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1678 | buf += "}"; |
| 1679 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1680 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1681 | bool sawIdTypedCatch = false; |
| 1682 | Stmt *lastCatchBody = 0; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1683 | while (catchList) { |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1684 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1685 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | if (catchList == S->getCatchStmts()) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1687 | buf = "if ("; // we are generating code for the first catch clause |
| 1688 | else |
| 1689 | buf = "else if ("; |
| 1690 | startLoc = catchList->getLocStart(); |
| 1691 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1693 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1695 | const char *lParenLoc = strchr(startBuf, '('); |
| 1696 | |
Steve Naroff | e6b7ffd | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1697 | if (catchList->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1698 | // Now rewrite the body... |
| 1699 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1700 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1701 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1702 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1703 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1704 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1706 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1707 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1708 | } else if (catchDecl) { |
| 1709 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1710 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1711 | buf += "1) { "; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1712 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1713 | sawIdTypedCatch = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | } else if (const PointerType *pType = t->getAs<PointerType>()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1715 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1717 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1718 | if (cls) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1719 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1720 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1721 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1722 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | // Now rewrite the body... |
| 1726 | lastCatchBody = catchList->getCatchBody(); |
| 1727 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1728 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1729 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1730 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1731 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1732 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1734 | buf = " = _caught;"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1736 | // declares the @catch parameter). |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1737 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1738 | } else { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1739 | assert(false && "@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1740 | } |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1741 | // make sure all the catch bodies get rewritten! |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1742 | catchList = catchList->getNextCatchStmt(); |
| 1743 | } |
| 1744 | // Complete the catch list... |
| 1745 | if (lastCatchBody) { |
| 1746 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1747 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1748 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1750 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1751 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1752 | buf = "} /* last catch end */\n"; |
| 1753 | buf += "else {\n"; |
| 1754 | buf += " _rethrow = _caught;\n"; |
| 1755 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1756 | buf += "} } /* @catch end */\n"; |
| 1757 | if (!S->getFinallyStmt()) |
| 1758 | buf += "}\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1759 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1761 | // Set lastCurlyLoc |
| 1762 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1763 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1764 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1765 | startLoc = finalStmt->getLocStart(); |
| 1766 | startBuf = SM->getCharacterData(startLoc); |
| 1767 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1769 | buf = "/* @finally */"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1770 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1772 | Stmt *body = finalStmt->getFinallyBody(); |
| 1773 | SourceLocation startLoc = body->getLocStart(); |
| 1774 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1775 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1776 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1778 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1780 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1781 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1782 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1783 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1784 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1785 | InsertText(endLoc, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1787 | // Set lastCurlyLoc |
| 1788 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1790 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1791 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1792 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1793 | buf = "{ /* implicit finally clause */\n"; |
| 1794 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1795 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1796 | buf += "}"; |
| 1797 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Now check for any return/continue/go statements within the @try. |
| 1800 | // The implicit finally clause won't called if the @try contains any |
| 1801 | // jump statements. |
| 1802 | bool hasReturns = false; |
| 1803 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1804 | if (hasReturns) |
| 1805 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1806 | } |
| 1807 | // Now emit the final closing curly brace... |
| 1808 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1809 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1810 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1814 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1818 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1819 | return 0; |
| 1820 | } |
| 1821 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1823 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1824 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1825 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1826 | // Get the start location and compute the semi location. |
| 1827 | SourceLocation startLoc = S->getLocStart(); |
| 1828 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1830 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1831 | |
| 1832 | std::string buf; |
| 1833 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1834 | if (S->getThrowExpr()) |
| 1835 | buf = "objc_exception_throw("; |
| 1836 | else // add an implicit argument |
| 1837 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1839 | // handle "@ throw" correctly. |
| 1840 | const char *wBuf = strchr(startBuf, 'w'); |
| 1841 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1842 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1844 | const char *semiBuf = strchr(startBuf, ';'); |
| 1845 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1846 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1847 | buf = ");"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1848 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1849 | return 0; |
| 1850 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1851 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1852 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1853 | // Create a new string expression. |
| 1854 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1855 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1856 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1857 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1858 | StrEncoding.length(), false,StrType, |
| 1859 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1860 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1862 | // Replace this subexpr in the parent. |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1863 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1864 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1867 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1868 | if (!SelGetUidFunctionDecl) |
| 1869 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1870 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1871 | // Create a call to sel_registerName("selName"). |
| 1872 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1873 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1875 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1876 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1877 | false, argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1878 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1879 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1880 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1881 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1882 | return SelExp; |
| 1883 | } |
| 1884 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1885 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1886 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1887 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1888 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1890 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1891 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1893 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 1894 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1896 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | DRE, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1898 | /*isLvalue=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1900 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1902 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1903 | SourceLocation()); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1906 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1907 | const char *&startRef, const char *&endRef) { |
| 1908 | while (startBuf < endBuf) { |
| 1909 | if (*startBuf == '<') |
| 1910 | startRef = startBuf; // mark the start. |
| 1911 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1912 | if (startRef && *startRef == '<') { |
| 1913 | endRef = startBuf; // mark the end. |
| 1914 | return true; |
| 1915 | } |
| 1916 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1917 | } |
| 1918 | startBuf++; |
| 1919 | } |
| 1920 | return false; |
| 1921 | } |
| 1922 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1923 | static void scanToNextArgument(const char *&argRef) { |
| 1924 | int angle = 0; |
| 1925 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1926 | if (*argRef == '<') |
| 1927 | angle++; |
| 1928 | else if (*argRef == '>') |
| 1929 | angle--; |
| 1930 | argRef++; |
| 1931 | } |
| 1932 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1933 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1934 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1935 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1936 | return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType(); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1939 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1940 | QualType Type = E->getType(); |
| 1941 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1942 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1944 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1945 | Loc = ECE->getLParenLoc(); |
| 1946 | EndLoc = ECE->getRParenLoc(); |
| 1947 | } else { |
| 1948 | Loc = E->getLocStart(); |
| 1949 | EndLoc = E->getLocEnd(); |
| 1950 | } |
| 1951 | // This will defend against trying to rewrite synthesized expressions. |
| 1952 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1953 | return; |
| 1954 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1955 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1956 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1957 | const char *startRef = 0, *endRef = 0; |
| 1958 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1959 | // Get the locations of the startRef, endRef. |
| 1960 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1961 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1962 | // Comment out the protocol references. |
| 1963 | InsertText(LessLoc, "/*", 2); |
| 1964 | InsertText(GreaterLoc, "*/", 2); |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1969 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1970 | SourceLocation Loc; |
| 1971 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1972 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1973 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1974 | Loc = VD->getLocation(); |
| 1975 | Type = VD->getType(); |
| 1976 | } |
| 1977 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1978 | Loc = FD->getLocation(); |
| 1979 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1980 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1981 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1982 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1983 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1984 | if (!proto) |
| 1985 | return; |
| 1986 | Type = proto->getResultType(); |
| 1987 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 1988 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 1989 | Loc = FD->getLocation(); |
| 1990 | Type = FD->getType(); |
| 1991 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1992 | else |
| 1993 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1995 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1996 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1998 | const char *endBuf = SM->getCharacterData(Loc); |
| 1999 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2000 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2001 | startBuf--; // scan backward (from the decl location) for return type. |
| 2002 | const char *startRef = 0, *endRef = 0; |
| 2003 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2004 | // Get the locations of the startRef, endRef. |
| 2005 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2006 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2007 | // Comment out the protocol references. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2008 | InsertText(LessLoc, "/*", 2); |
| 2009 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2012 | if (!proto) |
| 2013 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2014 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2015 | const char *startBuf = SM->getCharacterData(Loc); |
| 2016 | const char *startFuncBuf = startBuf; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2017 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2018 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2019 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2021 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2022 | // scan forward (from the decl location) for argument types. |
| 2023 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2024 | const char *startRef = 0, *endRef = 0; |
| 2025 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2026 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | SourceLocation LessLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2028 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2030 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2031 | // Comment out the protocol references. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2032 | InsertText(LessLoc, "/*", 2); |
| 2033 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2034 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2035 | startBuf = ++endBuf; |
| 2036 | } |
| 2037 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2038 | // If the function name is derived from a macro expansion, then the |
| 2039 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2040 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2041 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2042 | startBuf++; |
| 2043 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2044 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2047 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2048 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2049 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2050 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2051 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2052 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2053 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2054 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2055 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2057 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2058 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2061 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2062 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2063 | if (FD->getIdentifier() && |
| 2064 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2065 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2066 | return; |
| 2067 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2068 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2071 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2072 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2073 | if (SuperContructorFunctionDecl) |
| 2074 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2075 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2076 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2077 | QualType argT = Context->getObjCIdType(); |
| 2078 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2079 | ArgTys.push_back(argT); |
| 2080 | ArgTys.push_back(argT); |
| 2081 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2082 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2083 | false, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2084 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2086 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2087 | FunctionDecl::Extern, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2090 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2091 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2092 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2093 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2094 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2095 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2096 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2097 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2098 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2099 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2100 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2101 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2102 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2103 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2104 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2105 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2106 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2109 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2110 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2111 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2112 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2113 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2114 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2115 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2116 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2117 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2118 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2119 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2120 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2121 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2122 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2123 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2124 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2125 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2127 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2128 | FunctionDecl::Extern, false); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2131 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2132 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2133 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2134 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2135 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2136 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2137 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2138 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2139 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2140 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2141 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2142 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2143 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2144 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2146 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2147 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2151 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2152 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2154 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2155 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2156 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2157 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2158 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2159 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2160 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2161 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2162 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2163 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2164 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2165 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2166 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2167 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2168 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2170 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2171 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2174 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2175 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2176 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2177 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2178 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2179 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2180 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2181 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2182 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2183 | ArgTys.push_back(argT); |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2184 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2185 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2186 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2187 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2189 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2190 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2193 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2194 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2195 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2196 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2197 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2198 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2199 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2200 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2201 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2203 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2204 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2207 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2208 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2209 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2210 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2211 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2212 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2213 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2214 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2215 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2217 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2218 | FunctionDecl::Extern, false); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2221 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2222 | QualType strType = getConstantStringStructType(); |
| 2223 | |
| 2224 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2225 | |
| 2226 | std::string tmpName = InFileName; |
| 2227 | unsigned i; |
| 2228 | for (i=0; i < tmpName.length(); i++) { |
| 2229 | char c = tmpName.at(i); |
| 2230 | // replace any non alphanumeric characters with '_'. |
| 2231 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2232 | tmpName[i] = '_'; |
| 2233 | } |
| 2234 | S += tmpName; |
| 2235 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2236 | S += utostr(NumObjCStringLiterals++); |
| 2237 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2238 | Preamble += "static __NSConstantStringImpl " + S; |
| 2239 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2240 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2241 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2242 | std::string prettyBufS; |
| 2243 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2244 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2245 | PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2246 | Preamble += prettyBuf.str(); |
| 2247 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2248 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
| 2250 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2251 | &Context->Idents.get(S.c_str()), strType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2252 | VarDecl::Static); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2253 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2254 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | Context->getPointerType(DRE->getType()), |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2256 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2257 | // cast to NSConstantString * |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2259 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | Unop, Exp->getType(), |
| 2261 | SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2262 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2263 | ReplaceStmt(Exp, cast); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2264 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2265 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2268 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2269 | // check if we are sending a message to 'super' |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2270 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | |
Douglas Gregor | 8ea1f53 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2272 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | const ObjCObjectPointerType *OPT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2274 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2275 | assert(OPT); |
| 2276 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | a9b3cae | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2277 | return IT->getDecl(); |
| 2278 | } |
| 2279 | return 0; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2283 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2284 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2285 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2287 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2288 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2290 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2292 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2294 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2295 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2296 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2298 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2299 | FieldTypes[i], 0, |
| 2300 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2301 | /*Mutable=*/false)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2302 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2304 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2305 | } |
| 2306 | return Context->getTagDeclType(SuperStructDecl); |
| 2307 | } |
| 2308 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2309 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2310 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2311 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2313 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2314 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2316 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2318 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2320 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2322 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2324 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2325 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2326 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2328 | ConstantStringDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2329 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2330 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2331 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2332 | /*Mutable=*/true)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2336 | } |
| 2337 | return Context->getTagDeclType(ConstantStringDecl); |
| 2338 | } |
| 2339 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2340 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2341 | if (!SelGetUidFunctionDecl) |
| 2342 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2343 | if (!MsgSendFunctionDecl) |
| 2344 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2345 | if (!MsgSendSuperFunctionDecl) |
| 2346 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2347 | if (!MsgSendStretFunctionDecl) |
| 2348 | SynthMsgSendStretFunctionDecl(); |
| 2349 | if (!MsgSendSuperStretFunctionDecl) |
| 2350 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2351 | if (!MsgSendFpretFunctionDecl) |
| 2352 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2353 | if (!GetClassFunctionDecl) |
| 2354 | SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2355 | if (!GetMetaClassFunctionDecl) |
| 2356 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2358 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2359 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2360 | // May need to use objc_msgSend_stret() as well. |
| 2361 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2362 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2363 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2364 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2365 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2366 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2367 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2370 | // Synthesize a call to objc_msgSend(). |
| 2371 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2372 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2374 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2375 | if (clsName) { // class message. |
Steve Naroff | 6c79f97 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2376 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2377 | // the 'super' idiom within a class method. |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2378 | if (clsName->getName() == "super") { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2379 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2380 | if (MsgSendStretFlavor) |
| 2381 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2382 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
| 2384 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2385 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2386 | |
| 2387 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2389 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2390 | InitExprs.push_back( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2392 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2394 | Context->getObjCIdType(), |
| 2395 | SourceLocation()), |
| 2396 | Context->getObjCIdType(), |
| 2397 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
| 2398 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2399 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2400 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2401 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2402 | SuperDecl->getIdentifier()->getNameStart(), |
| 2403 | SuperDecl->getIdentifier()->getLength(), |
| 2404 | false, argType, SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2405 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2406 | &ClsExprs[0], |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2407 | ClsExprs.size()); |
| 2408 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2409 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2411 | CastExpr::CK_Unknown, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2412 | Cls, Context->getObjCIdType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | SourceLocation(), SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2414 | // struct objc_super |
| 2415 | QualType superType = getSuperStructType(); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2416 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2418 | if (LangOpts.Microsoft) { |
| 2419 | SynthSuperContructorFunctionDecl(); |
| 2420 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2422 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2423 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2425 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2426 | // The code for super is a little tricky to prevent collision with |
| 2427 | // the structure definition in the header. The rewriter has it's own |
| 2428 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2429 | // we need the cast below. For example: |
| 2430 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2431 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2432 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2434 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2435 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
| 2436 | CastExpr::CK_Unknown, SuperRep, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2437 | Context->getPointerType(superType), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | SourceLocation(), SourceLocation()); |
| 2439 | } else { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2440 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2442 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2443 | SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2444 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 07d754a | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2445 | false); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2446 | // struct objc_super * |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2447 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2449 | SourceLocation()); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2450 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2451 | MsgExprs.push_back(SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2452 | } else { |
| 2453 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2454 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2455 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2456 | clsName->getNameStart(), |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2457 | clsName->getLength(), |
| 2458 | false, argType, |
| 2459 | SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2460 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | &ClsExprs[0], |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2462 | ClsExprs.size()); |
| 2463 | MsgExprs.push_back(Cls); |
| 2464 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2465 | } else { // instance message. |
| 2466 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2467 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2468 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2469 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2470 | if (MsgSendStretFlavor) |
| 2471 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2472 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2474 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2476 | InitExprs.push_back( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2478 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 97adf60 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2480 | Context->getObjCIdType(), |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2481 | SourceLocation()), |
| 2482 | Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2483 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2485 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2486 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2488 | SuperDecl->getIdentifier()->getNameStart(), |
| 2489 | SuperDecl->getIdentifier()->getLength(), |
| 2490 | false, argType, SourceLocation())); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2491 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | &ClsExprs[0], |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2493 | ClsExprs.size()); |
Fariborz Jahanian | d5db92b | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2494 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2495 | InitExprs.push_back( |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2496 | // set 'super class', using objc_getClass(). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2498 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2500 | // struct objc_super |
| 2501 | QualType superType = getSuperStructType(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2502 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2504 | if (LangOpts.Microsoft) { |
| 2505 | SynthSuperContructorFunctionDecl(); |
| 2506 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2508 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2509 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2511 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2512 | // The code for super is a little tricky to prevent collision with |
| 2513 | // the structure definition in the header. The rewriter has it's own |
| 2514 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2515 | // we need the cast below. For example: |
| 2516 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2517 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2518 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2520 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2522 | CastExpr::CK_Unknown, |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2523 | SuperRep, Context->getPointerType(superType), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | SourceLocation(), SourceLocation()); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2525 | } else { |
| 2526 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2528 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2529 | SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2530 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2531 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2532 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2533 | } else { |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2534 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2535 | // Foo<Proto> *. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2536 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2537 | recExpr = CE->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2539 | CastExpr::CK_Unknown, recExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2541 | SourceLocation(), SourceLocation()); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2542 | MsgExprs.push_back(recExpr); |
| 2543 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2544 | } |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2545 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2546 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2547 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2549 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2550 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2551 | false, argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2552 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2553 | &SelExprs[0], SelExprs.size()); |
| 2554 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2555 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2556 | // Now push any user supplied arguments. |
| 2557 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2558 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2559 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2560 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2561 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2562 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2563 | ? Context->getObjCIdType() |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2564 | : ICE->getType(); |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2565 | userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | userExpr, type, SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2567 | SourceLocation()); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2568 | } |
| 2569 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2570 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2571 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2572 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2573 | userExpr = CE->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2575 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | userExpr, Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2577 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2578 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2580 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2581 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2582 | // out the argument in the original expression (since we aren't deleting |
| 2583 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2584 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2585 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2586 | // Generate the funky cast. |
| 2587 | CastExpr *cast; |
| 2588 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2589 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2591 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2592 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2593 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2594 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2595 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2596 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2597 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2598 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2599 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2600 | E = OMD->param_end(); PI != E; ++PI) { |
| 2601 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2603 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2604 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2605 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2606 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2607 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2608 | } |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2609 | ArgTypes.push_back(t); |
| 2610 | } |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2611 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2612 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2613 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2614 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2615 | } |
| 2616 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2617 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2619 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2621 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2622 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2624 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2625 | // xx.m:13: warning: function called through a non-compatible type |
| 2626 | // xx.m:13: note: if this code is reached, the program will abort |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2628 | CastExpr::CK_Unknown, DRE, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2629 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2630 | SourceLocation(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2632 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2634 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 327f0f4 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2635 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2636 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2637 | castType = Context->getPointerType(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast, |
| 2639 | castType, SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2640 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2643 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2645 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2646 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2648 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2649 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2650 | if (MsgSendStretFlavor) { |
| 2651 | // We have the method which returns a struct/union. Must also generate |
| 2652 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2653 | // expression which dictate which one to envoke depending on size of |
| 2654 | // method's return type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2656 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2658 | SourceLocation()); |
| 2659 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2661 | CastExpr::CK_Unknown, STDRE, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2662 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2663 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2664 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2666 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2667 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2668 | castType = Context->getPointerType(castType); |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2669 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, |
| 2670 | cast, castType, SourceLocation(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2672 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2673 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2675 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2676 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2678 | FT->getResultType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2680 | // Build sizeof(returnType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2682 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2683 | Context->getSizeType(), |
| 2684 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2685 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2686 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2687 | // For X86 it is more complicated and some kind of target specific routine |
| 2688 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2690 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2692 | Context->IntTy, |
| 2693 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2695 | BinaryOperator::LE, |
| 2696 | Context->IntTy, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2697 | SourceLocation()); |
| 2698 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2700 | new (Context) ConditionalOperator(lessThanExpr, |
| 2701 | SourceLocation(), CE, |
| 2702 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2703 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2706 | return ReplacingStmt; |
| 2707 | } |
| 2708 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2709 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2710 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2712 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2713 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
| 2715 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2716 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2719 | // typedef struct objc_object Protocol; |
| 2720 | QualType RewriteObjC::getProtocolType() { |
| 2721 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2722 | TypeSourceInfo *TInfo |
| 2723 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2724 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2726 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2727 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2728 | } |
| 2729 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2730 | } |
| 2731 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2732 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2733 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2734 | /// The forward references (and metadata) are generated in |
| 2735 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2736 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2737 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2738 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 2740 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2741 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2742 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2743 | Context->getPointerType(DRE->getType()), |
| 2744 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(), |
| 2746 | CastExpr::CK_Unknown, |
| 2747 | DerefExpr, DerefExpr->getType(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2748 | SourceLocation(), SourceLocation()); |
| 2749 | ReplaceStmt(Exp, castExpr); |
| 2750 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2751 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2752 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2757 | const char *endBuf) { |
| 2758 | while (startBuf < endBuf) { |
| 2759 | if (*startBuf == '#') { |
| 2760 | // Skip whitespace. |
| 2761 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2762 | ; |
| 2763 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2764 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2765 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2766 | !strncmp(startBuf, "define", strlen("define")) || |
| 2767 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2768 | !strncmp(startBuf, "else", strlen("else")) || |
| 2769 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2770 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2771 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2772 | !strncmp(startBuf, "include", strlen("include")) || |
| 2773 | !strncmp(startBuf, "import", strlen("import")) || |
| 2774 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2775 | return true; |
| 2776 | } |
| 2777 | startBuf++; |
| 2778 | } |
| 2779 | return false; |
| 2780 | } |
| 2781 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2782 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2783 | /// an objective-c class with ivars. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2784 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2785 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2786 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2788 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2789 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2790 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2791 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2792 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2793 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2794 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2795 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2797 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2798 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2800 | // If no ivars and no root or if its root, directly or indirectly, |
| 2801 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2802 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2803 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2804 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2805 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2806 | return; |
| 2807 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | |
| 2809 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2810 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2811 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2812 | Result += CDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2813 | if (LangOpts.Microsoft) |
| 2814 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2815 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2816 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2817 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2819 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2820 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2821 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2822 | // NSURL.h, for example): |
| 2823 | // |
| 2824 | // #ifdef XYZ |
| 2825 | // @interface Foo : NSObject |
| 2826 | // #else |
| 2827 | // @interface FooBar : NSObject |
| 2828 | // #endif |
| 2829 | // { |
| 2830 | // int i; |
| 2831 | // } |
| 2832 | // @end |
| 2833 | // |
| 2834 | // This clause is segregated to avoid breaking the common case. |
| 2835 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2837 | CDecl->getClassLoc(); |
| 2838 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2839 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2841 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2842 | // advance to the end of the referenced protocols. |
| 2843 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2844 | endHeader++; |
| 2845 | } |
| 2846 | // rewrite the original header |
| 2847 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2848 | } else { |
| 2849 | // rewrite the original header *without* disturbing the '{' |
Steve Naroff | b0e3390 | 2009-12-04 21:36:32 +0000 | [diff] [blame] | 2850 | ReplaceText(LocStart, cursor-startBuf, Result.c_str(), Result.size()); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2851 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2852 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2853 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2854 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2855 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2856 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2857 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2859 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2860 | SourceLocation OnePastCurly = |
| 2861 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2862 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2863 | } |
| 2864 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2866 | // Now comment out any visibility specifiers. |
| 2867 | while (cursor < endBuf) { |
| 2868 | if (*cursor == '@') { |
| 2869 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2870 | // Skip whitespace. |
| 2871 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2872 | /*scan*/; |
| 2873 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2874 | // FIXME: presence of @public, etc. inside comment results in |
| 2875 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2876 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2877 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2878 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2879 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2880 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2881 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2882 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2883 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2884 | // for type checking. |
| 2885 | else if (*cursor == '<') { |
| 2886 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2887 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2888 | cursor = strchr(cursor, '>'); |
| 2889 | cursor++; |
| 2890 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2891 | InsertText(atLoc, " */", 3); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2892 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2893 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2894 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2895 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2896 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2897 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2898 | // Don't forget to add a ';'!! |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2899 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2900 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2901 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2902 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2903 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2904 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2905 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2906 | Result += "_IVARS;\n};\n"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2907 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2908 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2909 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2910 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2911 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2914 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2915 | /// class methods. |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2916 | template<typename MethodIterator> |
| 2917 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2918 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2919 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2920 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2921 | const char *ClassName, |
| 2922 | std::string &Result) { |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2923 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2925 | static bool objc_impl_method = false; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2926 | if (!objc_impl_method) { |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2927 | /* struct _objc_method { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2928 | SEL _cmd; |
| 2929 | char *method_types; |
| 2930 | void *_imp; |
| 2931 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2932 | */ |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2933 | Result += "\nstruct _objc_method {\n"; |
| 2934 | Result += "\tSEL _cmd;\n"; |
| 2935 | Result += "\tchar *method_types;\n"; |
| 2936 | Result += "\tvoid *_imp;\n"; |
| 2937 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2938 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2939 | objc_impl_method = true; |
Fariborz Jahanian | 74a1cfa | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2940 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2942 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2944 | /* struct { |
| 2945 | struct _objc_method_list *next_method; |
| 2946 | int method_count; |
| 2947 | struct _objc_method method_list[]; |
| 2948 | } |
| 2949 | */ |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2950 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2951 | Result += "\nstatic struct {\n"; |
| 2952 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2953 | Result += "\tint method_count;\n"; |
| 2954 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2955 | Result += utostr(NumMethods); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2956 | Result += "];\n} _OBJC_"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2957 | Result += prefix; |
| 2958 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2959 | Result += "_METHODS_"; |
| 2960 | Result += ClassName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2961 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2962 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2963 | Result += "_meth\")))= "; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2964 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2966 | Result += "\t,{{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2967 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2968 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2969 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2970 | Result += "\", \""; |
| 2971 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2972 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2973 | Result += MethodInternalNames[*MethodBegin]; |
| 2974 | Result += "}\n"; |
| 2975 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2976 | Result += "\t ,{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2977 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2978 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2979 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2980 | Result += "\", \""; |
| 2981 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2982 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2983 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2984 | Result += "}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2985 | } |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2986 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2989 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2990 | void RewriteObjC:: |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2991 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 2992 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2993 | static bool objc_protocol_methods = false; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2994 | |
| 2995 | // Output struct protocol_methods holder of method selector and type. |
| 2996 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2997 | /* struct protocol_methods { |
| 2998 | SEL _cmd; |
| 2999 | char *method_types; |
| 3000 | } |
| 3001 | */ |
| 3002 | Result += "\nstruct _protocol_methods {\n"; |
| 3003 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3004 | Result += "\tchar *method_types;\n"; |
| 3005 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3007 | objc_protocol_methods = true; |
| 3008 | } |
| 3009 | // Do not synthesize the protocol more than once. |
| 3010 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3011 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3013 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3014 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3015 | PDecl->instmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3016 | /* struct _objc_protocol_method_list { |
| 3017 | int protocol_method_count; |
| 3018 | struct protocol_methods protocols[]; |
| 3019 | } |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3020 | */ |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3021 | Result += "\nstatic struct {\n"; |
| 3022 | Result += "\tint protocol_method_count;\n"; |
| 3023 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3024 | Result += utostr(NumMethods); |
| 3025 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3026 | Result += PDecl->getNameAsString(); |
| 3027 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3028 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3030 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | for (ObjCProtocolDecl::instmeth_iterator |
| 3032 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3033 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3034 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3035 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3036 | else |
| 3037 | Result += "\t ,{(struct objc_selector *)\""; |
| 3038 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3039 | std::string MethodTypeString; |
| 3040 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3041 | Result += "\", \""; |
| 3042 | Result += MethodTypeString; |
| 3043 | Result += "\"}\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3044 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3045 | Result += "\t }\n};\n"; |
| 3046 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3048 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3049 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3050 | PDecl->classmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3051 | if (NumMethods > 0) { |
| 3052 | /* struct _objc_protocol_method_list { |
| 3053 | int protocol_method_count; |
| 3054 | struct protocol_methods protocols[]; |
| 3055 | } |
| 3056 | */ |
| 3057 | Result += "\nstatic struct {\n"; |
| 3058 | Result += "\tint protocol_method_count;\n"; |
| 3059 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3060 | Result += utostr(NumMethods); |
| 3061 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3062 | Result += PDecl->getNameAsString(); |
| 3063 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3064 | "{\n\t"; |
| 3065 | Result += utostr(NumMethods); |
| 3066 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3068 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3070 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3071 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3072 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3073 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3074 | else |
| 3075 | Result += "\t ,{(struct objc_selector *)\""; |
| 3076 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3077 | std::string MethodTypeString; |
| 3078 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3079 | Result += "\", \""; |
| 3080 | Result += MethodTypeString; |
| 3081 | Result += "\"}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3082 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3083 | Result += "\t }\n};\n"; |
| 3084 | } |
| 3085 | |
| 3086 | // Output: |
| 3087 | /* struct _objc_protocol { |
| 3088 | // Objective-C 1.0 extensions |
| 3089 | struct _objc_protocol_extension *isa; |
| 3090 | char *protocol_name; |
| 3091 | struct _objc_protocol **protocol_list; |
| 3092 | struct _objc_protocol_method_list *instance_methods; |
| 3093 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | }; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3095 | */ |
| 3096 | static bool objc_protocol = false; |
| 3097 | if (!objc_protocol) { |
| 3098 | Result += "\nstruct _objc_protocol {\n"; |
| 3099 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3100 | Result += "\tchar *protocol_name;\n"; |
| 3101 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3102 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3103 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3104 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3106 | objc_protocol = true; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3107 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3109 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3110 | Result += PDecl->getNameAsString(); |
| 3111 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3112 | "{\n\t0, \""; |
| 3113 | Result += PDecl->getNameAsString(); |
| 3114 | Result += "\", 0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3115 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3116 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3117 | Result += PDecl->getNameAsString(); |
| 3118 | Result += ", "; |
| 3119 | } |
| 3120 | else |
| 3121 | Result += "0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3122 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3123 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3124 | Result += PDecl->getNameAsString(); |
| 3125 | Result += "\n"; |
| 3126 | } |
| 3127 | else |
| 3128 | Result += "0\n"; |
| 3129 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3131 | // Mark this protocol as having been generated. |
| 3132 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3133 | assert(false && "protocol already synthesized"); |
| 3134 | |
| 3135 | } |
| 3136 | |
| 3137 | void RewriteObjC:: |
| 3138 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3139 | const char *prefix, const char *ClassName, |
| 3140 | std::string &Result) { |
| 3141 | if (Protocols.empty()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3143 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3144 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3145 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3146 | // Output the top lovel protocol meta-data for the class. |
| 3147 | /* struct _objc_protocol_list { |
| 3148 | struct _objc_protocol_list *next; |
| 3149 | int protocol_count; |
| 3150 | struct _objc_protocol *class_protocols[]; |
| 3151 | } |
| 3152 | */ |
| 3153 | Result += "\nstatic struct {\n"; |
| 3154 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3155 | Result += "\tint protocol_count;\n"; |
| 3156 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3157 | Result += utostr(Protocols.size()); |
| 3158 | Result += "];\n} _OBJC_"; |
| 3159 | Result += prefix; |
| 3160 | Result += "_PROTOCOLS_"; |
| 3161 | Result += ClassName; |
| 3162 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3163 | "{\n\t0, "; |
| 3164 | Result += utostr(Protocols.size()); |
| 3165 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3167 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3168 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3169 | Result += " \n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3171 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3172 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3173 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3174 | Result += "\n"; |
| 3175 | } |
| 3176 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3179 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3181 | /// implementation. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3182 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3183 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3184 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3185 | // Find category declaration for this implementation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3186 | ObjCCategoryDecl *CDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3188 | CDecl = CDecl->getNextClassCategory()) |
| 3189 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3190 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3192 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3193 | FullCategoryName += '_'; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3194 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3196 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3198 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3199 | |
| 3200 | // If any of our property implementations have associated getters or |
| 3201 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3202 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3203 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3204 | Prop != PropEnd; ++Prop) { |
| 3205 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3206 | continue; |
| 3207 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3208 | continue; |
| 3209 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3210 | if (!PD) |
| 3211 | continue; |
| 3212 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3213 | InstanceMethods.push_back(Getter); |
| 3214 | if (PD->isReadOnly()) |
| 3215 | continue; |
| 3216 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3217 | InstanceMethods.push_back(Setter); |
| 3218 | } |
| 3219 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3220 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3221 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3223 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3224 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3225 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3226 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3228 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 989e039 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3229 | // Null CDecl is case of a category implementation with no category interface |
| 3230 | if (CDecl) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3231 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3232 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3233 | /* struct _objc_category { |
| 3234 | char *category_name; |
| 3235 | char *class_name; |
| 3236 | struct _objc_method_list *instance_methods; |
| 3237 | struct _objc_method_list *class_methods; |
| 3238 | struct _objc_protocol_list *protocols; |
| 3239 | // Objective-C 1.0 extensions |
| 3240 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3242 | // @property decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3244 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3246 | static bool objc_category = false; |
| 3247 | if (!objc_category) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3248 | Result += "\nstruct _objc_category {\n"; |
| 3249 | Result += "\tchar *category_name;\n"; |
| 3250 | Result += "\tchar *class_name;\n"; |
| 3251 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3252 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3253 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3254 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3255 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3256 | Result += "};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3257 | objc_category = true; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3258 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3259 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3260 | Result += FullCategoryName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3261 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3262 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3263 | Result += "\"\n\t, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3264 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3265 | Result += "\"\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3267 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3268 | Result += "\t, (struct _objc_method_list *)" |
| 3269 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3270 | Result += FullCategoryName; |
| 3271 | Result += "\n"; |
| 3272 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3273 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3274 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3275 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3276 | Result += "\t, (struct _objc_method_list *)" |
| 3277 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3278 | Result += FullCategoryName; |
| 3279 | Result += "\n"; |
| 3280 | } |
| 3281 | else |
| 3282 | Result += "\t, 0\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3284 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3286 | Result += FullCategoryName; |
| 3287 | Result += "\n"; |
| 3288 | } |
| 3289 | else |
| 3290 | Result += "\t, 0\n"; |
| 3291 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3294 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3295 | /// ivar offset. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3296 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3297 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3298 | std::string &Result) { |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3299 | if (ivar->isBitField()) { |
| 3300 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3301 | // place all bitfields at offset 0. |
| 3302 | Result += "0"; |
| 3303 | } else { |
| 3304 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3305 | Result += IDecl->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3306 | if (LangOpts.Microsoft) |
| 3307 | Result += "_IMPL"; |
| 3308 | Result += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3309 | Result += ivar->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3310 | Result += ")"; |
| 3311 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3314 | //===----------------------------------------------------------------------===// |
| 3315 | // Meta Data Emission |
| 3316 | //===----------------------------------------------------------------------===// |
| 3317 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3318 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3319 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3320 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3322 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3323 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3325 | // produce correct synthesis as yet. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3326 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3327 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3329 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3330 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | ? IDecl->ivar_size() |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3332 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3333 | if (NumIvars > 0) { |
| 3334 | static bool objc_ivar = false; |
| 3335 | if (!objc_ivar) { |
| 3336 | /* struct _objc_ivar { |
| 3337 | char *ivar_name; |
| 3338 | char *ivar_type; |
| 3339 | int ivar_offset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3341 | */ |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3342 | Result += "\nstruct _objc_ivar {\n"; |
| 3343 | Result += "\tchar *ivar_name;\n"; |
| 3344 | Result += "\tchar *ivar_type;\n"; |
| 3345 | Result += "\tint ivar_offset;\n"; |
| 3346 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3348 | objc_ivar = true; |
| 3349 | } |
| 3350 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3351 | /* struct { |
| 3352 | int ivar_count; |
| 3353 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | }; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3355 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | Result += "\nstatic struct {\n"; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3357 | Result += "\tint ivar_count;\n"; |
| 3358 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3359 | Result += utostr(NumIvars); |
| 3360 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3361 | Result += IDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3362 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3363 | "{\n\t"; |
| 3364 | Result += utostr(NumIvars); |
| 3365 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3366 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3367 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3368 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3369 | if (!IDecl->ivar_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | for (ObjCImplementationDecl::ivar_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3371 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3372 | IV != IVEnd; ++IV) |
| 3373 | IVars.push_back(*IV); |
| 3374 | IVI = IVars.begin(); |
| 3375 | IVE = IVars.end(); |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3376 | } else { |
| 3377 | IVI = CDecl->ivar_begin(); |
| 3378 | IVE = CDecl->ivar_end(); |
| 3379 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3380 | Result += "\t,{{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3381 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3382 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3383 | std::string TmpString, StrEncoding; |
| 3384 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3385 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3386 | Result += StrEncoding; |
| 3387 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3388 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3389 | Result += "}\n"; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3390 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3391 | Result += "\t ,{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3392 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3393 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3394 | std::string TmpString, StrEncoding; |
| 3395 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3396 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3397 | Result += StrEncoding; |
| 3398 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3399 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3400 | Result += "}\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3403 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3404 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3406 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3408 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3409 | |
| 3410 | // If any of our property implementations have associated getters or |
| 3411 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3412 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3413 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3414 | Prop != PropEnd; ++Prop) { |
| 3415 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3416 | continue; |
| 3417 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3418 | continue; |
| 3419 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3420 | if (!PD) |
| 3421 | continue; |
| 3422 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3423 | InstanceMethods.push_back(Getter); |
| 3424 | if (PD->isReadOnly()) |
| 3425 | continue; |
| 3426 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3427 | InstanceMethods.push_back(Setter); |
| 3428 | } |
| 3429 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3430 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3432 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3433 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3434 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3436 | // Protocols referenced in class declaration? |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3437 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3438 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3440 | // Declaration of class/meta-class metadata |
| 3441 | /* struct _objc_class { |
| 3442 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3443 | const char *super_class_name; |
| 3444 | char *name; |
| 3445 | long version; |
| 3446 | long info; |
| 3447 | long instance_size; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3448 | struct _objc_ivar_list *ivars; |
| 3449 | struct _objc_method_list *methods; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3450 | struct objc_cache *cache; |
| 3451 | struct objc_protocol_list *protocols; |
| 3452 | const char *ivar_layout; |
| 3453 | struct _objc_class_ext *ext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | }; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3455 | */ |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3456 | static bool objc_class = false; |
| 3457 | if (!objc_class) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3458 | Result += "\nstruct _objc_class {\n"; |
| 3459 | Result += "\tstruct _objc_class *isa;\n"; |
| 3460 | Result += "\tconst char *super_class_name;\n"; |
| 3461 | Result += "\tchar *name;\n"; |
| 3462 | Result += "\tlong version;\n"; |
| 3463 | Result += "\tlong info;\n"; |
| 3464 | Result += "\tlong instance_size;\n"; |
| 3465 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3466 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3467 | Result += "\tstruct objc_cache *cache;\n"; |
| 3468 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3469 | Result += "\tconst char *ivar_layout;\n"; |
| 3470 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3471 | Result += "};\n"; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3472 | objc_class = true; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3475 | // Meta-class metadata generation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3476 | ObjCInterfaceDecl *RootClass = 0; |
| 3477 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3478 | while (SuperClass) { |
| 3479 | RootClass = SuperClass; |
| 3480 | SuperClass = SuperClass->getSuperClass(); |
| 3481 | } |
| 3482 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3484 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3485 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3486 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3487 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3488 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3489 | Result += "\""; |
| 3490 | |
| 3491 | if (SuperClass) { |
| 3492 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3493 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3494 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3495 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3496 | Result += "\""; |
| 3497 | } |
| 3498 | else { |
| 3499 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3500 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3501 | Result += "\""; |
| 3502 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3503 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3504 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3505 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3506 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3507 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3508 | Result += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | Result += "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3510 | } |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3511 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3512 | Result += ", 0\n"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3513 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3514 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3515 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3516 | Result += ",0,0\n"; |
| 3517 | } |
Fariborz Jahanian | 486f718 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3518 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3519 | Result += "\t,0,0,0,0\n"; |
| 3520 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3522 | // class metadata generation. |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3523 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3524 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3525 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3526 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3527 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3528 | if (SuperClass) { |
| 3529 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3530 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3531 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3532 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3533 | Result += "\""; |
| 3534 | } |
| 3535 | else { |
| 3536 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3537 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3538 | Result += "\""; |
| 3539 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3540 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3541 | Result += ", 0,1"; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3542 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3543 | Result += ",0"; |
| 3544 | else { |
| 3545 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3546 | Result += ",sizeof(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3547 | Result += CDecl->getNameAsString(); |
Steve Naroff | 14a0746 | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3548 | if (LangOpts.Microsoft) |
| 3549 | Result += "_IMPL"; |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3550 | Result += ")"; |
| 3551 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3552 | if (NumIvars > 0) { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3553 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3554 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3555 | Result += "\n\t"; |
| 3556 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3557 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3558 | Result += ",0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3559 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3560 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3561 | Result += CDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | Result += ", 0\n\t"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3563 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3564 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3565 | Result += ",0,0"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3566 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3567 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3568 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3569 | Result += ", 0,0\n"; |
| 3570 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3571 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3572 | Result += ",0,0,0\n"; |
| 3573 | Result += "};\n"; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3574 | } |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3575 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3576 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3577 | /// and emits meta-data. |
| 3578 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3579 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3580 | int ClsDefCount = ClassImplementation.size(); |
| 3581 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3583 | // Rewrite implemented methods |
| 3584 | for (int i = 0; i < ClsDefCount; i++) |
| 3585 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3587 | for (int i = 0; i < CatDefCount; i++) |
| 3588 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3589 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3590 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3591 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3592 | int ClsDefCount = ClassImplementation.size(); |
| 3593 | int CatDefCount = CategoryImplementation.size(); |
| 3594 | |
Steve Naroff | 30ac222 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3595 | // This is needed for determining instance variable offsets. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3597 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3598 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3599 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3601 | // For each implemented category, write out all its meta data. |
| 3602 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3603 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3604 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3605 | // Write objc_symtab metadata |
| 3606 | /* |
| 3607 | struct _objc_symtab |
| 3608 | { |
| 3609 | long sel_ref_cnt; |
| 3610 | SEL *refs; |
| 3611 | short cls_def_cnt; |
| 3612 | short cat_def_cnt; |
| 3613 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | }; |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3615 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3617 | Result += "\nstruct _objc_symtab {\n"; |
| 3618 | Result += "\tlong sel_ref_cnt;\n"; |
| 3619 | Result += "\tSEL *refs;\n"; |
| 3620 | Result += "\tshort cls_def_cnt;\n"; |
| 3621 | Result += "\tshort cat_def_cnt;\n"; |
| 3622 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3623 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3624 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3625 | Result += "static struct _objc_symtab " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3626 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3628 | + ", " + utostr(CatDefCount) + "\n"; |
| 3629 | for (int i = 0; i < ClsDefCount; i++) { |
| 3630 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3631 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3632 | Result += "\n"; |
| 3633 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3635 | for (int i = 0; i < CatDefCount; i++) { |
| 3636 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3637 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3638 | Result += "_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3639 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3640 | Result += "\n"; |
| 3641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3643 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3644 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3645 | // Write objc_module metadata |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3647 | /* |
| 3648 | struct _objc_module { |
| 3649 | long version; |
| 3650 | long size; |
| 3651 | const char *name; |
| 3652 | struct _objc_symtab *symtab; |
| 3653 | } |
| 3654 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3656 | Result += "\nstruct _objc_module {\n"; |
| 3657 | Result += "\tlong version;\n"; |
| 3658 | Result += "\tlong size;\n"; |
| 3659 | Result += "\tconst char *name;\n"; |
| 3660 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3661 | Result += "};\n\n"; |
| 3662 | Result += "static struct _objc_module " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3663 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3665 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3666 | Result += "};\n\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3667 | |
| 3668 | if (LangOpts.Microsoft) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3669 | if (ProtocolExprDecls.size()) { |
| 3670 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3671 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3673 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3674 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3675 | Result += (*I)->getNameAsString(); |
| 3676 | Result += " = &_OBJC_PROTOCOL_"; |
| 3677 | Result += (*I)->getNameAsString(); |
| 3678 | Result += ";\n"; |
| 3679 | } |
| 3680 | Result += "#pragma data_seg(pop)\n\n"; |
| 3681 | } |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3682 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | cab93d5 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3683 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3684 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3685 | Result += "&_OBJC_MODULES;\n"; |
| 3686 | Result += "#pragma data_seg(pop)\n\n"; |
| 3687 | } |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3688 | } |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3689 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3690 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3691 | const char *funcName, |
| 3692 | std::string Tag) { |
| 3693 | const FunctionType *AFT = CE->getFunctionType(); |
| 3694 | QualType RT = AFT->getResultType(); |
| 3695 | std::string StructRef = "struct " + Tag; |
| 3696 | std::string S = "static " + RT.getAsString() + " __" + |
| 3697 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3698 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3699 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3701 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3702 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3703 | // block (to reference imported block decl refs). |
| 3704 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3705 | } else if (BD->param_empty()) { |
| 3706 | S += "(" + StructRef + " *__cself)"; |
| 3707 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3708 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3709 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3710 | S += '('; |
| 3711 | // first add the implicit argument. |
| 3712 | S += StructRef + " *__cself, "; |
| 3713 | std::string ParamStr; |
| 3714 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3715 | E = BD->param_end(); AI != E; ++AI) { |
| 3716 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3717 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3718 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3719 | S += ParamStr; |
| 3720 | } |
| 3721 | if (FT->isVariadic()) { |
| 3722 | if (!BD->param_empty()) S += ", "; |
| 3723 | S += "..."; |
| 3724 | } |
| 3725 | S += ')'; |
| 3726 | } |
| 3727 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3729 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3730 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3732 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3733 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3734 | std::string Name = (*I)->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3736 | Context->PrintingPolicy); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3737 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3739 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3741 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3742 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3743 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3744 | // Handle nested closure invocation. For example: |
| 3745 | // |
| 3746 | // void (^myImportedClosure)(void); |
| 3747 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3749 | // void (^anotherClosure)(void); |
| 3750 | // anotherClosure = ^(void) { |
| 3751 | // myImportedClosure(); // import and invoke the closure |
| 3752 | // }; |
| 3753 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3754 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3755 | S += "struct __block_impl *"; |
| 3756 | else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3757 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3758 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3759 | } |
| 3760 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3761 | const char *cstr = RewrittenStr.c_str(); |
| 3762 | while (*cstr++ != '{') ; |
| 3763 | S += cstr; |
| 3764 | S += "\n"; |
| 3765 | return S; |
| 3766 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3767 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3768 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3769 | const char *funcName, |
| 3770 | std::string Tag) { |
| 3771 | std::string StructRef = "struct " + Tag; |
| 3772 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3774 | S += funcName; |
| 3775 | S += "_block_copy_" + utostr(i); |
| 3776 | S += "(" + StructRef; |
| 3777 | S += "*dst, " + StructRef; |
| 3778 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3780 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3781 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3782 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3783 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3784 | S += (*I)->getNameAsString(); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3785 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3786 | } |
| 3787 | S += "\nstatic void __"; |
| 3788 | S += funcName; |
| 3789 | S += "_block_dispose_" + utostr(i); |
| 3790 | S += "(" + StructRef; |
| 3791 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3793 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3794 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3795 | S += (*I)->getNameAsString(); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3796 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3797 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3799 | return S; |
| 3800 | } |
| 3801 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3802 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3803 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3804 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3805 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3807 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3808 | S += " struct " + Desc; |
| 3809 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3810 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3811 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3812 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3813 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3815 | if (BlockDeclRefs.size()) { |
| 3816 | // Output all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3818 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3819 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3820 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3821 | std::string ArgName = "_" + FieldName; |
| 3822 | // Handle nested closure invocation. For example: |
| 3823 | // |
| 3824 | // void (^myImportedBlock)(void); |
| 3825 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3827 | // void (^anotherBlock)(void); |
| 3828 | // anotherBlock = ^(void) { |
| 3829 | // myImportedBlock(); // import and invoke the closure |
| 3830 | // }; |
| 3831 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3832 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3833 | S += "struct __block_impl *"; |
| 3834 | Constructor += ", void *" + ArgName; |
| 3835 | } else { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3836 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3837 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3838 | Constructor += ", " + ArgName; |
| 3839 | } |
| 3840 | S += FieldName + ";\n"; |
| 3841 | } |
| 3842 | // Output all "by ref" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3844 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3845 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3846 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3847 | std::string ArgName = "_" + FieldName; |
| 3848 | // Handle nested closure invocation. For example: |
| 3849 | // |
| 3850 | // void (^myImportedBlock)(void); |
| 3851 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | // void (^anotherBlock)(void); |
| 3854 | // anotherBlock = ^(void) { |
| 3855 | // myImportedBlock(); // import and invoke the closure |
| 3856 | // }; |
| 3857 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3858 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3859 | S += "struct __block_impl *"; |
| 3860 | Constructor += ", void *" + ArgName; |
| 3861 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3863 | Context->PrintingPolicy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3865 | Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3866 | Constructor += ", " + ArgName; |
| 3867 | } |
| 3868 | S += FieldName + "; // by ref\n"; |
| 3869 | } |
| 3870 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3871 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3872 | if (GlobalVarDecl) |
| 3873 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3874 | else |
| 3875 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3876 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3878 | Constructor += " Desc = desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3880 | // Initialize all "by copy" arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3882 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3883 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3884 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3885 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3886 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3887 | else |
| 3888 | Constructor += Name + " = _"; |
| 3889 | Constructor += Name + ";\n"; |
| 3890 | } |
| 3891 | // Initialize all "by ref" arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3893 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3894 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3895 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3896 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3897 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3898 | else |
| 3899 | Constructor += Name + " = _"; |
| 3900 | Constructor += Name + ";\n"; |
| 3901 | } |
| 3902 | } else { |
| 3903 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3904 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3905 | if (GlobalVarDecl) |
| 3906 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3907 | else |
| 3908 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3909 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3910 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3911 | } |
| 3912 | Constructor += " "; |
| 3913 | Constructor += "}\n"; |
| 3914 | S += Constructor; |
| 3915 | S += "};\n"; |
| 3916 | return S; |
| 3917 | } |
| 3918 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3919 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3920 | std::string ImplTag, int i, |
| 3921 | const char *FunName, |
| 3922 | unsigned hasCopy) { |
| 3923 | std::string S = "\nstatic struct " + DescTag; |
| 3924 | |
| 3925 | S += " {\n unsigned long reserved;\n"; |
| 3926 | S += " unsigned long Block_size;\n"; |
| 3927 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3928 | S += " void (*copy)(struct "; |
| 3929 | S += ImplTag; S += "*, struct "; |
| 3930 | S += ImplTag; S += "*);\n"; |
| 3931 | |
| 3932 | S += " void (*dispose)(struct "; |
| 3933 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3934 | } |
| 3935 | S += "} "; |
| 3936 | |
| 3937 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3938 | S += ImplTag + ")"; |
| 3939 | if (hasCopy) { |
| 3940 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 3941 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 3942 | } |
| 3943 | S += "};\n"; |
| 3944 | return S; |
| 3945 | } |
| 3946 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3947 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3948 | const char *FunName) { |
| 3949 | // Insert closures that were part of the function. |
| 3950 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3951 | |
| 3952 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3953 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3954 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3955 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3957 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3958 | |
| 3959 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3960 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3961 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3963 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3964 | |
| 3965 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3966 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3967 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3968 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3969 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3970 | ImportedBlockDecls.size() > 0); |
| 3971 | InsertText(FunLocStart, BD.c_str(), BD.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3972 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3973 | BlockDeclRefs.clear(); |
| 3974 | BlockByRefDecls.clear(); |
| 3975 | BlockByCopyDecls.clear(); |
| 3976 | BlockCallExprs.clear(); |
| 3977 | ImportedBlockDecls.clear(); |
| 3978 | } |
| 3979 | Blocks.clear(); |
| 3980 | RewrittenBlockExprs.clear(); |
| 3981 | } |
| 3982 | |
| 3983 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3984 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3985 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3987 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3988 | } |
| 3989 | |
| 3990 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3991 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3992 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3993 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3994 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3995 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3996 | // Convert colons to underscores. |
| 3997 | std::string::size_type loc = 0; |
| 3998 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3999 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4001 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4002 | } |
| 4003 | |
| 4004 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4005 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4006 | CI != E; ++CI) |
| 4007 | if (*CI) { |
| 4008 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4009 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4010 | else |
| 4011 | GetBlockDeclRefExprs(*CI); |
| 4012 | } |
| 4013 | // Handle specific things. |
| 4014 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4015 | // FIXME: Handle enums. |
| 4016 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4017 | BlockDeclRefs.push_back(CDRE); |
| 4018 | return; |
| 4019 | } |
| 4020 | |
| 4021 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 4022 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4023 | CI != E; ++CI) |
| 4024 | if (*CI) { |
| 4025 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4026 | GetBlockCallExprs(CBE->getBody()); |
| 4027 | else |
| 4028 | GetBlockCallExprs(*CI); |
| 4029 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4032 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4033 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 4034 | } |
| 4035 | } |
| 4036 | return; |
| 4037 | } |
| 4038 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4039 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4041 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4043 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4044 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4045 | } else if (const BlockDeclRefExpr *CDRE = |
| 4046 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4047 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4048 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4049 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4050 | } |
| 4051 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4052 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4053 | } |
| 4054 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4055 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4056 | else if (const ConditionalOperator *CEXPR = |
| 4057 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4058 | Expr *LHSExp = CEXPR->getLHS(); |
| 4059 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4060 | Expr *RHSExp = CEXPR->getRHS(); |
| 4061 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4062 | Expr *CONDExp = CEXPR->getCond(); |
| 4063 | ConditionalOperator *CondExpr = |
| 4064 | new (Context) ConditionalOperator(CONDExp, |
| 4065 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4066 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4067 | Exp->getType()); |
| 4068 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4069 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4070 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4071 | } else { |
| 4072 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4073 | } |
| 4074 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4075 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4076 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4077 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4078 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4080 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4081 | SourceLocation(), |
| 4082 | &Context->Idents.get("__block_impl")); |
| 4083 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4084 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4085 | // Generate a funky cast. |
| 4086 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4088 | // Push the block argument type. |
| 4089 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4090 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4092 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4093 | QualType t = *I; |
| 4094 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4095 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4096 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4097 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4098 | } |
| 4099 | ArgTypes.push_back(t); |
| 4100 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4101 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4102 | // Now do the pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4103 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4104 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4106 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
| 4108 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4109 | CastExpr::CK_Unknown, |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4110 | const_cast<Expr*>(BlockExp), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4111 | PtrBlock, SourceLocation(), |
| 4112 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4113 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4114 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4115 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4116 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4118 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4120 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4121 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4122 | FD->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4124 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, |
| 4125 | CastExpr::CK_Unknown, ME, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4126 | PtrToFuncCastType, |
| 4127 | SourceLocation(), |
| 4128 | SourceLocation()); |
| 4129 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4131 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4132 | // Add the implicit argument. |
| 4133 | BlkExprs.push_back(BlkCast); |
| 4134 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4136 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4137 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4138 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4139 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4140 | BlkExprs.size(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4141 | Exp->getType(), SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4142 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4146 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4147 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4150 | // We need to return the rewritten expression to handle cases where the |
| 4151 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4152 | // For example: |
| 4153 | // |
| 4154 | // int main() { |
| 4155 | // __block Foo *f; |
| 4156 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4158 | // void (^myblock)() = ^() { |
| 4159 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4160 | // i = 77; |
| 4161 | // }; |
| 4162 | //} |
| 4163 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4164 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4165 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4166 | Context->getPointerType(BDRE->getType()), |
| 4167 | SourceLocation()); |
| 4168 | // Need parens to enforce precedence. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4169 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4170 | ReplaceStmt(BDRE, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4171 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4174 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4175 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4176 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4177 | |
| 4178 | // Need to avoid trying to rewrite synthesized casts. |
| 4179 | if (LocStart.isInvalid()) |
| 4180 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4181 | // Need to avoid trying to rewrite casts contained in macros. |
| 4182 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4183 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4185 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4186 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4188 | // advance the location to startArgList. |
| 4189 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4190 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4191 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4192 | switch (*argPtr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4193 | case '^': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4194 | // Replace the '^' with '*'. |
| 4195 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4196 | ReplaceText(LocStart, 1, "*", 1); |
| 4197 | break; |
| 4198 | } |
| 4199 | } |
| 4200 | return; |
| 4201 | } |
| 4202 | |
| 4203 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4204 | SourceLocation DeclLoc = FD->getLocation(); |
| 4205 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4206 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4207 | // We have 1 or more arguments that have closure pointers. |
| 4208 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4209 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4211 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4213 | parenCount++; |
| 4214 | // advance the location to startArgList. |
| 4215 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4216 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4218 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4220 | while (*argPtr++ && parenCount) { |
| 4221 | switch (*argPtr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | case '^': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4223 | // Replace the '^' with '*'. |
| 4224 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4225 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4226 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | case '(': |
| 4228 | parenCount++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4229 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4230 | case ')': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4231 | parenCount--; |
| 4232 | break; |
| 4233 | } |
| 4234 | } |
| 4235 | return; |
| 4236 | } |
| 4237 | |
| 4238 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4239 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4240 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4241 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4242 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4243 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4244 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4245 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4246 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4247 | } |
| 4248 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4249 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4250 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4251 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4252 | return true; |
| 4253 | } |
| 4254 | return false; |
| 4255 | } |
| 4256 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4257 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4258 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4259 | const char *argPtr = strchr(Name, '('); |
| 4260 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4262 | LParen = argPtr; // output the start. |
| 4263 | argPtr++; // skip past the left paren. |
| 4264 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4266 | while (*argPtr && parenCount) { |
| 4267 | switch (*argPtr) { |
| 4268 | case '(': parenCount++; break; |
| 4269 | case ')': parenCount--; break; |
| 4270 | default: break; |
| 4271 | } |
| 4272 | if (parenCount) argPtr++; |
| 4273 | } |
| 4274 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4275 | RParen = argPtr; // output the end |
| 4276 | } |
| 4277 | |
| 4278 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4279 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4280 | RewriteBlockPointerFunctionArgs(FD); |
| 4281 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4283 | // Handle Variables and Typedefs. |
| 4284 | SourceLocation DeclLoc = ND->getLocation(); |
| 4285 | QualType DeclT; |
| 4286 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4287 | DeclT = VD->getType(); |
| 4288 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4289 | DeclT = TDD->getUnderlyingType(); |
| 4290 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4291 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4292 | else |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4293 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4295 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4296 | const char *endBuf = startBuf; |
| 4297 | // scan backward (from the decl location) for the end of the previous decl. |
| 4298 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4299 | startBuf--; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4301 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4302 | // may take block argument types (which will be handled below). |
| 4303 | if (*startBuf == '^') { |
| 4304 | // Replace the '^' with '*', computing a negative offset. |
| 4305 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4306 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4307 | } |
| 4308 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4309 | // Replace the '^' with '*' for arguments. |
| 4310 | DeclLoc = ND->getLocation(); |
| 4311 | startBuf = SM->getCharacterData(DeclLoc); |
| 4312 | const char *argListBegin, *argListEnd; |
| 4313 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4314 | while (argListBegin < argListEnd) { |
| 4315 | if (*argListBegin == '^') { |
| 4316 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4317 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4318 | } |
| 4319 | argListBegin++; |
| 4320 | } |
| 4321 | } |
| 4322 | return; |
| 4323 | } |
| 4324 | |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4325 | void RewriteObjC::RewriteByRefVar(NamedDecl *ND) { |
| 4326 | return; |
| 4327 | } |
| 4328 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4330 | // Add initializers for any closure decl refs. |
| 4331 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4332 | if (BlockDeclRefs.size()) { |
| 4333 | // Unique all "by copy" declarations. |
| 4334 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4335 | if (!BlockDeclRefs[i]->isByRef()) |
| 4336 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4337 | // Unique all "by ref" declarations. |
| 4338 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4339 | if (BlockDeclRefs[i]->isByRef()) { |
| 4340 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4341 | } |
| 4342 | // Find any imported blocks...they will need special attention. |
| 4343 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4344 | if (BlockDeclRefs[i]->isByRef() || |
| 4345 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4346 | BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 832d890 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4347 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4348 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4349 | } |
| 4350 | } |
| 4351 | } |
| 4352 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4353 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4354 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4355 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4357 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4358 | false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4361 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4362 | Blocks.push_back(Exp); |
| 4363 | |
| 4364 | CollectBlockDeclRefInfo(Exp); |
| 4365 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4366 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4367 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4368 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4369 | else if (CurMethodDef) { |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4370 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4371 | // Convert colons to underscores. |
| 4372 | std::string::size_type loc = 0; |
| 4373 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4374 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4375 | } else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4376 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4378 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4380 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4381 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4382 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4383 | // Get a pointer to the function type so we can cast appropriately. |
| 4384 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4385 | |
| 4386 | FunctionDecl *FD; |
| 4387 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4389 | // Simulate a contructor call... |
| 4390 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4391 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4392 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4393 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4395 | // Initialize the block function. |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4396 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4397 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4398 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4399 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4400 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4401 | Context->VoidPtrTy, SourceLocation(), |
| 4402 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | InitExprs.push_back(castExpr); |
| 4404 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4405 | // Initialize the block descriptor. |
| 4406 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4408 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4409 | &Context->Idents.get(DescData.c_str()), |
| 4410 | Context->VoidPtrTy, 0, |
| 4411 | VarDecl::Static); |
| 4412 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 4413 | new (Context) DeclRefExpr(NewVD, |
| 4414 | Context->VoidPtrTy, SourceLocation()), |
| 4415 | UnaryOperator::AddrOf, |
| 4416 | Context->getPointerType(Context->VoidPtrTy), |
| 4417 | SourceLocation()); |
| 4418 | InitExprs.push_back(DescRefExpr); |
| 4419 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4420 | // Add initializers for any closure decl refs. |
| 4421 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4422 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4423 | // Output all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4425 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4426 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4427 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4428 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4429 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4430 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4431 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4432 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4434 | CastExpr::CK_Unknown, Arg, |
| 4435 | Context->VoidPtrTy, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4436 | SourceLocation(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4437 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4438 | } else { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4439 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4440 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4441 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4443 | } |
| 4444 | // Output all "by ref" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4446 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4447 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4448 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4449 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | Context->getPointerType(Exp->getType()), |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4451 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4455 | if (ImportedBlockDecls.size()) { // generate "1<<25" to indicate we have helper functions. |
| 4456 | unsigned IntSize = |
| 4457 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4458 | BinaryOperator *Exp = new (Context) BinaryOperator( |
| 4459 | new (Context) IntegerLiteral(llvm::APInt(IntSize, 1), |
| 4460 | Context->IntTy,SourceLocation()), |
| 4461 | new (Context) IntegerLiteral(llvm::APInt(IntSize, 25), |
| 4462 | Context->IntTy, SourceLocation()), |
| 4463 | BinaryOperator::Shl, Context->IntTy, SourceLocation()); |
| 4464 | InitExprs.push_back(Exp); |
| 4465 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4466 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4467 | FType, SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4468 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4470 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4471 | NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4472 | FType, SourceLocation(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4473 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4474 | BlockDeclRefs.clear(); |
| 4475 | BlockByRefDecls.clear(); |
| 4476 | BlockByCopyDecls.clear(); |
| 4477 | ImportedBlockDecls.clear(); |
| 4478 | return NewRep; |
| 4479 | } |
| 4480 | |
| 4481 | //===----------------------------------------------------------------------===// |
| 4482 | // Function Body / Expression rewriting |
| 4483 | //===----------------------------------------------------------------------===// |
| 4484 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4485 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4486 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4487 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4488 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4489 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4490 | // we get this right. |
| 4491 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4492 | // Perform a bottom up traversal of all children. |
| 4493 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4494 | CI != E; ++CI) |
| 4495 | if (*CI) |
| 4496 | CollectPropertySetters(*CI); |
| 4497 | |
| 4498 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4499 | if (BinOp->isAssignmentOp()) { |
| 4500 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4501 | PropSetters[PRE] = BinOp; |
| 4502 | } |
| 4503 | } |
| 4504 | } |
| 4505 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4506 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4508 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4509 | Stmts.push_back(S); |
| 4510 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4511 | Stmts.push_back(S); |
Anders Carlsson | 3caa2b4 | 2009-12-22 06:13:42 +0000 | [diff] [blame^] | 4512 | ++BcLabelCount; |
| 4513 | ObjCBcLabelNo.push_back(BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4515 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4516 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4518 | // Perform a bottom up rewrite of all children. |
| 4519 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4520 | CI != E; ++CI) |
| 4521 | if (*CI) { |
| 4522 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | if (newStmt) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4524 | *CI = newStmt; |
| 4525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4527 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4528 | // Rewrite the block body in place. |
| 4529 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4531 | // Now we snarf the rewritten text and stash it away for later use. |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4532 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4533 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4535 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4536 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4537 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4538 | return blockTranscribed; |
| 4539 | } |
| 4540 | // Handle specific things. |
| 4541 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4542 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4543 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4544 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4545 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4546 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4547 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4548 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4549 | if (BinOp) { |
| 4550 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4551 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4552 | DisableReplaceStmt = true; |
| 4553 | // Save the source range. Even if we disable the replacement, the |
| 4554 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4555 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | // self.errorHandler = handler ? handler : |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4557 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4558 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4559 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4560 | DisableReplaceStmt = false; |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4561 | // |
| 4562 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4563 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4564 | // to see the original expression). Consider this example: |
| 4565 | // |
| 4566 | // Foo *obj1, *obj2; |
| 4567 | // |
| 4568 | // obj1.i = [obj2 rrrr]; |
| 4569 | // |
| 4570 | // 'BinOp' for the previous expression looks like: |
| 4571 | // |
| 4572 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4573 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4574 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4575 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4576 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4577 | // |
| 4578 | // 'newStmt' represents the rewritten message expression. For example: |
| 4579 | // |
| 4580 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4581 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4582 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4583 | // (CStyleCastExpr 0x231d220 'void *' |
| 4584 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4585 | // |
| 4586 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4587 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4588 | // the original RHS (since we haven't altered BinOp). |
| 4589 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4590 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4591 | // node. As a result, we now leak the original AST nodes. |
| 4592 | // |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4593 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4594 | } else { |
| 4595 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4596 | } |
| 4597 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4598 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4599 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4601 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4602 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4604 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4605 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4606 | // Before we rewrite it, put the original message expression in a comment. |
| 4607 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4608 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4609 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4610 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4611 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4613 | std::string messString; |
| 4614 | messString += "// "; |
| 4615 | messString.append(startBuf, endBuf-startBuf+1); |
| 4616 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
| 4618 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4619 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4620 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4621 | // Tried this, but it didn't work either... |
| 4622 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4623 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4624 | return RewriteMessageExpr(MessExpr); |
| 4625 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4627 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4628 | return RewriteObjCTryStmt(StmtTry); |
| 4629 | |
| 4630 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4631 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4632 | |
| 4633 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4634 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4636 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4637 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | |
| 4639 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4640 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4642 | OrigStmtRange.getEnd()); |
| 4643 | if (BreakStmt *StmtBreakStmt = |
| 4644 | dyn_cast<BreakStmt>(S)) |
| 4645 | return RewriteBreakStmt(StmtBreakStmt); |
| 4646 | if (ContinueStmt *StmtContinueStmt = |
| 4647 | dyn_cast<ContinueStmt>(S)) |
| 4648 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | |
| 4650 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4651 | // and cast exprs. |
| 4652 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4653 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4654 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4656 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4657 | // the context of an ObjCForCollectionStmt. For example: |
| 4658 | // NSArray *someArray; |
| 4659 | // for (id <FooProtocol> index in someArray) ; |
| 4660 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4661 | // and it depends on the original text locations/positions. |
Benjamin Kramer | acc5fa1 | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 4662 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4663 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4665 | // Blocks rewrite rules. |
| 4666 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4667 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4668 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4669 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4670 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4671 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4673 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4674 | if (ND->hasAttr<BlocksAttr>()) |
| 4675 | RewriteByRefVar(ND); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4676 | } |
| 4677 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4678 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4679 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4681 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4682 | } |
| 4683 | } |
| 4684 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4686 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4687 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4688 | |
| 4689 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4690 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4691 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4693 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4694 | && "Statement stack mismatch"); |
| 4695 | Stmts.pop_back(); |
| 4696 | } |
| 4697 | // Handle blocks rewriting. |
| 4698 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4699 | if (BDRE->isByRef()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4700 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4701 | } |
| 4702 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4703 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4704 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4705 | ReplaceStmt(S, BlockCall); |
| 4706 | return BlockCall; |
| 4707 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4708 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4709 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4710 | RewriteCastExpr(CE); |
| 4711 | } |
| 4712 | #if 0 |
| 4713 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4714 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4715 | // Get the new text. |
| 4716 | std::string SStr; |
| 4717 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 0905f14 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4718 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4719 | const std::string &Str = Buf.str(); |
| 4720 | |
| 4721 | printf("CAST = %s\n", &Str[0]); |
| 4722 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4723 | delete S; |
| 4724 | return Replacement; |
| 4725 | } |
| 4726 | #endif |
| 4727 | // Return this stmt unmodified. |
| 4728 | return S; |
| 4729 | } |
| 4730 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4731 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4732 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4733 | e = RD->field_end(); i != e; ++i) { |
| 4734 | FieldDecl *FD = *i; |
| 4735 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4736 | RewriteBlockPointerDecl(FD); |
| 4737 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4738 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4739 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4740 | } |
| 4741 | } |
| 4742 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4744 | /// main file of the input. |
| 4745 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4746 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | b136888 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4747 | if (FD->isOverloadedOperator()) |
| 4748 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4750 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4751 | // prototype. This enables us to rewrite function declarations and |
| 4752 | // definitions using the same code. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4753 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4754 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 4755 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4756 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4757 | CurFunctionDef = FD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4758 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4759 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4760 | Body = |
| 4761 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4762 | FD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4763 | CurrentBody = 0; |
| 4764 | if (PropParentMap) { |
| 4765 | delete PropParentMap; |
| 4766 | PropParentMap = 0; |
| 4767 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4768 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4769 | // and any copy/dispose helper functions. |
| 4770 | InsertBlockLiteralsWithinFunction(FD); |
| 4771 | CurFunctionDef = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4773 | return; |
| 4774 | } |
| 4775 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4776 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4777 | CurMethodDef = MD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4778 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4779 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4780 | Body = |
| 4781 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4782 | MD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4783 | CurrentBody = 0; |
| 4784 | if (PropParentMap) { |
| 4785 | delete PropParentMap; |
| 4786 | PropParentMap = 0; |
| 4787 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4788 | InsertBlockLiteralsWithinMethod(MD); |
| 4789 | CurMethodDef = 0; |
| 4790 | } |
| 4791 | } |
| 4792 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4793 | ClassImplementation.push_back(CI); |
| 4794 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4795 | CategoryImplementation.push_back(CI); |
| 4796 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4797 | RewriteForwardClassDecl(CD); |
| 4798 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4799 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4800 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4801 | RewriteBlockPointerDecl(VD); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4802 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4803 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4804 | if (VD->getInit()) { |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4805 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4806 | RewriteCastExpr(CE); |
| 4807 | } |
| 4808 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4809 | } else if (VD->getType()->isRecordType()) { |
| 4810 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4811 | if (RD->isDefinition()) |
| 4812 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4813 | } |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4814 | if (VD->getInit()) { |
| 4815 | GlobalVarDecl = VD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4816 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4817 | CurrentBody = VD->getInit(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4818 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4819 | CurrentBody = 0; |
| 4820 | if (PropParentMap) { |
| 4821 | delete PropParentMap; |
| 4822 | PropParentMap = 0; |
| 4823 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4824 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4825 | VD->getNameAsCString()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4826 | GlobalVarDecl = 0; |
| 4827 | |
| 4828 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4829 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4830 | RewriteCastExpr(CE); |
| 4831 | } |
| 4832 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4833 | return; |
| 4834 | } |
| 4835 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4836 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4837 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4838 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4839 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4840 | else if (TD->getUnderlyingType()->isRecordType()) { |
| 4841 | RecordDecl *RD = TD->getUnderlyingType()->getAs<RecordType>()->getDecl(); |
| 4842 | if (RD->isDefinition()) |
| 4843 | RewriteRecordBody(RD); |
| 4844 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4845 | return; |
| 4846 | } |
| 4847 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4848 | if (RD->isDefinition()) |
| 4849 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4850 | return; |
| 4851 | } |
| 4852 | // Nothing yet. |
| 4853 | } |
| 4854 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4855 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4856 | // Get the top-level buffer that this corresponds to. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4857 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4858 | // Rewrite tabs if we care. |
| 4859 | //RewriteTabs(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4860 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4861 | if (Diags.hasErrorOccurred()) |
| 4862 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4864 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4866 | // Here's a great place to add any extra declarations that may be needed. |
| 4867 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4869 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 4870 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 4871 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4873 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4874 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4875 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4876 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4877 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4878 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4880 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4881 | //printf("Changed:\n"); |
| 4882 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4883 | } else { |
| 4884 | fprintf(stderr, "No changes\n"); |
| 4885 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4886 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4887 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 4888 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4889 | // Rewrite Objective-c meta data* |
| 4890 | std::string ResultStr; |
| 4891 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4892 | // Emit metadata. |
| 4893 | *OutFile << ResultStr; |
| 4894 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4895 | OutFile->flush(); |
| 4896 | } |
| 4897 | |