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 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/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" |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 35 | enum { |
| 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 37 | block, ... */ |
| 38 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 39 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 40 | __block variable */ |
| 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 42 | helpers */ |
| 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 44 | support routines */ |
| 45 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 46 | }; |
| 47 | |
| 48 | enum { |
| 49 | BLOCK_NEEDS_FREE = (1 << 24), |
| 50 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 51 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 52 | BLOCK_IS_GC = (1 << 27), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 55 | }; |
| 56 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 57 | Rewriter Rewrite; |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 58 | Diagnostic &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 59 | const LangOptions &LangOpts; |
Steve Naroff | 7b3579b | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 60 | unsigned RewriteFailedDiag; |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 61 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 63 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 64 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 65 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 66 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 67 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 68 | SourceLocation LastIncLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 70 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 71 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 72 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 74 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 75 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 76 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 77 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 78 | // Remember all the @protocol(<expr>) expressions. |
| 79 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 80 | |
| 81 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 82 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 83 | unsigned NumObjCStringLiterals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 87 | FunctionDecl *MsgSendStretFunctionDecl; |
| 88 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 89 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 90 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 91 | FunctionDecl *GetMetaClassFunctionDecl; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 92 | FunctionDecl *GetSuperClassFunctionDecl; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 93 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 94 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 95 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 97 | // ObjC string constant support. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 98 | VarDecl *ConstantStringClassReference; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 99 | RecordDecl *NSStringRecord; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Fariborz Jahanian | 19d42bf | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 101 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 102 | int BcLabelCount; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 104 | // Needed for super. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 105 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 106 | RecordDecl *SuperStructDecl; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 107 | RecordDecl *ConstantStringDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 109 | TypeDecl *ProtocolTypeDecl; |
| 110 | QualType getProtocolType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 112 | // Needed for header files being rewritten |
| 113 | bool IsHeader; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 115 | std::string InFileName; |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 116 | llvm::raw_ostream* OutFile; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 117 | |
| 118 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 119 | bool objc_impl_method; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 120 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 121 | std::string Preamble; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 122 | |
| 123 | // Block expressions. |
| 124 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 125 | llvm::SmallVector<int, 32> InnerDeclRefsCount; |
| 126 | llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
| 127 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 128 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 130 | // Block related declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 131 | llvm::SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 133 | llvm::SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 134 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 135 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 136 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 137 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 138 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 139 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 140 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 141 | // This maps a property to it's assignment statement. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 142 | llvm::DenseMap<Expr *, BinaryOperator *> PropSetters; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 143 | // This maps a property to it's synthesied message expression. |
| 144 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 145 | llvm::DenseMap<Expr *, Stmt *> PropGetters; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 147 | // This maps an original source AST to it's rewritten form. This allows |
| 148 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 149 | // This is needed to support some of the exotic property rewriting. |
| 150 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 151 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 152 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 153 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 154 | VarDecl *GlobalVarDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 156 | bool DisableReplaceStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 158 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 159 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 160 | virtual void Initialize(ASTContext &context); |
| 161 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 162 | // Top Level Driver code. |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 163 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 164 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 165 | HandleTopLevelSingleDecl(*I); |
| 166 | } |
| 167 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 168 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 169 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 170 | Diagnostic &D, const LangOptions &LOpts, |
| 171 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 172 | |
| 173 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 175 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 177 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 180 | if (ReplacingStmt) |
| 181 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 182 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 183 | if (DisableReplaceStmt) |
| 184 | return; // Used when rewriting the assignment of a property setter. |
| 185 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 186 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 187 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 188 | ReplacedNodes[Old] = New; |
| 189 | return; |
| 190 | } |
| 191 | if (SilenceRewriteMacroWarning) |
| 192 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 193 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 194 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 195 | } |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 196 | |
| 197 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 198 | // Measaure the old text. |
| 199 | int Size = Rewrite.getRangeSize(SrcRange); |
| 200 | if (Size == -1) { |
| 201 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 202 | << Old->getSourceRange(); |
| 203 | return; |
| 204 | } |
| 205 | // Get the new text. |
| 206 | std::string SStr; |
| 207 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 208 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 209 | const std::string &Str = S.str(); |
| 210 | |
| 211 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 212 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 213 | ReplacedNodes[Old] = New; |
| 214 | return; |
| 215 | } |
| 216 | if (SilenceRewriteMacroWarning) |
| 217 | return; |
| 218 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 219 | << Old->getSourceRange(); |
| 220 | } |
| 221 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 222 | void InsertText(SourceLocation Loc, llvm::StringRef Str, |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 223 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 224 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 225 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 226 | SilenceRewriteMacroWarning) |
| 227 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 229 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 232 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 233 | llvm::StringRef Str) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 234 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 235 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 236 | SilenceRewriteMacroWarning) |
| 237 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 239 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 240 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 242 | // Syntactic Rewriting. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 243 | void RewriteInclude(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 244 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 245 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 246 | ObjCImplementationDecl *IMD, |
| 247 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 249 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 250 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 251 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 252 | const FunctionType *&FPRetType); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 253 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 254 | ValueDecl *VD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 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); |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 261 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 262 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 263 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 264 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 265 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 266 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 267 | bool needToScanForQualifiers(QualType T); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 268 | QualType getSuperStructType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 269 | QualType getConstantStringStructType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 270 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 271 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 273 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 274 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 275 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 277 | Stmt *CurrentBody; |
| 278 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 280 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 281 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 282 | bool &replaced); |
| 283 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 284 | Stmt *RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr); |
| 285 | Stmt *RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 286 | SourceRange SrcRange); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 287 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 288 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 289 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 291 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 292 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 293 | void RewriteTryReturnStmts(Stmt *S); |
| 294 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 295 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 297 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 298 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 299 | SourceLocation OrigEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 301 | Expr **args, unsigned nargs, |
| 302 | SourceLocation StartLoc=SourceLocation(), |
| 303 | SourceLocation EndLoc=SourceLocation()); |
| 304 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 305 | SourceLocation StartLoc=SourceLocation(), |
| 306 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 307 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 308 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 309 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 311 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 312 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 313 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 314 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 315 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 316 | void SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 317 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 318 | void SynthGetSuperClassFunctionDecl(); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 319 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 320 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 322 | // Metadata emission. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 323 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 324 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 326 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 327 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 329 | template<typename MethodIterator> |
| 330 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 331 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 332 | bool IsInstanceMethod, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 333 | llvm::StringRef prefix, |
| 334 | llvm::StringRef ClassName, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 335 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 337 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 338 | llvm::StringRef prefix, |
| 339 | llvm::StringRef ClassName, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 340 | std::string &Result); |
| 341 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 342 | llvm::StringRef prefix, |
| 343 | llvm::StringRef ClassName, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 344 | std::string &Result); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 345 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 346 | std::string &Result); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 347 | void SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 349 | std::string &Result); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 350 | void RewriteImplementations(); |
| 351 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 353 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 355 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 357 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 358 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
| 360 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 362 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 363 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 364 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 365 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 366 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
| 368 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 369 | llvm::StringRef funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 371 | llvm::StringRef funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 372 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 373 | std::string Tag, std::string Desc); |
| 374 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 375 | std::string ImplTag, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 376 | int i, llvm::StringRef funcName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 377 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 378 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 379 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 380 | llvm::StringRef FunName); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 381 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 383 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 385 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 386 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 387 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 389 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 390 | // 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] | 391 | bool isTopLevelBlockPointerType(QualType T) { |
| 392 | return isa<BlockPointerType>(T); |
| 393 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 395 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 396 | /// to a function pointer type and upon success, returns true; false |
| 397 | /// otherwise. |
| 398 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 399 | if (isTopLevelBlockPointerType(T)) { |
| 400 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 401 | T = Context->getPointerType(BPT->getPointeeType()); |
| 402 | return true; |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 407 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 408 | bool isObjCType(QualType T) { |
| 409 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 410 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 412 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 414 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 415 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 416 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 418 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 420 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 426 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 427 | const char *&RParen); |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 428 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 430 | FunctionDecl *SynthBlockInitFunctionDecl(llvm::StringRef name); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 431 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 432 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 434 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 436 | if (From[i] == '"') |
| 437 | To += "\\\""; |
| 438 | else |
| 439 | To += From[i]; |
| 440 | } |
| 441 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 442 | }; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 443 | |
| 444 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 445 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 446 | CastKind Kind, Expr *E) { |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 447 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 448 | return CStyleCastExpr::Create(*Ctx, Ty, Kind, E, 0, TInfo, |
| 449 | SourceLocation(), SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 454 | NamedDecl *D) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 455 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 457 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 458 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 459 | // All the args are checked/rewritten. Don't call twice! |
| 460 | RewriteBlockPointerDecl(D); |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 467 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 468 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 469 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 472 | static bool IsHeaderFile(const std::string &Filename) { |
| 473 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 475 | if (DotPos == std::string::npos) { |
| 476 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 480 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 481 | // C header: .h |
| 482 | // C++ header: .hh or .H; |
| 483 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 485 | |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 486 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 487 | Diagnostic &D, const LangOptions &LOpts, |
| 488 | bool silenceMacroWarn) |
| 489 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 490 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 491 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 493 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 495 | "rewriter doesn't support user-specified control flow semantics " |
| 496 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 499 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 500 | llvm::raw_ostream* OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | Diagnostic &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 502 | const LangOptions &LOpts, |
| 503 | bool SilenceRewriteMacroWarning) { |
| 504 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 505 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 506 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 507 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 508 | Context = &context; |
| 509 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 510 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 511 | MsgSendFunctionDecl = 0; |
| 512 | MsgSendSuperFunctionDecl = 0; |
| 513 | MsgSendStretFunctionDecl = 0; |
| 514 | MsgSendSuperStretFunctionDecl = 0; |
| 515 | MsgSendFpretFunctionDecl = 0; |
| 516 | GetClassFunctionDecl = 0; |
| 517 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 518 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 519 | SelGetUidFunctionDecl = 0; |
| 520 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 521 | ConstantStringClassReference = 0; |
| 522 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 523 | CurMethodDef = 0; |
| 524 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 525 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 526 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 527 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 528 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 529 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 530 | BcLabelCount = 0; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 531 | SuperContructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 532 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 533 | PropParentMap = 0; |
| 534 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 535 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 536 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 538 | // Get the ID and start/end of the main file. |
| 539 | MainFileID = SM->getMainFileID(); |
| 540 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 541 | MainFileStart = MainBuf->getBufferStart(); |
| 542 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 544 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 546 | // declaring objc_selector outside the parameter list removes a silly |
| 547 | // scope related warning... |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 548 | if (IsHeader) |
Steve Naroff | fcc6fd5 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 549 | Preamble = "#pragma once\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 550 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 551 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 552 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 553 | if (LangOpts.Microsoft) { |
| 554 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 555 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 556 | ": "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 557 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 558 | } |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 559 | Preamble += "};\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 560 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 561 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 562 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 563 | Preamble += "#endif\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 564 | if (LangOpts.Microsoft) { |
| 565 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 566 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 567 | } else |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 568 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 569 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 570 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 571 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 572 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 573 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 574 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 575 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 576 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 577 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 578 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 579 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 580 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 581 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 582 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 583 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 584 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 585 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 586 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 587 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 588 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 589 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | d30f8c5 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 590 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 8dd1525 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 591 | // @synchronized hooks. |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 592 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 593 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 594 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 595 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 596 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 597 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | 4dbab8a | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 598 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 599 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 600 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 601 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 602 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 603 | Preamble += "#endif\n"; |
| 604 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 605 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 606 | Preamble += " int *isa;\n"; |
| 607 | Preamble += " int flags;\n"; |
| 608 | Preamble += " char *str;\n"; |
| 609 | Preamble += " long length;\n"; |
| 610 | Preamble += "};\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 611 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 612 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 613 | Preamble += "#else\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 614 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 615 | Preamble += "#endif\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 616 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 617 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 618 | // Blocks preamble. |
| 619 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 620 | Preamble += "#define BLOCK_IMPL\n"; |
| 621 | Preamble += "struct __block_impl {\n"; |
| 622 | Preamble += " void *isa;\n"; |
| 623 | Preamble += " int Flags;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 624 | Preamble += " int Reserved;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 625 | Preamble += " void *FuncPtr;\n"; |
| 626 | Preamble += "};\n"; |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 627 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | 287a2bf | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 628 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 629 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 630 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 631 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 632 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 633 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 634 | Preamble += "#else\n"; |
Steve Naroff | 7bf01ea | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 635 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 636 | 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] | 637 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 638 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 639 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 640 | Preamble += "#endif\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 641 | if (LangOpts.Microsoft) { |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 642 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 643 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 8d269dc | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 644 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 645 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 8d269dc | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 646 | Preamble += "#endif\n"; |
Fariborz Jahanian | f0462ff | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 647 | Preamble += "#define __weak\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 648 | } |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 649 | else { |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 650 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 651 | Preamble += "#define __weak\n"; |
| 652 | } |
Fariborz Jahanian | db217c4 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 653 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 654 | // as this avoids warning in any 64bit/32bit compilation model. |
| 655 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 659 | //===----------------------------------------------------------------------===// |
| 660 | // Top Level Driver Code |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 663 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 664 | if (Diags.hasErrorOccurred()) |
| 665 | return; |
| 666 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 667 | // Two cases: either the decl could be in the main file, or it could be in a |
| 668 | // #included file. If the former, rewrite it now. If the later, check to see |
| 669 | // if we rewrote the #include/#import. |
| 670 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 671 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 673 | // If this is for a builtin, ignore it. |
| 674 | if (Loc.isInvalid()) return; |
| 675 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 676 | // Look for built-in declarations that we need to refer during the rewrite. |
| 677 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 678 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 679 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 680 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 681 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 682 | ConstantStringClassReference = FVD; |
| 683 | return; |
| 684 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 685 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 686 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 687 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 688 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 689 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 690 | RewriteProtocolDecl(PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 692 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 693 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 694 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 695 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 696 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 697 | DIEnd = LSD->decls_end(); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 698 | DI != DIEnd; ++DI) |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 699 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 700 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 701 | // 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] | 702 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 703 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 706 | //===----------------------------------------------------------------------===// |
| 707 | // Syntactic (non-AST) Rewriting Code |
| 708 | //===----------------------------------------------------------------------===// |
| 709 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 710 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 711 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 712 | llvm::StringRef MainBuf = SM->getBufferData(MainFileID); |
| 713 | const char *MainBufStart = MainBuf.begin(); |
| 714 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 715 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 717 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 718 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 719 | if (*BufPtr == '#') { |
| 720 | if (++BufPtr == MainBufEnd) |
| 721 | return; |
| 722 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 723 | if (++BufPtr == MainBufEnd) |
| 724 | return; |
| 725 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 726 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | SourceLocation ImportLoc = |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 728 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 729 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 730 | BufPtr += ImportLen; |
| 731 | } |
| 732 | } |
| 733 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 736 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 737 | ObjCIvarDecl *OID) { |
| 738 | std::string S; |
| 739 | S = "((struct "; |
| 740 | S += ClassDecl->getIdentifier()->getName(); |
| 741 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 742 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 743 | return S; |
| 744 | } |
| 745 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 746 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 747 | ObjCImplementationDecl *IMD, |
| 748 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 749 | static bool objcGetPropertyDefined = false; |
| 750 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 751 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 752 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 753 | const char *startBuf = SM->getCharacterData(startLoc); |
| 754 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 755 | const char *semiBuf = strchr(startBuf, ';'); |
| 756 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 757 | SourceLocation onePastSemiLoc = |
| 758 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 759 | |
| 760 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 761 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 763 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 764 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 765 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 768 | if (!OID) |
| 769 | return; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 770 | unsigned Attributes = PD->getPropertyAttributes(); |
| 771 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 772 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 773 | ObjCPropertyDecl::OBJC_PR_copy)); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 774 | std::string Getr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 775 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 776 | objcGetPropertyDefined = true; |
| 777 | // FIXME. Is this attribute correct in all cases? |
| 778 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 779 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 780 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 781 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 782 | Getr += "{ "; |
| 783 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 0629704 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 784 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 785 | if (GenGetProperty) { |
| 786 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 787 | Getr += "typedef "; |
| 788 | const FunctionType *FPRetType = 0; |
| 789 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 790 | FPRetType); |
| 791 | Getr += " _TYPE"; |
| 792 | if (FPRetType) { |
| 793 | Getr += ")"; // close the precedence "scope" for "*". |
| 794 | |
| 795 | // Now, emit the argument types (if any). |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 796 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 797 | Getr += "("; |
| 798 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 799 | if (i) Getr += ", "; |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 800 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 801 | Context->PrintingPolicy); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 802 | Getr += ParamStr; |
| 803 | } |
| 804 | if (FT->isVariadic()) { |
| 805 | if (FT->getNumArgs()) Getr += ", "; |
| 806 | Getr += "..."; |
| 807 | } |
| 808 | Getr += ")"; |
| 809 | } else |
| 810 | Getr += "()"; |
| 811 | } |
| 812 | Getr += ";\n"; |
| 813 | Getr += "return (_TYPE)"; |
| 814 | Getr += "objc_getProperty(self, _cmd, "; |
| 815 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Getr); |
| 816 | Getr += ", 1)"; |
| 817 | } |
| 818 | else |
| 819 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 820 | Getr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 821 | InsertText(onePastSemiLoc, Getr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 822 | if (PD->isReadOnly()) |
| 823 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 825 | // Generate the 'setter' function. |
| 826 | std::string Setr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 827 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 828 | ObjCPropertyDecl::OBJC_PR_copy); |
| 829 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 830 | objcSetPropertyDefined = true; |
| 831 | // FIXME. Is this attribute correct in all cases? |
| 832 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 833 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 834 | } |
| 835 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 836 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 837 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 838 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 839 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 840 | if (GenSetProperty) { |
| 841 | Setr += "objc_setProperty (self, _cmd, "; |
| 842 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Setr); |
| 843 | Setr += ", (id)"; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 844 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 845 | Setr += ", "; |
| 846 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 847 | Setr += "0, "; |
| 848 | else |
| 849 | Setr += "1, "; |
| 850 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 851 | Setr += "1)"; |
| 852 | else |
| 853 | Setr += "0)"; |
| 854 | } |
| 855 | else { |
| 856 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 857 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 858 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 859 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 860 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 862 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 863 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 864 | // Get the start location and compute the semi location. |
| 865 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 866 | const char *startBuf = SM->getCharacterData(startLoc); |
| 867 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 869 | // Translate to typedef's that forward reference structs with the same name |
| 870 | // as the class. As a convenience, we include the original declaration |
| 871 | // as a comment. |
| 872 | std::string typedefString; |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 873 | typedefString += "// @class "; |
| 874 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 875 | I != E; ++I) { |
| 876 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 877 | typedefString += ForwardDecl->getNameAsString(); |
| 878 | if (I+1 != E) |
| 879 | typedefString += ", "; |
| 880 | else |
| 881 | typedefString += ";\n"; |
| 882 | } |
| 883 | |
Chris Lattner | 9ee23b7 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 884 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 885 | I != E; ++I) { |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 886 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 887 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 888 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 889 | typedefString += "\n"; |
| 890 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 891 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 892 | typedefString += "\n"; |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 893 | typedefString += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 894 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 895 | typedefString += ";\n#endif\n"; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 896 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 898 | // Replace the @class with typedefs corresponding to the classes. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 899 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 902 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 903 | // When method is a synthesized one, such as a getter/setter there is |
| 904 | // nothing to rewrite. |
| 905 | if (Method->isSynthesized()) |
| 906 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 907 | SourceLocation LocStart = Method->getLocStart(); |
| 908 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 910 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 911 | SM->getInstantiationLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 912 | InsertText(LocStart, "#if 0\n"); |
| 913 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 914 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 915 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 920 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 922 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 923 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 926 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 927 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 929 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 930 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 932 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 933 | E = CatDecl->prop_end(); I != E; ++I) |
| 934 | RewriteProperty(*I); |
| 935 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | for (ObjCCategoryDecl::instmeth_iterator |
| 937 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 938 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 939 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 941 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 942 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 943 | RewriteMethodDeclaration(*I); |
| 944 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 945 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 946 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 947 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 950 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 951 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 953 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 954 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
| 956 | for (ObjCProtocolDecl::instmeth_iterator |
| 957 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 958 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 959 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 960 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 961 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 962 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 963 | RewriteMethodDeclaration(*I); |
| 964 | |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 965 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 966 | E = PDecl->prop_end(); I != E; ++I) |
| 967 | RewriteProperty(*I); |
| 968 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 969 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 970 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 971 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 972 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 973 | // Must comment out @optional/@required |
| 974 | const char *startBuf = SM->getCharacterData(LocStart); |
| 975 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 976 | for (const char *p = startBuf; p < endBuf; p++) { |
| 977 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 978 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 979 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 981 | } |
| 982 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 983 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 984 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 990 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 991 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 992 | if (LocStart.isInvalid()) |
| 993 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 994 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 995 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 998 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 999 | const FunctionType *&FPRetType) { |
| 1000 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1001 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1002 | else if (T->isFunctionPointerType() || |
| 1003 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1004 | // needs special handling, since pointer-to-functions have special |
| 1005 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1006 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1007 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1008 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1009 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1010 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1011 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1012 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1013 | ResultStr += FPRetType->getResultType().getAsString( |
| 1014 | Context->PrintingPolicy); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1015 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1018 | ResultStr += T.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
| 1022 | std::string &ResultStr) { |
| 1023 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1024 | const FunctionType *FPRetType = 0; |
| 1025 | ResultStr += "\nstatic "; |
| 1026 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1027 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1029 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1030 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1032 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1033 | NameStr += "_I_"; |
| 1034 | else |
| 1035 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1037 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1038 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1041 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1042 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1043 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1044 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1046 | { |
| 1047 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1048 | int len = selString.size(); |
| 1049 | for (int i = 0; i < len; i++) |
| 1050 | if (selString[i] == ':') |
| 1051 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1052 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1053 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1054 | // Remember this name for metadata emission |
| 1055 | MethodInternalNames[OMD] = NameStr; |
| 1056 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1058 | // Rewrite arguments |
| 1059 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1061 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1062 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1063 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1064 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1065 | if (!LangOpts.Microsoft) { |
| 1066 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 1067 | ResultStr += "struct "; |
| 1068 | } |
| 1069 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1070 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1071 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1072 | } |
| 1073 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1074 | ResultStr += Context->getObjCClassType().getAsString( |
| 1075 | Context->PrintingPolicy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1077 | ResultStr += " self, "; |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1078 | ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1079 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1081 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1082 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1083 | E = OMD->param_end(); PI != E; ++PI) { |
| 1084 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1085 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1086 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1087 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1088 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1089 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1090 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1091 | QualType QT = PDecl->getType(); |
| 1092 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1093 | if (convertBlockPointerToFunctionPointer(QT)) |
| 1094 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
| 1095 | else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1096 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1097 | ResultStr += Name; |
| 1098 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1099 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1100 | if (OMD->isVariadic()) |
| 1101 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1102 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1104 | if (FPRetType) { |
| 1105 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1107 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1108 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1109 | ResultStr += "("; |
| 1110 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1111 | if (i) ResultStr += ", "; |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1112 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1113 | Context->PrintingPolicy); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1114 | ResultStr += ParamStr; |
| 1115 | } |
| 1116 | if (FT->isVariadic()) { |
| 1117 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1118 | ResultStr += "..."; |
| 1119 | } |
| 1120 | ResultStr += ")"; |
| 1121 | } else { |
| 1122 | ResultStr += "()"; |
| 1123 | } |
| 1124 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1125 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1126 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1127 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1128 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1130 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1132 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1133 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1134 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1135 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1136 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1137 | ObjCMethodDecl *OMD = *I; |
| 1138 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1139 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1140 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1141 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1142 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1143 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1144 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1145 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1147 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1148 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1149 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1150 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1151 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1152 | ObjCMethodDecl *OMD = *I; |
| 1153 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1154 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1155 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1157 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1158 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1159 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1160 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1161 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1162 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1164 | I != E; ++I) { |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1165 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1168 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1171 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1172 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1173 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1174 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1175 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1176 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1177 | ResultStr += "\n"; |
| 1178 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1179 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1180 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1181 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1182 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1183 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1184 | // Mark this typedef as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1185 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1186 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1187 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
| 1189 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1190 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1191 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1193 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1194 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1195 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1197 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1198 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1199 | RewriteMethodDeclaration(*I); |
| 1200 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1201 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1202 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1203 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1206 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1207 | SourceRange SrcRange) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1208 | ObjCMethodDecl *OMD = 0; |
| 1209 | QualType Ty; |
| 1210 | Selector Sel; |
| 1211 | Stmt *Receiver; |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1212 | bool Super = false; |
| 1213 | QualType SuperTy; |
| 1214 | SourceLocation SuperLocation; |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1215 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ObjCImplicitSetterGetterRefExpr. |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1216 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1217 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) { |
| 1218 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1219 | OMD = PDecl->getSetterMethodDecl(); |
| 1220 | Ty = PDecl->getType(); |
| 1221 | Sel = PDecl->getSetterName(); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1222 | Super = PropRefExpr->isSuperReceiver(); |
| 1223 | if (!Super) |
| 1224 | Receiver = PropRefExpr->getBase(); |
| 1225 | else { |
| 1226 | SuperTy = PropRefExpr->getSuperType(); |
| 1227 | SuperLocation = PropRefExpr->getSuperLocation(); |
| 1228 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1229 | } |
| 1230 | else if (ObjCImplicitSetterGetterRefExpr *ImplicitRefExpr = |
| 1231 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(BinOp->getLHS())) { |
| 1232 | OMD = ImplicitRefExpr->getSetterMethod(); |
| 1233 | Sel = OMD->getSelector(); |
| 1234 | Ty = ImplicitRefExpr->getType(); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1235 | Super = ImplicitRefExpr->isSuperReceiver(); |
| 1236 | if (!Super) |
| 1237 | Receiver = ImplicitRefExpr->getBase(); |
| 1238 | else { |
| 1239 | SuperTy = ImplicitRefExpr->getSuperType(); |
| 1240 | SuperLocation = ImplicitRefExpr->getSuperLocation(); |
| 1241 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | assert(OMD && "RewritePropertyOrImplicitSetter - null OMD"); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1245 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1246 | ExprVec.push_back(newStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1248 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1249 | if (PropGetters[Exp]) |
| 1250 | // This allows us to handle chain/nested property/implicit getters. |
| 1251 | Receiver = PropGetters[Exp]; |
| 1252 | |
| 1253 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1254 | if (Super) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1255 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1256 | Ty.getNonReferenceType(), |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1257 | /*FIXME?*/SourceLocation(), |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1258 | SuperLocation, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1259 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1260 | SuperTy, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1261 | Sel, OMD, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1262 | &ExprVec[0], 1, |
| 1263 | /*FIXME:*/SourceLocation()); |
| 1264 | else |
| 1265 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1266 | Ty.getNonReferenceType(), |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1267 | /*FIXME: */SourceLocation(), |
| 1268 | cast<Expr>(Receiver), |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1269 | Sel, OMD, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1270 | &ExprVec[0], 1, |
| 1271 | /*FIXME:*/SourceLocation()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1272 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1274 | // Now do the actual rewrite. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1275 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | df70577 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1276 | //delete BinOp; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1277 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1278 | // to things that stay around. |
| 1279 | Context->Deallocate(MsgExpr); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1280 | return ReplacingStmt; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1283 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr) { |
| 1284 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ImplicitGetter. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1285 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1286 | Stmt *Receiver; |
| 1287 | ObjCMethodDecl *OMD = 0; |
| 1288 | QualType Ty; |
| 1289 | Selector Sel; |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1290 | bool Super = false; |
| 1291 | QualType SuperTy; |
| 1292 | SourceLocation SuperLocation; |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1293 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1294 | dyn_cast<ObjCPropertyRefExpr>(PropOrGetterRefExpr)) { |
| 1295 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1296 | OMD = PDecl->getGetterMethodDecl(); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1297 | Ty = PDecl->getType(); |
| 1298 | Sel = PDecl->getGetterName(); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1299 | Super = PropRefExpr->isSuperReceiver(); |
| 1300 | if (!Super) |
| 1301 | Receiver = PropRefExpr->getBase(); |
| 1302 | else { |
| 1303 | SuperTy = PropRefExpr->getSuperType(); |
| 1304 | SuperLocation = PropRefExpr->getSuperLocation(); |
| 1305 | } |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1306 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1307 | else if (ObjCImplicitSetterGetterRefExpr *ImplicitRefExpr = |
| 1308 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(PropOrGetterRefExpr)) { |
| 1309 | OMD = ImplicitRefExpr->getGetterMethod(); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1310 | Sel = OMD->getSelector(); |
| 1311 | Ty = ImplicitRefExpr->getType(); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1312 | Super = ImplicitRefExpr->isSuperReceiver(); |
| 1313 | if (!Super) |
| 1314 | Receiver = ImplicitRefExpr->getBase(); |
| 1315 | else { |
| 1316 | SuperTy = ImplicitRefExpr->getSuperType(); |
| 1317 | SuperLocation = ImplicitRefExpr->getSuperLocation(); |
| 1318 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | assert (OMD && "RewritePropertyOrImplicitGetter - OMD is null"); |
| 1322 | |
| 1323 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1324 | if (PropGetters[Exp]) |
| 1325 | // This allows us to handle chain/nested property/implicit getters. |
| 1326 | Receiver = PropGetters[Exp]; |
| 1327 | |
| 1328 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1329 | if (Super) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1330 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1331 | Ty.getNonReferenceType(), |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1332 | /*FIXME?*/SourceLocation(), |
| 1333 | SuperLocation, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1334 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame^] | 1335 | SuperTy, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1336 | Sel, OMD, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1337 | 0, 0, |
| 1338 | /*FIXME:*/SourceLocation()); |
| 1339 | else |
| 1340 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1341 | Ty.getNonReferenceType(), |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1342 | /*FIXME:*/SourceLocation(), |
| 1343 | cast<Expr>(Receiver), |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1344 | Sel, OMD, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1345 | 0, 0, |
| 1346 | /*FIXME:*/SourceLocation()); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1347 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1348 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (!PropParentMap) |
| 1351 | PropParentMap = new ParentMap(CurrentBody); |
| 1352 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1353 | Stmt *Parent = PropParentMap->getParent(PropOrGetterRefExpr); |
| 1354 | if (Parent && (isa<ObjCPropertyRefExpr>(Parent) || |
| 1355 | isa<ObjCImplicitSetterGetterRefExpr>(Parent))) { |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1356 | // We stash away the ReplacingStmt since actually doing the |
| 1357 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1358 | PropGetters[PropOrGetterRefExpr] = ReplacingStmt; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1359 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1360 | // to things that stay around. |
| 1361 | Context->Deallocate(MsgExpr); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1362 | return PropOrGetterRefExpr; // return the original... |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1363 | } else { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1364 | ReplaceStmt(PropOrGetterRefExpr, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1366 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1367 | // to things that stay around. |
| 1368 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1369 | return ReplacingStmt; |
| 1370 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1374 | SourceLocation OrigStart, |
| 1375 | bool &replaced) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1376 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1377 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1378 | if (CurMethodDef) { |
Fariborz Jahanian | f9e8c2b | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1379 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1380 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1381 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | f0ed69c | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1382 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1383 | // lookup which class implements the instance variable. |
| 1384 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1385 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1386 | clsDeclared); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1387 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1389 | // Synthesize an explicit cast to gain access to the ivar. |
| 1390 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1391 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1392 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1393 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1394 | SourceLocation(), II); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1395 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1396 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1397 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1398 | CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1399 | IV->getBase()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1400 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1401 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1402 | IV->getBase()->getLocEnd(), |
| 1403 | castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1404 | replaced = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | if (IV->isFreeIvar() && |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1406 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1407 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1408 | IV->getLocation(), |
| 1409 | D->getType()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1410 | // delete IV; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1411 | return ME; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1412 | } |
Fariborz Jahanian | 8131081 | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1413 | // Get the new text |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1414 | // Cannot delete IV->getBase(), since PE points to it. |
| 1415 | // Replace the old base with the cast. This is important when doing |
| 1416 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | IV->setBase(PE); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1418 | return IV; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1419 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1420 | } else { // we are outside a method. |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1421 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1423 | // Explicit ivar refs need to have a cast inserted. |
| 1424 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 12e2e86 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1425 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | 9146e44 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1426 | ObjCInterfaceType *iFaceDecl = |
| 1427 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1428 | // lookup which class implements the instance variable. |
| 1429 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1431 | clsDeclared); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1432 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1434 | // Synthesize an explicit cast to gain access to the ivar. |
| 1435 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1436 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1437 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1438 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1439 | SourceLocation(), II); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1440 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1441 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1442 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1443 | CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1444 | IV->getBase()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1445 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1446 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 34873d2 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1447 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1448 | replaced = true; |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1449 | // Cannot delete IV->getBase(), since PE points to it. |
| 1450 | // Replace the old base with the cast. This is important when doing |
| 1451 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | IV->setBase(PE); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1453 | return IV; |
| 1454 | } |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1455 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1456 | return IV; |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1459 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1460 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1461 | CI != E; ++CI) { |
| 1462 | if (*CI) { |
| 1463 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1464 | if (newStmt) |
| 1465 | *CI = newStmt; |
| 1466 | } |
| 1467 | } |
| 1468 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1469 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1470 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1471 | replaced); |
| 1472 | return newStmt; |
Fariborz Jahanian | 3143338 | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1473 | } |
| 1474 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1475 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1476 | return newStmt; |
| 1477 | } |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1478 | return S; |
| 1479 | } |
| 1480 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1481 | /// SynthCountByEnumWithState - To print: |
| 1482 | /// ((unsigned int (*) |
| 1483 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1485 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | /// "countByEnumeratingWithState:objects:count:"), |
| 1487 | /// &enumState, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1488 | /// (id *)items, (unsigned int)16) |
| 1489 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1490 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1491 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1492 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1493 | buf += "\n\t\t"; |
| 1494 | buf += "((id)l_collection,\n\t\t"; |
| 1495 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1496 | buf += "\n\t\t"; |
| 1497 | buf += "&enumState, " |
| 1498 | "(id *)items, (unsigned int)16)"; |
| 1499 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1500 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1501 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1502 | /// statement to exit to its outer synthesized loop. |
| 1503 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1504 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1505 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1506 | return S; |
| 1507 | // replace break with goto __break_label |
| 1508 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1510 | SourceLocation startLoc = S->getLocStart(); |
| 1511 | buf = "goto __break_label_"; |
| 1512 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1513 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1519 | /// statement to continue with its inner synthesized loop. |
| 1520 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1521 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1522 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1523 | return S; |
| 1524 | // replace continue with goto __continue_label |
| 1525 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1527 | SourceLocation startLoc = S->getLocStart(); |
| 1528 | buf = "goto __continue_label_"; |
| 1529 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1530 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1535 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1536 | /// It rewrites: |
| 1537 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1539 | /// Into: |
| 1540 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1542 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1543 | /// id items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1544 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1546 | /// objects:items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1547 | /// if (limit) { |
| 1548 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1549 | /// do { |
| 1550 | /// unsigned long counter = 0; |
| 1551 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1553 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1554 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1555 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1556 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1557 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1559 | /// objects:items count:16]); |
| 1560 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1561 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1562 | /// } |
| 1563 | /// else |
| 1564 | /// elem = nil; |
| 1565 | /// } |
| 1566 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1567 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1568 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1569 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1571 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1573 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1575 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1576 | const char *startBuf = SM->getCharacterData(startLoc); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1577 | llvm::StringRef elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1578 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1579 | std::string buf; |
| 1580 | buf = "\n{\n\t"; |
| 1581 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1582 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1583 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1584 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1585 | if (ElementType->isObjCQualifiedIdType() || |
| 1586 | ElementType->isObjCQualifiedInterfaceType()) |
| 1587 | // Simply use 'id' for all qualified types. |
| 1588 | elementTypeAsString = "id"; |
| 1589 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1590 | elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1591 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1592 | buf += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1593 | elementName = D->getName(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1594 | buf += elementName; |
| 1595 | buf += ";\n\t"; |
| 1596 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1597 | else { |
| 1598 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1599 | elementName = DR->getDecl()->getName(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1600 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1601 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1602 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1603 | // Simply use 'id' for all qualified types. |
| 1604 | elementTypeAsString = "id"; |
| 1605 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1606 | elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1607 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1609 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1610 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1611 | // id items[16]; |
| 1612 | buf += "id items[16];\n\t"; |
| 1613 | // id l_collection = (id) |
| 1614 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1615 | // Find start location of 'collection' the hard way! |
| 1616 | const char *startCollectionBuf = startBuf; |
| 1617 | startCollectionBuf += 3; // skip 'for' |
| 1618 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1619 | startCollectionBuf++; // skip '(' |
| 1620 | // find 'in' and skip it. |
| 1621 | while (*startCollectionBuf != ' ' || |
| 1622 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1623 | (*(startCollectionBuf+3) != ' ' && |
| 1624 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1625 | startCollectionBuf++; |
| 1626 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
| 1628 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1629 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1630 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1631 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1632 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1633 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1634 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1636 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1637 | // objects:items count:16]; |
| 1638 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1640 | // ((unsigned int (*) |
| 1641 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1643 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | // "countByEnumeratingWithState:objects:count:"), |
| 1645 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1646 | // (id *)items, (unsigned int)16); |
| 1647 | buf += "unsigned long limit =\n\t\t"; |
| 1648 | SynthCountByEnumWithState(buf); |
| 1649 | buf += ";\n\t"; |
| 1650 | /// if (limit) { |
| 1651 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1652 | /// do { |
| 1653 | /// unsigned long counter = 0; |
| 1654 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1656 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1657 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1658 | buf += "if (limit) {\n\t"; |
| 1659 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1660 | buf += "do {\n\t\t"; |
| 1661 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1662 | buf += "do {\n\t\t\t"; |
| 1663 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1664 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1665 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1666 | buf += " = ("; |
| 1667 | buf += elementTypeAsString; |
| 1668 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1669 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1670 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1672 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1673 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1675 | /// objects:items count:16]); |
| 1676 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1677 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1678 | /// } |
| 1679 | /// else |
| 1680 | /// elem = nil; |
| 1681 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1683 | buf = ";\n\t"; |
| 1684 | buf += "__continue_label_"; |
| 1685 | buf += utostr(ObjCBcLabelNo.back()); |
| 1686 | buf += ": ;"; |
| 1687 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1688 | buf += "} while (counter < limit);\n\t"; |
| 1689 | buf += "} while (limit = "; |
| 1690 | SynthCountByEnumWithState(buf); |
| 1691 | buf += ");\n\t"; |
| 1692 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1693 | buf += " = (("; |
| 1694 | buf += elementTypeAsString; |
| 1695 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1696 | buf += "__break_label_"; |
| 1697 | buf += utostr(ObjCBcLabelNo.back()); |
| 1698 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1699 | buf += "}\n\t"; |
| 1700 | buf += "else\n\t\t"; |
| 1701 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1702 | buf += " = (("; |
| 1703 | buf += elementTypeAsString; |
| 1704 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1705 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1707 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1708 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1709 | if (isa<CompoundStmt>(S->getBody())) { |
| 1710 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1711 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1712 | } else { |
| 1713 | /* Need to treat single statements specially. For example: |
| 1714 | * |
| 1715 | * for (A *a in b) if (stuff()) break; |
| 1716 | * for (A *a in b) xxxyy; |
| 1717 | * |
| 1718 | * The following code simply scans ahead to the semi to find the actual end. |
| 1719 | */ |
| 1720 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1721 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1722 | assert(semiBuf && "Can't find ';'"); |
| 1723 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1724 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1725 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1726 | Stmts.pop_back(); |
| 1727 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1728 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1732 | /// This routine rewrites @synchronized(expr) stmt; |
| 1733 | /// into: |
| 1734 | /// objc_sync_enter(expr); |
| 1735 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1736 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1737 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1738 | // Get the start location and compute the semi location. |
| 1739 | SourceLocation startLoc = S->getLocStart(); |
| 1740 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1742 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
| 1744 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1745 | buf = "objc_sync_enter((id)"; |
| 1746 | const char *lparenBuf = startBuf; |
| 1747 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1748 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1750 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1751 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1752 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1753 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1754 | while (*endBuf != ')') endBuf--; |
| 1755 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1756 | buf = ");\n"; |
| 1757 | // declare a new scope with two variables, _stack and _rethrow. |
| 1758 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1759 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1760 | buf += "char *pointers[4];} _stack;\n"; |
| 1761 | buf += "id volatile _rethrow = 0;\n"; |
| 1762 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1763 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1764 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1765 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1766 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1768 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1769 | SourceLocation lastCurlyLoc = startLoc; |
| 1770 | buf = "}\nelse {\n"; |
| 1771 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1772 | buf += "}\n"; |
| 1773 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1774 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1775 | |
| 1776 | std::string syncBuf; |
| 1777 | syncBuf += " objc_sync_exit("; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1778 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1779 | CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1780 | S->getSynchExpr()); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1781 | std::string syncExprBufS; |
| 1782 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1783 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1784 | PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1785 | syncBuf += syncExprBuf.str(); |
| 1786 | syncBuf += ");"; |
| 1787 | |
| 1788 | buf += syncBuf; |
| 1789 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1790 | buf += "}\n"; |
| 1791 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1793 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1794 | |
| 1795 | bool hasReturns = false; |
| 1796 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1797 | if (hasReturns) |
| 1798 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1799 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
| 1802 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1803 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1804 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1805 | // Perform a bottom up traversal of all children. |
| 1806 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1807 | CI != E; ++CI) |
| 1808 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1809 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1810 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1811 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1813 | TryFinallyContainsReturnDiag); |
| 1814 | } |
| 1815 | return; |
| 1816 | } |
| 1817 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1818 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1819 | { |
| 1820 | // Perform a bottom up traversal of all children. |
| 1821 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1822 | CI != E; ++CI) |
| 1823 | if (*CI) |
| 1824 | HasReturnStmts(*CI, hasReturns); |
| 1825 | |
| 1826 | if (isa<ReturnStmt>(S)) |
| 1827 | hasReturns = true; |
| 1828 | return; |
| 1829 | } |
| 1830 | |
| 1831 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1832 | // Perform a bottom up traversal of all children. |
| 1833 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1834 | CI != E; ++CI) |
| 1835 | if (*CI) { |
| 1836 | RewriteTryReturnStmts(*CI); |
| 1837 | } |
| 1838 | if (isa<ReturnStmt>(S)) { |
| 1839 | SourceLocation startLoc = S->getLocStart(); |
| 1840 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1841 | |
| 1842 | const char *semiBuf = strchr(startBuf, ';'); |
| 1843 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1844 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1845 | |
| 1846 | std::string buf; |
| 1847 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1848 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1849 | ReplaceText(startLoc, 6, buf); |
| 1850 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1851 | } |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1856 | // Perform a bottom up traversal of all children. |
| 1857 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1858 | CI != E; ++CI) |
| 1859 | if (*CI) { |
| 1860 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1861 | } |
| 1862 | if (isa<ReturnStmt>(S)) { |
| 1863 | SourceLocation startLoc = S->getLocStart(); |
| 1864 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1865 | |
| 1866 | const char *semiBuf = strchr(startBuf, ';'); |
| 1867 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1868 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1869 | |
| 1870 | std::string buf; |
| 1871 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1872 | buf += syncExitBuf; |
| 1873 | buf += " return"; |
| 1874 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1875 | ReplaceText(startLoc, 6, buf); |
| 1876 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1877 | } |
| 1878 | return; |
| 1879 | } |
| 1880 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1881 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1882 | // Get the start location and compute the semi location. |
| 1883 | SourceLocation startLoc = S->getLocStart(); |
| 1884 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1886 | assert((*startBuf == '@') && "bogus @try location"); |
| 1887 | |
| 1888 | std::string buf; |
| 1889 | // declare a new scope with two variables, _stack and _rethrow. |
| 1890 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1891 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1892 | buf += "char *pointers[4];} _stack;\n"; |
| 1893 | buf += "id volatile _rethrow = 0;\n"; |
| 1894 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1895 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1896 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1897 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1899 | startLoc = S->getTryBody()->getLocEnd(); |
| 1900 | startBuf = SM->getCharacterData(startLoc); |
| 1901 | |
| 1902 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1904 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1905 | if (S->getNumCatchStmts()) { |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1906 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1907 | buf = " /* @catch begin */ else {\n"; |
| 1908 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1909 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1910 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1911 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1912 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1914 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1915 | } else { /* no catch list */ |
| 1916 | buf = "}\nelse {\n"; |
| 1917 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1918 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1919 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1920 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1921 | bool sawIdTypedCatch = false; |
| 1922 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1923 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1924 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1925 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1927 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1928 | buf = "if ("; // we are generating code for the first catch clause |
| 1929 | else |
| 1930 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1931 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1932 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1934 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1936 | const char *lParenLoc = strchr(startBuf, '('); |
| 1937 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1938 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1939 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1940 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1941 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1942 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1943 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1944 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1945 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1947 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1948 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1949 | } else if (catchDecl) { |
| 1950 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1951 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1952 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1953 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1954 | sawIdTypedCatch = true; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1955 | } else if (const ObjCObjectPointerType *Ptr = |
| 1956 | t->getAs<ObjCObjectPointerType>()) { |
| 1957 | // Should be a pointer to a class. |
| 1958 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1959 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1960 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1961 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1962 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1963 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1967 | lastCatchBody = Catch->getCatchBody(); |
| 1968 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1969 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1970 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1971 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1972 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1973 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1976 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1977 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1978 | } else { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1979 | assert(false && "@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1980 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1981 | } |
| 1982 | // Complete the catch list... |
| 1983 | if (lastCatchBody) { |
| 1984 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1985 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1986 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1988 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1989 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1990 | buf = "} /* last catch end */\n"; |
| 1991 | buf += "else {\n"; |
| 1992 | buf += " _rethrow = _caught;\n"; |
| 1993 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1994 | buf += "} } /* @catch end */\n"; |
| 1995 | if (!S->getFinallyStmt()) |
| 1996 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1997 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1999 | // Set lastCurlyLoc |
| 2000 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 2001 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2002 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2003 | startLoc = finalStmt->getLocStart(); |
| 2004 | startBuf = SM->getCharacterData(startLoc); |
| 2005 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2007 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2009 | Stmt *body = finalStmt->getFinallyBody(); |
| 2010 | SourceLocation startLoc = body->getLocStart(); |
| 2011 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2012 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 2013 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2015 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2017 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2018 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2019 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2020 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2022 | // Set lastCurlyLoc |
| 2023 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 2025 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2026 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2027 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 2028 | buf = "{ /* implicit finally clause */\n"; |
| 2029 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 2030 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 2031 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2032 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2033 | |
| 2034 | // Now check for any return/continue/go statements within the @try. |
| 2035 | // The implicit finally clause won't called if the @try contains any |
| 2036 | // jump statements. |
| 2037 | bool hasReturns = false; |
| 2038 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2039 | if (hasReturns) |
| 2040 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2041 | } |
| 2042 | // Now emit the final closing curly brace... |
| 2043 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2044 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2045 | return 0; |
| 2046 | } |
| 2047 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2049 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2050 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2051 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2052 | // Get the start location and compute the semi location. |
| 2053 | SourceLocation startLoc = S->getLocStart(); |
| 2054 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2056 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2057 | |
| 2058 | std::string buf; |
| 2059 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2060 | if (S->getThrowExpr()) |
| 2061 | buf = "objc_exception_throw("; |
| 2062 | else // add an implicit argument |
| 2063 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2065 | // handle "@ throw" correctly. |
| 2066 | const char *wBuf = strchr(startBuf, 'w'); |
| 2067 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2068 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2070 | const char *semiBuf = strchr(startBuf, ';'); |
| 2071 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2072 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2073 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2074 | return 0; |
| 2075 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2076 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2077 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2078 | // Create a new string expression. |
| 2079 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2080 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2081 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2082 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 2083 | StrEncoding.length(), false,StrType, |
| 2084 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2085 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2087 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2088 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2089 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2092 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2093 | if (!SelGetUidFunctionDecl) |
| 2094 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2095 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2096 | // Create a call to sel_registerName("selName"). |
| 2097 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2098 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2100 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2101 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2102 | false, argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2103 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2104 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2105 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2106 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2107 | return SelExp; |
| 2108 | } |
| 2109 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2110 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2111 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2112 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2113 | // 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] | 2114 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2116 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2117 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2119 | // 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] | 2120 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2121 | ImplicitCastExpr *ICE = |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2122 | ImplicitCastExpr::Create(*Context, pToFunc, CK_Unknown, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2123 | DRE, 0, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2125 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2127 | CallExpr *Exp = |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2128 | new (Context) CallExpr(*Context, ICE, args, nargs, |
| 2129 | FT->getCallResultType(*Context), EndLoc); |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2130 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2133 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2134 | const char *&startRef, const char *&endRef) { |
| 2135 | while (startBuf < endBuf) { |
| 2136 | if (*startBuf == '<') |
| 2137 | startRef = startBuf; // mark the start. |
| 2138 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2139 | if (startRef && *startRef == '<') { |
| 2140 | endRef = startBuf; // mark the end. |
| 2141 | return true; |
| 2142 | } |
| 2143 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2144 | } |
| 2145 | startBuf++; |
| 2146 | } |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2150 | static void scanToNextArgument(const char *&argRef) { |
| 2151 | int angle = 0; |
| 2152 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2153 | if (*argRef == '<') |
| 2154 | angle++; |
| 2155 | else if (*argRef == '>') |
| 2156 | angle--; |
| 2157 | argRef++; |
| 2158 | } |
| 2159 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2160 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2161 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2162 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2163 | if (T->isObjCQualifiedIdType()) |
| 2164 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2165 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2166 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2167 | return true; |
| 2168 | } |
| 2169 | if (T->isObjCObjectPointerType()) { |
| 2170 | T = T->getPointeeType(); |
| 2171 | return T->isObjCQualifiedInterfaceType(); |
| 2172 | } |
Fariborz Jahanian | 7bf13c4 | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2173 | if (T->isArrayType()) { |
| 2174 | QualType ElemTy = Context->getBaseElementType(T); |
| 2175 | return needToScanForQualifiers(ElemTy); |
| 2176 | } |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2177 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2180 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2181 | QualType Type = E->getType(); |
| 2182 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2183 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2185 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2186 | Loc = ECE->getLParenLoc(); |
| 2187 | EndLoc = ECE->getRParenLoc(); |
| 2188 | } else { |
| 2189 | Loc = E->getLocStart(); |
| 2190 | EndLoc = E->getLocEnd(); |
| 2191 | } |
| 2192 | // This will defend against trying to rewrite synthesized expressions. |
| 2193 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2194 | return; |
| 2195 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2196 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2197 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2198 | const char *startRef = 0, *endRef = 0; |
| 2199 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2200 | // Get the locations of the startRef, endRef. |
| 2201 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2202 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2203 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2204 | InsertText(LessLoc, "/*"); |
| 2205 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2210 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2211 | SourceLocation Loc; |
| 2212 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2213 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2214 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2215 | Loc = VD->getLocation(); |
| 2216 | Type = VD->getType(); |
| 2217 | } |
| 2218 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2219 | Loc = FD->getLocation(); |
| 2220 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2221 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2222 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2223 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2224 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2225 | if (!proto) |
| 2226 | return; |
| 2227 | Type = proto->getResultType(); |
| 2228 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2229 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2230 | Loc = FD->getLocation(); |
| 2231 | Type = FD->getType(); |
| 2232 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2233 | else |
| 2234 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2236 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2237 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2239 | const char *endBuf = SM->getCharacterData(Loc); |
| 2240 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2241 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2242 | startBuf--; // scan backward (from the decl location) for return type. |
| 2243 | const char *startRef = 0, *endRef = 0; |
| 2244 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2245 | // Get the locations of the startRef, endRef. |
| 2246 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2247 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2248 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2249 | InsertText(LessLoc, "/*"); |
| 2250 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2251 | } |
| 2252 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2253 | if (!proto) |
| 2254 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2255 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2256 | const char *startBuf = SM->getCharacterData(Loc); |
| 2257 | const char *startFuncBuf = startBuf; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2258 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2259 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2260 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2262 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2263 | // scan forward (from the decl location) for argument types. |
| 2264 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2265 | const char *startRef = 0, *endRef = 0; |
| 2266 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2267 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | SourceLocation LessLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2269 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2271 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2272 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2273 | InsertText(LessLoc, "/*"); |
| 2274 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2275 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2276 | startBuf = ++endBuf; |
| 2277 | } |
| 2278 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2279 | // If the function name is derived from a macro expansion, then the |
| 2280 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2281 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2282 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2283 | startBuf++; |
| 2284 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2285 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2288 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2289 | QualType QT = ND->getType(); |
| 2290 | const Type* TypePtr = QT->getAs<Type>(); |
| 2291 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2292 | return; |
| 2293 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2294 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2295 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2296 | TypePtr = QT->getAs<Type>(); |
| 2297 | } |
| 2298 | // FIXME. This will not work for multiple declarators; as in: |
| 2299 | // __typeof__(a) b,c,d; |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2300 | std::string TypeAsString(QT.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2301 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2302 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2303 | if (ND->getInit()) { |
| 2304 | std::string Name(ND->getNameAsString()); |
| 2305 | TypeAsString += " " + Name + " = "; |
| 2306 | Expr *E = ND->getInit(); |
| 2307 | SourceLocation startLoc; |
| 2308 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2309 | startLoc = ECE->getLParenLoc(); |
| 2310 | else |
| 2311 | startLoc = E->getLocStart(); |
| 2312 | startLoc = SM->getInstantiationLoc(startLoc); |
| 2313 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2314 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2315 | } |
| 2316 | else { |
| 2317 | SourceLocation X = ND->getLocEnd(); |
| 2318 | X = SM->getInstantiationLoc(X); |
| 2319 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2320 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2324 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2325 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2326 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2327 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2328 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2329 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2330 | &ArgTys[0], ArgTys.size(), |
| 2331 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2332 | false, false, 0, 0, |
| 2333 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2334 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2336 | SelGetUidIdent, getFuncType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2337 | SC_Extern, |
| 2338 | SC_None, false); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2341 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2342 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2343 | if (FD->getIdentifier() && |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2344 | FD->getName() == "sel_registerName") { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2345 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2346 | return; |
| 2347 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2348 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2351 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2352 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2353 | const char *argPtr = TypeString.c_str(); |
| 2354 | if (!strchr(argPtr, '^')) { |
| 2355 | Str += TypeString; |
| 2356 | return; |
| 2357 | } |
| 2358 | while (*argPtr) { |
| 2359 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2360 | argPtr++; |
| 2361 | } |
| 2362 | } |
| 2363 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2364 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2365 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2366 | ValueDecl *VD) { |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2367 | QualType Type = VD->getType(); |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2368 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2369 | const char *argPtr = TypeString.c_str(); |
| 2370 | int paren = 0; |
| 2371 | while (*argPtr) { |
| 2372 | switch (*argPtr) { |
| 2373 | case '(': |
| 2374 | Str += *argPtr; |
| 2375 | paren++; |
| 2376 | break; |
| 2377 | case ')': |
| 2378 | Str += *argPtr; |
| 2379 | paren--; |
| 2380 | break; |
| 2381 | case '^': |
| 2382 | Str += '*'; |
| 2383 | if (paren == 1) |
| 2384 | Str += VD->getNameAsString(); |
| 2385 | break; |
| 2386 | default: |
| 2387 | Str += *argPtr; |
| 2388 | break; |
| 2389 | } |
| 2390 | argPtr++; |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2395 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2396 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2397 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2398 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2399 | if (!proto) |
| 2400 | return; |
| 2401 | QualType Type = proto->getResultType(); |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2402 | std::string FdStr = Type.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2403 | FdStr += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2404 | FdStr += FD->getName(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2405 | FdStr += "("; |
| 2406 | unsigned numArgs = proto->getNumArgs(); |
| 2407 | for (unsigned i = 0; i < numArgs; i++) { |
| 2408 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2409 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2410 | if (i+1 < numArgs) |
| 2411 | FdStr += ", "; |
| 2412 | } |
| 2413 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2414 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2415 | CurFunctionDeclToDeclareForBlock = 0; |
| 2416 | } |
| 2417 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2418 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2419 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2420 | if (SuperContructorFunctionDecl) |
| 2421 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2422 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2423 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2424 | QualType argT = Context->getObjCIdType(); |
| 2425 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2426 | ArgTys.push_back(argT); |
| 2427 | ArgTys.push_back(argT); |
| 2428 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2429 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2430 | false, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2431 | false, false, 0, 0, |
| 2432 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2433 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2435 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2436 | SC_Extern, |
| 2437 | SC_None, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2440 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2441 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2442 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2443 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2444 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2445 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2446 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2447 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2448 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2449 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2450 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2451 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2452 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2453 | false, false, 0, 0, |
| 2454 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2455 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2456 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2457 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2458 | SC_Extern, |
| 2459 | SC_None, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2462 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2463 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2464 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2465 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2466 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2467 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2468 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2469 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2470 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2471 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2472 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2473 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2474 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2475 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2476 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2477 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2478 | false, false, 0, 0, |
| 2479 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2480 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2482 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2483 | SC_Extern, |
| 2484 | SC_None, false); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2487 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2488 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2489 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2490 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2491 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2492 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2493 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2494 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2495 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2496 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2497 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2498 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2499 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2500 | false, false, 0, 0, |
| 2501 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2502 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2504 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2505 | SC_Extern, |
| 2506 | SC_None, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2510 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2511 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2513 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2514 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2515 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2516 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2517 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2518 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2519 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2520 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2521 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2522 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2523 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2524 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2525 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2526 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2527 | false, false, 0, 0, |
| 2528 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2529 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2531 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2532 | SC_Extern, |
| 2533 | SC_None, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2536 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2537 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2538 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2539 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2540 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2541 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2542 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2543 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2544 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2545 | ArgTys.push_back(argT); |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2546 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2547 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2548 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2549 | false, false, 0, 0, |
| 2550 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2551 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2553 | msgSendIdent, msgSendType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2554 | SC_Extern, |
| 2555 | SC_None, false); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2558 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2559 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2560 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2561 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2562 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2563 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2564 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2565 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2566 | false, false, 0, 0, |
| 2567 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2568 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2570 | getClassIdent, getClassType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2571 | SC_Extern, |
| 2572 | SC_None, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2575 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2576 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2577 | IdentifierInfo *getSuperClassIdent = |
| 2578 | &Context->Idents.get("class_getSuperclass"); |
| 2579 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2580 | ArgTys.push_back(Context->getObjCClassType()); |
| 2581 | QualType getClassType = Context->getFunctionType(Context->getObjCClassType(), |
| 2582 | &ArgTys[0], ArgTys.size(), |
| 2583 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2584 | false, false, 0, 0, |
| 2585 | FunctionType::ExtInfo()); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2586 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2587 | SourceLocation(), |
| 2588 | getSuperClassIdent, |
| 2589 | getClassType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2590 | SC_Extern, |
| 2591 | SC_None, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2592 | false); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2595 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2596 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2597 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2598 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2599 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2600 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2601 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2602 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2603 | false, false, 0, 0, |
| 2604 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2605 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2607 | getClassIdent, getClassType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2608 | SC_Extern, |
| 2609 | SC_None, false); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2612 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2613 | QualType strType = getConstantStringStructType(); |
| 2614 | |
| 2615 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2616 | |
| 2617 | std::string tmpName = InFileName; |
| 2618 | unsigned i; |
| 2619 | for (i=0; i < tmpName.length(); i++) { |
| 2620 | char c = tmpName.at(i); |
| 2621 | // replace any non alphanumeric characters with '_'. |
| 2622 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2623 | tmpName[i] = '_'; |
| 2624 | } |
| 2625 | S += tmpName; |
| 2626 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2627 | S += utostr(NumObjCStringLiterals++); |
| 2628 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2629 | Preamble += "static __NSConstantStringImpl " + S; |
| 2630 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2631 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2632 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2633 | std::string prettyBufS; |
| 2634 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2635 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2636 | PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2637 | Preamble += prettyBuf.str(); |
| 2638 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2639 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | |
| 2641 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2642 | &Context->Idents.get(S), strType, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2643 | SC_Static, SC_None); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2644 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2645 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | Context->getPointerType(DRE->getType()), |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2647 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2648 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2649 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2650 | CK_Unknown, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2651 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2652 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2653 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2656 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2657 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2658 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2659 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2661 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2662 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2664 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2666 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2668 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2669 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2670 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2672 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2673 | FieldTypes[i], 0, |
| 2674 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2675 | /*Mutable=*/false)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2678 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2679 | } |
| 2680 | return Context->getTagDeclType(SuperStructDecl); |
| 2681 | } |
| 2682 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2683 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2684 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2685 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2687 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2688 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2690 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2692 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2694 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2696 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2698 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2699 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2700 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2702 | ConstantStringDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2703 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2704 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2705 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2706 | /*Mutable=*/true)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2709 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2710 | } |
| 2711 | return Context->getTagDeclType(ConstantStringDecl); |
| 2712 | } |
| 2713 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2714 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2715 | SourceLocation StartLoc, |
| 2716 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2717 | if (!SelGetUidFunctionDecl) |
| 2718 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2719 | if (!MsgSendFunctionDecl) |
| 2720 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2721 | if (!MsgSendSuperFunctionDecl) |
| 2722 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2723 | if (!MsgSendStretFunctionDecl) |
| 2724 | SynthMsgSendStretFunctionDecl(); |
| 2725 | if (!MsgSendSuperStretFunctionDecl) |
| 2726 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2727 | if (!MsgSendFpretFunctionDecl) |
| 2728 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2729 | if (!GetClassFunctionDecl) |
| 2730 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2731 | if (!GetSuperClassFunctionDecl) |
| 2732 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2733 | if (!GetMetaClassFunctionDecl) |
| 2734 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2736 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2737 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2738 | // May need to use objc_msgSend_stret() as well. |
| 2739 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2740 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2741 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2742 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2743 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2744 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2745 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2748 | // Synthesize a call to objc_msgSend(). |
| 2749 | llvm::SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2750 | switch (Exp->getReceiverKind()) { |
| 2751 | case ObjCMessageExpr::SuperClass: { |
| 2752 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2753 | if (MsgSendStretFlavor) |
| 2754 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2755 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2757 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2759 | llvm::SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2761 | // set the receiver to self, the first argument to all methods. |
| 2762 | InitExprs.push_back( |
| 2763 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2764 | CK_Unknown, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2765 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2766 | Context->getObjCIdType(), |
| 2767 | SourceLocation())) |
| 2768 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2770 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2771 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2772 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2773 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2774 | ClassDecl->getIdentifier()->getNameStart(), |
| 2775 | ClassDecl->getIdentifier()->getLength(), |
| 2776 | false, argType, SourceLocation())); |
| 2777 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2778 | &ClsExprs[0], |
| 2779 | ClsExprs.size(), |
| 2780 | StartLoc, |
| 2781 | EndLoc); |
| 2782 | // (Class)objc_getClass("CurrentClass") |
| 2783 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2784 | Context->getObjCClassType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2785 | CK_Unknown, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2786 | ClsExprs.clear(); |
| 2787 | ClsExprs.push_back(ArgExpr); |
| 2788 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2789 | &ClsExprs[0], ClsExprs.size(), |
| 2790 | StartLoc, EndLoc); |
| 2791 | |
| 2792 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2793 | // To turn off a warning, type-cast to 'id' |
| 2794 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2795 | NoTypeInfoCStyleCastExpr(Context, |
| 2796 | Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2797 | CK_Unknown, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2798 | // struct objc_super |
| 2799 | QualType superType = getSuperStructType(); |
| 2800 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2802 | if (LangOpts.Microsoft) { |
| 2803 | SynthSuperContructorFunctionDecl(); |
| 2804 | // Simulate a contructor call... |
| 2805 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2806 | superType, SourceLocation()); |
| 2807 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2808 | InitExprs.size(), |
| 2809 | superType, SourceLocation()); |
| 2810 | // The code for super is a little tricky to prevent collision with |
| 2811 | // the structure definition in the header. The rewriter has it's own |
| 2812 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2813 | // we need the cast below. For example: |
| 2814 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2815 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2816 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2817 | Context->getPointerType(SuperRep->getType()), |
| 2818 | SourceLocation()); |
| 2819 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2820 | Context->getPointerType(superType), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2821 | CK_Unknown, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2822 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2823 | // (struct objc_super) { <exprs from above> } |
| 2824 | InitListExpr *ILE = |
| 2825 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2826 | &InitExprs[0], InitExprs.size(), |
| 2827 | SourceLocation()); |
| 2828 | TypeSourceInfo *superTInfo |
| 2829 | = Context->getTrivialTypeSourceInfo(superType); |
| 2830 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2831 | superType, ILE, false); |
| 2832 | // struct objc_super * |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2833 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2834 | Context->getPointerType(SuperRep->getType()), |
| 2835 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2836 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2837 | MsgExprs.push_back(SuperRep); |
| 2838 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2839 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2840 | |
| 2841 | case ObjCMessageExpr::Class: { |
| 2842 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2843 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2844 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2845 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2846 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2847 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2848 | clsName->getNameStart(), |
| 2849 | clsName->getLength(), |
| 2850 | false, argType, |
| 2851 | SourceLocation())); |
| 2852 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2853 | &ClsExprs[0], |
| 2854 | ClsExprs.size(), |
| 2855 | StartLoc, EndLoc); |
| 2856 | MsgExprs.push_back(Cls); |
| 2857 | break; |
| 2858 | } |
| 2859 | |
| 2860 | case ObjCMessageExpr::SuperInstance:{ |
| 2861 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2862 | if (MsgSendStretFlavor) |
| 2863 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2864 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2865 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 2866 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2867 | |
| 2868 | InitExprs.push_back( |
| 2869 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2870 | CK_Unknown, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2871 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2872 | Context->getObjCIdType(), |
| 2873 | SourceLocation())) |
| 2874 | ); // set the 'receiver'. |
| 2875 | |
| 2876 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2877 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2878 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2879 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2880 | ClassDecl->getIdentifier()->getNameStart(), |
| 2881 | ClassDecl->getIdentifier()->getLength(), |
| 2882 | false, argType, SourceLocation())); |
| 2883 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2884 | &ClsExprs[0], |
| 2885 | ClsExprs.size(), |
| 2886 | StartLoc, EndLoc); |
| 2887 | // (Class)objc_getClass("CurrentClass") |
| 2888 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2889 | Context->getObjCClassType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2890 | CK_Unknown, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2891 | ClsExprs.clear(); |
| 2892 | ClsExprs.push_back(ArgExpr); |
| 2893 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2894 | &ClsExprs[0], ClsExprs.size(), |
| 2895 | StartLoc, EndLoc); |
| 2896 | |
| 2897 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2898 | // To turn off a warning, type-cast to 'id' |
| 2899 | InitExprs.push_back( |
| 2900 | // set 'super class', using class_getSuperclass(). |
| 2901 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2902 | CK_Unknown, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2903 | // struct objc_super |
| 2904 | QualType superType = getSuperStructType(); |
| 2905 | Expr *SuperRep; |
| 2906 | |
| 2907 | if (LangOpts.Microsoft) { |
| 2908 | SynthSuperContructorFunctionDecl(); |
| 2909 | // Simulate a contructor call... |
| 2910 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2911 | superType, SourceLocation()); |
| 2912 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2913 | InitExprs.size(), |
| 2914 | superType, SourceLocation()); |
| 2915 | // The code for super is a little tricky to prevent collision with |
| 2916 | // the structure definition in the header. The rewriter has it's own |
| 2917 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2918 | // we need the cast below. For example: |
| 2919 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2920 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2921 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2922 | Context->getPointerType(SuperRep->getType()), |
| 2923 | SourceLocation()); |
| 2924 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2925 | Context->getPointerType(superType), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2926 | CK_Unknown, SuperRep); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2927 | } else { |
| 2928 | // (struct objc_super) { <exprs from above> } |
| 2929 | InitListExpr *ILE = |
| 2930 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2931 | &InitExprs[0], InitExprs.size(), |
| 2932 | SourceLocation()); |
| 2933 | TypeSourceInfo *superTInfo |
| 2934 | = Context->getTrivialTypeSourceInfo(superType); |
| 2935 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2936 | superType, ILE, false); |
| 2937 | } |
| 2938 | MsgExprs.push_back(SuperRep); |
| 2939 | break; |
| 2940 | } |
| 2941 | |
| 2942 | case ObjCMessageExpr::Instance: { |
| 2943 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2944 | // Foo<Proto> *. |
| 2945 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2946 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2947 | recExpr = CE->getSubExpr(); |
| 2948 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2949 | CK_Unknown, recExpr); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2950 | MsgExprs.push_back(recExpr); |
| 2951 | break; |
| 2952 | } |
| 2953 | } |
| 2954 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2955 | // 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] | 2956 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2957 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2959 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2960 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2961 | false, argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2962 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2963 | &SelExprs[0], SelExprs.size(), |
| 2964 | StartLoc, |
| 2965 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2966 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2968 | // Now push any user supplied arguments. |
| 2969 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2970 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2971 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2972 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2973 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2974 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2975 | ? Context->getObjCIdType() |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2976 | : ICE->getType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2977 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2978 | (void)convertBlockPointerToFunctionPointer(type); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2979 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2980 | userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2981 | } |
| 2982 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2983 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2984 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2985 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2986 | userExpr = CE->getSubExpr(); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2987 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2988 | CK_Unknown, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2989 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2991 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2992 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2993 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2994 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2995 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2996 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2997 | // Generate the funky cast. |
| 2998 | CastExpr *cast; |
| 2999 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3000 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3002 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 3003 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3004 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3005 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3006 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3007 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3008 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3009 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3010 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3011 | E = OMD->param_end(); PI != E; ++PI) { |
| 3012 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3014 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3015 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3016 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3017 | ArgTypes.push_back(t); |
| 3018 | } |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3019 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 3020 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3021 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3022 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3023 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3024 | } |
| 3025 | // 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] | 3026 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3028 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3030 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3031 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3033 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3034 | // xx.m:13: warning: function called through a non-compatible type |
| 3035 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3036 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3037 | Context->getPointerType(Context->VoidTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3038 | CK_Unknown, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3040 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3042 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 327f0f4 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 3043 | // If we don't have a method decl, force a variadic cast. |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3044 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3045 | false, false, 0, 0, |
| 3046 | FunctionType::ExtInfo()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3047 | castType = Context->getPointerType(castType); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3048 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3049 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3050 | |
| 3051 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3052 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3054 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3055 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | MsgExprs.size(), |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3057 | FT->getResultType(), EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3058 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3059 | if (MsgSendStretFlavor) { |
| 3060 | // We have the method which returns a struct/union. Must also generate |
| 3061 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3062 | // expression which dictate which one to envoke depending on size of |
| 3063 | // method's return type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3065 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3067 | SourceLocation()); |
| 3068 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3069 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3070 | Context->getPointerType(Context->VoidTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3071 | CK_Unknown, STDRE); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3072 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3074 | &ArgTypes[0], ArgTypes.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3075 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3076 | false, false, 0, 0, |
| 3077 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3078 | castType = Context->getPointerType(castType); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3079 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3080 | cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3082 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3083 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3085 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3086 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3088 | FT->getResultType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3090 | // Build sizeof(returnType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3092 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3093 | Context->getSizeType(), |
| 3094 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3095 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3096 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3097 | // For X86 it is more complicated and some kind of target specific routine |
| 3098 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3100 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3101 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3102 | llvm::APInt(IntSize, 8), |
| 3103 | Context->IntTy, |
| 3104 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3106 | BO_LE, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | Context->IntTy, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3108 | SourceLocation()); |
| 3109 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3111 | new (Context) ConditionalOperator(lessThanExpr, |
| 3112 | SourceLocation(), CE, |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3113 | SourceLocation(), STCE, (Expr*)0, |
| 3114 | returnType); |
| 3115 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3116 | CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3117 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3118 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3119 | return ReplacingStmt; |
| 3120 | } |
| 3121 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3122 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3123 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3124 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3126 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3127 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3129 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3130 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3133 | // typedef struct objc_object Protocol; |
| 3134 | QualType RewriteObjC::getProtocolType() { |
| 3135 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3136 | TypeSourceInfo *TInfo |
| 3137 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3138 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3140 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3141 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3142 | } |
| 3143 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3144 | } |
| 3145 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3146 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3147 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3148 | /// The forward references (and metadata) are generated in |
| 3149 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3150 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3151 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3152 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3154 | ID, getProtocolType(), 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3155 | SC_Extern, SC_None); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3156 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3157 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3158 | Context->getPointerType(DRE->getType()), |
| 3159 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3160 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3161 | CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3162 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3163 | ReplaceStmt(Exp, castExpr); |
| 3164 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3165 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3166 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3171 | const char *endBuf) { |
| 3172 | while (startBuf < endBuf) { |
| 3173 | if (*startBuf == '#') { |
| 3174 | // Skip whitespace. |
| 3175 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3176 | ; |
| 3177 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3178 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3179 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3180 | !strncmp(startBuf, "define", strlen("define")) || |
| 3181 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3182 | !strncmp(startBuf, "else", strlen("else")) || |
| 3183 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3184 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3185 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3186 | !strncmp(startBuf, "include", strlen("include")) || |
| 3187 | !strncmp(startBuf, "import", strlen("import")) || |
| 3188 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3189 | return true; |
| 3190 | } |
| 3191 | startBuf++; |
| 3192 | } |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3196 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3197 | /// an objective-c class with ivars. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3198 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3199 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3200 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3201 | assert(CDecl->getName() != "" && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3202 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3203 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3204 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3205 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3206 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3207 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3208 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3209 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3211 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3212 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3214 | // If no ivars and no root or if its root, directly or indirectly, |
| 3215 | // 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] | 3216 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3217 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3218 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3219 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3220 | return; |
| 3221 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | |
| 3223 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3224 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3225 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3226 | Result += CDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3227 | if (LangOpts.Microsoft) |
| 3228 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3229 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3230 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3231 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3233 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3234 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3235 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3236 | // NSURL.h, for example): |
| 3237 | // |
| 3238 | // #ifdef XYZ |
| 3239 | // @interface Foo : NSObject |
| 3240 | // #else |
| 3241 | // @interface FooBar : NSObject |
| 3242 | // #endif |
| 3243 | // { |
| 3244 | // int i; |
| 3245 | // } |
| 3246 | // @end |
| 3247 | // |
| 3248 | // This clause is segregated to avoid breaking the common case. |
| 3249 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3251 | CDecl->getClassLoc(); |
| 3252 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3253 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3254 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3255 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3256 | // advance to the end of the referenced protocols. |
| 3257 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3258 | endHeader++; |
| 3259 | } |
| 3260 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3261 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3262 | } else { |
| 3263 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3264 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3265 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3266 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3267 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3268 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3269 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3270 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3271 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3273 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3274 | SourceLocation OnePastCurly = |
| 3275 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3276 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3277 | } |
| 3278 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3280 | // Now comment out any visibility specifiers. |
| 3281 | while (cursor < endBuf) { |
| 3282 | if (*cursor == '@') { |
| 3283 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3284 | // Skip whitespace. |
| 3285 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3286 | /*scan*/; |
| 3287 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3288 | // FIXME: presence of @public, etc. inside comment results in |
| 3289 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3290 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3291 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3292 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3293 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3294 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3295 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3296 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3297 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3298 | // for type checking. |
| 3299 | else if (*cursor == '<') { |
| 3300 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3301 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3302 | cursor = strchr(cursor, '>'); |
| 3303 | cursor++; |
| 3304 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3305 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3306 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3307 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3308 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3309 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3310 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3311 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3312 | // Don't forget to add a ';'!! |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3313 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3314 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3315 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3316 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3317 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3318 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3319 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3320 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3321 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3322 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3323 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3324 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3325 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3328 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3329 | /// class methods. |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3330 | template<typename MethodIterator> |
| 3331 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3332 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3333 | bool IsInstanceMethod, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3334 | llvm::StringRef prefix, |
| 3335 | llvm::StringRef ClassName, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3336 | std::string &Result) { |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3337 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3339 | if (!objc_impl_method) { |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3340 | /* struct _objc_method { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3341 | SEL _cmd; |
| 3342 | char *method_types; |
| 3343 | void *_imp; |
| 3344 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3345 | */ |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3346 | Result += "\nstruct _objc_method {\n"; |
| 3347 | Result += "\tSEL _cmd;\n"; |
| 3348 | Result += "\tchar *method_types;\n"; |
| 3349 | Result += "\tvoid *_imp;\n"; |
| 3350 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3352 | objc_impl_method = true; |
Fariborz Jahanian | 74a1cfa | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3353 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3355 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3357 | /* struct { |
| 3358 | struct _objc_method_list *next_method; |
| 3359 | int method_count; |
| 3360 | struct _objc_method method_list[]; |
| 3361 | } |
| 3362 | */ |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3363 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3364 | Result += "\nstatic struct {\n"; |
| 3365 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3366 | Result += "\tint method_count;\n"; |
| 3367 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3368 | Result += utostr(NumMethods); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3369 | Result += "];\n} _OBJC_"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3370 | Result += prefix; |
| 3371 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3372 | Result += "_METHODS_"; |
| 3373 | Result += ClassName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3374 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3375 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3376 | Result += "_meth\")))= "; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3377 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3379 | Result += "\t,{{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3380 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3381 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3382 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3383 | Result += "\", \""; |
| 3384 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3385 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3386 | Result += MethodInternalNames[*MethodBegin]; |
| 3387 | Result += "}\n"; |
| 3388 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3389 | Result += "\t ,{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3390 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3391 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3392 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3393 | Result += "\", \""; |
| 3394 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3395 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3396 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3397 | Result += "}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3398 | } |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3399 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3402 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3403 | void RewriteObjC:: |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3404 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, llvm::StringRef prefix, |
| 3405 | llvm::StringRef ClassName, std::string &Result) { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3406 | static bool objc_protocol_methods = false; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3407 | |
| 3408 | // Output struct protocol_methods holder of method selector and type. |
| 3409 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3410 | /* struct protocol_methods { |
| 3411 | SEL _cmd; |
| 3412 | char *method_types; |
| 3413 | } |
| 3414 | */ |
| 3415 | Result += "\nstruct _protocol_methods {\n"; |
| 3416 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3417 | Result += "\tchar *method_types;\n"; |
| 3418 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3420 | objc_protocol_methods = true; |
| 3421 | } |
| 3422 | // Do not synthesize the protocol more than once. |
| 3423 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3424 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3426 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3427 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3428 | PDecl->instmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3429 | /* struct _objc_protocol_method_list { |
| 3430 | int protocol_method_count; |
| 3431 | struct protocol_methods protocols[]; |
| 3432 | } |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3433 | */ |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3434 | Result += "\nstatic struct {\n"; |
| 3435 | Result += "\tint protocol_method_count;\n"; |
| 3436 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3437 | Result += utostr(NumMethods); |
| 3438 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3439 | Result += PDecl->getNameAsString(); |
| 3440 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3441 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3443 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | for (ObjCProtocolDecl::instmeth_iterator |
| 3445 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3446 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3447 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3448 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3449 | else |
| 3450 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3451 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3452 | std::string MethodTypeString; |
| 3453 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3454 | Result += "\", \""; |
| 3455 | Result += MethodTypeString; |
| 3456 | Result += "\"}\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3457 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3458 | Result += "\t }\n};\n"; |
| 3459 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3461 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3462 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3463 | PDecl->classmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3464 | if (NumMethods > 0) { |
| 3465 | /* struct _objc_protocol_method_list { |
| 3466 | int protocol_method_count; |
| 3467 | struct protocol_methods protocols[]; |
| 3468 | } |
| 3469 | */ |
| 3470 | Result += "\nstatic struct {\n"; |
| 3471 | Result += "\tint protocol_method_count;\n"; |
| 3472 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3473 | Result += utostr(NumMethods); |
| 3474 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3475 | Result += PDecl->getNameAsString(); |
| 3476 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3477 | "{\n\t"; |
| 3478 | Result += utostr(NumMethods); |
| 3479 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3481 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3483 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3484 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3485 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3486 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3487 | else |
| 3488 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3489 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3490 | std::string MethodTypeString; |
| 3491 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3492 | Result += "\", \""; |
| 3493 | Result += MethodTypeString; |
| 3494 | Result += "\"}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3495 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3496 | Result += "\t }\n};\n"; |
| 3497 | } |
| 3498 | |
| 3499 | // Output: |
| 3500 | /* struct _objc_protocol { |
| 3501 | // Objective-C 1.0 extensions |
| 3502 | struct _objc_protocol_extension *isa; |
| 3503 | char *protocol_name; |
| 3504 | struct _objc_protocol **protocol_list; |
| 3505 | struct _objc_protocol_method_list *instance_methods; |
| 3506 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3507 | }; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3508 | */ |
| 3509 | static bool objc_protocol = false; |
| 3510 | if (!objc_protocol) { |
| 3511 | Result += "\nstruct _objc_protocol {\n"; |
| 3512 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3513 | Result += "\tchar *protocol_name;\n"; |
| 3514 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3515 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3516 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3517 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3519 | objc_protocol = true; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3522 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3523 | Result += PDecl->getNameAsString(); |
| 3524 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3525 | "{\n\t0, \""; |
| 3526 | Result += PDecl->getNameAsString(); |
| 3527 | Result += "\", 0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3528 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3529 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3530 | Result += PDecl->getNameAsString(); |
| 3531 | Result += ", "; |
| 3532 | } |
| 3533 | else |
| 3534 | Result += "0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3535 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3536 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3537 | Result += PDecl->getNameAsString(); |
| 3538 | Result += "\n"; |
| 3539 | } |
| 3540 | else |
| 3541 | Result += "0\n"; |
| 3542 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3544 | // Mark this protocol as having been generated. |
| 3545 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3546 | assert(false && "protocol already synthesized"); |
| 3547 | |
| 3548 | } |
| 3549 | |
| 3550 | void RewriteObjC:: |
| 3551 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3552 | llvm::StringRef prefix, llvm::StringRef ClassName, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3553 | std::string &Result) { |
| 3554 | if (Protocols.empty()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3555 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3556 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3557 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3558 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3559 | // Output the top lovel protocol meta-data for the class. |
| 3560 | /* struct _objc_protocol_list { |
| 3561 | struct _objc_protocol_list *next; |
| 3562 | int protocol_count; |
| 3563 | struct _objc_protocol *class_protocols[]; |
| 3564 | } |
| 3565 | */ |
| 3566 | Result += "\nstatic struct {\n"; |
| 3567 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3568 | Result += "\tint protocol_count;\n"; |
| 3569 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3570 | Result += utostr(Protocols.size()); |
| 3571 | Result += "];\n} _OBJC_"; |
| 3572 | Result += prefix; |
| 3573 | Result += "_PROTOCOLS_"; |
| 3574 | Result += ClassName; |
| 3575 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3576 | "{\n\t0, "; |
| 3577 | Result += utostr(Protocols.size()); |
| 3578 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3580 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3581 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3582 | Result += " \n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3584 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3585 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3586 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3587 | Result += "\n"; |
| 3588 | } |
| 3589 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3592 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3593 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3594 | /// implementation. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3595 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3596 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3597 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3598 | // Find category declaration for this implementation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3599 | ObjCCategoryDecl *CDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3601 | CDecl = CDecl->getNextClassCategory()) |
| 3602 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3603 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3605 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3606 | FullCategoryName += '_'; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3607 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3608 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3609 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3610 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3611 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3612 | |
| 3613 | // If any of our property implementations have associated getters or |
| 3614 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3615 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3616 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3617 | Prop != PropEnd; ++Prop) { |
| 3618 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3619 | continue; |
| 3620 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3621 | continue; |
| 3622 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3623 | if (!PD) |
| 3624 | continue; |
| 3625 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3626 | InstanceMethods.push_back(Getter); |
| 3627 | if (PD->isReadOnly()) |
| 3628 | continue; |
| 3629 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3630 | InstanceMethods.push_back(Setter); |
| 3631 | } |
| 3632 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3633 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3634 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3636 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3637 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3638 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3639 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3641 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 989e039 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3642 | // Null CDecl is case of a category implementation with no category interface |
| 3643 | if (CDecl) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3644 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3645 | FullCategoryName, Result); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3646 | /* struct _objc_category { |
| 3647 | char *category_name; |
| 3648 | char *class_name; |
| 3649 | struct _objc_method_list *instance_methods; |
| 3650 | struct _objc_method_list *class_methods; |
| 3651 | struct _objc_protocol_list *protocols; |
| 3652 | // Objective-C 1.0 extensions |
| 3653 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3655 | // @property decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3657 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3659 | static bool objc_category = false; |
| 3660 | if (!objc_category) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3661 | Result += "\nstruct _objc_category {\n"; |
| 3662 | Result += "\tchar *category_name;\n"; |
| 3663 | Result += "\tchar *class_name;\n"; |
| 3664 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3665 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3666 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3668 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3669 | Result += "};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3670 | objc_category = true; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3671 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3672 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3673 | Result += FullCategoryName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3674 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3675 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3676 | Result += "\"\n\t, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3677 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3678 | Result += "\"\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3680 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3681 | Result += "\t, (struct _objc_method_list *)" |
| 3682 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3683 | Result += FullCategoryName; |
| 3684 | Result += "\n"; |
| 3685 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3686 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3687 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3688 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3689 | Result += "\t, (struct _objc_method_list *)" |
| 3690 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3691 | Result += FullCategoryName; |
| 3692 | Result += "\n"; |
| 3693 | } |
| 3694 | else |
| 3695 | Result += "\t, 0\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3697 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3699 | Result += FullCategoryName; |
| 3700 | Result += "\n"; |
| 3701 | } |
| 3702 | else |
| 3703 | Result += "\t, 0\n"; |
| 3704 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3707 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3708 | /// ivar offset. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 3709 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3711 | std::string &Result) { |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3712 | if (ivar->isBitField()) { |
| 3713 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3714 | // place all bitfields at offset 0. |
| 3715 | Result += "0"; |
| 3716 | } else { |
| 3717 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3718 | Result += IDecl->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3719 | if (LangOpts.Microsoft) |
| 3720 | Result += "_IMPL"; |
| 3721 | Result += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3722 | Result += ivar->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3723 | Result += ")"; |
| 3724 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3727 | //===----------------------------------------------------------------------===// |
| 3728 | // Meta Data Emission |
| 3729 | //===----------------------------------------------------------------------===// |
| 3730 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3731 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3732 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3733 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3735 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3736 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3738 | // produce correct synthesis as yet. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3739 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3740 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3742 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3743 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | ? IDecl->ivar_size() |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3745 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3746 | if (NumIvars > 0) { |
| 3747 | static bool objc_ivar = false; |
| 3748 | if (!objc_ivar) { |
| 3749 | /* struct _objc_ivar { |
| 3750 | char *ivar_name; |
| 3751 | char *ivar_type; |
| 3752 | int ivar_offset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3754 | */ |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3755 | Result += "\nstruct _objc_ivar {\n"; |
| 3756 | Result += "\tchar *ivar_name;\n"; |
| 3757 | Result += "\tchar *ivar_type;\n"; |
| 3758 | Result += "\tint ivar_offset;\n"; |
| 3759 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3761 | objc_ivar = true; |
| 3762 | } |
| 3763 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3764 | /* struct { |
| 3765 | int ivar_count; |
| 3766 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | }; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3768 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | Result += "\nstatic struct {\n"; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3770 | Result += "\tint ivar_count;\n"; |
| 3771 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3772 | Result += utostr(NumIvars); |
| 3773 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3774 | Result += IDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3775 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3776 | "{\n\t"; |
| 3777 | Result += utostr(NumIvars); |
| 3778 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3780 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3781 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3782 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3783 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3784 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3785 | IV != IVEnd; ++IV) |
| 3786 | IVars.push_back(*IV); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3787 | IVI = IDecl->ivar_begin(); |
| 3788 | IVE = IDecl->ivar_end(); |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3789 | } else { |
| 3790 | IVI = CDecl->ivar_begin(); |
| 3791 | IVE = CDecl->ivar_end(); |
| 3792 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3793 | Result += "\t,{{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3794 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3795 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3796 | std::string TmpString, StrEncoding; |
| 3797 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3798 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3799 | Result += StrEncoding; |
| 3800 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3801 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3802 | Result += "}\n"; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3803 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3804 | Result += "\t ,{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3805 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3806 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3807 | std::string TmpString, StrEncoding; |
| 3808 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3809 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3810 | Result += StrEncoding; |
| 3811 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3812 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3813 | Result += "}\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3814 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3815 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3816 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3817 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3819 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3821 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3822 | |
| 3823 | // If any of our property implementations have associated getters or |
| 3824 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3825 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3826 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3827 | Prop != PropEnd; ++Prop) { |
| 3828 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3829 | continue; |
| 3830 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3831 | continue; |
| 3832 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3833 | if (!PD) |
| 3834 | continue; |
| 3835 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3836 | InstanceMethods.push_back(Getter); |
| 3837 | if (PD->isReadOnly()) |
| 3838 | continue; |
| 3839 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3840 | InstanceMethods.push_back(Setter); |
| 3841 | } |
| 3842 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3843 | true, "", IDecl->getName(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3845 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3846 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3847 | false, "", IDecl->getName(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3849 | // Protocols referenced in class declaration? |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3850 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3851 | "CLASS", CDecl->getName(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3853 | // Declaration of class/meta-class metadata |
| 3854 | /* struct _objc_class { |
| 3855 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3856 | const char *super_class_name; |
| 3857 | char *name; |
| 3858 | long version; |
| 3859 | long info; |
| 3860 | long instance_size; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3861 | struct _objc_ivar_list *ivars; |
| 3862 | struct _objc_method_list *methods; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3863 | struct objc_cache *cache; |
| 3864 | struct objc_protocol_list *protocols; |
| 3865 | const char *ivar_layout; |
| 3866 | struct _objc_class_ext *ext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3867 | }; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3868 | */ |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3869 | static bool objc_class = false; |
| 3870 | if (!objc_class) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3871 | Result += "\nstruct _objc_class {\n"; |
| 3872 | Result += "\tstruct _objc_class *isa;\n"; |
| 3873 | Result += "\tconst char *super_class_name;\n"; |
| 3874 | Result += "\tchar *name;\n"; |
| 3875 | Result += "\tlong version;\n"; |
| 3876 | Result += "\tlong info;\n"; |
| 3877 | Result += "\tlong instance_size;\n"; |
| 3878 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3879 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3880 | Result += "\tstruct objc_cache *cache;\n"; |
| 3881 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3882 | Result += "\tconst char *ivar_layout;\n"; |
| 3883 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3884 | Result += "};\n"; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3885 | objc_class = true; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3886 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3888 | // Meta-class metadata generation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3889 | ObjCInterfaceDecl *RootClass = 0; |
| 3890 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3891 | while (SuperClass) { |
| 3892 | RootClass = SuperClass; |
| 3893 | SuperClass = SuperClass->getSuperClass(); |
| 3894 | } |
| 3895 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3897 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3898 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3899 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3900 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3901 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3902 | Result += "\""; |
| 3903 | |
| 3904 | if (SuperClass) { |
| 3905 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3906 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3907 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3908 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3909 | Result += "\""; |
| 3910 | } |
| 3911 | else { |
| 3912 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3913 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3914 | Result += "\""; |
| 3915 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3916 | // 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] | 3917 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3918 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3919 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3920 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3921 | Result += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3922 | Result += "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3923 | } |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3924 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3925 | Result += ", 0\n"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3926 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3927 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3928 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3929 | Result += ",0,0\n"; |
| 3930 | } |
Fariborz Jahanian | 486f718 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3931 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3932 | Result += "\t,0,0,0,0\n"; |
| 3933 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3935 | // class metadata generation. |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3936 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3937 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3938 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3939 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3940 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3941 | if (SuperClass) { |
| 3942 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3943 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3944 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3945 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3946 | Result += "\""; |
| 3947 | } |
| 3948 | else { |
| 3949 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3950 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3951 | Result += "\""; |
| 3952 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3953 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3954 | Result += ", 0,1"; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3955 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3956 | Result += ",0"; |
| 3957 | else { |
| 3958 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3959 | Result += ",sizeof(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3960 | Result += CDecl->getNameAsString(); |
Steve Naroff | 14a0746 | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3961 | if (LangOpts.Microsoft) |
| 3962 | Result += "_IMPL"; |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3963 | Result += ")"; |
| 3964 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3965 | if (NumIvars > 0) { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3966 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3967 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3968 | Result += "\n\t"; |
| 3969 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3970 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3971 | Result += ",0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3972 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3973 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3974 | Result += CDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | Result += ", 0\n\t"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3976 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3977 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3978 | Result += ",0,0"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3979 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3980 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3981 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3982 | Result += ", 0,0\n"; |
| 3983 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3984 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3985 | Result += ",0,0,0\n"; |
| 3986 | Result += "};\n"; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3987 | } |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3988 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3989 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3990 | /// and emits meta-data. |
| 3991 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3992 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3993 | int ClsDefCount = ClassImplementation.size(); |
| 3994 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3996 | // Rewrite implemented methods |
| 3997 | for (int i = 0; i < ClsDefCount; i++) |
| 3998 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 4000 | for (int i = 0; i < CatDefCount; i++) |
| 4001 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4004 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 4005 | int ClsDefCount = ClassImplementation.size(); |
| 4006 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 4007 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4008 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4009 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4010 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4012 | // For each implemented category, write out all its meta data. |
| 4013 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4014 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4015 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4016 | // Write objc_symtab metadata |
| 4017 | /* |
| 4018 | struct _objc_symtab |
| 4019 | { |
| 4020 | long sel_ref_cnt; |
| 4021 | SEL *refs; |
| 4022 | short cls_def_cnt; |
| 4023 | short cat_def_cnt; |
| 4024 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | }; |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4026 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4027 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4028 | Result += "\nstruct _objc_symtab {\n"; |
| 4029 | Result += "\tlong sel_ref_cnt;\n"; |
| 4030 | Result += "\tSEL *refs;\n"; |
| 4031 | Result += "\tshort cls_def_cnt;\n"; |
| 4032 | Result += "\tshort cat_def_cnt;\n"; |
| 4033 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4034 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4036 | Result += "static struct _objc_symtab " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4037 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4039 | + ", " + utostr(CatDefCount) + "\n"; |
| 4040 | for (int i = 0; i < ClsDefCount; i++) { |
| 4041 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4042 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4043 | Result += "\n"; |
| 4044 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4045 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4046 | for (int i = 0; i < CatDefCount; i++) { |
| 4047 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4048 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4049 | Result += "_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4050 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4051 | Result += "\n"; |
| 4052 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4054 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4056 | // Write objc_module metadata |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4058 | /* |
| 4059 | struct _objc_module { |
| 4060 | long version; |
| 4061 | long size; |
| 4062 | const char *name; |
| 4063 | struct _objc_symtab *symtab; |
| 4064 | } |
| 4065 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4067 | Result += "\nstruct _objc_module {\n"; |
| 4068 | Result += "\tlong version;\n"; |
| 4069 | Result += "\tlong size;\n"; |
| 4070 | Result += "\tconst char *name;\n"; |
| 4071 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4072 | Result += "};\n\n"; |
| 4073 | Result += "static struct _objc_module " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4074 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4076 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4077 | Result += "};\n\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4078 | |
| 4079 | if (LangOpts.Microsoft) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4080 | if (ProtocolExprDecls.size()) { |
| 4081 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4082 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4084 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4085 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4086 | Result += (*I)->getNameAsString(); |
| 4087 | Result += " = &_OBJC_PROTOCOL_"; |
| 4088 | Result += (*I)->getNameAsString(); |
| 4089 | Result += ";\n"; |
| 4090 | } |
| 4091 | Result += "#pragma data_seg(pop)\n\n"; |
| 4092 | } |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4093 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | cab93d5 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4094 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4095 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4096 | Result += "&_OBJC_MODULES;\n"; |
| 4097 | Result += "#pragma data_seg(pop)\n\n"; |
| 4098 | } |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4099 | } |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4100 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4101 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4102 | const std::string &Name, |
| 4103 | ValueDecl *VD) { |
| 4104 | assert(BlockByRefDeclNo.count(VD) && |
| 4105 | "RewriteByRefString: ByRef decl missing"); |
| 4106 | ResultStr += "struct __Block_byref_" + Name + |
| 4107 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4108 | } |
| 4109 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4110 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4111 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4112 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4113 | return false; |
| 4114 | } |
| 4115 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4116 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4117 | llvm::StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4118 | std::string Tag) { |
| 4119 | const FunctionType *AFT = CE->getFunctionType(); |
| 4120 | QualType RT = AFT->getResultType(); |
| 4121 | std::string StructRef = "struct " + Tag; |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 4122 | std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" + |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4123 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4124 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4125 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4127 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | // 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] | 4129 | // block (to reference imported block decl refs). |
| 4130 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4131 | } else if (BD->param_empty()) { |
| 4132 | S += "(" + StructRef + " *__cself)"; |
| 4133 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4134 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4135 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4136 | S += '('; |
| 4137 | // first add the implicit argument. |
| 4138 | S += StructRef + " *__cself, "; |
| 4139 | std::string ParamStr; |
| 4140 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4141 | E = BD->param_end(); AI != E; ++AI) { |
| 4142 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4143 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4144 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4145 | if (convertBlockPointerToFunctionPointer(QT)) |
| 4146 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4147 | else |
| 4148 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4149 | S += ParamStr; |
| 4150 | } |
| 4151 | if (FT->isVariadic()) { |
| 4152 | if (!BD->param_empty()) S += ", "; |
| 4153 | S += "..."; |
| 4154 | } |
| 4155 | S += ')'; |
| 4156 | } |
| 4157 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4159 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4160 | // First, emit a declaration for all "by ref" decls. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4161 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4162 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4163 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4164 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4165 | std::string TypeString; |
| 4166 | RewriteByRefString(TypeString, Name, (*I)); |
| 4167 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4168 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4169 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4171 | // Next, emit a declaration for all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4172 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4173 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4174 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4175 | // Handle nested closure invocation. For example: |
| 4176 | // |
| 4177 | // void (^myImportedClosure)(void); |
| 4178 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4180 | // void (^anotherClosure)(void); |
| 4181 | // anotherClosure = ^(void) { |
| 4182 | // myImportedClosure(); // import and invoke the closure |
| 4183 | // }; |
| 4184 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4185 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4186 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4187 | S += " = ("; |
| 4188 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4189 | S += ")"; |
| 4190 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4191 | } |
| 4192 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4193 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4194 | QualType QT = (*I)->getType(); |
| 4195 | if (HasLocalVariableExternalStorage(*I)) |
| 4196 | QT = Context->getPointerType(QT); |
| 4197 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4198 | S += Name + " = __cself->" + |
| 4199 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4200 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4201 | } |
| 4202 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4203 | const char *cstr = RewrittenStr.c_str(); |
| 4204 | while (*cstr++ != '{') ; |
| 4205 | S += cstr; |
| 4206 | S += "\n"; |
| 4207 | return S; |
| 4208 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4209 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4210 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4211 | llvm::StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4212 | std::string Tag) { |
| 4213 | std::string StructRef = "struct " + Tag; |
| 4214 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4216 | S += funcName; |
| 4217 | S += "_block_copy_" + utostr(i); |
| 4218 | S += "(" + StructRef; |
| 4219 | S += "*dst, " + StructRef; |
| 4220 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4222 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4223 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4224 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4225 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4226 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4227 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4228 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4229 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4230 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4231 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4232 | S += "}\n"; |
| 4233 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4234 | S += "\nstatic void __"; |
| 4235 | S += funcName; |
| 4236 | S += "_block_dispose_" + utostr(i); |
| 4237 | S += "(" + StructRef; |
| 4238 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4240 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4241 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4242 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4243 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4244 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4245 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4246 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4248 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4249 | return S; |
| 4250 | } |
| 4251 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4252 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4253 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4254 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4255 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4257 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4258 | S += " struct " + Desc; |
| 4259 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4261 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4262 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4263 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4264 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4265 | if (BlockDeclRefs.size()) { |
| 4266 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4267 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4268 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4269 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4270 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4271 | std::string ArgName = "_" + FieldName; |
| 4272 | // Handle nested closure invocation. For example: |
| 4273 | // |
| 4274 | // void (^myImportedBlock)(void); |
| 4275 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4277 | // void (^anotherBlock)(void); |
| 4278 | // anotherBlock = ^(void) { |
| 4279 | // myImportedBlock(); // import and invoke the closure |
| 4280 | // }; |
| 4281 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4282 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4283 | S += "struct __block_impl *"; |
| 4284 | Constructor += ", void *" + ArgName; |
| 4285 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4286 | QualType QT = (*I)->getType(); |
| 4287 | if (HasLocalVariableExternalStorage(*I)) |
| 4288 | QT = Context->getPointerType(QT); |
| 4289 | QT.getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4290 | QT.getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4291 | Constructor += ", " + ArgName; |
| 4292 | } |
| 4293 | S += FieldName + ";\n"; |
| 4294 | } |
| 4295 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4296 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4297 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4298 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4299 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4300 | std::string ArgName = "_" + FieldName; |
| 4301 | // Handle nested closure invocation. For example: |
| 4302 | // |
| 4303 | // void (^myImportedBlock)(void); |
| 4304 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4306 | // void (^anotherBlock)(void); |
| 4307 | // anotherBlock = ^(void) { |
| 4308 | // myImportedBlock(); // import and invoke the closure |
| 4309 | // }; |
| 4310 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4311 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4312 | S += "struct __block_impl *"; |
| 4313 | Constructor += ", void *" + ArgName; |
| 4314 | } else { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4315 | std::string TypeString; |
| 4316 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4317 | TypeString += " *"; |
| 4318 | FieldName = TypeString + FieldName; |
| 4319 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4320 | Constructor += ", " + ArgName; |
| 4321 | } |
| 4322 | S += FieldName + "; // by ref\n"; |
| 4323 | } |
| 4324 | // Finish writing the constructor. |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4325 | Constructor += ", int flags=0)"; |
| 4326 | // Initialize all "by copy" arguments. |
| 4327 | bool firsTime = true; |
| 4328 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4329 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4330 | std::string Name = (*I)->getNameAsString(); |
| 4331 | if (firsTime) { |
| 4332 | Constructor += " : "; |
| 4333 | firsTime = false; |
| 4334 | } |
| 4335 | else |
| 4336 | Constructor += ", "; |
| 4337 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4338 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4339 | else |
| 4340 | Constructor += Name + "(_" + Name + ")"; |
| 4341 | } |
| 4342 | // Initialize all "by ref" arguments. |
| 4343 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4344 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4345 | std::string Name = (*I)->getNameAsString(); |
| 4346 | if (firsTime) { |
| 4347 | Constructor += " : "; |
| 4348 | firsTime = false; |
| 4349 | } |
| 4350 | else |
| 4351 | Constructor += ", "; |
| 4352 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4353 | Constructor += Name + "((struct __block_impl *)_" |
| 4354 | + Name + "->__forwarding)"; |
| 4355 | else |
| 4356 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 4357 | } |
| 4358 | |
| 4359 | Constructor += " {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4360 | if (GlobalVarDecl) |
| 4361 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4362 | else |
| 4363 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4364 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4366 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4367 | } else { |
| 4368 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4369 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4370 | if (GlobalVarDecl) |
| 4371 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4372 | else |
| 4373 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4374 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4375 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4376 | } |
| 4377 | Constructor += " "; |
| 4378 | Constructor += "}\n"; |
| 4379 | S += Constructor; |
| 4380 | S += "};\n"; |
| 4381 | return S; |
| 4382 | } |
| 4383 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4384 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4385 | std::string ImplTag, int i, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4386 | llvm::StringRef FunName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4387 | unsigned hasCopy) { |
| 4388 | std::string S = "\nstatic struct " + DescTag; |
| 4389 | |
| 4390 | S += " {\n unsigned long reserved;\n"; |
| 4391 | S += " unsigned long Block_size;\n"; |
| 4392 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4393 | S += " void (*copy)(struct "; |
| 4394 | S += ImplTag; S += "*, struct "; |
| 4395 | S += ImplTag; S += "*);\n"; |
| 4396 | |
| 4397 | S += " void (*dispose)(struct "; |
| 4398 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4399 | } |
| 4400 | S += "} "; |
| 4401 | |
| 4402 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4403 | S += ImplTag + ")"; |
| 4404 | if (hasCopy) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4405 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4406 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4407 | } |
| 4408 | S += "};\n"; |
| 4409 | return S; |
| 4410 | } |
| 4411 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4412 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4413 | llvm::StringRef FunName) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4414 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4415 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4416 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4417 | bool RewriteSC = (GlobalVarDecl && |
| 4418 | !Blocks.empty() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4419 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4420 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4421 | if (RewriteSC) { |
| 4422 | std::string SC(" void __"); |
| 4423 | SC += GlobalVarDecl->getNameAsString(); |
| 4424 | SC += "() {}"; |
| 4425 | InsertText(FunLocStart, SC); |
| 4426 | } |
| 4427 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4428 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4429 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4430 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4431 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4432 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4433 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4434 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4435 | ValueDecl *VD = Exp->getDecl(); |
| 4436 | BlockDeclRefs.push_back(Exp); |
| 4437 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4438 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4439 | BlockByCopyDecls.push_back(VD); |
| 4440 | } |
| 4441 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4442 | BlockByRefDeclsPtrSet.insert(VD); |
| 4443 | BlockByRefDecls.push_back(VD); |
| 4444 | } |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 4445 | // imported objects in the inner blocks not used in the outer |
| 4446 | // blocks must be copied/disposed in the outer block as well. |
| 4447 | if (Exp->isByRef() || |
| 4448 | VD->getType()->isObjCObjectPointerType() || |
| 4449 | VD->getType()->isBlockPointerType()) |
| 4450 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4451 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4452 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4453 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4454 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4456 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4457 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4458 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4459 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4460 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4462 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4463 | |
| 4464 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4465 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4466 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4467 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4468 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4469 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4470 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4471 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4472 | BlockDeclRefs.clear(); |
| 4473 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4474 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4475 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4476 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4477 | ImportedBlockDecls.clear(); |
| 4478 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4479 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4480 | // Must insert any 'const/volatile/static here. Since it has been |
| 4481 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4482 | std::string SC; |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4483 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4484 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4485 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4486 | SC += "const "; |
| 4487 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4488 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4489 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4490 | SC += "restrict "; |
| 4491 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4494 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4495 | InnerDeclRefsCount.clear(); |
| 4496 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4497 | RewrittenBlockExprs.clear(); |
| 4498 | } |
| 4499 | |
| 4500 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4501 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4502 | llvm::StringRef FuncName = FD->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4504 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4505 | } |
| 4506 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4507 | static void BuildUniqueMethodName(std::string &Name, |
| 4508 | ObjCMethodDecl *MD) { |
| 4509 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4510 | Name = IFace->getName(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4511 | Name += "__" + MD->getSelector().getAsString(); |
| 4512 | // Convert colons to underscores. |
| 4513 | std::string::size_type loc = 0; |
| 4514 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4515 | Name.replace(loc, 1, "_"); |
| 4516 | } |
| 4517 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4518 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4519 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4520 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4521 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4522 | std::string FuncName; |
| 4523 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4524 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4528 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4529 | CI != E; ++CI) |
| 4530 | if (*CI) { |
| 4531 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4532 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4533 | else |
| 4534 | GetBlockDeclRefExprs(*CI); |
| 4535 | } |
| 4536 | // Handle specific things. |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4537 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4538 | // FIXME: Handle enums. |
| 4539 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4540 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4541 | } |
| 4542 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4543 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4544 | BlockDeclRefExpr *BDRE = |
| 4545 | new (Context)BlockDeclRefExpr(DRE->getDecl(), DRE->getType(), |
| 4546 | DRE->getLocation(), false); |
| 4547 | BlockDeclRefs.push_back(BDRE); |
| 4548 | } |
| 4549 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4550 | return; |
| 4551 | } |
| 4552 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4553 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4554 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4555 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4556 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4557 | CI != E; ++CI) |
| 4558 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4559 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4560 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4561 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4562 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4563 | InnerContexts); |
| 4564 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4565 | else |
| 4566 | GetInnerBlockDeclRefExprs(*CI, |
| 4567 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4568 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4569 | |
| 4570 | } |
| 4571 | // Handle specific things. |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4572 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4573 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4574 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4575 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4576 | } |
| 4577 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4578 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4579 | if (Var->isFunctionOrMethodVarDecl()) |
| 4580 | ImportedLocalExternalDecls.insert(Var); |
| 4581 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4582 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4583 | return; |
| 4584 | } |
| 4585 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4586 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4587 | /// whose result type may be a block pointer or whose argument type(s) |
| 4588 | /// might be block pointers to an equivalent funtion type replacing |
| 4589 | /// all block pointers to function pointers. |
| 4590 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4591 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4592 | // FTP will be null for closures that don't take arguments. |
| 4593 | // Generate a funky cast. |
| 4594 | llvm::SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4595 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4596 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4597 | |
| 4598 | if (FTP) { |
| 4599 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4600 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4601 | QualType t = *I; |
| 4602 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4603 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4604 | HasBlockType = true; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4605 | ArgTypes.push_back(t); |
| 4606 | } |
| 4607 | } |
| 4608 | QualType FuncType; |
| 4609 | // FIXME. Does this work if block takes no argument but has a return type |
| 4610 | // which is of block type? |
| 4611 | if (HasBlockType) |
| 4612 | FuncType = Context->getFunctionType(Res, |
| 4613 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4614 | false, false, 0, 0, FunctionType::ExtInfo()); |
| 4615 | else FuncType = QualType(FT, 0); |
| 4616 | return FuncType; |
| 4617 | } |
| 4618 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4619 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4620 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4621 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4623 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4624 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4625 | } else if (const BlockDeclRefExpr *CDRE = |
| 4626 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4627 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4628 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4629 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4630 | } |
| 4631 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4632 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4633 | } |
| 4634 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4635 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4636 | else if (const ConditionalOperator *CEXPR = |
| 4637 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4638 | Expr *LHSExp = CEXPR->getLHS(); |
| 4639 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4640 | Expr *RHSExp = CEXPR->getRHS(); |
| 4641 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4642 | Expr *CONDExp = CEXPR->getCond(); |
| 4643 | ConditionalOperator *CondExpr = |
| 4644 | new (Context) ConditionalOperator(CONDExp, |
| 4645 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4646 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4647 | (Expr*)0, |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4648 | Exp->getType()); |
| 4649 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4650 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4651 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4652 | } else { |
| 4653 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4654 | } |
| 4655 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4656 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4657 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4658 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4659 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4661 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4662 | SourceLocation(), |
| 4663 | &Context->Idents.get("__block_impl")); |
| 4664 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4665 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4666 | // Generate a funky cast. |
| 4667 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4669 | // Push the block argument type. |
| 4670 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4671 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4673 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4674 | QualType t = *I; |
| 4675 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4676 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4677 | ArgTypes.push_back(t); |
| 4678 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4679 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4680 | // Now do the pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4682 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4683 | false, false, 0, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4684 | FunctionType::ExtInfo()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4686 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4687 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4688 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4689 | CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4690 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4691 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4692 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4693 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4694 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4696 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4698 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4699 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4700 | FD->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4702 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4703 | CK_Unknown, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4704 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4706 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4707 | // Add the implicit argument. |
| 4708 | BlkExprs.push_back(BlkCast); |
| 4709 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4711 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4712 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4713 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4714 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4715 | BlkExprs.size(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4716 | Exp->getType(), SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4717 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4720 | // We need to return the rewritten expression to handle cases where the |
| 4721 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4722 | // For example: |
| 4723 | // |
| 4724 | // int main() { |
| 4725 | // __block Foo *f; |
| 4726 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4728 | // void (^myblock)() = ^() { |
| 4729 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4730 | // i = 77; |
| 4731 | // }; |
| 4732 | //} |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4733 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4734 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4735 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4736 | ValueDecl *VD; |
| 4737 | bool isArrow = true; |
| 4738 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4739 | VD = BDRE->getDecl(); |
| 4740 | else { |
| 4741 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4742 | isArrow = false; |
| 4743 | } |
| 4744 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4745 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4746 | &Context->Idents.get("__forwarding"), |
| 4747 | Context->VoidPtrTy, 0, |
| 4748 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4749 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4750 | FD, SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4751 | FD->getType()); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4752 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4753 | llvm::StringRef Name = VD->getName(); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4754 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4755 | &Context->Idents.get(Name), |
| 4756 | Context->VoidPtrTy, 0, |
| 4757 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4758 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4759 | DeclRefExp->getType()); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4760 | |
| 4761 | |
| 4762 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4763 | // Need parens to enforce precedence. |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4764 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4765 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4766 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4767 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4770 | // Rewrites the imported local variable V with external storage |
| 4771 | // (static, extern, etc.) as *V |
| 4772 | // |
| 4773 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4774 | ValueDecl *VD = DRE->getDecl(); |
| 4775 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4776 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4777 | return DRE; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4778 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4779 | DRE->getType(), DRE->getLocation()); |
| 4780 | // Need parens to enforce precedence. |
| 4781 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4782 | Exp); |
| 4783 | ReplaceStmt(DRE, PE); |
| 4784 | return PE; |
| 4785 | } |
| 4786 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4787 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4788 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4789 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4790 | |
| 4791 | // Need to avoid trying to rewrite synthesized casts. |
| 4792 | if (LocStart.isInvalid()) |
| 4793 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4794 | // Need to avoid trying to rewrite casts contained in macros. |
| 4795 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4796 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4798 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4799 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4800 | QualType QT = CE->getType(); |
| 4801 | const Type* TypePtr = QT->getAs<Type>(); |
| 4802 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4803 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4804 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4805 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4806 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4807 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4808 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4809 | return; |
| 4810 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4811 | // advance the location to startArgList. |
| 4812 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4814 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4815 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4816 | case '^': |
| 4817 | // Replace the '^' with '*'. |
| 4818 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4819 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4820 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4821 | } |
| 4822 | } |
| 4823 | return; |
| 4824 | } |
| 4825 | |
| 4826 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4827 | SourceLocation DeclLoc = FD->getLocation(); |
| 4828 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4830 | // We have 1 or more arguments that have closure pointers. |
| 4831 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4832 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4834 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4836 | parenCount++; |
| 4837 | // advance the location to startArgList. |
| 4838 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4839 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4841 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4843 | while (*argPtr++ && parenCount) { |
| 4844 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4845 | case '^': |
| 4846 | // Replace the '^' with '*'. |
| 4847 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4848 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4849 | break; |
| 4850 | case '(': |
| 4851 | parenCount++; |
| 4852 | break; |
| 4853 | case ')': |
| 4854 | parenCount--; |
| 4855 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4856 | } |
| 4857 | } |
| 4858 | return; |
| 4859 | } |
| 4860 | |
| 4861 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4862 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4863 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4864 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4865 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4866 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4867 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4868 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4869 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4870 | } |
| 4871 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4873 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4874 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4875 | return true; |
| 4876 | } |
| 4877 | return false; |
| 4878 | } |
| 4879 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4880 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4881 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4882 | const char *argPtr = strchr(Name, '('); |
| 4883 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4885 | LParen = argPtr; // output the start. |
| 4886 | argPtr++; // skip past the left paren. |
| 4887 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4889 | while (*argPtr && parenCount) { |
| 4890 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4891 | case '(': parenCount++; break; |
| 4892 | case ')': parenCount--; break; |
| 4893 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4894 | } |
| 4895 | if (parenCount) argPtr++; |
| 4896 | } |
| 4897 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4898 | RParen = argPtr; // output the end |
| 4899 | } |
| 4900 | |
| 4901 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4902 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4903 | RewriteBlockPointerFunctionArgs(FD); |
| 4904 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4906 | // Handle Variables and Typedefs. |
| 4907 | SourceLocation DeclLoc = ND->getLocation(); |
| 4908 | QualType DeclT; |
| 4909 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4910 | DeclT = VD->getType(); |
| 4911 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4912 | DeclT = TDD->getUnderlyingType(); |
| 4913 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4914 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | else |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4916 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4918 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4919 | const char *endBuf = startBuf; |
| 4920 | // scan backward (from the decl location) for the end of the previous decl. |
| 4921 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4922 | startBuf--; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4923 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4924 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4925 | // may take block argument types (which will be handled below). |
| 4926 | if (*startBuf == '^') { |
| 4927 | // Replace the '^' with '*', computing a negative offset. |
| 4928 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4929 | ReplaceText(DeclLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4930 | } |
| 4931 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4932 | // Replace the '^' with '*' for arguments. |
| 4933 | DeclLoc = ND->getLocation(); |
| 4934 | startBuf = SM->getCharacterData(DeclLoc); |
| 4935 | const char *argListBegin, *argListEnd; |
| 4936 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4937 | while (argListBegin < argListEnd) { |
| 4938 | if (*argListBegin == '^') { |
| 4939 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4940 | ReplaceText(CaretLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4941 | } |
| 4942 | argListBegin++; |
| 4943 | } |
| 4944 | } |
| 4945 | return; |
| 4946 | } |
| 4947 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4948 | |
| 4949 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4950 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4951 | /// struct Block_byref_id_object *src) { |
| 4952 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4953 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4954 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4955 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4956 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4957 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4958 | /// } |
| 4959 | /// And: |
| 4960 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4961 | /// _Block_object_dispose(_src->object, |
| 4962 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4963 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4964 | /// _Block_object_dispose(_src->object, |
| 4965 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4966 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4967 | /// } |
| 4968 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4969 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4970 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4971 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4972 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4973 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4974 | CopyDestroyCache.insert(flag); |
| 4975 | S = "static void __Block_byref_id_object_copy_"; |
| 4976 | S += utostr(flag); |
| 4977 | S += "(void *dst, void *src) {\n"; |
| 4978 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4979 | // offset into the object pointer is computed as: |
| 4980 | // void * + void* + int + int + void* + void * |
| 4981 | unsigned IntSize = |
| 4982 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4983 | unsigned VoidPtrSize = |
| 4984 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4985 | |
| 4986 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4987 | S += " _Block_object_assign((char*)dst + "; |
| 4988 | S += utostr(offset); |
| 4989 | S += ", *(void * *) ((char*)src + "; |
| 4990 | S += utostr(offset); |
| 4991 | S += "), "; |
| 4992 | S += utostr(flag); |
| 4993 | S += ");\n}\n"; |
| 4994 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4995 | S += "static void __Block_byref_id_object_dispose_"; |
| 4996 | S += utostr(flag); |
| 4997 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4998 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4999 | S += utostr(offset); |
| 5000 | S += "), "; |
| 5001 | S += utostr(flag); |
| 5002 | S += ");\n}\n"; |
| 5003 | return S; |
| 5004 | } |
| 5005 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5006 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5007 | /// the declaration into: |
| 5008 | /// struct __Block_byref_ND { |
| 5009 | /// void *__isa; // NULL for everything except __weak pointers |
| 5010 | /// struct __Block_byref_ND *__forwarding; |
| 5011 | /// int32_t __flags; |
| 5012 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5013 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5014 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5015 | /// typex ND; |
| 5016 | /// }; |
| 5017 | /// |
| 5018 | /// It then replaces declaration of ND variable with: |
| 5019 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5020 | /// __size=sizeof(struct __Block_byref_ND), |
| 5021 | /// ND=initializer-if-any}; |
| 5022 | /// |
| 5023 | /// |
| 5024 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5025 | // Insert declaration for the function in which block literal is |
| 5026 | // used. |
| 5027 | if (CurFunctionDeclToDeclareForBlock) |
| 5028 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5029 | int flag = 0; |
| 5030 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5031 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5032 | if (DeclLoc.isInvalid()) |
| 5033 | // If type location is missing, it is because of missing type (a warning). |
| 5034 | // Use variable's location which is good for this case. |
| 5035 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5036 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5037 | SourceLocation X = ND->getLocEnd(); |
| 5038 | X = SM->getInstantiationLoc(X); |
| 5039 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5040 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5041 | std::string ByrefType; |
| 5042 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5043 | ByrefType += " {\n"; |
| 5044 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5045 | RewriteByRefString(ByrefType, Name, ND); |
| 5046 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5047 | ByrefType += " int __flags;\n"; |
| 5048 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5049 | // Add void *__Block_byref_id_object_copy; |
| 5050 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5051 | QualType Ty = ND->getType(); |
| 5052 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5053 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5054 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5055 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5059 | ByrefType += " " + Name + ";\n"; |
| 5060 | ByrefType += "};\n"; |
| 5061 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5062 | SourceLocation FunLocStart; |
| 5063 | if (CurFunctionDef) |
| 5064 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5065 | else { |
| 5066 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5067 | FunLocStart = CurMethodDef->getLocStart(); |
| 5068 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5069 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5070 | if (Ty.isObjCGCWeak()) { |
| 5071 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5072 | isa = 1; |
| 5073 | } |
| 5074 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5075 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5076 | flag = BLOCK_BYREF_CALLER; |
| 5077 | QualType Ty = ND->getType(); |
| 5078 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5079 | if (Ty->isBlockPointerType()) |
| 5080 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5081 | else |
| 5082 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5083 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5084 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5085 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5086 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5087 | |
| 5088 | // struct __Block_byref_ND ND = |
| 5089 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5090 | // initializer-if-any}; |
| 5091 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5092 | unsigned flags = 0; |
| 5093 | if (HasCopyAndDispose) |
| 5094 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5095 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5096 | ByrefType.clear(); |
| 5097 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5098 | std::string ForwardingCastType("("); |
| 5099 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5100 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5101 | ByrefType += " " + Name + " = {(void*)"; |
| 5102 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5103 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5104 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5105 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5106 | ByrefType += "sizeof("; |
| 5107 | RewriteByRefString(ByrefType, Name, ND); |
| 5108 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5109 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5110 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5111 | ByrefType += utostr(flag); |
| 5112 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5113 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5114 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5115 | ByrefType += "};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5116 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5117 | } |
| 5118 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5119 | SourceLocation startLoc; |
| 5120 | Expr *E = ND->getInit(); |
| 5121 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5122 | startLoc = ECE->getLParenLoc(); |
| 5123 | else |
| 5124 | startLoc = E->getLocStart(); |
Fariborz Jahanian | b8646ed | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 5125 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5126 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5127 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5128 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5129 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5130 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5131 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5132 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5133 | ByrefType += "sizeof("; |
| 5134 | RewriteByRefString(ByrefType, Name, ND); |
| 5135 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5136 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5137 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5138 | ByrefType += utostr(flag); |
| 5139 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5140 | ByrefType += utostr(flag); |
| 5141 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5142 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5143 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5144 | |
| 5145 | // Complete the newly synthesized compound expression by inserting a right |
| 5146 | // curly brace before the end of the declaration. |
| 5147 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5148 | // also assumes there is only one declarator. For example, the following |
| 5149 | // isn't currently supported by this routine (in general): |
| 5150 | // |
| 5151 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5152 | // |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5153 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5154 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5155 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5156 | SourceLocation semiLoc = |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5157 | startLoc.getFileLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5158 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5159 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5160 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5161 | return; |
| 5162 | } |
| 5163 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5165 | // Add initializers for any closure decl refs. |
| 5166 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5167 | if (BlockDeclRefs.size()) { |
| 5168 | // Unique all "by copy" declarations. |
| 5169 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5170 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5171 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5172 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5173 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5174 | } |
| 5175 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5176 | // Unique all "by ref" declarations. |
| 5177 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5178 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5179 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5180 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5181 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5182 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5183 | } |
| 5184 | // Find any imported blocks...they will need special attention. |
| 5185 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5186 | if (BlockDeclRefs[i]->isByRef() || |
| 5187 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5188 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5189 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5190 | } |
| 5191 | } |
| 5192 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5193 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(llvm::StringRef name) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5194 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5195 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5196 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5197 | ID, FType, 0, SC_Extern, |
| 5198 | SC_None, false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5201 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
| 5202 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5203 | Blocks.push_back(Exp); |
| 5204 | |
| 5205 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5206 | |
| 5207 | // Add inner imported variables now used in current block. |
| 5208 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5209 | if (!InnerBlockDeclRefs.empty()) { |
| 5210 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5211 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5212 | ValueDecl *VD = Exp->getDecl(); |
| 5213 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5214 | // We need to save the copied-in variables in nested |
| 5215 | // blocks because it is needed at the end for some of the API generations. |
| 5216 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5217 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5218 | BlockDeclRefs.push_back(Exp); |
| 5219 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5220 | BlockByCopyDecls.push_back(VD); |
| 5221 | } |
| 5222 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5223 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5224 | BlockDeclRefs.push_back(Exp); |
| 5225 | BlockByRefDeclsPtrSet.insert(VD); |
| 5226 | BlockByRefDecls.push_back(VD); |
| 5227 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5228 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5229 | // Find any imported blocks...they will need special attention. |
| 5230 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5231 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5232 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5233 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5234 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5235 | } |
| 5236 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5237 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5238 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5239 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5240 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5241 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5242 | else if (CurMethodDef) |
| 5243 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5244 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5245 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5246 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5247 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5249 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5250 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5252 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 5253 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5254 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5255 | |
| 5256 | FunctionDecl *FD; |
| 5257 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5259 | // Simulate a contructor call... |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5260 | FD = SynthBlockInitFunctionDecl(Tag); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5261 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5262 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5263 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5264 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5265 | // Initialize the block function. |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5266 | FD = SynthBlockInitFunctionDecl(Func); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5267 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 5268 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5269 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5270 | CK_Unknown, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | InitExprs.push_back(castExpr); |
| 5272 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5273 | // Initialize the block descriptor. |
| 5274 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5276 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5277 | &Context->Idents.get(DescData.c_str()), |
| 5278 | Context->VoidPtrTy, 0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5279 | SC_Static, SC_None); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5280 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 5281 | new (Context) DeclRefExpr(NewVD, |
| 5282 | Context->VoidPtrTy, SourceLocation()), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5283 | UO_AddrOf, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5284 | Context->getPointerType(Context->VoidPtrTy), |
| 5285 | SourceLocation()); |
| 5286 | InitExprs.push_back(DescRefExpr); |
| 5287 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5288 | // Add initializers for any closure decl refs. |
| 5289 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5290 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5291 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5292 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5293 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5294 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5295 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5296 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5297 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5298 | if (HasLocalVariableExternalStorage(*I)) { |
| 5299 | QualType QT = (*I)->getType(); |
| 5300 | QT = Context->getPointerType(QT); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5301 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5302 | SourceLocation()); |
| 5303 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5304 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5305 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5306 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5307 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5308 | CK_Unknown, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5309 | } else { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5310 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5311 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5312 | if (HasLocalVariableExternalStorage(*I)) { |
| 5313 | QualType QT = (*I)->getType(); |
| 5314 | QT = Context->getPointerType(QT); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5315 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5316 | SourceLocation()); |
| 5317 | } |
| 5318 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5319 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5320 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5321 | } |
| 5322 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5323 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5324 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5325 | ValueDecl *ND = (*I); |
| 5326 | std::string Name(ND->getNameAsString()); |
| 5327 | std::string RecName; |
| 5328 | RewriteByRefString(RecName, Name, ND); |
| 5329 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5330 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5331 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5332 | SourceLocation(), II); |
| 5333 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5334 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5335 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5336 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5337 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5338 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | Context->getPointerType(Exp->getType()), |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5340 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5341 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_Unknown, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5342 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5343 | } |
| 5344 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5345 | if (ImportedBlockDecls.size()) { |
| 5346 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5347 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5348 | unsigned IntSize = |
| 5349 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5350 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5351 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5352 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5353 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5354 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5355 | FType, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5356 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5357 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5358 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5359 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_Unknown, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5360 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5361 | BlockDeclRefs.clear(); |
| 5362 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5363 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5364 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5365 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5366 | ImportedBlockDecls.clear(); |
| 5367 | return NewRep; |
| 5368 | } |
| 5369 | |
| 5370 | //===----------------------------------------------------------------------===// |
| 5371 | // Function Body / Expression rewriting |
| 5372 | //===----------------------------------------------------------------------===// |
| 5373 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5374 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5375 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5376 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5377 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5378 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5379 | // we get this right. |
| 5380 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5381 | // Perform a bottom up traversal of all children. |
| 5382 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5383 | CI != E; ++CI) |
| 5384 | if (*CI) |
| 5385 | CollectPropertySetters(*CI); |
| 5386 | |
| 5387 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5388 | if (BinOp->isAssignmentOp()) { |
Fariborz Jahanian | fef5d16 | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5389 | if (isa<ObjCPropertyRefExpr>(BinOp->getLHS()) || |
| 5390 | isa<ObjCImplicitSetterGetterRefExpr>(BinOp->getLHS())) |
| 5391 | PropSetters[BinOp->getLHS()] = BinOp; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5396 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5397 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5398 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5399 | Stmts.push_back(S); |
| 5400 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5401 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5402 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5405 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5406 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5407 | // Perform a bottom up rewrite of all children. |
| 5408 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5409 | CI != E; ++CI) |
| 5410 | if (*CI) { |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5411 | Stmt *newStmt; |
| 5412 | Stmt *S = (*CI); |
| 5413 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5414 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5415 | bool replaced = false; |
| 5416 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 5417 | if (replaced) { |
| 5418 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5419 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5420 | else |
| 5421 | ReplaceStmt(S, newStmt); |
| 5422 | } |
| 5423 | } |
| 5424 | else |
| 5425 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | if (newStmt) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5427 | *CI = newStmt; |
Fariborz Jahanian | 163488f | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5428 | // If dealing with an assignment with LHS being a property reference |
| 5429 | // expression, the entire assignment tree is rewritten into a property |
| 5430 | // setter messaging. This involvs the RHS too. Do not attempt to rewrite |
| 5431 | // RHS again. |
Fariborz Jahanian | fef5d16 | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5432 | if (Expr *Exp = dyn_cast<Expr>(S)) |
| 5433 | if (isa<ObjCPropertyRefExpr>(Exp) || |
| 5434 | isa<ObjCImplicitSetterGetterRefExpr>(Exp)) { |
| 5435 | if (PropSetters[Exp]) { |
| 5436 | ++CI; |
| 5437 | continue; |
| 5438 | } |
Fariborz Jahanian | 163488f | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5439 | } |
Fariborz Jahanian | fef5d16 | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5440 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5442 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5443 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5444 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5445 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5446 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5447 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5448 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5449 | // Rewrite the block body in place. |
| 5450 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5451 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5452 | // 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] | 5453 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5454 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5455 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5456 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5457 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5458 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5459 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5460 | return blockTranscribed; |
| 5461 | } |
| 5462 | // Handle specific things. |
| 5463 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5464 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5466 | if (isa<ObjCPropertyRefExpr>(S) || isa<ObjCImplicitSetterGetterRefExpr>(S)) { |
| 5467 | Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(S); |
| 5468 | assert(PropOrImplicitRefExpr && "Property or implicit setter/getter is null"); |
| 5469 | |
| 5470 | BinaryOperator *BinOp = PropSetters[PropOrImplicitRefExpr]; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5471 | if (BinOp) { |
| 5472 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5473 | // 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] | 5474 | DisableReplaceStmt = true; |
| 5475 | // Save the source range. Even if we disable the replacement, the |
| 5476 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5477 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5478 | // self.errorHandler = handler ? handler : |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5479 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5480 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5481 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5482 | DisableReplaceStmt = false; |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5483 | // |
| 5484 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5485 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5486 | // to see the original expression). Consider this example: |
| 5487 | // |
| 5488 | // Foo *obj1, *obj2; |
| 5489 | // |
| 5490 | // obj1.i = [obj2 rrrr]; |
| 5491 | // |
| 5492 | // 'BinOp' for the previous expression looks like: |
| 5493 | // |
| 5494 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5495 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5496 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5497 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5498 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5499 | // |
| 5500 | // 'newStmt' represents the rewritten message expression. For example: |
| 5501 | // |
| 5502 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5503 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5504 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5505 | // (CStyleCastExpr 0x231d220 'void *' |
| 5506 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5507 | // |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5508 | // Note that 'newStmt' is passed to RewritePropertyOrImplicitSetter so that it |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5509 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5510 | // the original RHS (since we haven't altered BinOp). |
| 5511 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5512 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5513 | // node. As a result, we now leak the original AST nodes. |
| 5514 | // |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5515 | return RewritePropertyOrImplicitSetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5516 | } else { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5517 | return RewritePropertyOrImplicitGetter(PropOrImplicitRefExpr); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5518 | } |
| 5519 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5520 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5521 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5522 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5523 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5524 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5525 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5527 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5528 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5529 | // Before we rewrite it, put the original message expression in a comment. |
| 5530 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5531 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5533 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5534 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5535 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5536 | std::string messString; |
| 5537 | messString += "// "; |
| 5538 | messString.append(startBuf, endBuf-startBuf+1); |
| 5539 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5540 | |
| 5541 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5542 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5543 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5544 | // Tried this, but it didn't work either... |
| 5545 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5546 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5547 | return RewriteMessageExpr(MessExpr); |
| 5548 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5549 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5550 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5551 | return RewriteObjCTryStmt(StmtTry); |
| 5552 | |
| 5553 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5554 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5555 | |
| 5556 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5557 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5559 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5560 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
| 5562 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5563 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5564 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5565 | OrigStmtRange.getEnd()); |
| 5566 | if (BreakStmt *StmtBreakStmt = |
| 5567 | dyn_cast<BreakStmt>(S)) |
| 5568 | return RewriteBreakStmt(StmtBreakStmt); |
| 5569 | if (ContinueStmt *StmtContinueStmt = |
| 5570 | dyn_cast<ContinueStmt>(S)) |
| 5571 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5572 | |
| 5573 | // 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] | 5574 | // and cast exprs. |
| 5575 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5576 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5577 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5578 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5579 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5580 | // the context of an ObjCForCollectionStmt. For example: |
| 5581 | // NSArray *someArray; |
| 5582 | // for (id <FooProtocol> index in someArray) ; |
| 5583 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5584 | // and it depends on the original text locations/positions. |
Benjamin Kramer | acc5fa1 | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5585 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5586 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5588 | // Blocks rewrite rules. |
| 5589 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5590 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5591 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5592 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5593 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5594 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5596 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5597 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5598 | if (VD->hasAttr<BlocksAttr>()) { |
| 5599 | static unsigned uniqueByrefDeclCount = 0; |
| 5600 | assert(!BlockByRefDeclNo.count(ND) && |
| 5601 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5602 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5603 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5604 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5605 | else |
| 5606 | RewriteTypeOfDecl(VD); |
| 5607 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5608 | } |
| 5609 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5610 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5611 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5612 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5613 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5614 | } |
| 5615 | } |
| 5616 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5617 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5618 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5619 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | |
| 5621 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5622 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5623 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5624 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5625 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5626 | && "Statement stack mismatch"); |
| 5627 | Stmts.pop_back(); |
| 5628 | } |
| 5629 | // Handle blocks rewriting. |
| 5630 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5631 | if (BDRE->isByRef()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5632 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5633 | } |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5634 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5635 | ValueDecl *VD = DRE->getDecl(); |
| 5636 | if (VD->hasAttr<BlocksAttr>()) |
| 5637 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5638 | if (HasLocalVariableExternalStorage(VD)) |
| 5639 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5642 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5643 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5644 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5645 | ReplaceStmt(S, BlockCall); |
| 5646 | return BlockCall; |
| 5647 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5648 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5649 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5650 | RewriteCastExpr(CE); |
| 5651 | } |
| 5652 | #if 0 |
| 5653 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5654 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5655 | ICE->getSubExpr(), |
| 5656 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5657 | // Get the new text. |
| 5658 | std::string SStr; |
| 5659 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 0905f14 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5660 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5661 | const std::string &Str = Buf.str(); |
| 5662 | |
| 5663 | printf("CAST = %s\n", &Str[0]); |
| 5664 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5665 | delete S; |
| 5666 | return Replacement; |
| 5667 | } |
| 5668 | #endif |
| 5669 | // Return this stmt unmodified. |
| 5670 | return S; |
| 5671 | } |
| 5672 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5673 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5674 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5675 | e = RD->field_end(); i != e; ++i) { |
| 5676 | FieldDecl *FD = *i; |
| 5677 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5678 | RewriteBlockPointerDecl(FD); |
| 5679 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5680 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5681 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5682 | } |
| 5683 | } |
| 5684 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5685 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5686 | /// main file of the input. |
| 5687 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5688 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | b136888 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5689 | if (FD->isOverloadedOperator()) |
| 5690 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5692 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5693 | // prototype. This enables us to rewrite function declarations and |
| 5694 | // definitions using the same code. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5695 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5696 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5697 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 568bc84 | 2010-07-07 11:31:34 +0000 | [diff] [blame] | 5698 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5699 | CurFunctionDef = FD; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5700 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5701 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5702 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5703 | Body = |
| 5704 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5705 | FD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5706 | CurrentBody = 0; |
| 5707 | if (PropParentMap) { |
| 5708 | delete PropParentMap; |
| 5709 | PropParentMap = 0; |
| 5710 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5711 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5712 | // and any copy/dispose helper functions. |
| 5713 | InsertBlockLiteralsWithinFunction(FD); |
| 5714 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5715 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5716 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5717 | return; |
| 5718 | } |
| 5719 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5720 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5721 | CurMethodDef = MD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5722 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5723 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5724 | Body = |
| 5725 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5726 | MD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5727 | CurrentBody = 0; |
| 5728 | if (PropParentMap) { |
| 5729 | delete PropParentMap; |
| 5730 | PropParentMap = 0; |
| 5731 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5732 | InsertBlockLiteralsWithinMethod(MD); |
| 5733 | CurMethodDef = 0; |
| 5734 | } |
| 5735 | } |
| 5736 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5737 | ClassImplementation.push_back(CI); |
| 5738 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5739 | CategoryImplementation.push_back(CI); |
| 5740 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5741 | RewriteForwardClassDecl(CD); |
| 5742 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5743 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5744 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5745 | RewriteBlockPointerDecl(VD); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5746 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5747 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5748 | if (VD->getInit()) { |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5749 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5750 | RewriteCastExpr(CE); |
| 5751 | } |
| 5752 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5753 | } else if (VD->getType()->isRecordType()) { |
| 5754 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5755 | if (RD->isDefinition()) |
| 5756 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5757 | } |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5758 | if (VD->getInit()) { |
| 5759 | GlobalVarDecl = VD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5760 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5761 | CurrentBody = VD->getInit(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5762 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5763 | CurrentBody = 0; |
| 5764 | if (PropParentMap) { |
| 5765 | delete PropParentMap; |
| 5766 | PropParentMap = 0; |
| 5767 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5768 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5769 | VD->getName()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5770 | GlobalVarDecl = 0; |
| 5771 | |
| 5772 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5773 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5774 | RewriteCastExpr(CE); |
| 5775 | } |
| 5776 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5777 | return; |
| 5778 | } |
| 5779 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5780 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5781 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5782 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5783 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5784 | return; |
| 5785 | } |
| 5786 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5787 | if (RD->isDefinition()) |
| 5788 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5789 | return; |
| 5790 | } |
| 5791 | // Nothing yet. |
| 5792 | } |
| 5793 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5794 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5795 | if (Diags.hasErrorOccurred()) |
| 5796 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5797 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5798 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5799 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5800 | // Here's a great place to add any extra declarations that may be needed. |
| 5801 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5802 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5803 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5804 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5805 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5806 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5807 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5808 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5809 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5810 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5811 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5813 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5814 | //printf("Changed:\n"); |
| 5815 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5816 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5817 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5818 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5819 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5820 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5821 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5822 | // Rewrite Objective-c meta data* |
| 5823 | std::string ResultStr; |
| 5824 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5825 | // Emit metadata. |
| 5826 | *OutFile << ResultStr; |
| 5827 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5828 | OutFile->flush(); |
| 5829 | } |