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" |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 29 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 30 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 33 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 34 | enum { |
| 35 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 36 | block, ... */ |
| 37 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 38 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 39 | __block variable */ |
| 40 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 41 | helpers */ |
| 42 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 43 | support routines */ |
| 44 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 45 | }; |
| 46 | |
| 47 | enum { |
| 48 | BLOCK_NEEDS_FREE = (1 << 24), |
| 49 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 50 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 51 | BLOCK_IS_GC = (1 << 27), |
| 52 | BLOCK_IS_GLOBAL = (1 << 28), |
| 53 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 54 | }; |
| 55 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 56 | Rewriter Rewrite; |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 57 | Diagnostic &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 58 | const LangOptions &LangOpts; |
Steve Naroff | 7b3579b | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 59 | unsigned RewriteFailedDiag; |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 60 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 62 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 63 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 64 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 65 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 66 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 67 | SourceLocation LastIncLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 69 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 70 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 71 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 72 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 74 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 75 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 76 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 77 | // Remember all the @protocol(<expr>) expressions. |
| 78 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 79 | |
| 80 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 81 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 82 | unsigned NumObjCStringLiterals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 84 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendStretFunctionDecl; |
| 87 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 88 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 89 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 90 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 91 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 92 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 93 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 95 | // ObjC string constant support. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 96 | VarDecl *ConstantStringClassReference; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 97 | RecordDecl *NSStringRecord; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Fariborz Jahanian | 19d42bf | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 99 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 100 | int BcLabelCount; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 102 | // Needed for super. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 103 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 104 | RecordDecl *SuperStructDecl; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 105 | RecordDecl *ConstantStringDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 107 | TypeDecl *ProtocolTypeDecl; |
| 108 | QualType getProtocolType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 110 | // Needed for header files being rewritten |
| 111 | bool IsHeader; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 113 | std::string InFileName; |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 114 | llvm::raw_ostream* OutFile; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 115 | |
| 116 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 117 | bool objc_impl_method; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 118 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 119 | std::string Preamble; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 120 | |
| 121 | // Block expressions. |
| 122 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 123 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 124 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 126 | // Block related declarations. |
| 127 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 128 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 129 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 130 | |
| 131 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 132 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 133 | // This maps a property to it's assignment statement. |
| 134 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 135 | // This maps a property to it's synthesied message expression. |
| 136 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 137 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 139 | // This maps an original source AST to it's rewritten form. This allows |
| 140 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 141 | // This is needed to support some of the exotic property rewriting. |
| 142 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 143 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 144 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 145 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 146 | VarDecl *GlobalVarDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 148 | bool DisableReplaceStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 150 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 151 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 152 | virtual void Initialize(ASTContext &context); |
| 153 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 154 | // Top Level Driver code. |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 155 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 156 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 157 | HandleTopLevelSingleDecl(*I); |
| 158 | } |
| 159 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 160 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 161 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 162 | Diagnostic &D, const LangOptions &LOpts, |
| 163 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 164 | |
| 165 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 167 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 169 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 170 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 172 | if (ReplacingStmt) |
| 173 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 174 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 175 | if (DisableReplaceStmt) |
| 176 | return; // Used when rewriting the assignment of a property setter. |
| 177 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | // If replacement succeeded or warning disabled return with no warning. |
| 179 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 180 | ReplacedNodes[Old] = New; |
| 181 | return; |
| 182 | } |
| 183 | if (SilenceRewriteMacroWarning) |
| 184 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 185 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 186 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 187 | } |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 188 | |
| 189 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 190 | // Measaure the old text. |
| 191 | int Size = Rewrite.getRangeSize(SrcRange); |
| 192 | if (Size == -1) { |
| 193 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 194 | << Old->getSourceRange(); |
| 195 | return; |
| 196 | } |
| 197 | // Get the new text. |
| 198 | std::string SStr; |
| 199 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 200 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 201 | const std::string &Str = S.str(); |
| 202 | |
| 203 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 204 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 205 | ReplacedNodes[Old] = New; |
| 206 | return; |
| 207 | } |
| 208 | if (SilenceRewriteMacroWarning) |
| 209 | return; |
| 210 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 211 | << Old->getSourceRange(); |
| 212 | } |
| 213 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 214 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 215 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 216 | // If insertion succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 217 | if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen), |
| 218 | InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 219 | SilenceRewriteMacroWarning) |
| 220 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 222 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 225 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 226 | // If removal succeeded or warning disabled return with no warning. |
| 227 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 228 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 230 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 231 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 233 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 234 | const char *NewStr, unsigned NewLength) { |
| 235 | // If removal succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 236 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 237 | llvm::StringRef(NewStr, NewLength)) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 238 | SilenceRewriteMacroWarning) |
| 239 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 241 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 244 | // Syntactic Rewriting. |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 245 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 246 | void RewriteInclude(); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 247 | void RewriteTabs(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 249 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 250 | ObjCImplementationDecl *IMD, |
| 251 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 252 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 253 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 254 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 255 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 256 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 257 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 258 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 259 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 260 | void RewriteFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 261 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 262 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 263 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 264 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 266 | QualType getSuperStructType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 267 | QualType getConstantStringStructType(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 268 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 270 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 271 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 272 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 274 | Stmt *CurrentBody; |
| 275 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 277 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 278 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 279 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 281 | SourceRange SrcRange); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 282 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 283 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 284 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 285 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 286 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 287 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 288 | void RewriteTryReturnStmts(Stmt *S); |
| 289 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 291 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 292 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 293 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 294 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 295 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 296 | SourceLocation OrigEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 298 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 299 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 300 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 301 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 302 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 304 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 305 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 306 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 307 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 308 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 309 | void SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 310 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 311 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 312 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 314 | // Metadata emission. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 315 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 316 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 318 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 319 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 321 | template<typename MethodIterator> |
| 322 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 323 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 324 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 325 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 326 | const char *ClassName, |
| 327 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 329 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 330 | const char *prefix, |
| 331 | const char *ClassName, |
| 332 | std::string &Result); |
| 333 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | const char *prefix, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 335 | const char *ClassName, |
| 336 | std::string &Result); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 337 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 338 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 340 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 341 | std::string &Result); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 342 | void RewriteImplementations(); |
| 343 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 345 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 347 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 349 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 350 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
| 352 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 353 | void RewriteBlockCall(CallExpr *Exp); |
| 354 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 355 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 356 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 357 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 358 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
| 360 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | const char *funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 363 | const char *funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 364 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 365 | std::string Tag, std::string Desc); |
| 366 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 367 | std::string ImplTag, |
| 368 | int i, const char *funcName, |
| 369 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 370 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 371 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 372 | const char *FunName); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 373 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 375 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 376 | void GetBlockCallExprs(Stmt *S); |
| 377 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 379 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 380 | // 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] | 381 | bool isTopLevelBlockPointerType(QualType T) { |
| 382 | return isa<BlockPointerType>(T); |
| 383 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 385 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 386 | bool isObjCType(QualType T) { |
| 387 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 388 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 390 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 392 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 393 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 394 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 396 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 398 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 404 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 405 | const char *&RParen); |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 406 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 408 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 409 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 411 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 413 | if (From[i] == '"') |
| 414 | To += "\\\""; |
| 415 | else |
| 416 | To += From[i]; |
| 417 | } |
| 418 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 419 | }; |
| 420 | } |
| 421 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 423 | NamedDecl *D) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 424 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 426 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 427 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 428 | // All the args are checked/rewritten. Don't call twice! |
| 429 | RewriteBlockPointerDecl(D); |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 436 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 437 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 438 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 441 | static bool IsHeaderFile(const std::string &Filename) { |
| 442 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 444 | if (DotPos == std::string::npos) { |
| 445 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 447 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 449 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 450 | // C header: .h |
| 451 | // C++ header: .hh or .H; |
| 452 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 454 | |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 455 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 456 | Diagnostic &D, const LangOptions &LOpts, |
| 457 | bool silenceMacroWarn) |
| 458 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 459 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 460 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 462 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 464 | "rewriter doesn't support user-specified control flow semantics " |
| 465 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 468 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 469 | llvm::raw_ostream* OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | Diagnostic &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 471 | const LangOptions &LOpts, |
| 472 | bool SilenceRewriteMacroWarning) { |
| 473 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 474 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 475 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 476 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 477 | Context = &context; |
| 478 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 479 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 480 | MsgSendFunctionDecl = 0; |
| 481 | MsgSendSuperFunctionDecl = 0; |
| 482 | MsgSendStretFunctionDecl = 0; |
| 483 | MsgSendSuperStretFunctionDecl = 0; |
| 484 | MsgSendFpretFunctionDecl = 0; |
| 485 | GetClassFunctionDecl = 0; |
| 486 | GetMetaClassFunctionDecl = 0; |
| 487 | SelGetUidFunctionDecl = 0; |
| 488 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 489 | ConstantStringClassReference = 0; |
| 490 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 491 | CurMethodDef = 0; |
| 492 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 493 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 494 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 495 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 496 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 497 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 498 | BcLabelCount = 0; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 499 | SuperContructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 500 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 501 | PropParentMap = 0; |
| 502 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 503 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 504 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 506 | // Get the ID and start/end of the main file. |
| 507 | MainFileID = SM->getMainFileID(); |
| 508 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 509 | MainFileStart = MainBuf->getBufferStart(); |
| 510 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 512 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 514 | // declaring objc_selector outside the parameter list removes a silly |
| 515 | // scope related warning... |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 516 | if (IsHeader) |
Steve Naroff | fcc6fd5 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 517 | Preamble = "#pragma once\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 518 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 519 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 520 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 521 | if (LangOpts.Microsoft) { |
| 522 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 523 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 524 | ": "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 525 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 526 | } |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 527 | Preamble += "};\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 528 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 529 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 530 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 531 | Preamble += "#endif\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 532 | if (LangOpts.Microsoft) { |
| 533 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 534 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 535 | } else |
| 536 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 537 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 538 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 539 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 540 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 541 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 542 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 543 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 544 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 545 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 546 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 547 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 548 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 549 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 550 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 551 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 552 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 553 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 554 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 555 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | d30f8c5 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 556 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 8dd1525 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 557 | // @synchronized hooks. |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 558 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 559 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 560 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 561 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 562 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 563 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | 4dbab8a | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 564 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 565 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 566 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 567 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 568 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 569 | Preamble += "#endif\n"; |
| 570 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 571 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 572 | Preamble += " int *isa;\n"; |
| 573 | Preamble += " int flags;\n"; |
| 574 | Preamble += " char *str;\n"; |
| 575 | Preamble += " long length;\n"; |
| 576 | Preamble += "};\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 577 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 578 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 579 | Preamble += "#else\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 580 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 581 | Preamble += "#endif\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 582 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 583 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 584 | // Blocks preamble. |
| 585 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 586 | Preamble += "#define BLOCK_IMPL\n"; |
| 587 | Preamble += "struct __block_impl {\n"; |
| 588 | Preamble += " void *isa;\n"; |
| 589 | Preamble += " int Flags;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 590 | Preamble += " int Reserved;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 591 | Preamble += " void *FuncPtr;\n"; |
| 592 | Preamble += "};\n"; |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 593 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | 287a2bf | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 594 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 595 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_assign(void *, const void *, const int);\n"; |
| 596 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 597 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 598 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 599 | Preamble += "#else\n"; |
Steve Naroff | 7bf01ea | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 600 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 601 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 602 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 603 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 604 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 605 | Preamble += "#endif\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 606 | if (LangOpts.Microsoft) { |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 607 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 608 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 609 | Preamble += "#define __attribute__(X)\n"; |
| 610 | } |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 611 | else { |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 612 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 613 | Preamble += "#define __weak\n"; |
| 614 | } |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 618 | //===----------------------------------------------------------------------===// |
| 619 | // Top Level Driver Code |
| 620 | //===----------------------------------------------------------------------===// |
| 621 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 622 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 623 | // Two cases: either the decl could be in the main file, or it could be in a |
| 624 | // #included file. If the former, rewrite it now. If the later, check to see |
| 625 | // if we rewrote the #include/#import. |
| 626 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 627 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 629 | // If this is for a builtin, ignore it. |
| 630 | if (Loc.isInvalid()) return; |
| 631 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 632 | // Look for built-in declarations that we need to refer during the rewrite. |
| 633 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 634 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 635 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 636 | // declared in <Foundation/NSString.h> |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 637 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 638 | ConstantStringClassReference = FVD; |
| 639 | return; |
| 640 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 641 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 642 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 643 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 644 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 645 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 646 | RewriteProtocolDecl(PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 648 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 649 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 650 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 651 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 652 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 653 | DIEnd = LSD->decls_end(); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 654 | DI != DIEnd; ++DI) |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 655 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 657 | // 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] | 658 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 659 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 662 | //===----------------------------------------------------------------------===// |
| 663 | // Syntactic (non-AST) Rewriting Code |
| 664 | //===----------------------------------------------------------------------===// |
| 665 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 666 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 667 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 668 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 669 | const char *MainBufStart = MainBuf.first; |
| 670 | const char *MainBufEnd = MainBuf.second; |
| 671 | size_t ImportLen = strlen("import"); |
| 672 | size_t IncludeLen = strlen("include"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 674 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 675 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 676 | if (*BufPtr == '#') { |
| 677 | if (++BufPtr == MainBufEnd) |
| 678 | return; |
| 679 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 680 | if (++BufPtr == MainBufEnd) |
| 681 | return; |
| 682 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 683 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | SourceLocation ImportLoc = |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 685 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 686 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 687 | BufPtr += ImportLen; |
| 688 | } |
| 689 | } |
| 690 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 693 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 694 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 695 | const char *MainBufStart = MainBuf.first; |
| 696 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 698 | // Loop over the whole file, looking for tabs. |
| 699 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 700 | if (*BufPtr != '\t') |
| 701 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 703 | // Okay, we found a tab. This tab will turn into at least one character, |
| 704 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 705 | unsigned VCol = 0; |
| 706 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 707 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 708 | ++VCol; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 710 | // Okay, now that we know the virtual column, we know how many spaces to |
| 711 | // insert. We assume 8-character tab-stops. |
| 712 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 714 | // Get the location of the tab. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 715 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 716 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 718 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 719 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 720 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 723 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 724 | ObjCIvarDecl *OID) { |
| 725 | std::string S; |
| 726 | S = "((struct "; |
| 727 | S += ClassDecl->getIdentifier()->getName(); |
| 728 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 729 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 730 | return S; |
| 731 | } |
| 732 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 733 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 734 | ObjCImplementationDecl *IMD, |
| 735 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 736 | SourceLocation startLoc = PID->getLocStart(); |
| 737 | InsertText(startLoc, "// ", 3); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 738 | const char *startBuf = SM->getCharacterData(startLoc); |
| 739 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 740 | const char *semiBuf = strchr(startBuf, ';'); |
| 741 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 742 | SourceLocation onePastSemiLoc = |
| 743 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 744 | |
| 745 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 746 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 748 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 749 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 750 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 751 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 753 | if (!OID) |
| 754 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 756 | std::string Getr; |
| 757 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 758 | Getr += "{ "; |
| 759 | // Synthesize an explicit cast to gain access to the ivar. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | // FIXME: deal with code generation implications for various property |
| 761 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 0629704 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 762 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 763 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 764 | Getr += "; }"; |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 765 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | if (PD->isReadOnly()) |
| 767 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 769 | // Generate the 'setter' function. |
| 770 | std::string Setr; |
| 771 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 772 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 773 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | // FIXME: deal with code generation implications for various property |
| 775 | // attributes (copy, retain, nonatomic). |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 776 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 777 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 778 | Setr += PD->getNameAsCString(); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 779 | Setr += "; }"; |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 780 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 782 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 783 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 784 | // Get the start location and compute the semi location. |
| 785 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 786 | const char *startBuf = SM->getCharacterData(startLoc); |
| 787 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 789 | // Translate to typedef's that forward reference structs with the same name |
| 790 | // as the class. As a convenience, we include the original declaration |
| 791 | // as a comment. |
| 792 | std::string typedefString; |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 793 | typedefString += "// @class "; |
| 794 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 795 | I != E; ++I) { |
| 796 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 797 | typedefString += ForwardDecl->getNameAsString(); |
| 798 | if (I+1 != E) |
| 799 | typedefString += ", "; |
| 800 | else |
| 801 | typedefString += ";\n"; |
| 802 | } |
| 803 | |
Chris Lattner | 9ee23b7 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 804 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 805 | I != E; ++I) { |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 806 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 807 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 808 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 809 | typedefString += "\n"; |
| 810 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 811 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 812 | typedefString += "\n"; |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 813 | typedefString += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 814 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 815 | typedefString += ";\n#endif\n"; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 818 | // Replace the @class with typedefs corresponding to the classes. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | ReplaceText(startLoc, semiPtr-startBuf+1, |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 820 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 823 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 824 | SourceLocation LocStart = Method->getLocStart(); |
| 825 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 827 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 828 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | e020fa1 | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 829 | InsertText(LocStart, "#if 0\n", 6); |
| 830 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 831 | } else { |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 832 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 837 | SourceLocation Loc = prop->getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 839 | ReplaceText(Loc, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 841 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 844 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 845 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 847 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 848 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
| 850 | for (ObjCCategoryDecl::instmeth_iterator |
| 851 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 852 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 853 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 855 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 856 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 857 | RewriteMethodDeclaration(*I); |
| 858 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 859 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 860 | ReplaceText(CatDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 863 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 864 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 866 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 868 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 869 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
| 871 | for (ObjCProtocolDecl::instmeth_iterator |
| 872 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 873 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 874 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 875 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 876 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 877 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 878 | RewriteMethodDeclaration(*I); |
| 879 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 880 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 881 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 882 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 883 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 884 | // Must comment out @optional/@required |
| 885 | const char *startBuf = SM->getCharacterData(LocStart); |
| 886 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 887 | for (const char *p = startBuf; p < endBuf; p++) { |
| 888 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 889 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 890 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 891 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 892 | CommentedOptional.c_str(), CommentedOptional.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 894 | } |
| 895 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 896 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 897 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 898 | ReplaceText(OptionalLoc, strlen("@required"), |
| 899 | CommentedRequired.c_str(), CommentedRequired.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 905 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 906 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 907 | if (LocStart.isInvalid()) |
| 908 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 909 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 910 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 914 | std::string &ResultStr) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 915 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 916 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 917 | ResultStr += "\nstatic "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 918 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 919 | ResultStr += "id"; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 920 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 921 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 922 | // needs special handling, since pointer-to-functions have special |
| 923 | // syntax (where a decaration models use). |
| 924 | QualType retType = OMD->getResultType(); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 925 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 926 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 927 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 928 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 929 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 930 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 931 | ResultStr += FPRetType->getResultType().getAsString(); |
| 932 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 933 | } |
| 934 | } else |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 935 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 936 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 938 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 939 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 941 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 942 | NameStr += "_I_"; |
| 943 | else |
| 944 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 946 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 947 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
| 949 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 950 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 951 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 952 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 953 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 955 | { |
| 956 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 957 | int len = selString.size(); |
| 958 | for (int i = 0; i < len; i++) |
| 959 | if (selString[i] == ':') |
| 960 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 961 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 962 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 963 | // Remember this name for metadata emission |
| 964 | MethodInternalNames[OMD] = NameStr; |
| 965 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 967 | // Rewrite arguments |
| 968 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 970 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 971 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 972 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 973 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 974 | if (!LangOpts.Microsoft) { |
| 975 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 976 | ResultStr += "struct "; |
| 977 | } |
| 978 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 979 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 980 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 981 | } |
| 982 | else |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 983 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 985 | ResultStr += " self, "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 986 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 987 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 989 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 990 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 991 | E = OMD->param_end(); PI != E; ++PI) { |
| 992 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 993 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 994 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 995 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 996 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 997 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 998 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 999 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1000 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1001 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1002 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 1003 | Context->PrintingPolicy); |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1004 | } else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1005 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1006 | ResultStr += Name; |
| 1007 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1008 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1009 | if (OMD->isVariadic()) |
| 1010 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1011 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1013 | if (FPRetType) { |
| 1014 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1016 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1017 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1018 | ResultStr += "("; |
| 1019 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1020 | if (i) ResultStr += ", "; |
| 1021 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1022 | ResultStr += ParamStr; |
| 1023 | } |
| 1024 | if (FT->isVariadic()) { |
| 1025 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1026 | ResultStr += "..."; |
| 1027 | } |
| 1028 | ResultStr += ")"; |
| 1029 | } else { |
| 1030 | ResultStr += "()"; |
| 1031 | } |
| 1032 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1033 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1034 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1035 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1036 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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->getLocStart(), "// ", 3); |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1040 | else |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1041 | InsertText(CID->getLocStart(), "// ", 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1043 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1044 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1045 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1046 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1047 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1048 | ObjCMethodDecl *OMD = *I; |
| 1049 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1050 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1051 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1053 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1054 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1055 | ReplaceText(LocStart, endBuf-startBuf, |
| 1056 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1057 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1059 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1060 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1061 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1062 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1063 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1064 | ObjCMethodDecl *OMD = *I; |
| 1065 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1066 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1067 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1069 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1070 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1071 | ReplaceText(LocStart, endBuf-startBuf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1073 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1074 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1075 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1077 | I != E; ++I) { |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1078 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1081 | if (IMD) |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1082 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1083 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1087 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1088 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1089 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1090 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1091 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1092 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1093 | ResultStr += "\n"; |
| 1094 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1095 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1096 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1097 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1098 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1099 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1100 | // Mark this typedef as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1101 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1102 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1103 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
| 1105 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1106 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1107 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1109 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1110 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1111 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1113 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1114 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1115 | RewriteMethodDeclaration(*I); |
| 1116 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1117 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1118 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1121 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1122 | SourceRange SrcRange) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1123 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1124 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1125 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1126 | ObjCMessageExpr *MsgExpr; |
| 1127 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1128 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1129 | ExprVec.push_back(newStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1131 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1132 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1133 | if (PRE && PropGetters[PRE]) { |
| 1134 | // This allows us to handle chain/nested property getters. |
| 1135 | Receiver = PropGetters[PRE]; |
| 1136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1138 | PDecl->getSetterName(), PDecl->getType(), |
| 1139 | PDecl->getSetterMethodDecl(), |
| 1140 | SourceLocation(), SourceLocation(), |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1141 | &ExprVec[0], 1); |
| 1142 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1144 | // Now do the actual rewrite. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1145 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | df70577 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1146 | //delete BinOp; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1147 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1148 | // to things that stay around. |
| 1149 | Context->Deallocate(MsgExpr); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1150 | return ReplacingStmt; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1153 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1154 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1155 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1156 | ObjCMessageExpr *MsgExpr; |
| 1157 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1159 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1161 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1162 | if (PRE && PropGetters[PRE]) { |
| 1163 | // This allows us to handle chain/nested property getters. |
| 1164 | Receiver = PropGetters[PRE]; |
| 1165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1167 | PDecl->getGetterName(), PDecl->getType(), |
| 1168 | PDecl->getGetterMethodDecl(), |
| 1169 | SourceLocation(), SourceLocation(), |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1170 | 0, 0); |
| 1171 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1172 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (!PropParentMap) |
| 1175 | PropParentMap = new ParentMap(CurrentBody); |
| 1176 | |
| 1177 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1178 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1179 | // We stash away the ReplacingStmt since actually doing the |
| 1180 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1181 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1182 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1183 | // to things that stay around. |
| 1184 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1185 | return PropRefExpr; // return the original... |
| 1186 | } else { |
| 1187 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1189 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1190 | // to things that stay around. |
| 1191 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1192 | return ReplacingStmt; |
| 1193 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1197 | SourceLocation OrigStart) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1198 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1199 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1200 | if (CurMethodDef) { |
Fariborz Jahanian | 12e2e86 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1201 | if (BaseExpr->getType()->isObjCObjectPointerType() && |
| 1202 | isa<DeclRefExpr>(BaseExpr)) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1203 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1204 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1205 | // lookup which class implements the instance variable. |
| 1206 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1208 | clsDeclared); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1209 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1211 | // Synthesize an explicit cast to gain access to the ivar. |
| 1212 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1213 | RecName += "_IMPL"; |
| 1214 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1215 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1216 | SourceLocation(), II); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1217 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1218 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1220 | CastExpr::CK_Unknown, |
| 1221 | IV->getBase(), |
| 1222 | castT,SourceLocation(), |
| 1223 | SourceLocation()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1224 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1225 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1226 | IV->getBase()->getLocEnd(), |
| 1227 | castExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | if (IV->isFreeIvar() && |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1229 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1230 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1231 | IV->getLocation(), |
| 1232 | D->getType()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1233 | ReplaceStmt(IV, ME); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1234 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1235 | return ME; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1236 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1238 | ReplaceStmt(IV->getBase(), PE); |
| 1239 | // Cannot delete IV->getBase(), since PE points to it. |
| 1240 | // Replace the old base with the cast. This is important when doing |
| 1241 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | IV->setBase(PE); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1243 | return IV; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1244 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1245 | } else { // we are outside a method. |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1246 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1248 | // Explicit ivar refs need to have a cast inserted. |
| 1249 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 12e2e86 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1250 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | 9146e44 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1251 | ObjCInterfaceType *iFaceDecl = |
| 1252 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1253 | // lookup which class implements the instance variable. |
| 1254 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1256 | clsDeclared); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1257 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1259 | // Synthesize an explicit cast to gain access to the ivar. |
| 1260 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1261 | RecName += "_IMPL"; |
| 1262 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1263 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1264 | SourceLocation(), II); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1265 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1266 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1268 | CastExpr::CK_Unknown, |
| 1269 | IV->getBase(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1270 | castT, SourceLocation(), |
| 1271 | SourceLocation()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1272 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1273 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 34873d2 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1274 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1275 | ReplaceStmt(IV->getBase(), PE); |
| 1276 | // Cannot delete IV->getBase(), since PE points to it. |
| 1277 | // Replace the old base with the cast. This is important when doing |
| 1278 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | IV->setBase(PE); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1280 | return IV; |
| 1281 | } |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1282 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1283 | return IV; |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1286 | /// SynthCountByEnumWithState - To print: |
| 1287 | /// ((unsigned int (*) |
| 1288 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1290 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | /// "countByEnumeratingWithState:objects:count:"), |
| 1292 | /// &enumState, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1293 | /// (id *)items, (unsigned int)16) |
| 1294 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1295 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1296 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1297 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1298 | buf += "\n\t\t"; |
| 1299 | buf += "((id)l_collection,\n\t\t"; |
| 1300 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1301 | buf += "\n\t\t"; |
| 1302 | buf += "&enumState, " |
| 1303 | "(id *)items, (unsigned int)16)"; |
| 1304 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1305 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1306 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1307 | /// statement to exit to its outer synthesized loop. |
| 1308 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1309 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1310 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1311 | return S; |
| 1312 | // replace break with goto __break_label |
| 1313 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1315 | SourceLocation startLoc = S->getLocStart(); |
| 1316 | buf = "goto __break_label_"; |
| 1317 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1318 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1324 | /// statement to continue with its inner synthesized loop. |
| 1325 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1326 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1327 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1328 | return S; |
| 1329 | // replace continue with goto __continue_label |
| 1330 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1332 | SourceLocation startLoc = S->getLocStart(); |
| 1333 | buf = "goto __continue_label_"; |
| 1334 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1335 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1340 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1341 | /// It rewrites: |
| 1342 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1344 | /// Into: |
| 1345 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1347 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1348 | /// id items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1349 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1351 | /// objects:items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1352 | /// if (limit) { |
| 1353 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1354 | /// do { |
| 1355 | /// unsigned long counter = 0; |
| 1356 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1358 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1359 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1360 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1361 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1362 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1364 | /// objects:items count:16]); |
| 1365 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1366 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1367 | /// } |
| 1368 | /// else |
| 1369 | /// elem = nil; |
| 1370 | /// } |
| 1371 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1372 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1373 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1374 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1376 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1378 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1380 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1381 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1382 | const char *elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1383 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1384 | std::string buf; |
| 1385 | buf = "\n{\n\t"; |
| 1386 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1387 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1388 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1389 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1390 | if (ElementType->isObjCQualifiedIdType() || |
| 1391 | ElementType->isObjCQualifiedInterfaceType()) |
| 1392 | // Simply use 'id' for all qualified types. |
| 1393 | elementTypeAsString = "id"; |
| 1394 | else |
| 1395 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1396 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1397 | buf += " "; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1398 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1399 | buf += elementName; |
| 1400 | buf += ";\n\t"; |
| 1401 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1402 | else { |
| 1403 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1404 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1405 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1406 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1407 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1408 | // Simply use 'id' for all qualified types. |
| 1409 | elementTypeAsString = "id"; |
| 1410 | else |
| 1411 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1414 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1415 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1416 | // id items[16]; |
| 1417 | buf += "id items[16];\n\t"; |
| 1418 | // id l_collection = (id) |
| 1419 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1420 | // Find start location of 'collection' the hard way! |
| 1421 | const char *startCollectionBuf = startBuf; |
| 1422 | startCollectionBuf += 3; // skip 'for' |
| 1423 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1424 | startCollectionBuf++; // skip '(' |
| 1425 | // find 'in' and skip it. |
| 1426 | while (*startCollectionBuf != ' ' || |
| 1427 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1428 | (*(startCollectionBuf+3) != ' ' && |
| 1429 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1430 | startCollectionBuf++; |
| 1431 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1434 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1435 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1436 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1437 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1438 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1439 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1440 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1442 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1443 | // objects:items count:16]; |
| 1444 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1446 | // ((unsigned int (*) |
| 1447 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1449 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | // "countByEnumeratingWithState:objects:count:"), |
| 1451 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1452 | // (id *)items, (unsigned int)16); |
| 1453 | buf += "unsigned long limit =\n\t\t"; |
| 1454 | SynthCountByEnumWithState(buf); |
| 1455 | buf += ";\n\t"; |
| 1456 | /// if (limit) { |
| 1457 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1458 | /// do { |
| 1459 | /// unsigned long counter = 0; |
| 1460 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1462 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1463 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1464 | buf += "if (limit) {\n\t"; |
| 1465 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1466 | buf += "do {\n\t\t"; |
| 1467 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1468 | buf += "do {\n\t\t\t"; |
| 1469 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1470 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1471 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1472 | buf += " = ("; |
| 1473 | buf += elementTypeAsString; |
| 1474 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1475 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1476 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1478 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1479 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1481 | /// objects:items count:16]); |
| 1482 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1483 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1484 | /// } |
| 1485 | /// else |
| 1486 | /// elem = nil; |
| 1487 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1489 | buf = ";\n\t"; |
| 1490 | buf += "__continue_label_"; |
| 1491 | buf += utostr(ObjCBcLabelNo.back()); |
| 1492 | buf += ": ;"; |
| 1493 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1494 | buf += "} while (counter < limit);\n\t"; |
| 1495 | buf += "} while (limit = "; |
| 1496 | SynthCountByEnumWithState(buf); |
| 1497 | buf += ");\n\t"; |
| 1498 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1499 | buf += " = (("; |
| 1500 | buf += elementTypeAsString; |
| 1501 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1502 | buf += "__break_label_"; |
| 1503 | buf += utostr(ObjCBcLabelNo.back()); |
| 1504 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1505 | buf += "}\n\t"; |
| 1506 | buf += "else\n\t\t"; |
| 1507 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1508 | buf += " = (("; |
| 1509 | buf += elementTypeAsString; |
| 1510 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1511 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1513 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1514 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1515 | if (isa<CompoundStmt>(S->getBody())) { |
| 1516 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1517 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1518 | } else { |
| 1519 | /* Need to treat single statements specially. For example: |
| 1520 | * |
| 1521 | * for (A *a in b) if (stuff()) break; |
| 1522 | * for (A *a in b) xxxyy; |
| 1523 | * |
| 1524 | * The following code simply scans ahead to the semi to find the actual end. |
| 1525 | */ |
| 1526 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1527 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1528 | assert(semiBuf && "Can't find ';'"); |
| 1529 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1530 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1531 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1532 | Stmts.pop_back(); |
| 1533 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1534 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1538 | /// This routine rewrites @synchronized(expr) stmt; |
| 1539 | /// into: |
| 1540 | /// objc_sync_enter(expr); |
| 1541 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1542 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1543 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1544 | // Get the start location and compute the semi location. |
| 1545 | SourceLocation startLoc = S->getLocStart(); |
| 1546 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1548 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
| 1550 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1551 | buf = "objc_sync_enter((id)"; |
| 1552 | const char *lparenBuf = startBuf; |
| 1553 | while (*lparenBuf != '(') lparenBuf++; |
| 1554 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1556 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1557 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1558 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1559 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1560 | while (*endBuf != ')') endBuf--; |
| 1561 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1562 | buf = ");\n"; |
| 1563 | // declare a new scope with two variables, _stack and _rethrow. |
| 1564 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1565 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1566 | buf += "char *pointers[4];} _stack;\n"; |
| 1567 | buf += "id volatile _rethrow = 0;\n"; |
| 1568 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1569 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1570 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1571 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1572 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1574 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1575 | SourceLocation lastCurlyLoc = startLoc; |
| 1576 | buf = "}\nelse {\n"; |
| 1577 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1578 | buf += "}\n"; |
| 1579 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1580 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1581 | |
| 1582 | std::string syncBuf; |
| 1583 | syncBuf += " objc_sync_exit("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1585 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | S->getSynchExpr(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1587 | Context->getObjCIdType(), |
| 1588 | SourceLocation(), |
| 1589 | SourceLocation()); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1590 | std::string syncExprBufS; |
| 1591 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1592 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1593 | PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1594 | syncBuf += syncExprBuf.str(); |
| 1595 | syncBuf += ");"; |
| 1596 | |
| 1597 | buf += syncBuf; |
| 1598 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1599 | buf += "}\n"; |
| 1600 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1602 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1603 | |
| 1604 | bool hasReturns = false; |
| 1605 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1606 | if (hasReturns) |
| 1607 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1608 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1612 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1613 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1614 | // Perform a bottom up traversal of all children. |
| 1615 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1616 | CI != E; ++CI) |
| 1617 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1618 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1619 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1620 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1622 | TryFinallyContainsReturnDiag); |
| 1623 | } |
| 1624 | return; |
| 1625 | } |
| 1626 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1627 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1628 | { |
| 1629 | // Perform a bottom up traversal of all children. |
| 1630 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1631 | CI != E; ++CI) |
| 1632 | if (*CI) |
| 1633 | HasReturnStmts(*CI, hasReturns); |
| 1634 | |
| 1635 | if (isa<ReturnStmt>(S)) |
| 1636 | hasReturns = true; |
| 1637 | return; |
| 1638 | } |
| 1639 | |
| 1640 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1641 | // Perform a bottom up traversal of all children. |
| 1642 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1643 | CI != E; ++CI) |
| 1644 | if (*CI) { |
| 1645 | RewriteTryReturnStmts(*CI); |
| 1646 | } |
| 1647 | if (isa<ReturnStmt>(S)) { |
| 1648 | SourceLocation startLoc = S->getLocStart(); |
| 1649 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1650 | |
| 1651 | const char *semiBuf = strchr(startBuf, ';'); |
| 1652 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1653 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1654 | |
| 1655 | std::string buf; |
| 1656 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1657 | |
| 1658 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1659 | InsertText(onePastSemiLoc, "}", 1); |
| 1660 | } |
| 1661 | return; |
| 1662 | } |
| 1663 | |
| 1664 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1665 | // Perform a bottom up traversal of all children. |
| 1666 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1667 | CI != E; ++CI) |
| 1668 | if (*CI) { |
| 1669 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1670 | } |
| 1671 | if (isa<ReturnStmt>(S)) { |
| 1672 | SourceLocation startLoc = S->getLocStart(); |
| 1673 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1674 | |
| 1675 | const char *semiBuf = strchr(startBuf, ';'); |
| 1676 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1677 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1678 | |
| 1679 | std::string buf; |
| 1680 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1681 | buf += syncExitBuf; |
| 1682 | buf += " return"; |
| 1683 | |
| 1684 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1685 | InsertText(onePastSemiLoc, "}", 1); |
| 1686 | } |
| 1687 | return; |
| 1688 | } |
| 1689 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1690 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1691 | // Get the start location and compute the semi location. |
| 1692 | SourceLocation startLoc = S->getLocStart(); |
| 1693 | const char *startBuf = SM->getCharacterData(startLoc); |
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 | assert((*startBuf == '@') && "bogus @try location"); |
| 1696 | |
| 1697 | std::string buf; |
| 1698 | // declare a new scope with two variables, _stack and _rethrow. |
| 1699 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1700 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1701 | buf += "char *pointers[4];} _stack;\n"; |
| 1702 | buf += "id volatile _rethrow = 0;\n"; |
| 1703 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1704 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1706 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1708 | startLoc = S->getTryBody()->getLocEnd(); |
| 1709 | startBuf = SM->getCharacterData(startLoc); |
| 1710 | |
| 1711 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1713 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1714 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1715 | if (catchList) { |
| 1716 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1717 | buf = " /* @catch begin */ else {\n"; |
| 1718 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1719 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1720 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1721 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1722 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1724 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1725 | } else { /* no catch list */ |
| 1726 | buf = "}\nelse {\n"; |
| 1727 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1728 | buf += "}"; |
| 1729 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1730 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1731 | bool sawIdTypedCatch = false; |
| 1732 | Stmt *lastCatchBody = 0; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1733 | while (catchList) { |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1734 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1735 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | if (catchList == S->getCatchStmts()) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1737 | buf = "if ("; // we are generating code for the first catch clause |
| 1738 | else |
| 1739 | buf = "else if ("; |
| 1740 | startLoc = catchList->getLocStart(); |
| 1741 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1743 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1745 | const char *lParenLoc = strchr(startBuf, '('); |
| 1746 | |
Steve Naroff | e6b7ffd | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1747 | if (catchList->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1748 | // Now rewrite the body... |
| 1749 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1750 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1751 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1752 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1753 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1754 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1756 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1757 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1758 | } else if (catchDecl) { |
| 1759 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1760 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1761 | buf += "1) { "; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1762 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1763 | sawIdTypedCatch = true; |
Fariborz Jahanian | 5951609 | 2010-01-12 01:22:23 +0000 | [diff] [blame] | 1764 | } else if (t->isObjCObjectPointerType()) { |
| 1765 | QualType InterfaceTy = t->getPointeeType(); |
| 1766 | const ObjCInterfaceType *cls = // Should be a pointer to a class. |
| 1767 | InterfaceTy->getAs<ObjCInterfaceType>(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1768 | if (cls) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1769 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1770 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1771 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1772 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | // Now rewrite the body... |
| 1776 | lastCatchBody = catchList->getCatchBody(); |
| 1777 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1778 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1779 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1780 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1781 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1782 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1784 | buf = " = _caught;"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1786 | // declares the @catch parameter). |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1787 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1788 | } else { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1789 | assert(false && "@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1790 | } |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1791 | // make sure all the catch bodies get rewritten! |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1792 | catchList = catchList->getNextCatchStmt(); |
| 1793 | } |
| 1794 | // Complete the catch list... |
| 1795 | if (lastCatchBody) { |
| 1796 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1797 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1798 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1800 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1801 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1802 | buf = "} /* last catch end */\n"; |
| 1803 | buf += "else {\n"; |
| 1804 | buf += " _rethrow = _caught;\n"; |
| 1805 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1806 | buf += "} } /* @catch end */\n"; |
| 1807 | if (!S->getFinallyStmt()) |
| 1808 | buf += "}\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1809 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1811 | // Set lastCurlyLoc |
| 1812 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1813 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1814 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1815 | startLoc = finalStmt->getLocStart(); |
| 1816 | startBuf = SM->getCharacterData(startLoc); |
| 1817 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1819 | buf = "/* @finally */"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1820 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1822 | Stmt *body = finalStmt->getFinallyBody(); |
| 1823 | SourceLocation startLoc = body->getLocStart(); |
| 1824 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1825 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1826 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1828 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1830 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1831 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1832 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1833 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1834 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1835 | InsertText(endLoc, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1837 | // Set lastCurlyLoc |
| 1838 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1840 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1841 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1842 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1843 | buf = "{ /* implicit finally clause */\n"; |
| 1844 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1845 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1846 | buf += "}"; |
| 1847 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1848 | |
| 1849 | // Now check for any return/continue/go statements within the @try. |
| 1850 | // The implicit finally clause won't called if the @try contains any |
| 1851 | // jump statements. |
| 1852 | bool hasReturns = false; |
| 1853 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1854 | if (hasReturns) |
| 1855 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1856 | } |
| 1857 | // Now emit the final closing curly brace... |
| 1858 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1859 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1860 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1864 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
| 1867 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1868 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1869 | return 0; |
| 1870 | } |
| 1871 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1873 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1874 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1875 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1876 | // Get the start location and compute the semi location. |
| 1877 | SourceLocation startLoc = S->getLocStart(); |
| 1878 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1880 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1881 | |
| 1882 | std::string buf; |
| 1883 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1884 | if (S->getThrowExpr()) |
| 1885 | buf = "objc_exception_throw("; |
| 1886 | else // add an implicit argument |
| 1887 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1889 | // handle "@ throw" correctly. |
| 1890 | const char *wBuf = strchr(startBuf, 'w'); |
| 1891 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1892 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1894 | const char *semiBuf = strchr(startBuf, ';'); |
| 1895 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1896 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1897 | buf = ");"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1898 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1901 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1902 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1903 | // Create a new string expression. |
| 1904 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1905 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1906 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1907 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1908 | StrEncoding.length(), false,StrType, |
| 1909 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1910 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1912 | // Replace this subexpr in the parent. |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1913 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1914 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1917 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1918 | if (!SelGetUidFunctionDecl) |
| 1919 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1920 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1921 | // Create a call to sel_registerName("selName"). |
| 1922 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1923 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1925 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1926 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1927 | false, argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1928 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1929 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1930 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1931 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1932 | return SelExp; |
| 1933 | } |
| 1934 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1935 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1936 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1937 | // 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] | 1938 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1940 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1941 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1943 | // 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] | 1944 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1946 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | DRE, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1948 | /*isLvalue=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1950 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1952 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1953 | SourceLocation()); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1956 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1957 | const char *&startRef, const char *&endRef) { |
| 1958 | while (startBuf < endBuf) { |
| 1959 | if (*startBuf == '<') |
| 1960 | startRef = startBuf; // mark the start. |
| 1961 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1962 | if (startRef && *startRef == '<') { |
| 1963 | endRef = startBuf; // mark the end. |
| 1964 | return true; |
| 1965 | } |
| 1966 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1967 | } |
| 1968 | startBuf++; |
| 1969 | } |
| 1970 | return false; |
| 1971 | } |
| 1972 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1973 | static void scanToNextArgument(const char *&argRef) { |
| 1974 | int angle = 0; |
| 1975 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1976 | if (*argRef == '<') |
| 1977 | angle++; |
| 1978 | else if (*argRef == '>') |
| 1979 | angle--; |
| 1980 | argRef++; |
| 1981 | } |
| 1982 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1983 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1984 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1985 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1986 | return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType(); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1989 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1990 | QualType Type = E->getType(); |
| 1991 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1992 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1994 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1995 | Loc = ECE->getLParenLoc(); |
| 1996 | EndLoc = ECE->getRParenLoc(); |
| 1997 | } else { |
| 1998 | Loc = E->getLocStart(); |
| 1999 | EndLoc = E->getLocEnd(); |
| 2000 | } |
| 2001 | // This will defend against trying to rewrite synthesized expressions. |
| 2002 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2003 | return; |
| 2004 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2005 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2006 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2007 | const char *startRef = 0, *endRef = 0; |
| 2008 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2009 | // Get the locations of the startRef, endRef. |
| 2010 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2011 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2012 | // Comment out the protocol references. |
| 2013 | InsertText(LessLoc, "/*", 2); |
| 2014 | InsertText(GreaterLoc, "*/", 2); |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2019 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2020 | SourceLocation Loc; |
| 2021 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2022 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2023 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2024 | Loc = VD->getLocation(); |
| 2025 | Type = VD->getType(); |
| 2026 | } |
| 2027 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2028 | Loc = FD->getLocation(); |
| 2029 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2030 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2031 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2032 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2033 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2034 | if (!proto) |
| 2035 | return; |
| 2036 | Type = proto->getResultType(); |
| 2037 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2038 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2039 | Loc = FD->getLocation(); |
| 2040 | Type = FD->getType(); |
| 2041 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2042 | else |
| 2043 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2045 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2046 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2048 | const char *endBuf = SM->getCharacterData(Loc); |
| 2049 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2050 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2051 | startBuf--; // scan backward (from the decl location) for return type. |
| 2052 | const char *startRef = 0, *endRef = 0; |
| 2053 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2054 | // Get the locations of the startRef, endRef. |
| 2055 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2056 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2057 | // Comment out the protocol references. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2058 | InsertText(LessLoc, "/*", 2); |
| 2059 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2060 | } |
| 2061 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2062 | if (!proto) |
| 2063 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2064 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2065 | const char *startBuf = SM->getCharacterData(Loc); |
| 2066 | const char *startFuncBuf = startBuf; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2067 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2068 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2069 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2071 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2072 | // scan forward (from the decl location) for argument types. |
| 2073 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2074 | const char *startRef = 0, *endRef = 0; |
| 2075 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2076 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | SourceLocation LessLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2078 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2080 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2081 | // Comment out the protocol references. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2082 | InsertText(LessLoc, "/*", 2); |
| 2083 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2084 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2085 | startBuf = ++endBuf; |
| 2086 | } |
| 2087 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2088 | // If the function name is derived from a macro expansion, then the |
| 2089 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2090 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2091 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2092 | startBuf++; |
| 2093 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2094 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2097 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2098 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2099 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2100 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2101 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2102 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2103 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2104 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2105 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2107 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2108 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2111 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2112 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2113 | if (FD->getIdentifier() && |
| 2114 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2115 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2116 | return; |
| 2117 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2118 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 2121 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2122 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2123 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2124 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2125 | if (!proto) |
| 2126 | return; |
| 2127 | QualType Type = proto->getResultType(); |
| 2128 | std::string FdStr = Type.getAsString(); |
| 2129 | FdStr += " "; |
| 2130 | FdStr += FD->getNameAsCString(); |
| 2131 | FdStr += "("; |
| 2132 | unsigned numArgs = proto->getNumArgs(); |
| 2133 | for (unsigned i = 0; i < numArgs; i++) { |
| 2134 | QualType ArgType = proto->getArgType(i); |
| 2135 | FdStr += ArgType.getAsString(); |
| 2136 | |
| 2137 | if (i+1 < numArgs) |
| 2138 | FdStr += ", "; |
| 2139 | } |
| 2140 | FdStr += ");\n"; |
| 2141 | InsertText(FunLocStart, FdStr.c_str(), FdStr.size()); |
| 2142 | CurFunctionDeclToDeclareForBlock = 0; |
| 2143 | } |
| 2144 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2145 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2146 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2147 | if (SuperContructorFunctionDecl) |
| 2148 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2149 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2150 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2151 | QualType argT = Context->getObjCIdType(); |
| 2152 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2153 | ArgTys.push_back(argT); |
| 2154 | ArgTys.push_back(argT); |
| 2155 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2156 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2157 | false, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2158 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2160 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2161 | FunctionDecl::Extern, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2164 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2165 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2166 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2167 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2168 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2169 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2170 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2171 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2172 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2173 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2174 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2175 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2176 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2177 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2178 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2179 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2180 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2183 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2184 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2185 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2186 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2187 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2188 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2189 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2190 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2191 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2192 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2193 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2194 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2195 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2196 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2197 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2198 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2199 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2201 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2202 | FunctionDecl::Extern, false); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2205 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2206 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2207 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2208 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2209 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2210 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2211 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2212 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2213 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2214 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2215 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2216 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2217 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2218 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2220 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2221 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2225 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2226 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2228 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2229 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2230 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2231 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2232 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2233 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2234 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2235 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2236 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2237 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2238 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2239 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2240 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2241 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2242 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2244 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2245 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2248 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2249 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2250 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2251 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2252 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2253 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2254 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2255 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2256 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2257 | ArgTys.push_back(argT); |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2258 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2259 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2260 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2261 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2263 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2264 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2267 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2268 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2269 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2270 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2271 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2272 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2273 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2274 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2275 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2277 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2278 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2281 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2282 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2283 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2284 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2285 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2286 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2287 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2288 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2289 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2291 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2292 | FunctionDecl::Extern, false); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2295 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2296 | QualType strType = getConstantStringStructType(); |
| 2297 | |
| 2298 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2299 | |
| 2300 | std::string tmpName = InFileName; |
| 2301 | unsigned i; |
| 2302 | for (i=0; i < tmpName.length(); i++) { |
| 2303 | char c = tmpName.at(i); |
| 2304 | // replace any non alphanumeric characters with '_'. |
| 2305 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2306 | tmpName[i] = '_'; |
| 2307 | } |
| 2308 | S += tmpName; |
| 2309 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2310 | S += utostr(NumObjCStringLiterals++); |
| 2311 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2312 | Preamble += "static __NSConstantStringImpl " + S; |
| 2313 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2314 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2315 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2316 | std::string prettyBufS; |
| 2317 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2318 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2319 | PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2320 | Preamble += prettyBuf.str(); |
| 2321 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2322 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | |
| 2324 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2325 | &Context->Idents.get(S.c_str()), strType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2326 | VarDecl::Static); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2327 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2328 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | Context->getPointerType(DRE->getType()), |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2330 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2331 | // cast to NSConstantString * |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2333 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | Unop, Exp->getType(), |
| 2335 | SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2336 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2337 | ReplaceStmt(Exp, cast); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2338 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2339 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2342 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2343 | // check if we are sending a message to 'super' |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2344 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2345 | |
Douglas Gregor | 8ea1f53 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2346 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2347 | const ObjCObjectPointerType *OPT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2348 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2349 | assert(OPT); |
| 2350 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | a9b3cae | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2351 | return IT->getDecl(); |
| 2352 | } |
| 2353 | return 0; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2357 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2358 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2359 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2361 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2362 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2364 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2366 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2368 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2369 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2370 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2372 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2373 | FieldTypes[i], 0, |
| 2374 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2375 | /*Mutable=*/false)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2376 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2378 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2379 | } |
| 2380 | return Context->getTagDeclType(SuperStructDecl); |
| 2381 | } |
| 2382 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2383 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2384 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2385 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2387 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2388 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2389 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2390 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2392 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2394 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2396 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2398 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2399 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2400 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2402 | ConstantStringDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2403 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2404 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2405 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2406 | /*Mutable=*/true)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2410 | } |
| 2411 | return Context->getTagDeclType(ConstantStringDecl); |
| 2412 | } |
| 2413 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2414 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2415 | if (!SelGetUidFunctionDecl) |
| 2416 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2417 | if (!MsgSendFunctionDecl) |
| 2418 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2419 | if (!MsgSendSuperFunctionDecl) |
| 2420 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2421 | if (!MsgSendStretFunctionDecl) |
| 2422 | SynthMsgSendStretFunctionDecl(); |
| 2423 | if (!MsgSendSuperStretFunctionDecl) |
| 2424 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2425 | if (!MsgSendFpretFunctionDecl) |
| 2426 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2427 | if (!GetClassFunctionDecl) |
| 2428 | SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2429 | if (!GetMetaClassFunctionDecl) |
| 2430 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2432 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2433 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2434 | // May need to use objc_msgSend_stret() as well. |
| 2435 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2436 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2437 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2438 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2439 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2440 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2441 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2444 | // Synthesize a call to objc_msgSend(). |
| 2445 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2446 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2448 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2449 | if (clsName) { // class message. |
Steve Naroff | 6c79f97 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2450 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2451 | // the 'super' idiom within a class method. |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2452 | if (clsName->getName() == "super") { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2453 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2454 | if (MsgSendStretFlavor) |
| 2455 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2456 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | |
| 2458 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2459 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2460 | |
| 2461 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2463 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2464 | InitExprs.push_back( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2466 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2468 | Context->getObjCIdType(), |
| 2469 | SourceLocation()), |
| 2470 | Context->getObjCIdType(), |
| 2471 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
| 2472 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2473 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2474 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2475 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2476 | SuperDecl->getIdentifier()->getNameStart(), |
| 2477 | SuperDecl->getIdentifier()->getLength(), |
| 2478 | false, argType, SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2479 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | &ClsExprs[0], |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2481 | ClsExprs.size()); |
| 2482 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2483 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2485 | CastExpr::CK_Unknown, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2486 | Cls, Context->getObjCIdType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | SourceLocation(), SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2488 | // struct objc_super |
| 2489 | QualType superType = getSuperStructType(); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2490 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2492 | if (LangOpts.Microsoft) { |
| 2493 | SynthSuperContructorFunctionDecl(); |
| 2494 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2496 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2497 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2499 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2500 | // The code for super is a little tricky to prevent collision with |
| 2501 | // the structure definition in the header. The rewriter has it's own |
| 2502 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2503 | // we need the cast below. For example: |
| 2504 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2505 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2506 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2508 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
| 2510 | CastExpr::CK_Unknown, SuperRep, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2511 | Context->getPointerType(superType), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | SourceLocation(), SourceLocation()); |
| 2513 | } else { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2514 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2516 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2517 | SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2518 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 07d754a | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2519 | false); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2520 | // struct objc_super * |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2521 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2523 | SourceLocation()); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2524 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2525 | MsgExprs.push_back(SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2526 | } else { |
| 2527 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2528 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2529 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2530 | clsName->getNameStart(), |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2531 | clsName->getLength(), |
| 2532 | false, argType, |
| 2533 | SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2534 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | &ClsExprs[0], |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2536 | ClsExprs.size()); |
| 2537 | MsgExprs.push_back(Cls); |
| 2538 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2539 | } else { // instance message. |
| 2540 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2541 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2542 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2543 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2544 | if (MsgSendStretFlavor) |
| 2545 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2546 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2548 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2550 | InitExprs.push_back( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2552 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 97adf60 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2554 | Context->getObjCIdType(), |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2555 | SourceLocation()), |
| 2556 | Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2557 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2559 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2560 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2562 | SuperDecl->getIdentifier()->getNameStart(), |
| 2563 | SuperDecl->getIdentifier()->getLength(), |
| 2564 | false, argType, SourceLocation())); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2565 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | &ClsExprs[0], |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2567 | ClsExprs.size()); |
Fariborz Jahanian | d5db92b | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2568 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2569 | InitExprs.push_back( |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2570 | // set 'super class', using objc_getClass(). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2572 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2574 | // struct objc_super |
| 2575 | QualType superType = getSuperStructType(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2576 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2578 | if (LangOpts.Microsoft) { |
| 2579 | SynthSuperContructorFunctionDecl(); |
| 2580 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2582 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2583 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2585 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2586 | // The code for super is a little tricky to prevent collision with |
| 2587 | // the structure definition in the header. The rewriter has it's own |
| 2588 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2589 | // we need the cast below. For example: |
| 2590 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2591 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2592 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2594 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2596 | CastExpr::CK_Unknown, |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2597 | SuperRep, Context->getPointerType(superType), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | SourceLocation(), SourceLocation()); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2599 | } else { |
| 2600 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2602 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2603 | SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2604 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2605 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2606 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2607 | } else { |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2608 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2609 | // Foo<Proto> *. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2610 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2611 | recExpr = CE->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2613 | CastExpr::CK_Unknown, recExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2615 | SourceLocation(), SourceLocation()); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2616 | MsgExprs.push_back(recExpr); |
| 2617 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2618 | } |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2619 | // 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] | 2620 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2621 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2623 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2624 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2625 | false, argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2626 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2627 | &SelExprs[0], SelExprs.size()); |
| 2628 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2630 | // Now push any user supplied arguments. |
| 2631 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2632 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2633 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2634 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2635 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2636 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2637 | ? Context->getObjCIdType() |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2638 | : ICE->getType(); |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2639 | userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | userExpr, type, SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2641 | SourceLocation()); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2642 | } |
| 2643 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2644 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2645 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2646 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2647 | userExpr = CE->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2649 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2650 | userExpr, Context->getObjCIdType(), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2651 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2652 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2654 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2655 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2656 | // out the argument in the original expression (since we aren't deleting |
| 2657 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2658 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2659 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2660 | // Generate the funky cast. |
| 2661 | CastExpr *cast; |
| 2662 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2663 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2665 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2666 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2667 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2668 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2669 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2670 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2671 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2672 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2673 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2674 | E = OMD->param_end(); PI != E; ++PI) { |
| 2675 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2677 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2678 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2679 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2680 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2681 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2682 | } |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2683 | ArgTypes.push_back(t); |
| 2684 | } |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2685 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2686 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2687 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2688 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2689 | } |
| 2690 | // 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] | 2691 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2693 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2695 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2696 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2698 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2699 | // xx.m:13: warning: function called through a non-compatible type |
| 2700 | // 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] | 2701 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2702 | CastExpr::CK_Unknown, DRE, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2703 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2704 | SourceLocation(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2706 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2708 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 327f0f4 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2709 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2710 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2711 | castType = Context->getPointerType(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast, |
| 2713 | castType, SourceLocation(), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2714 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2717 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2719 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2720 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2722 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2723 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2724 | if (MsgSendStretFlavor) { |
| 2725 | // We have the method which returns a struct/union. Must also generate |
| 2726 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2727 | // expression which dictate which one to envoke depending on size of |
| 2728 | // method's return type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2730 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2732 | SourceLocation()); |
| 2733 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2735 | CastExpr::CK_Unknown, STDRE, |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2736 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2737 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2738 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2740 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2741 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2742 | castType = Context->getPointerType(castType); |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2743 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, |
| 2744 | cast, castType, SourceLocation(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2746 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2747 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2749 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2750 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2751 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2752 | FT->getResultType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2754 | // Build sizeof(returnType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2756 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2757 | Context->getSizeType(), |
| 2758 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2759 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2760 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2761 | // For X86 it is more complicated and some kind of target specific routine |
| 2762 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2764 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2766 | Context->IntTy, |
| 2767 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2769 | BinaryOperator::LE, |
| 2770 | Context->IntTy, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2771 | SourceLocation()); |
| 2772 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2774 | new (Context) ConditionalOperator(lessThanExpr, |
| 2775 | SourceLocation(), CE, |
| 2776 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2777 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2778 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2780 | return ReplacingStmt; |
| 2781 | } |
| 2782 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2783 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2784 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2786 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2787 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | |
| 2789 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2790 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2793 | // typedef struct objc_object Protocol; |
| 2794 | QualType RewriteObjC::getProtocolType() { |
| 2795 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2796 | TypeSourceInfo *TInfo |
| 2797 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2798 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2800 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2801 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2802 | } |
| 2803 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2804 | } |
| 2805 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2806 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2807 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2808 | /// The forward references (and metadata) are generated in |
| 2809 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2810 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2811 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2812 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 2814 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2815 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2816 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2817 | Context->getPointerType(DRE->getType()), |
| 2818 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(), |
| 2820 | CastExpr::CK_Unknown, |
| 2821 | DerefExpr, DerefExpr->getType(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2822 | SourceLocation(), SourceLocation()); |
| 2823 | ReplaceStmt(Exp, castExpr); |
| 2824 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2826 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2831 | const char *endBuf) { |
| 2832 | while (startBuf < endBuf) { |
| 2833 | if (*startBuf == '#') { |
| 2834 | // Skip whitespace. |
| 2835 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2836 | ; |
| 2837 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2838 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2839 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2840 | !strncmp(startBuf, "define", strlen("define")) || |
| 2841 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2842 | !strncmp(startBuf, "else", strlen("else")) || |
| 2843 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2844 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2845 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2846 | !strncmp(startBuf, "include", strlen("include")) || |
| 2847 | !strncmp(startBuf, "import", strlen("import")) || |
| 2848 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2849 | return true; |
| 2850 | } |
| 2851 | startBuf++; |
| 2852 | } |
| 2853 | return false; |
| 2854 | } |
| 2855 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2856 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2857 | /// an objective-c class with ivars. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2858 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2859 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2860 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2862 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2863 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2864 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2865 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2866 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2867 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2868 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2869 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2871 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2872 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2874 | // If no ivars and no root or if its root, directly or indirectly, |
| 2875 | // 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] | 2876 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2877 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2878 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2879 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2880 | return; |
| 2881 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
| 2883 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2884 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2885 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2886 | Result += CDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2887 | if (LangOpts.Microsoft) |
| 2888 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2889 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2890 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2891 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2893 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2894 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2895 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2896 | // NSURL.h, for example): |
| 2897 | // |
| 2898 | // #ifdef XYZ |
| 2899 | // @interface Foo : NSObject |
| 2900 | // #else |
| 2901 | // @interface FooBar : NSObject |
| 2902 | // #endif |
| 2903 | // { |
| 2904 | // int i; |
| 2905 | // } |
| 2906 | // @end |
| 2907 | // |
| 2908 | // This clause is segregated to avoid breaking the common case. |
| 2909 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2911 | CDecl->getClassLoc(); |
| 2912 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2913 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2914 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2915 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2916 | // advance to the end of the referenced protocols. |
| 2917 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2918 | endHeader++; |
| 2919 | } |
| 2920 | // rewrite the original header |
| 2921 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2922 | } else { |
| 2923 | // rewrite the original header *without* disturbing the '{' |
Steve Naroff | b0e3390 | 2009-12-04 21:36:32 +0000 | [diff] [blame] | 2924 | ReplaceText(LocStart, cursor-startBuf, Result.c_str(), Result.size()); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2925 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2926 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2927 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2928 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2929 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2930 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2931 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2933 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2934 | SourceLocation OnePastCurly = |
| 2935 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2936 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2937 | } |
| 2938 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2940 | // Now comment out any visibility specifiers. |
| 2941 | while (cursor < endBuf) { |
| 2942 | if (*cursor == '@') { |
| 2943 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2944 | // Skip whitespace. |
| 2945 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2946 | /*scan*/; |
| 2947 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2948 | // FIXME: presence of @public, etc. inside comment results in |
| 2949 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2950 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2951 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2952 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2953 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2954 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2955 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2956 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2957 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2958 | // for type checking. |
| 2959 | else if (*cursor == '<') { |
| 2960 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2961 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2962 | cursor = strchr(cursor, '>'); |
| 2963 | cursor++; |
| 2964 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2965 | InsertText(atLoc, " */", 3); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2966 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2967 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2968 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2969 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2970 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2971 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2972 | // Don't forget to add a ';'!! |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2973 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2974 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2975 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2976 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2977 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2978 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2979 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2980 | Result += "_IVARS;\n};\n"; |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2981 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2982 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2983 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2984 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2985 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2988 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2989 | /// class methods. |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2990 | template<typename MethodIterator> |
| 2991 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2992 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2993 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2994 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2995 | const char *ClassName, |
| 2996 | std::string &Result) { |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2997 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2999 | if (!objc_impl_method) { |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3000 | /* struct _objc_method { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3001 | SEL _cmd; |
| 3002 | char *method_types; |
| 3003 | void *_imp; |
| 3004 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3005 | */ |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3006 | Result += "\nstruct _objc_method {\n"; |
| 3007 | Result += "\tSEL _cmd;\n"; |
| 3008 | Result += "\tchar *method_types;\n"; |
| 3009 | Result += "\tvoid *_imp;\n"; |
| 3010 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3012 | objc_impl_method = true; |
Fariborz Jahanian | 74a1cfa | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3013 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3015 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3017 | /* struct { |
| 3018 | struct _objc_method_list *next_method; |
| 3019 | int method_count; |
| 3020 | struct _objc_method method_list[]; |
| 3021 | } |
| 3022 | */ |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3023 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3024 | Result += "\nstatic struct {\n"; |
| 3025 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3026 | Result += "\tint method_count;\n"; |
| 3027 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3028 | Result += utostr(NumMethods); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3029 | Result += "];\n} _OBJC_"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3030 | Result += prefix; |
| 3031 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3032 | Result += "_METHODS_"; |
| 3033 | Result += ClassName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3034 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3035 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3036 | Result += "_meth\")))= "; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3037 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3038 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3039 | Result += "\t,{{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3040 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3041 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3042 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3043 | Result += "\", \""; |
| 3044 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3045 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3046 | Result += MethodInternalNames[*MethodBegin]; |
| 3047 | Result += "}\n"; |
| 3048 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3049 | Result += "\t ,{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3050 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3051 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3052 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3053 | Result += "\", \""; |
| 3054 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3055 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3056 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3057 | Result += "}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3058 | } |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3059 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3062 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3063 | void RewriteObjC:: |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3064 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3065 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3066 | static bool objc_protocol_methods = false; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3067 | |
| 3068 | // Output struct protocol_methods holder of method selector and type. |
| 3069 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3070 | /* struct protocol_methods { |
| 3071 | SEL _cmd; |
| 3072 | char *method_types; |
| 3073 | } |
| 3074 | */ |
| 3075 | Result += "\nstruct _protocol_methods {\n"; |
| 3076 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3077 | Result += "\tchar *method_types;\n"; |
| 3078 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3079 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3080 | objc_protocol_methods = true; |
| 3081 | } |
| 3082 | // Do not synthesize the protocol more than once. |
| 3083 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3084 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3086 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3087 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3088 | PDecl->instmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3089 | /* struct _objc_protocol_method_list { |
| 3090 | int protocol_method_count; |
| 3091 | struct protocol_methods protocols[]; |
| 3092 | } |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3093 | */ |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3094 | Result += "\nstatic struct {\n"; |
| 3095 | Result += "\tint protocol_method_count;\n"; |
| 3096 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3097 | Result += utostr(NumMethods); |
| 3098 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3099 | Result += PDecl->getNameAsString(); |
| 3100 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3101 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3102 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3103 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | for (ObjCProtocolDecl::instmeth_iterator |
| 3105 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3106 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3107 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3108 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3109 | else |
| 3110 | Result += "\t ,{(struct objc_selector *)\""; |
| 3111 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3112 | std::string MethodTypeString; |
| 3113 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3114 | Result += "\", \""; |
| 3115 | Result += MethodTypeString; |
| 3116 | Result += "\"}\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3117 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3118 | Result += "\t }\n};\n"; |
| 3119 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3121 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3122 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3123 | PDecl->classmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3124 | if (NumMethods > 0) { |
| 3125 | /* struct _objc_protocol_method_list { |
| 3126 | int protocol_method_count; |
| 3127 | struct protocol_methods protocols[]; |
| 3128 | } |
| 3129 | */ |
| 3130 | Result += "\nstatic struct {\n"; |
| 3131 | Result += "\tint protocol_method_count;\n"; |
| 3132 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3133 | Result += utostr(NumMethods); |
| 3134 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3135 | Result += PDecl->getNameAsString(); |
| 3136 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3137 | "{\n\t"; |
| 3138 | Result += utostr(NumMethods); |
| 3139 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3141 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3143 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3144 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3145 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3146 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3147 | else |
| 3148 | Result += "\t ,{(struct objc_selector *)\""; |
| 3149 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3150 | std::string MethodTypeString; |
| 3151 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3152 | Result += "\", \""; |
| 3153 | Result += MethodTypeString; |
| 3154 | Result += "\"}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3155 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3156 | Result += "\t }\n};\n"; |
| 3157 | } |
| 3158 | |
| 3159 | // Output: |
| 3160 | /* struct _objc_protocol { |
| 3161 | // Objective-C 1.0 extensions |
| 3162 | struct _objc_protocol_extension *isa; |
| 3163 | char *protocol_name; |
| 3164 | struct _objc_protocol **protocol_list; |
| 3165 | struct _objc_protocol_method_list *instance_methods; |
| 3166 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | }; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3168 | */ |
| 3169 | static bool objc_protocol = false; |
| 3170 | if (!objc_protocol) { |
| 3171 | Result += "\nstruct _objc_protocol {\n"; |
| 3172 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3173 | Result += "\tchar *protocol_name;\n"; |
| 3174 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3175 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3176 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3177 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3179 | objc_protocol = true; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3180 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3182 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3183 | Result += PDecl->getNameAsString(); |
| 3184 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3185 | "{\n\t0, \""; |
| 3186 | Result += PDecl->getNameAsString(); |
| 3187 | Result += "\", 0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3188 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3189 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3190 | Result += PDecl->getNameAsString(); |
| 3191 | Result += ", "; |
| 3192 | } |
| 3193 | else |
| 3194 | Result += "0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3195 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3196 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3197 | Result += PDecl->getNameAsString(); |
| 3198 | Result += "\n"; |
| 3199 | } |
| 3200 | else |
| 3201 | Result += "0\n"; |
| 3202 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3204 | // Mark this protocol as having been generated. |
| 3205 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3206 | assert(false && "protocol already synthesized"); |
| 3207 | |
| 3208 | } |
| 3209 | |
| 3210 | void RewriteObjC:: |
| 3211 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3212 | const char *prefix, const char *ClassName, |
| 3213 | std::string &Result) { |
| 3214 | if (Protocols.empty()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3216 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3217 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3218 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3219 | // Output the top lovel protocol meta-data for the class. |
| 3220 | /* struct _objc_protocol_list { |
| 3221 | struct _objc_protocol_list *next; |
| 3222 | int protocol_count; |
| 3223 | struct _objc_protocol *class_protocols[]; |
| 3224 | } |
| 3225 | */ |
| 3226 | Result += "\nstatic struct {\n"; |
| 3227 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3228 | Result += "\tint protocol_count;\n"; |
| 3229 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3230 | Result += utostr(Protocols.size()); |
| 3231 | Result += "];\n} _OBJC_"; |
| 3232 | Result += prefix; |
| 3233 | Result += "_PROTOCOLS_"; |
| 3234 | Result += ClassName; |
| 3235 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3236 | "{\n\t0, "; |
| 3237 | Result += utostr(Protocols.size()); |
| 3238 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3240 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3241 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3242 | Result += " \n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3244 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3245 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3246 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3247 | Result += "\n"; |
| 3248 | } |
| 3249 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3252 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3254 | /// implementation. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3255 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3256 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3257 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3258 | // Find category declaration for this implementation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3259 | ObjCCategoryDecl *CDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3261 | CDecl = CDecl->getNextClassCategory()) |
| 3262 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3263 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3265 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3266 | FullCategoryName += '_'; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3267 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3269 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3271 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3272 | |
| 3273 | // If any of our property implementations have associated getters or |
| 3274 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3275 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3276 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3277 | Prop != PropEnd; ++Prop) { |
| 3278 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3279 | continue; |
| 3280 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3281 | continue; |
| 3282 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3283 | if (!PD) |
| 3284 | continue; |
| 3285 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3286 | InstanceMethods.push_back(Getter); |
| 3287 | if (PD->isReadOnly()) |
| 3288 | continue; |
| 3289 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3290 | InstanceMethods.push_back(Setter); |
| 3291 | } |
| 3292 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3293 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3294 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3296 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3297 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3298 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3299 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3301 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 989e039 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3302 | // Null CDecl is case of a category implementation with no category interface |
| 3303 | if (CDecl) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3304 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3305 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3306 | /* struct _objc_category { |
| 3307 | char *category_name; |
| 3308 | char *class_name; |
| 3309 | struct _objc_method_list *instance_methods; |
| 3310 | struct _objc_method_list *class_methods; |
| 3311 | struct _objc_protocol_list *protocols; |
| 3312 | // Objective-C 1.0 extensions |
| 3313 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3315 | // @property decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3317 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3319 | static bool objc_category = false; |
| 3320 | if (!objc_category) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3321 | Result += "\nstruct _objc_category {\n"; |
| 3322 | Result += "\tchar *category_name;\n"; |
| 3323 | Result += "\tchar *class_name;\n"; |
| 3324 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3325 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3326 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3328 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3329 | Result += "};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3330 | objc_category = true; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3331 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3332 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3333 | Result += FullCategoryName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3334 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3335 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3336 | Result += "\"\n\t, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3337 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3338 | Result += "\"\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3340 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3341 | Result += "\t, (struct _objc_method_list *)" |
| 3342 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3343 | Result += FullCategoryName; |
| 3344 | Result += "\n"; |
| 3345 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3346 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3347 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3348 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3349 | Result += "\t, (struct _objc_method_list *)" |
| 3350 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3351 | Result += FullCategoryName; |
| 3352 | Result += "\n"; |
| 3353 | } |
| 3354 | else |
| 3355 | Result += "\t, 0\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3357 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3359 | Result += FullCategoryName; |
| 3360 | Result += "\n"; |
| 3361 | } |
| 3362 | else |
| 3363 | Result += "\t, 0\n"; |
| 3364 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3367 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3368 | /// ivar offset. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3370 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3371 | std::string &Result) { |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3372 | if (ivar->isBitField()) { |
| 3373 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3374 | // place all bitfields at offset 0. |
| 3375 | Result += "0"; |
| 3376 | } else { |
| 3377 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3378 | Result += IDecl->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3379 | if (LangOpts.Microsoft) |
| 3380 | Result += "_IMPL"; |
| 3381 | Result += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3382 | Result += ivar->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3383 | Result += ")"; |
| 3384 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3387 | //===----------------------------------------------------------------------===// |
| 3388 | // Meta Data Emission |
| 3389 | //===----------------------------------------------------------------------===// |
| 3390 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3391 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3392 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3393 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3395 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3396 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3398 | // produce correct synthesis as yet. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3399 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3402 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3403 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | ? IDecl->ivar_size() |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3405 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3406 | if (NumIvars > 0) { |
| 3407 | static bool objc_ivar = false; |
| 3408 | if (!objc_ivar) { |
| 3409 | /* struct _objc_ivar { |
| 3410 | char *ivar_name; |
| 3411 | char *ivar_type; |
| 3412 | int ivar_offset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3414 | */ |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3415 | Result += "\nstruct _objc_ivar {\n"; |
| 3416 | Result += "\tchar *ivar_name;\n"; |
| 3417 | Result += "\tchar *ivar_type;\n"; |
| 3418 | Result += "\tint ivar_offset;\n"; |
| 3419 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3421 | objc_ivar = true; |
| 3422 | } |
| 3423 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3424 | /* struct { |
| 3425 | int ivar_count; |
| 3426 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | }; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3428 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | Result += "\nstatic struct {\n"; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3430 | Result += "\tint ivar_count;\n"; |
| 3431 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3432 | Result += utostr(NumIvars); |
| 3433 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3434 | Result += IDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3435 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3436 | "{\n\t"; |
| 3437 | Result += utostr(NumIvars); |
| 3438 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3440 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3441 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3442 | if (!IDecl->ivar_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | for (ObjCImplementationDecl::ivar_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3444 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3445 | IV != IVEnd; ++IV) |
| 3446 | IVars.push_back(*IV); |
| 3447 | IVI = IVars.begin(); |
| 3448 | IVE = IVars.end(); |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3449 | } else { |
| 3450 | IVI = CDecl->ivar_begin(); |
| 3451 | IVE = CDecl->ivar_end(); |
| 3452 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3453 | Result += "\t,{{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3454 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3455 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3456 | std::string TmpString, StrEncoding; |
| 3457 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3458 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3459 | Result += StrEncoding; |
| 3460 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3461 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3462 | Result += "}\n"; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3463 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3464 | Result += "\t ,{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3465 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3466 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3467 | std::string TmpString, StrEncoding; |
| 3468 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3469 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3470 | Result += StrEncoding; |
| 3471 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3472 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3473 | Result += "}\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3474 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3476 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3477 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3479 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3481 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3482 | |
| 3483 | // If any of our property implementations have associated getters or |
| 3484 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3485 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3486 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3487 | Prop != PropEnd; ++Prop) { |
| 3488 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3489 | continue; |
| 3490 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3491 | continue; |
| 3492 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3493 | if (!PD) |
| 3494 | continue; |
| 3495 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3496 | InstanceMethods.push_back(Getter); |
| 3497 | if (PD->isReadOnly()) |
| 3498 | continue; |
| 3499 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3500 | InstanceMethods.push_back(Setter); |
| 3501 | } |
| 3502 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3503 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3505 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3506 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3507 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3509 | // Protocols referenced in class declaration? |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3510 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3511 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3513 | // Declaration of class/meta-class metadata |
| 3514 | /* struct _objc_class { |
| 3515 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3516 | const char *super_class_name; |
| 3517 | char *name; |
| 3518 | long version; |
| 3519 | long info; |
| 3520 | long instance_size; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3521 | struct _objc_ivar_list *ivars; |
| 3522 | struct _objc_method_list *methods; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3523 | struct objc_cache *cache; |
| 3524 | struct objc_protocol_list *protocols; |
| 3525 | const char *ivar_layout; |
| 3526 | struct _objc_class_ext *ext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | }; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3528 | */ |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3529 | static bool objc_class = false; |
| 3530 | if (!objc_class) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3531 | Result += "\nstruct _objc_class {\n"; |
| 3532 | Result += "\tstruct _objc_class *isa;\n"; |
| 3533 | Result += "\tconst char *super_class_name;\n"; |
| 3534 | Result += "\tchar *name;\n"; |
| 3535 | Result += "\tlong version;\n"; |
| 3536 | Result += "\tlong info;\n"; |
| 3537 | Result += "\tlong instance_size;\n"; |
| 3538 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3539 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3540 | Result += "\tstruct objc_cache *cache;\n"; |
| 3541 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3542 | Result += "\tconst char *ivar_layout;\n"; |
| 3543 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3544 | Result += "};\n"; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3545 | objc_class = true; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3548 | // Meta-class metadata generation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3549 | ObjCInterfaceDecl *RootClass = 0; |
| 3550 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3551 | while (SuperClass) { |
| 3552 | RootClass = SuperClass; |
| 3553 | SuperClass = SuperClass->getSuperClass(); |
| 3554 | } |
| 3555 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3557 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3558 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3559 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3560 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3561 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3562 | Result += "\""; |
| 3563 | |
| 3564 | if (SuperClass) { |
| 3565 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3566 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3567 | Result += "\", \""; |
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 += "\""; |
| 3570 | } |
| 3571 | else { |
| 3572 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3573 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3574 | Result += "\""; |
| 3575 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3576 | // 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] | 3577 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3578 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3579 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3580 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3581 | Result += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | Result += "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3583 | } |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3584 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3585 | Result += ", 0\n"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3586 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3587 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3588 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3589 | Result += ",0,0\n"; |
| 3590 | } |
Fariborz Jahanian | 486f718 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3591 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3592 | Result += "\t,0,0,0,0\n"; |
| 3593 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3595 | // class metadata generation. |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3596 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3597 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3598 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3599 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3600 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3601 | if (SuperClass) { |
| 3602 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3603 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3604 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3605 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3606 | Result += "\""; |
| 3607 | } |
| 3608 | else { |
| 3609 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3610 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3611 | Result += "\""; |
| 3612 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3613 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3614 | Result += ", 0,1"; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3615 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3616 | Result += ",0"; |
| 3617 | else { |
| 3618 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3619 | Result += ",sizeof(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3620 | Result += CDecl->getNameAsString(); |
Steve Naroff | 14a0746 | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3621 | if (LangOpts.Microsoft) |
| 3622 | Result += "_IMPL"; |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3623 | Result += ")"; |
| 3624 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3625 | if (NumIvars > 0) { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3626 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3627 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3628 | Result += "\n\t"; |
| 3629 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3630 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3631 | Result += ",0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3632 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3633 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3634 | Result += CDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | Result += ", 0\n\t"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3636 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3637 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3638 | Result += ",0,0"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3639 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3640 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3641 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3642 | Result += ", 0,0\n"; |
| 3643 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3644 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3645 | Result += ",0,0,0\n"; |
| 3646 | Result += "};\n"; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3647 | } |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3648 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3649 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3650 | /// and emits meta-data. |
| 3651 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3652 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3653 | int ClsDefCount = ClassImplementation.size(); |
| 3654 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3656 | // Rewrite implemented methods |
| 3657 | for (int i = 0; i < ClsDefCount; i++) |
| 3658 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3660 | for (int i = 0; i < CatDefCount; i++) |
| 3661 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3664 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3665 | int ClsDefCount = ClassImplementation.size(); |
| 3666 | int CatDefCount = CategoryImplementation.size(); |
| 3667 | |
Steve Naroff | 30ac222 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3668 | // This is needed for determining instance variable offsets. |
Fariborz Jahanian | 9ab6349 | 2010-01-07 18:31:42 +0000 | [diff] [blame] | 3669 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3670 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3671 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3672 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3674 | // For each implemented category, write out all its meta data. |
| 3675 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3676 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3677 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3678 | // Write objc_symtab metadata |
| 3679 | /* |
| 3680 | struct _objc_symtab |
| 3681 | { |
| 3682 | long sel_ref_cnt; |
| 3683 | SEL *refs; |
| 3684 | short cls_def_cnt; |
| 3685 | short cat_def_cnt; |
| 3686 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | }; |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3688 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3690 | Result += "\nstruct _objc_symtab {\n"; |
| 3691 | Result += "\tlong sel_ref_cnt;\n"; |
| 3692 | Result += "\tSEL *refs;\n"; |
| 3693 | Result += "\tshort cls_def_cnt;\n"; |
| 3694 | Result += "\tshort cat_def_cnt;\n"; |
| 3695 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3696 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3698 | Result += "static struct _objc_symtab " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3699 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3701 | + ", " + utostr(CatDefCount) + "\n"; |
| 3702 | for (int i = 0; i < ClsDefCount; i++) { |
| 3703 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3704 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3705 | Result += "\n"; |
| 3706 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3708 | for (int i = 0; i < CatDefCount; i++) { |
| 3709 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3710 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3711 | Result += "_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3712 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3713 | Result += "\n"; |
| 3714 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3716 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3718 | // Write objc_module metadata |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3720 | /* |
| 3721 | struct _objc_module { |
| 3722 | long version; |
| 3723 | long size; |
| 3724 | const char *name; |
| 3725 | struct _objc_symtab *symtab; |
| 3726 | } |
| 3727 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3729 | Result += "\nstruct _objc_module {\n"; |
| 3730 | Result += "\tlong version;\n"; |
| 3731 | Result += "\tlong size;\n"; |
| 3732 | Result += "\tconst char *name;\n"; |
| 3733 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3734 | Result += "};\n\n"; |
| 3735 | Result += "static struct _objc_module " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3736 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3738 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3739 | Result += "};\n\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (LangOpts.Microsoft) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3742 | if (ProtocolExprDecls.size()) { |
| 3743 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3744 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3746 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3747 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3748 | Result += (*I)->getNameAsString(); |
| 3749 | Result += " = &_OBJC_PROTOCOL_"; |
| 3750 | Result += (*I)->getNameAsString(); |
| 3751 | Result += ";\n"; |
| 3752 | } |
| 3753 | Result += "#pragma data_seg(pop)\n\n"; |
| 3754 | } |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3755 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | cab93d5 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3756 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3757 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3758 | Result += "&_OBJC_MODULES;\n"; |
| 3759 | Result += "#pragma data_seg(pop)\n\n"; |
| 3760 | } |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3761 | } |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3762 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3763 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3764 | const char *funcName, |
| 3765 | std::string Tag) { |
| 3766 | const FunctionType *AFT = CE->getFunctionType(); |
| 3767 | QualType RT = AFT->getResultType(); |
| 3768 | std::string StructRef = "struct " + Tag; |
| 3769 | std::string S = "static " + RT.getAsString() + " __" + |
| 3770 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3771 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3772 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3774 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | // 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] | 3776 | // block (to reference imported block decl refs). |
| 3777 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3778 | } else if (BD->param_empty()) { |
| 3779 | S += "(" + StructRef + " *__cself)"; |
| 3780 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3781 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3782 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3783 | S += '('; |
| 3784 | // first add the implicit argument. |
| 3785 | S += StructRef + " *__cself, "; |
| 3786 | std::string ParamStr; |
| 3787 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3788 | E = BD->param_end(); AI != E; ++AI) { |
| 3789 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3790 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3791 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3792 | S += ParamStr; |
| 3793 | } |
| 3794 | if (FT->isVariadic()) { |
| 3795 | if (!BD->param_empty()) S += ", "; |
| 3796 | S += "..."; |
| 3797 | } |
| 3798 | S += ')'; |
| 3799 | } |
| 3800 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3802 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3803 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3805 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3806 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3807 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3808 | std::string TypeString = "struct __Block_byref_" + Name + " *"; |
| 3809 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3810 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3812 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3814 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3815 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3816 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3817 | // Handle nested closure invocation. For example: |
| 3818 | // |
| 3819 | // void (^myImportedClosure)(void); |
| 3820 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3822 | // void (^anotherClosure)(void); |
| 3823 | // anotherClosure = ^(void) { |
| 3824 | // myImportedClosure(); // import and invoke the closure |
| 3825 | // }; |
| 3826 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3827 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3828 | S += "struct __block_impl *"; |
| 3829 | else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3830 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3831 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3832 | } |
| 3833 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3834 | const char *cstr = RewrittenStr.c_str(); |
| 3835 | while (*cstr++ != '{') ; |
| 3836 | S += cstr; |
| 3837 | S += "\n"; |
| 3838 | return S; |
| 3839 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3840 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3841 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3842 | const char *funcName, |
| 3843 | std::string Tag) { |
| 3844 | std::string StructRef = "struct " + Tag; |
| 3845 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3847 | S += funcName; |
| 3848 | S += "_block_copy_" + utostr(i); |
| 3849 | S += "(" + StructRef; |
| 3850 | S += "*dst, " + StructRef; |
| 3851 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3854 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3855 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3856 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3857 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3858 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3859 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3860 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3861 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3862 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3863 | S += "}\n"; |
| 3864 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | S += "\nstatic void __"; |
| 3866 | S += funcName; |
| 3867 | S += "_block_dispose_" + utostr(i); |
| 3868 | S += "(" + StructRef; |
| 3869 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3871 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3872 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3873 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3874 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3875 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3876 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3877 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3878 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3880 | return S; |
| 3881 | } |
| 3882 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3883 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3884 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3885 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3886 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3888 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3889 | S += " struct " + Desc; |
| 3890 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3892 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3893 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3894 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3896 | if (BlockDeclRefs.size()) { |
| 3897 | // Output all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3899 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3900 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3901 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3902 | std::string ArgName = "_" + FieldName; |
| 3903 | // Handle nested closure invocation. For example: |
| 3904 | // |
| 3905 | // void (^myImportedBlock)(void); |
| 3906 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3908 | // void (^anotherBlock)(void); |
| 3909 | // anotherBlock = ^(void) { |
| 3910 | // myImportedBlock(); // import and invoke the closure |
| 3911 | // }; |
| 3912 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3913 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3914 | S += "struct __block_impl *"; |
| 3915 | Constructor += ", void *" + ArgName; |
| 3916 | } else { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3917 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3918 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3919 | Constructor += ", " + ArgName; |
| 3920 | } |
| 3921 | S += FieldName + ";\n"; |
| 3922 | } |
| 3923 | // Output all "by ref" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3925 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3926 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3927 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3928 | std::string ArgName = "_" + FieldName; |
| 3929 | // Handle nested closure invocation. For example: |
| 3930 | // |
| 3931 | // void (^myImportedBlock)(void); |
| 3932 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3934 | // void (^anotherBlock)(void); |
| 3935 | // anotherBlock = ^(void) { |
| 3936 | // myImportedBlock(); // import and invoke the closure |
| 3937 | // }; |
| 3938 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3939 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3940 | S += "struct __block_impl *"; |
| 3941 | Constructor += ", void *" + ArgName; |
| 3942 | } else { |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3943 | std::string TypeString = "struct __Block_byref_" + FieldName; |
| 3944 | TypeString += " *"; |
| 3945 | FieldName = TypeString + FieldName; |
| 3946 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3947 | Constructor += ", " + ArgName; |
| 3948 | } |
| 3949 | S += FieldName + "; // by ref\n"; |
| 3950 | } |
| 3951 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3952 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3953 | if (GlobalVarDecl) |
| 3954 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3955 | else |
| 3956 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3957 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3959 | Constructor += " Desc = desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3961 | // Initialize all "by copy" arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3963 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3964 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3965 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3966 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3967 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3968 | else |
| 3969 | Constructor += Name + " = _"; |
| 3970 | Constructor += Name + ";\n"; |
| 3971 | } |
| 3972 | // Initialize all "by ref" arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3974 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3975 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3976 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3977 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3978 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3979 | else |
| 3980 | Constructor += Name + " = _"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3981 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } else { |
| 3984 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3985 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3986 | if (GlobalVarDecl) |
| 3987 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3988 | else |
| 3989 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3990 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3991 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3992 | } |
| 3993 | Constructor += " "; |
| 3994 | Constructor += "}\n"; |
| 3995 | S += Constructor; |
| 3996 | S += "};\n"; |
| 3997 | return S; |
| 3998 | } |
| 3999 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4000 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4001 | std::string ImplTag, int i, |
| 4002 | const char *FunName, |
| 4003 | unsigned hasCopy) { |
| 4004 | std::string S = "\nstatic struct " + DescTag; |
| 4005 | |
| 4006 | S += " {\n unsigned long reserved;\n"; |
| 4007 | S += " unsigned long Block_size;\n"; |
| 4008 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4009 | S += " void (*copy)(struct "; |
| 4010 | S += ImplTag; S += "*, struct "; |
| 4011 | S += ImplTag; S += "*);\n"; |
| 4012 | |
| 4013 | S += " void (*dispose)(struct "; |
| 4014 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4015 | } |
| 4016 | S += "} "; |
| 4017 | |
| 4018 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4019 | S += ImplTag + ")"; |
| 4020 | if (hasCopy) { |
| 4021 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 4022 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 4023 | } |
| 4024 | S += "};\n"; |
| 4025 | return S; |
| 4026 | } |
| 4027 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4028 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 4029 | const char *FunName) { |
| 4030 | // Insert declaration for the function in which block literal is used. |
| 4031 | if (CurFunctionDeclToDeclareForBlock) |
| 4032 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4033 | // Insert closures that were part of the function. |
| 4034 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 4035 | |
| 4036 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4037 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4038 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 4039 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4041 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4042 | |
| 4043 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 4044 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4045 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 4048 | |
| 4049 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4050 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4051 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4052 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4053 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4054 | ImportedBlockDecls.size() > 0); |
| 4055 | InsertText(FunLocStart, BD.c_str(), BD.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4057 | BlockDeclRefs.clear(); |
| 4058 | BlockByRefDecls.clear(); |
| 4059 | BlockByCopyDecls.clear(); |
| 4060 | BlockCallExprs.clear(); |
| 4061 | ImportedBlockDecls.clear(); |
| 4062 | } |
| 4063 | Blocks.clear(); |
| 4064 | RewrittenBlockExprs.clear(); |
| 4065 | } |
| 4066 | |
| 4067 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4068 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4069 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4071 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4072 | } |
| 4073 | |
| 4074 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4075 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4076 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 4077 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 4078 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4079 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4080 | // Convert colons to underscores. |
| 4081 | std::string::size_type loc = 0; |
| 4082 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4083 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4085 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4086 | } |
| 4087 | |
| 4088 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4089 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4090 | CI != E; ++CI) |
| 4091 | if (*CI) { |
| 4092 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4093 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4094 | else |
| 4095 | GetBlockDeclRefExprs(*CI); |
| 4096 | } |
| 4097 | // Handle specific things. |
| 4098 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4099 | // FIXME: Handle enums. |
| 4100 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4101 | BlockDeclRefs.push_back(CDRE); |
| 4102 | return; |
| 4103 | } |
| 4104 | |
| 4105 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 4106 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4107 | CI != E; ++CI) |
| 4108 | if (*CI) { |
| 4109 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4110 | GetBlockCallExprs(CBE->getBody()); |
| 4111 | else |
| 4112 | GetBlockCallExprs(*CI); |
| 4113 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4116 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4117 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 4118 | } |
| 4119 | } |
| 4120 | return; |
| 4121 | } |
| 4122 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4123 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4124 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4125 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4127 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4128 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4129 | } else if (const BlockDeclRefExpr *CDRE = |
| 4130 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4131 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4132 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4133 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4134 | } |
| 4135 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4136 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4137 | } |
| 4138 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4139 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4140 | else if (const ConditionalOperator *CEXPR = |
| 4141 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4142 | Expr *LHSExp = CEXPR->getLHS(); |
| 4143 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4144 | Expr *RHSExp = CEXPR->getRHS(); |
| 4145 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4146 | Expr *CONDExp = CEXPR->getCond(); |
| 4147 | ConditionalOperator *CondExpr = |
| 4148 | new (Context) ConditionalOperator(CONDExp, |
| 4149 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4150 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4151 | Exp->getType()); |
| 4152 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4153 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4154 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4155 | } else { |
| 4156 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4157 | } |
| 4158 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4159 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4160 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4161 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4162 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4164 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4165 | SourceLocation(), |
| 4166 | &Context->Idents.get("__block_impl")); |
| 4167 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4168 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4169 | // Generate a funky cast. |
| 4170 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4172 | // Push the block argument type. |
| 4173 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4174 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4176 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4177 | QualType t = *I; |
| 4178 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4179 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4180 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4181 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4182 | } |
| 4183 | ArgTypes.push_back(t); |
| 4184 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4185 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4186 | // Now do the pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4188 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4190 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
| 4192 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4193 | CastExpr::CK_Unknown, |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4194 | const_cast<Expr*>(BlockExp), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4195 | PtrBlock, SourceLocation(), |
| 4196 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4197 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4198 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4199 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4200 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4202 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4204 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4205 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4206 | FD->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4208 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, |
| 4209 | CastExpr::CK_Unknown, ME, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4210 | PtrToFuncCastType, |
| 4211 | SourceLocation(), |
| 4212 | SourceLocation()); |
| 4213 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4215 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4216 | // Add the implicit argument. |
| 4217 | BlkExprs.push_back(BlkCast); |
| 4218 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4220 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4221 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4222 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4223 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4224 | BlkExprs.size(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4225 | Exp->getType(), SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4226 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4230 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4231 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4234 | // We need to return the rewritten expression to handle cases where the |
| 4235 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4236 | // For example: |
| 4237 | // |
| 4238 | // int main() { |
| 4239 | // __block Foo *f; |
| 4240 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4242 | // void (^myblock)() = ^() { |
| 4243 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4244 | // i = 77; |
| 4245 | // }; |
| 4246 | //} |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4247 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4248 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4249 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4250 | ValueDecl *VD; |
| 4251 | bool isArrow = true; |
| 4252 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4253 | VD = BDRE->getDecl(); |
| 4254 | else { |
| 4255 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4256 | isArrow = false; |
| 4257 | } |
| 4258 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4259 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4260 | &Context->Idents.get("__forwarding"), |
| 4261 | Context->VoidPtrTy, 0, |
| 4262 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4263 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4264 | FD, SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4265 | FD->getType()); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4266 | |
| 4267 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4268 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4269 | &Context->Idents.get(Name), |
| 4270 | Context->VoidPtrTy, 0, |
| 4271 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4272 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4273 | DeclRefExp->getType()); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4274 | |
| 4275 | |
| 4276 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4277 | // Need parens to enforce precedence. |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4278 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4279 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4280 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4281 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4284 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4285 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4286 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4287 | |
| 4288 | // Need to avoid trying to rewrite synthesized casts. |
| 4289 | if (LocStart.isInvalid()) |
| 4290 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4291 | // Need to avoid trying to rewrite casts contained in macros. |
| 4292 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4293 | return; |
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(LocStart); |
| 4296 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4297 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4298 | // advance the location to startArgList. |
| 4299 | const char *argPtr = 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 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4302 | switch (*argPtr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4303 | case '^': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4304 | // Replace the '^' with '*'. |
| 4305 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4306 | ReplaceText(LocStart, 1, "*", 1); |
| 4307 | break; |
| 4308 | } |
| 4309 | } |
| 4310 | return; |
| 4311 | } |
| 4312 | |
| 4313 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4314 | SourceLocation DeclLoc = FD->getLocation(); |
| 4315 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4317 | // We have 1 or more arguments that have closure pointers. |
| 4318 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4319 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4321 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4323 | parenCount++; |
| 4324 | // advance the location to startArgList. |
| 4325 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4326 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4327 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4328 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4330 | while (*argPtr++ && parenCount) { |
| 4331 | switch (*argPtr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | case '^': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4333 | // Replace the '^' with '*'. |
| 4334 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4335 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4336 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | case '(': |
| 4338 | parenCount++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4339 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | case ')': |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4341 | parenCount--; |
| 4342 | break; |
| 4343 | } |
| 4344 | } |
| 4345 | return; |
| 4346 | } |
| 4347 | |
| 4348 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4349 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4350 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4351 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4352 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4353 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4354 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4355 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4356 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4357 | } |
| 4358 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4360 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4361 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4362 | return true; |
| 4363 | } |
| 4364 | return false; |
| 4365 | } |
| 4366 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4367 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4368 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4369 | const char *argPtr = strchr(Name, '('); |
| 4370 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4371 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4372 | LParen = argPtr; // output the start. |
| 4373 | argPtr++; // skip past the left paren. |
| 4374 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4376 | while (*argPtr && parenCount) { |
| 4377 | switch (*argPtr) { |
| 4378 | case '(': parenCount++; break; |
| 4379 | case ')': parenCount--; break; |
| 4380 | default: break; |
| 4381 | } |
| 4382 | if (parenCount) argPtr++; |
| 4383 | } |
| 4384 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4385 | RParen = argPtr; // output the end |
| 4386 | } |
| 4387 | |
| 4388 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4389 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4390 | RewriteBlockPointerFunctionArgs(FD); |
| 4391 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4392 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4393 | // Handle Variables and Typedefs. |
| 4394 | SourceLocation DeclLoc = ND->getLocation(); |
| 4395 | QualType DeclT; |
| 4396 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4397 | DeclT = VD->getType(); |
| 4398 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4399 | DeclT = TDD->getUnderlyingType(); |
| 4400 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4401 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4402 | else |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4403 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4404 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4405 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4406 | const char *endBuf = startBuf; |
| 4407 | // scan backward (from the decl location) for the end of the previous decl. |
| 4408 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4409 | startBuf--; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4410 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4411 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4412 | // may take block argument types (which will be handled below). |
| 4413 | if (*startBuf == '^') { |
| 4414 | // Replace the '^' with '*', computing a negative offset. |
| 4415 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4416 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4417 | } |
| 4418 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4419 | // Replace the '^' with '*' for arguments. |
| 4420 | DeclLoc = ND->getLocation(); |
| 4421 | startBuf = SM->getCharacterData(DeclLoc); |
| 4422 | const char *argListBegin, *argListEnd; |
| 4423 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4424 | while (argListBegin < argListEnd) { |
| 4425 | if (*argListBegin == '^') { |
| 4426 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4427 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4428 | } |
| 4429 | argListBegin++; |
| 4430 | } |
| 4431 | } |
| 4432 | return; |
| 4433 | } |
| 4434 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4435 | |
| 4436 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4437 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4438 | /// struct Block_byref_id_object *src) { |
| 4439 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4440 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4441 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4442 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4443 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4444 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4445 | /// } |
| 4446 | /// And: |
| 4447 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4448 | /// _Block_object_dispose(_src->object, |
| 4449 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4450 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4451 | /// _Block_object_dispose(_src->object, |
| 4452 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4453 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4454 | /// } |
| 4455 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4456 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4457 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4458 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4459 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4460 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4461 | CopyDestroyCache.insert(flag); |
| 4462 | S = "static void __Block_byref_id_object_copy_"; |
| 4463 | S += utostr(flag); |
| 4464 | S += "(void *dst, void *src) {\n"; |
| 4465 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4466 | // offset into the object pointer is computed as: |
| 4467 | // void * + void* + int + int + void* + void * |
| 4468 | unsigned IntSize = |
| 4469 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4470 | unsigned VoidPtrSize = |
| 4471 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4472 | |
| 4473 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4474 | S += " _Block_object_assign((char*)dst + "; |
| 4475 | S += utostr(offset); |
| 4476 | S += ", *(void * *) ((char*)src + "; |
| 4477 | S += utostr(offset); |
| 4478 | S += "), "; |
| 4479 | S += utostr(flag); |
| 4480 | S += ");\n}\n"; |
| 4481 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4482 | S += "static void __Block_byref_id_object_dispose_"; |
| 4483 | S += utostr(flag); |
| 4484 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4485 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4486 | S += utostr(offset); |
| 4487 | S += "), "; |
| 4488 | S += utostr(flag); |
| 4489 | S += ");\n}\n"; |
| 4490 | return S; |
| 4491 | } |
| 4492 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4493 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4494 | /// the declaration into: |
| 4495 | /// struct __Block_byref_ND { |
| 4496 | /// void *__isa; // NULL for everything except __weak pointers |
| 4497 | /// struct __Block_byref_ND *__forwarding; |
| 4498 | /// int32_t __flags; |
| 4499 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4500 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4501 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4502 | /// typex ND; |
| 4503 | /// }; |
| 4504 | /// |
| 4505 | /// It then replaces declaration of ND variable with: |
| 4506 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4507 | /// __size=sizeof(struct __Block_byref_ND), |
| 4508 | /// ND=initializer-if-any}; |
| 4509 | /// |
| 4510 | /// |
| 4511 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 4512 | // Insert declaration for the function in which block literal is |
| 4513 | // used. |
| 4514 | if (CurFunctionDeclToDeclareForBlock) |
| 4515 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4516 | int flag = 0; |
| 4517 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4518 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4519 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4520 | SourceLocation X = ND->getLocEnd(); |
| 4521 | X = SM->getInstantiationLoc(X); |
| 4522 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4523 | std::string Name(ND->getNameAsString()); |
| 4524 | std::string ByrefType = "struct __Block_byref_"; |
| 4525 | ByrefType += Name; |
| 4526 | ByrefType += " {\n"; |
| 4527 | ByrefType += " void *__isa;\n"; |
| 4528 | ByrefType += " struct __Block_byref_" + Name + " *__forwarding;\n"; |
| 4529 | ByrefType += " int __flags;\n"; |
| 4530 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4531 | // Add void *__Block_byref_id_object_copy; |
| 4532 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4533 | QualType Ty = ND->getType(); |
| 4534 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4535 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4536 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4537 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4541 | ByrefType += " " + Name + ";\n"; |
| 4542 | ByrefType += "};\n"; |
| 4543 | // Insert this type in global scope. It is needed by helper function. |
| 4544 | assert(CurFunctionDef && "RewriteByRefVar - CurFunctionDef is null"); |
| 4545 | SourceLocation FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4546 | InsertText(FunLocStart, ByrefType.c_str(), ByrefType.size()); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4547 | if (Ty.isObjCGCWeak()) { |
| 4548 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4549 | isa = 1; |
| 4550 | } |
| 4551 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4552 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4553 | flag = BLOCK_BYREF_CALLER; |
| 4554 | QualType Ty = ND->getType(); |
| 4555 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4556 | if (Ty->isBlockPointerType()) |
| 4557 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4558 | else |
| 4559 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4560 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4561 | if (!HF.empty()) |
| 4562 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4563 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4564 | |
| 4565 | // struct __Block_byref_ND ND = |
| 4566 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4567 | // initializer-if-any}; |
| 4568 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4569 | unsigned flags = 0; |
| 4570 | if (HasCopyAndDispose) |
| 4571 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4572 | Name = ND->getNameAsString(); |
| 4573 | ByrefType = "struct __Block_byref_" + Name; |
| 4574 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4575 | ByrefType += " " + Name + " = {(void*)"; |
| 4576 | ByrefType += utostr(isa); |
| 4577 | ByrefType += ", &" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4578 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4579 | ByrefType += ", "; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4580 | ByrefType += "sizeof(struct __Block_byref_" + Name + ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4581 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4582 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4583 | ByrefType += utostr(flag); |
| 4584 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4585 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4586 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4587 | ByrefType += "};\n"; |
| 4588 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), |
| 4589 | ByrefType.c_str(), ByrefType.size()); |
| 4590 | } |
| 4591 | else { |
| 4592 | SourceLocation startLoc = ND->getInit()->getLocStart(); |
Fariborz Jahanian | b8646ed | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 4593 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4594 | ByrefType += " " + Name; |
| 4595 | ReplaceText(DeclLoc, endBuf-startBuf, |
| 4596 | ByrefType.c_str(), ByrefType.size()); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4597 | ByrefType = " = {(void*)"; |
| 4598 | ByrefType += utostr(isa); |
| 4599 | ByrefType += ", &" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4600 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4601 | ByrefType += ", "; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4602 | ByrefType += "sizeof(struct __Block_byref_" + Name + "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4603 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4604 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4605 | ByrefType += utostr(flag); |
| 4606 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4607 | ByrefType += utostr(flag); |
| 4608 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4609 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4610 | InsertText(startLoc, ByrefType.c_str(), ByrefType.size()); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Complete the newly synthesized compound expression by inserting a right |
| 4613 | // curly brace before the end of the declaration. |
| 4614 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4615 | // also assumes there is only one declarator. For example, the following |
| 4616 | // isn't currently supported by this routine (in general): |
| 4617 | // |
| 4618 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4619 | // |
| 4620 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4621 | const char *semiBuf = strchr(startBuf, ';'); |
| 4622 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4623 | SourceLocation semiLoc = |
| 4624 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 4625 | |
| 4626 | InsertText(semiLoc, "}", 1); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4627 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4628 | return; |
| 4629 | } |
| 4630 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4632 | // Add initializers for any closure decl refs. |
| 4633 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4634 | if (BlockDeclRefs.size()) { |
| 4635 | // Unique all "by copy" declarations. |
| 4636 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4637 | if (!BlockDeclRefs[i]->isByRef()) |
| 4638 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4639 | // Unique all "by ref" declarations. |
| 4640 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4641 | if (BlockDeclRefs[i]->isByRef()) { |
| 4642 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4643 | } |
| 4644 | // Find any imported blocks...they will need special attention. |
| 4645 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4646 | if (BlockDeclRefs[i]->isByRef() || |
| 4647 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4648 | BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 832d890 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4649 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4650 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4651 | } |
| 4652 | } |
| 4653 | } |
| 4654 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4655 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4656 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4657 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4659 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4660 | false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4663 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4664 | Blocks.push_back(Exp); |
| 4665 | |
| 4666 | CollectBlockDeclRefInfo(Exp); |
| 4667 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4669 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4670 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4671 | else if (CurMethodDef) { |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4672 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4673 | // Convert colons to underscores. |
| 4674 | std::string::size_type loc = 0; |
| 4675 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4676 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4677 | } else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4678 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4680 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4682 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4683 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4685 | // Get a pointer to the function type so we can cast appropriately. |
| 4686 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4687 | |
| 4688 | FunctionDecl *FD; |
| 4689 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4691 | // Simulate a contructor call... |
| 4692 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4693 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4695 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4696 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4697 | // Initialize the block function. |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4698 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4699 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4700 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4702 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4703 | Context->VoidPtrTy, SourceLocation(), |
| 4704 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | InitExprs.push_back(castExpr); |
| 4706 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4707 | // Initialize the block descriptor. |
| 4708 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4710 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4711 | &Context->Idents.get(DescData.c_str()), |
| 4712 | Context->VoidPtrTy, 0, |
| 4713 | VarDecl::Static); |
| 4714 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 4715 | new (Context) DeclRefExpr(NewVD, |
| 4716 | Context->VoidPtrTy, SourceLocation()), |
| 4717 | UnaryOperator::AddrOf, |
| 4718 | Context->getPointerType(Context->VoidPtrTy), |
| 4719 | SourceLocation()); |
| 4720 | InitExprs.push_back(DescRefExpr); |
| 4721 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4722 | // Add initializers for any closure decl refs. |
| 4723 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4724 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | // Output all "by copy" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4727 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4728 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4729 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4730 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4731 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4732 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4733 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4734 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4736 | CastExpr::CK_Unknown, Arg, |
| 4737 | Context->VoidPtrTy, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4738 | SourceLocation(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4739 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4740 | } else { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4741 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4742 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4745 | } |
| 4746 | // Output all "by ref" declarations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4748 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4749 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4750 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4751 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4752 | Context->getPointerType(Exp->getType()), |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4753 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4757 | if (ImportedBlockDecls.size()) { |
| 4758 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4759 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4760 | unsigned IntSize = |
| 4761 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4762 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 4763 | Context->IntTy, SourceLocation()); |
| 4764 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4765 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4766 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4767 | FType, SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4768 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4769 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4770 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4771 | NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4772 | FType, SourceLocation(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4773 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4774 | BlockDeclRefs.clear(); |
| 4775 | BlockByRefDecls.clear(); |
| 4776 | BlockByCopyDecls.clear(); |
| 4777 | ImportedBlockDecls.clear(); |
| 4778 | return NewRep; |
| 4779 | } |
| 4780 | |
| 4781 | //===----------------------------------------------------------------------===// |
| 4782 | // Function Body / Expression rewriting |
| 4783 | //===----------------------------------------------------------------------===// |
| 4784 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4785 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4786 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4787 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4788 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4789 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4790 | // we get this right. |
| 4791 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4792 | // Perform a bottom up traversal of all children. |
| 4793 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4794 | CI != E; ++CI) |
| 4795 | if (*CI) |
| 4796 | CollectPropertySetters(*CI); |
| 4797 | |
| 4798 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4799 | if (BinOp->isAssignmentOp()) { |
| 4800 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4801 | PropSetters[PRE] = BinOp; |
| 4802 | } |
| 4803 | } |
| 4804 | } |
| 4805 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4806 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4808 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4809 | Stmts.push_back(S); |
| 4810 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4811 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4812 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4813 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4815 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4817 | // Perform a bottom up rewrite of all children. |
| 4818 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4819 | CI != E; ++CI) |
| 4820 | if (*CI) { |
| 4821 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | if (newStmt) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4823 | *CI = newStmt; |
| 4824 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4826 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4827 | // Rewrite the block body in place. |
| 4828 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4830 | // Now we snarf the rewritten text and stash it away for later use. |
Ted Kremenek | db2ef37 | 2010-01-07 18:00:35 +0000 | [diff] [blame] | 4831 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4832 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4834 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4835 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4836 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4837 | return blockTranscribed; |
| 4838 | } |
| 4839 | // Handle specific things. |
| 4840 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4841 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4843 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4844 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4845 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4846 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4847 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4848 | if (BinOp) { |
| 4849 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4850 | // 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] | 4851 | DisableReplaceStmt = true; |
| 4852 | // Save the source range. Even if we disable the replacement, the |
| 4853 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4854 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | // self.errorHandler = handler ? handler : |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4856 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4857 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4858 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4859 | DisableReplaceStmt = false; |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4860 | // |
| 4861 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4862 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4863 | // to see the original expression). Consider this example: |
| 4864 | // |
| 4865 | // Foo *obj1, *obj2; |
| 4866 | // |
| 4867 | // obj1.i = [obj2 rrrr]; |
| 4868 | // |
| 4869 | // 'BinOp' for the previous expression looks like: |
| 4870 | // |
| 4871 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4872 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4873 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4874 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4875 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4876 | // |
| 4877 | // 'newStmt' represents the rewritten message expression. For example: |
| 4878 | // |
| 4879 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4880 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4881 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4882 | // (CStyleCastExpr 0x231d220 'void *' |
| 4883 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4884 | // |
| 4885 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4886 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4887 | // the original RHS (since we haven't altered BinOp). |
| 4888 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4890 | // node. As a result, we now leak the original AST nodes. |
| 4891 | // |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4892 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4893 | } else { |
| 4894 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4895 | } |
| 4896 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4897 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4898 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4900 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4901 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4903 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4904 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4905 | // Before we rewrite it, put the original message expression in a comment. |
| 4906 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4907 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4908 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4909 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4910 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4911 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4912 | std::string messString; |
| 4913 | messString += "// "; |
| 4914 | messString.append(startBuf, endBuf-startBuf+1); |
| 4915 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4916 | |
| 4917 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4918 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4919 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4920 | // Tried this, but it didn't work either... |
| 4921 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4922 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4923 | return RewriteMessageExpr(MessExpr); |
| 4924 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4925 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4926 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4927 | return RewriteObjCTryStmt(StmtTry); |
| 4928 | |
| 4929 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4930 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4931 | |
| 4932 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4933 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4934 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4935 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4936 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | |
| 4938 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4939 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4941 | OrigStmtRange.getEnd()); |
| 4942 | if (BreakStmt *StmtBreakStmt = |
| 4943 | dyn_cast<BreakStmt>(S)) |
| 4944 | return RewriteBreakStmt(StmtBreakStmt); |
| 4945 | if (ContinueStmt *StmtContinueStmt = |
| 4946 | dyn_cast<ContinueStmt>(S)) |
| 4947 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | |
| 4949 | // 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] | 4950 | // and cast exprs. |
| 4951 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4952 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4953 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4955 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4956 | // the context of an ObjCForCollectionStmt. For example: |
| 4957 | // NSArray *someArray; |
| 4958 | // for (id <FooProtocol> index in someArray) ; |
| 4959 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4960 | // and it depends on the original text locations/positions. |
Benjamin Kramer | acc5fa1 | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 4961 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4962 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4964 | // Blocks rewrite rules. |
| 4965 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4966 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4967 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4968 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4969 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4970 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4971 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4972 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4973 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) |
| 4974 | if (VD->hasAttr<BlocksAttr>()) |
| 4975 | RewriteByRefVar(VD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4976 | } |
| 4977 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4978 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4979 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4981 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4982 | } |
| 4983 | } |
| 4984 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4985 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4986 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4987 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4988 | |
| 4989 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4990 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4991 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4992 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4993 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4994 | && "Statement stack mismatch"); |
| 4995 | Stmts.pop_back(); |
| 4996 | } |
| 4997 | // Handle blocks rewriting. |
| 4998 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4999 | if (BDRE->isByRef()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5000 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5001 | } |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5002 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5003 | ValueDecl *VD = DRE->getDecl(); |
| 5004 | if (VD->hasAttr<BlocksAttr>()) |
| 5005 | return RewriteBlockDeclRefExpr(DRE); |
| 5006 | } |
| 5007 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5008 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5009 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5010 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5011 | ReplaceStmt(S, BlockCall); |
| 5012 | return BlockCall; |
| 5013 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5014 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5015 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5016 | RewriteCastExpr(CE); |
| 5017 | } |
| 5018 | #if 0 |
| 5019 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5020 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5021 | // Get the new text. |
| 5022 | std::string SStr; |
| 5023 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 0905f14 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5024 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5025 | const std::string &Str = Buf.str(); |
| 5026 | |
| 5027 | printf("CAST = %s\n", &Str[0]); |
| 5028 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5029 | delete S; |
| 5030 | return Replacement; |
| 5031 | } |
| 5032 | #endif |
| 5033 | // Return this stmt unmodified. |
| 5034 | return S; |
| 5035 | } |
| 5036 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5037 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5038 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5039 | e = RD->field_end(); i != e; ++i) { |
| 5040 | FieldDecl *FD = *i; |
| 5041 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5042 | RewriteBlockPointerDecl(FD); |
| 5043 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5044 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5045 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5046 | } |
| 5047 | } |
| 5048 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5049 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5050 | /// main file of the input. |
| 5051 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5052 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | b136888 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5053 | if (FD->isOverloadedOperator()) |
| 5054 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5056 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5057 | // prototype. This enables us to rewrite function declarations and |
| 5058 | // definitions using the same code. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5059 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5060 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5061 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5062 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5063 | CurFunctionDef = FD; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 5064 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5065 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5066 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5067 | Body = |
| 5068 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5069 | FD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5070 | CurrentBody = 0; |
| 5071 | if (PropParentMap) { |
| 5072 | delete PropParentMap; |
| 5073 | PropParentMap = 0; |
| 5074 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5075 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5076 | // and any copy/dispose helper functions. |
| 5077 | InsertBlockLiteralsWithinFunction(FD); |
| 5078 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame^] | 5079 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5080 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5081 | return; |
| 5082 | } |
| 5083 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5084 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5085 | CurMethodDef = MD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5086 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5087 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5088 | Body = |
| 5089 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5090 | MD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5091 | CurrentBody = 0; |
| 5092 | if (PropParentMap) { |
| 5093 | delete PropParentMap; |
| 5094 | PropParentMap = 0; |
| 5095 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5096 | InsertBlockLiteralsWithinMethod(MD); |
| 5097 | CurMethodDef = 0; |
| 5098 | } |
| 5099 | } |
| 5100 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5101 | ClassImplementation.push_back(CI); |
| 5102 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5103 | CategoryImplementation.push_back(CI); |
| 5104 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5105 | RewriteForwardClassDecl(CD); |
| 5106 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5107 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5108 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5109 | RewriteBlockPointerDecl(VD); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5110 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5111 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5112 | if (VD->getInit()) { |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5113 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5114 | RewriteCastExpr(CE); |
| 5115 | } |
| 5116 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5117 | } else if (VD->getType()->isRecordType()) { |
| 5118 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5119 | if (RD->isDefinition()) |
| 5120 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5121 | } |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5122 | if (VD->getInit()) { |
| 5123 | GlobalVarDecl = VD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5124 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5125 | CurrentBody = VD->getInit(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5126 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5127 | CurrentBody = 0; |
| 5128 | if (PropParentMap) { |
| 5129 | delete PropParentMap; |
| 5130 | PropParentMap = 0; |
| 5131 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5132 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5133 | VD->getNameAsCString()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5134 | GlobalVarDecl = 0; |
| 5135 | |
| 5136 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5137 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5138 | RewriteCastExpr(CE); |
| 5139 | } |
| 5140 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5141 | return; |
| 5142 | } |
| 5143 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5144 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5145 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5146 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5147 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5148 | else if (TD->getUnderlyingType()->isRecordType()) { |
| 5149 | RecordDecl *RD = TD->getUnderlyingType()->getAs<RecordType>()->getDecl(); |
| 5150 | if (RD->isDefinition()) |
| 5151 | RewriteRecordBody(RD); |
| 5152 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5153 | return; |
| 5154 | } |
| 5155 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5156 | if (RD->isDefinition()) |
| 5157 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5158 | return; |
| 5159 | } |
| 5160 | // Nothing yet. |
| 5161 | } |
| 5162 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5163 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5164 | // Get the top-level buffer that this corresponds to. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5165 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5166 | // Rewrite tabs if we care. |
| 5167 | //RewriteTabs(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5168 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5169 | if (Diags.hasErrorOccurred()) |
| 5170 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5171 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5172 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5173 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5174 | // Here's a great place to add any extra declarations that may be needed. |
| 5175 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5176 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5177 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5178 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5179 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5181 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5182 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5183 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5184 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5185 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5186 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5187 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5188 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5189 | //printf("Changed:\n"); |
| 5190 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5191 | } else { |
| 5192 | fprintf(stderr, "No changes\n"); |
| 5193 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5194 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5195 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5196 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5197 | // Rewrite Objective-c meta data* |
| 5198 | std::string ResultStr; |
| 5199 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5200 | // Emit metadata. |
| 5201 | *OutFile << ResultStr; |
| 5202 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5203 | OutFile->flush(); |
| 5204 | } |
| 5205 | |