Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 35 | enum { |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 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 */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 42 | helpers */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 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 | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 57 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 58 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 59 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 60 | unsigned RewriteFailedDiag; |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 61 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 63 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 64 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 65 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 66 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 67 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 68 | SourceLocation LastIncLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 70 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 71 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 72 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 74 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 75 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 76 | SmallVector<Stmt *, 32> Stmts; |
| 77 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 78 | // Remember all the @protocol(<expr>) expressions. |
| 79 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 80 | |
| 81 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 82 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 83 | unsigned NumObjCStringLiterals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 87 | FunctionDecl *MsgSendStretFunctionDecl; |
| 88 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 89 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 90 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 91 | FunctionDecl *GetMetaClassFunctionDecl; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 92 | FunctionDecl *GetSuperClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 93 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 94 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 95 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 97 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 98 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 99 | RecordDecl *NSStringRecord; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 101 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 102 | int BcLabelCount; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 104 | // Needed for super. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 105 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 106 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 107 | RecordDecl *ConstantStringDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 109 | TypeDecl *ProtocolTypeDecl; |
| 110 | QualType getProtocolType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 112 | // Needed for header files being rewritten |
| 113 | bool IsHeader; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 115 | std::string InFileName; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 116 | raw_ostream* OutFile; |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 117 | |
| 118 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 119 | bool objc_impl_method; |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 120 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 121 | std::string Preamble; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 122 | |
| 123 | // Block expressions. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 124 | SmallVector<BlockExpr *, 32> Blocks; |
| 125 | SmallVector<int, 32> InnerDeclRefsCount; |
| 126 | SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 130 | // Block related declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 131 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 134 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 135 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 136 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 137 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 138 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 139 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 140 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 141 | // This maps a property to it's assignment statement. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 142 | llvm::DenseMap<Expr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 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 | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 145 | llvm::DenseMap<Expr *, Stmt *> PropGetters; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Steve Naroff | 4c3580e | 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 | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 151 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 152 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 153 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 154 | VarDecl *GlobalVarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 156 | bool DisableReplaceStmt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 158 | static const int OBJC_ABI_VERSION = 7; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 159 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 160 | virtual void Initialize(ASTContext &context); |
| 161 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 162 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 163 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 164 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 165 | if (isa<ObjCClassDecl>((*I))) { |
| 166 | RewriteForwardClassDecl(D); |
| 167 | break; |
| 168 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 169 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 171 | } |
| 172 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 173 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 174 | RewriteObjC(std::string inFile, raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 175 | Diagnostic &D, const LangOptions &LOpts, |
| 176 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 177 | |
| 178 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 180 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 182 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 183 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 185 | if (ReplacingStmt) |
| 186 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 187 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 188 | if (DisableReplaceStmt) |
| 189 | return; // Used when rewriting the assignment of a property setter. |
| 190 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 191 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 192 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 193 | ReplacedNodes[Old] = New; |
| 194 | return; |
| 195 | } |
| 196 | if (SilenceRewriteMacroWarning) |
| 197 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 198 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 199 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 200 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 201 | |
| 202 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 203 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 204 | int Size = Rewrite.getRangeSize(SrcRange); |
| 205 | if (Size == -1) { |
| 206 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 207 | << Old->getSourceRange(); |
| 208 | return; |
| 209 | } |
| 210 | // Get the new text. |
| 211 | std::string SStr; |
| 212 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 213 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 214 | const std::string &Str = S.str(); |
| 215 | |
| 216 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 217 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 218 | ReplacedNodes[Old] = New; |
| 219 | return; |
| 220 | } |
| 221 | if (SilenceRewriteMacroWarning) |
| 222 | return; |
| 223 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 224 | << Old->getSourceRange(); |
| 225 | } |
| 226 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 227 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 228 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 229 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 230 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 231 | SilenceRewriteMacroWarning) |
| 232 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 234 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 237 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 239 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 240 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 241 | SilenceRewriteMacroWarning) |
| 242 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 244 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 247 | // Syntactic Rewriting. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 248 | void RewriteInclude(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 249 | void RewriteForwardClassDecl(DeclGroupRef D); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 250 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 251 | void RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl, |
| 252 | const std::string &typedefString); |
| 253 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 254 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 255 | ObjCImplementationDecl *IMD, |
| 256 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 257 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 258 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 259 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 260 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 261 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 262 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 263 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 264 | ValueDecl *VD, bool def=false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 266 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 267 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 268 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 269 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 270 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 271 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 272 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 273 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 274 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 275 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 276 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 277 | bool needToScanForQualifiers(QualType T); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 278 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 279 | QualType getConstantStringStructType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 280 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 281 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 283 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 284 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 285 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 287 | Stmt *CurrentBody; |
| 288 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 290 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 291 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 292 | bool &replaced); |
| 293 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 294 | Stmt *RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr); |
| 295 | Stmt *RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 296 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 297 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 298 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 299 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 300 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 301 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 302 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 303 | void RewriteTryReturnStmts(Stmt *S); |
| 304 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 305 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 306 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 307 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 308 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 309 | SourceLocation OrigEnd); |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 310 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 312 | Expr **args, unsigned nargs, |
| 313 | SourceLocation StartLoc=SourceLocation(), |
| 314 | SourceLocation EndLoc=SourceLocation()); |
| 315 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 316 | SourceLocation StartLoc=SourceLocation(), |
| 317 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 318 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 319 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 320 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 322 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 323 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 324 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 325 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 326 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 327 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 328 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 329 | void SynthGetSuperClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 330 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 331 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 333 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 334 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 335 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 337 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 338 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 340 | template<typename MethodIterator> |
| 341 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 342 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 343 | bool IsInstanceMethod, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 344 | StringRef prefix, |
| 345 | StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 346 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 348 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 349 | StringRef prefix, |
| 350 | StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 351 | std::string &Result); |
| 352 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 353 | StringRef prefix, |
| 354 | StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 355 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 356 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 357 | std::string &Result); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 358 | void SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 359 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 360 | void RewriteImplementations(); |
| 361 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 363 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 365 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 367 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 368 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
| 370 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 371 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 372 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 373 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 374 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 375 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 376 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
| 378 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 379 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 381 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 382 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 383 | std::string Tag, std::string Desc); |
| 384 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 385 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 386 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 387 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 388 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 389 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 390 | StringRef FunName); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 391 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 393 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 394 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 395 | void GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 396 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 397 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 399 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 400 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 401 | bool isTopLevelBlockPointerType(QualType T) { |
| 402 | return isa<BlockPointerType>(T); |
| 403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 405 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 406 | /// to a function pointer type and upon success, returns true; false |
| 407 | /// otherwise. |
| 408 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 409 | if (isTopLevelBlockPointerType(T)) { |
| 410 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 411 | T = Context->getPointerType(BPT->getPointeeType()); |
| 412 | return true; |
| 413 | } |
| 414 | return false; |
| 415 | } |
| 416 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 417 | void convertToUnqualifiedObjCType(QualType &T) { |
| 418 | if (T->isObjCQualifiedIdType()) |
| 419 | T = Context->getObjCIdType(); |
| 420 | else if (T->isObjCQualifiedClassType()) |
| 421 | T = Context->getObjCClassType(); |
| 422 | else if (T->isObjCObjectPointerType() && |
| 423 | T->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 424 | T = Context->getObjCIdType(); |
| 425 | } |
| 426 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 427 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 428 | bool isObjCType(QualType T) { |
| 429 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 430 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 432 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 434 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 435 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 436 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 438 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 440 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 441 | return true; |
| 442 | } |
| 443 | return false; |
| 444 | } |
| 445 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 446 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 447 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 448 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 449 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 451 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 452 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 453 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 455 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 457 | if (From[i] == '"') |
| 458 | To += "\\\""; |
| 459 | else |
| 460 | To += From[i]; |
| 461 | } |
| 462 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 463 | |
| 464 | QualType getSimpleFunctionType(QualType result, |
| 465 | const QualType *args, |
| 466 | unsigned numArgs, |
| 467 | bool variadic = false) { |
| 468 | FunctionProtoType::ExtProtoInfo fpi; |
| 469 | fpi.Variadic = variadic; |
| 470 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 471 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 472 | }; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 473 | |
| 474 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 475 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 476 | CastKind Kind, Expr *E) { |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 477 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 478 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 479 | SourceLocation(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 480 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 484 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 485 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 486 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 488 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 489 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 490 | // All the args are checked/rewritten. Don't call twice! |
| 491 | RewriteBlockPointerDecl(D); |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 498 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 499 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 500 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 503 | static bool IsHeaderFile(const std::string &Filename) { |
| 504 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 506 | if (DotPos == std::string::npos) { |
| 507 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 511 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 512 | // C header: .h |
| 513 | // C++ header: .hh or .H; |
| 514 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 517 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 518 | Diagnostic &D, const LangOptions &LOpts, |
| 519 | bool silenceMacroWarn) |
| 520 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 521 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 522 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 524 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 526 | "rewriter doesn't support user-specified control flow semantics " |
| 527 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 530 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 531 | raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 533 | const LangOptions &LOpts, |
| 534 | bool SilenceRewriteMacroWarning) { |
| 535 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 536 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 537 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 538 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 539 | Context = &context; |
| 540 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 541 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 542 | MsgSendFunctionDecl = 0; |
| 543 | MsgSendSuperFunctionDecl = 0; |
| 544 | MsgSendStretFunctionDecl = 0; |
| 545 | MsgSendSuperStretFunctionDecl = 0; |
| 546 | MsgSendFpretFunctionDecl = 0; |
| 547 | GetClassFunctionDecl = 0; |
| 548 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 549 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 550 | SelGetUidFunctionDecl = 0; |
| 551 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 552 | ConstantStringClassReference = 0; |
| 553 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 554 | CurMethodDef = 0; |
| 555 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 556 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 557 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 558 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 559 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 560 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 561 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 562 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 563 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 564 | PropParentMap = 0; |
| 565 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 566 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 567 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 569 | // Get the ID and start/end of the main file. |
| 570 | MainFileID = SM->getMainFileID(); |
| 571 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 572 | MainFileStart = MainBuf->getBufferStart(); |
| 573 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 575 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 577 | // declaring objc_selector outside the parameter list removes a silly |
| 578 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 579 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 580 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 581 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 582 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 583 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 584 | if (LangOpts.Microsoft) { |
| 585 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 586 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 587 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 588 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 589 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 590 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 591 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 592 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 593 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 594 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 595 | if (LangOpts.Microsoft) { |
| 596 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 597 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 598 | } else |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 599 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 600 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 601 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 602 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 603 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 604 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 605 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 606 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 607 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 608 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 609 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 610 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 611 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 612 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 613 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 614 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 615 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 616 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 617 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 618 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 619 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 620 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 621 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 622 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 623 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 624 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 625 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 626 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 627 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 628 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 629 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 630 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 631 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 632 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 633 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 634 | Preamble += "#endif\n"; |
| 635 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 636 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 637 | Preamble += " int *isa;\n"; |
| 638 | Preamble += " int flags;\n"; |
| 639 | Preamble += " char *str;\n"; |
| 640 | Preamble += " long length;\n"; |
| 641 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 642 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 643 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 644 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 645 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 646 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 647 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 648 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 649 | // Blocks preamble. |
| 650 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 651 | Preamble += "#define BLOCK_IMPL\n"; |
| 652 | Preamble += "struct __block_impl {\n"; |
| 653 | Preamble += " void *isa;\n"; |
| 654 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 655 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 656 | Preamble += " void *FuncPtr;\n"; |
| 657 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 658 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 659 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 660 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 661 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 662 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 663 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 664 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 665 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 666 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 667 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 668 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 669 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 670 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 671 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 672 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 673 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 674 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 675 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 676 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 677 | Preamble += "#endif\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 678 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 679 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 680 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 681 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 682 | Preamble += "#define __weak\n"; |
| 683 | } |
Fariborz Jahanian | df49652 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 684 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 685 | // as this avoids warning in any 64bit/32bit compilation model. |
| 686 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 690 | //===----------------------------------------------------------------------===// |
| 691 | // Top Level Driver Code |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 694 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 695 | if (Diags.hasErrorOccurred()) |
| 696 | return; |
| 697 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 698 | // Two cases: either the decl could be in the main file, or it could be in a |
| 699 | // #included file. If the former, rewrite it now. If the later, check to see |
| 700 | // if we rewrote the #include/#import. |
| 701 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 702 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 704 | // If this is for a builtin, ignore it. |
| 705 | if (Loc.isInvalid()) return; |
| 706 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 707 | // Look for built-in declarations that we need to refer during the rewrite. |
| 708 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 709 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 710 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 711 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 712 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 713 | ConstantStringClassReference = FVD; |
| 714 | return; |
| 715 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 716 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 717 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 718 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 719 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 720 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 721 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 723 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 724 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 725 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 726 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 727 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 728 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 729 | DI != DIEnd; ) { |
| 730 | if (isa<ObjCClassDecl>((*DI))) { |
| 731 | SmallVector<Decl *, 8> DG; |
| 732 | Decl *D = (*DI); |
| 733 | SourceLocation Loc = D->getLocation(); |
| 734 | while (DI != DIEnd && |
| 735 | isa<ObjCClassDecl>(D) && D->getLocation() == Loc) { |
| 736 | DG.push_back(D); |
| 737 | ++DI; |
| 738 | D = (*DI); |
| 739 | } |
| 740 | RewriteForwardClassDecl(DG); |
| 741 | continue; |
| 742 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 743 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 744 | ++DI; |
| 745 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 746 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 747 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | cf7e958 | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 748 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 749 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 752 | //===----------------------------------------------------------------------===// |
| 753 | // Syntactic (non-AST) Rewriting Code |
| 754 | //===----------------------------------------------------------------------===// |
| 755 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 756 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 757 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 758 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 759 | const char *MainBufStart = MainBuf.begin(); |
| 760 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 761 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 763 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 764 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 765 | if (*BufPtr == '#') { |
| 766 | if (++BufPtr == MainBufEnd) |
| 767 | return; |
| 768 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 769 | if (++BufPtr == MainBufEnd) |
| 770 | return; |
| 771 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 772 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | SourceLocation ImportLoc = |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 774 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 775 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 776 | BufPtr += ImportLen; |
| 777 | } |
| 778 | } |
| 779 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 782 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 783 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 784 | std::string S; |
| 785 | S = "((struct "; |
| 786 | S += ClassDecl->getIdentifier()->getName(); |
| 787 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 788 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 789 | return S; |
| 790 | } |
| 791 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 792 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 793 | ObjCImplementationDecl *IMD, |
| 794 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 795 | static bool objcGetPropertyDefined = false; |
| 796 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 797 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 798 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 799 | const char *startBuf = SM->getCharacterData(startLoc); |
| 800 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 801 | const char *semiBuf = strchr(startBuf, ';'); |
| 802 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 803 | SourceLocation onePastSemiLoc = |
| 804 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 805 | |
| 806 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 807 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 809 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 810 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 811 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 813 | if (!OID) |
| 814 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 815 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 816 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 817 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 818 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 819 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 820 | std::string Getr; |
| 821 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 822 | objcGetPropertyDefined = true; |
| 823 | // FIXME. Is this attribute correct in all cases? |
| 824 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 825 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 826 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 827 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 828 | PD->getGetterMethodDecl(), Getr); |
| 829 | Getr += "{ "; |
| 830 | // Synthesize an explicit cast to gain access to the ivar. |
| 831 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 832 | if (GenGetProperty) { |
| 833 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 834 | Getr += "typedef "; |
| 835 | const FunctionType *FPRetType = 0; |
| 836 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 837 | FPRetType); |
| 838 | Getr += " _TYPE"; |
| 839 | if (FPRetType) { |
| 840 | Getr += ")"; // close the precedence "scope" for "*". |
| 841 | |
| 842 | // Now, emit the argument types (if any). |
| 843 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 844 | Getr += "("; |
| 845 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 846 | if (i) Getr += ", "; |
| 847 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 848 | Context->PrintingPolicy); |
| 849 | Getr += ParamStr; |
| 850 | } |
| 851 | if (FT->isVariadic()) { |
| 852 | if (FT->getNumArgs()) Getr += ", "; |
| 853 | Getr += "..."; |
| 854 | } |
| 855 | Getr += ")"; |
| 856 | } else |
| 857 | Getr += "()"; |
| 858 | } |
| 859 | Getr += ";\n"; |
| 860 | Getr += "return (_TYPE)"; |
| 861 | Getr += "objc_getProperty(self, _cmd, "; |
| 862 | SynthesizeIvarOffsetComputation(OID, Getr); |
| 863 | Getr += ", 1)"; |
| 864 | } |
| 865 | else |
| 866 | Getr += "return " + getIvarAccessString(OID); |
| 867 | Getr += "; }"; |
| 868 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 869 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 870 | |
| 871 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 872 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 874 | // Generate the 'setter' function. |
| 875 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 876 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 877 | ObjCPropertyDecl::OBJC_PR_copy); |
| 878 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 879 | objcSetPropertyDefined = true; |
| 880 | // FIXME. Is this attribute correct in all cases? |
| 881 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 882 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 883 | } |
| 884 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 885 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 886 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 887 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 888 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 889 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 890 | if (GenSetProperty) { |
| 891 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 892 | SynthesizeIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 893 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 894 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 895 | Setr += ", "; |
| 896 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 897 | Setr += "0, "; |
| 898 | else |
| 899 | Setr += "1, "; |
| 900 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 901 | Setr += "1)"; |
| 902 | else |
| 903 | Setr += "0)"; |
| 904 | } |
| 905 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 906 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 907 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 908 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 909 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 910 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 911 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 912 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 913 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 914 | std::string &typedefString) { |
| 915 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 916 | typedefString += ForwardDecl->getNameAsString(); |
| 917 | typedefString += "\n"; |
| 918 | typedefString += "#define _REWRITER_typedef_"; |
| 919 | typedefString += ForwardDecl->getNameAsString(); |
| 920 | typedefString += "\n"; |
| 921 | typedefString += "typedef struct objc_object "; |
| 922 | typedefString += ForwardDecl->getNameAsString(); |
| 923 | typedefString += ";\n#endif\n"; |
| 924 | } |
| 925 | |
| 926 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl, |
| 927 | const std::string &typedefString) { |
| 928 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 929 | const char *startBuf = SM->getCharacterData(startLoc); |
| 930 | const char *semiPtr = strchr(startBuf, ';'); |
| 931 | // Replace the @class with typedefs corresponding to the classes. |
| 932 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 933 | } |
| 934 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 935 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 936 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 937 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 938 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(*I); |
| 939 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 940 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 941 | // Translate to typedef's that forward reference structs with the same name |
| 942 | // as the class. As a convenience, we include the original declaration |
| 943 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 944 | typedefString += "// @class "; |
| 945 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 946 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 947 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 948 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 949 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 950 | DeclGroupRef::iterator I = D.begin(); |
| 951 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(*I), typedefString); |
| 952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame^] | 954 | void RewriteObjC::RewriteForwardClassDecl( |
| 955 | const llvm::SmallVector<Decl*, 8> &D) { |
| 956 | std::string typedefString; |
| 957 | for (unsigned i = 0; i < D.size(); i++) { |
| 958 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(D[i]); |
| 959 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 960 | if (i == 0) { |
| 961 | typedefString += "// @class "; |
| 962 | typedefString += ForwardDecl->getNameAsString(); |
| 963 | typedefString += ";\n"; |
| 964 | } |
| 965 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 966 | } |
| 967 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 970 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 971 | // When method is a synthesized one, such as a getter/setter there is |
| 972 | // nothing to rewrite. |
| 973 | if (Method->isSynthesized()) |
| 974 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 975 | SourceLocation LocStart = Method->getLocStart(); |
| 976 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 978 | if (SM->getExpansionLineNumber(LocEnd) > |
| 979 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 980 | InsertText(LocStart, "#if 0\n"); |
| 981 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 982 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 983 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 988 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 990 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 991 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 994 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 995 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 997 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 998 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 1000 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1001 | E = CatDecl->prop_end(); I != E; ++I) |
| 1002 | RewriteProperty(*I); |
| 1003 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | for (ObjCCategoryDecl::instmeth_iterator |
| 1005 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1006 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1007 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1009 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1010 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1011 | RewriteMethodDeclaration(*I); |
| 1012 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1013 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1014 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1015 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1018 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1019 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1021 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1022 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | |
| 1024 | for (ObjCProtocolDecl::instmeth_iterator |
| 1025 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1026 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1027 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1028 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1029 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1030 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1031 | RewriteMethodDeclaration(*I); |
| 1032 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1033 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1034 | E = PDecl->prop_end(); I != E; ++I) |
| 1035 | RewriteProperty(*I); |
| 1036 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1037 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1038 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1039 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1041 | // Must comment out @optional/@required |
| 1042 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1043 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1044 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1045 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1046 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1047 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1049 | } |
| 1050 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1051 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1052 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1058 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1059 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1060 | if (LocStart.isInvalid()) |
| 1061 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1062 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1063 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1066 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1067 | const FunctionType *&FPRetType) { |
| 1068 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1069 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1070 | else if (T->isFunctionPointerType() || |
| 1071 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1072 | // needs special handling, since pointer-to-functions have special |
| 1073 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1074 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1075 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1076 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1077 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1078 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1079 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1080 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1081 | ResultStr += FPRetType->getResultType().getAsString( |
| 1082 | Context->PrintingPolicy); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1083 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1086 | ResultStr += T.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1089 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1090 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1091 | std::string &ResultStr) { |
| 1092 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1093 | const FunctionType *FPRetType = 0; |
| 1094 | ResultStr += "\nstatic "; |
| 1095 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1096 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1098 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1099 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1101 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1102 | NameStr += "_I_"; |
| 1103 | else |
| 1104 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1106 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1107 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1110 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1111 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1112 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1115 | { |
| 1116 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1117 | int len = selString.size(); |
| 1118 | for (int i = 0; i < len; i++) |
| 1119 | if (selString[i] == ':') |
| 1120 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1121 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1122 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1123 | // Remember this name for metadata emission |
| 1124 | MethodInternalNames[OMD] = NameStr; |
| 1125 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1127 | // Rewrite arguments |
| 1128 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1130 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1131 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1132 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1133 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1134 | if (!LangOpts.Microsoft) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1135 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1136 | ResultStr += "struct "; |
| 1137 | } |
| 1138 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1139 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1140 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1141 | } |
| 1142 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1143 | ResultStr += Context->getObjCClassType().getAsString( |
| 1144 | Context->PrintingPolicy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1146 | ResultStr += " self, "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1147 | ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1148 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1150 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1151 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1152 | E = OMD->param_end(); PI != E; ++PI) { |
| 1153 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1154 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1155 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1156 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1157 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1158 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1159 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1160 | QualType QT = PDecl->getType(); |
| 1161 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1162 | if (convertBlockPointerToFunctionPointer(QT)) |
| 1163 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
| 1164 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1165 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1166 | ResultStr += Name; |
| 1167 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1168 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1169 | if (OMD->isVariadic()) |
| 1170 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1171 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1173 | if (FPRetType) { |
| 1174 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1176 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1177 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1178 | ResultStr += "("; |
| 1179 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1180 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1181 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1182 | Context->PrintingPolicy); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1183 | ResultStr += ParamStr; |
| 1184 | } |
| 1185 | if (FT->isVariadic()) { |
| 1186 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1187 | ResultStr += "..."; |
| 1188 | } |
| 1189 | ResultStr += ")"; |
| 1190 | } else { |
| 1191 | ResultStr += "()"; |
| 1192 | } |
| 1193 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1194 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1195 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1196 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1197 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1199 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1201 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1202 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1203 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1204 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1205 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1206 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1207 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1208 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1209 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1210 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1211 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1212 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1213 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1216 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1217 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1218 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1219 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1220 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1221 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1222 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1223 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1224 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1226 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1227 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1228 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1229 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1230 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1231 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1233 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1234 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1237 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1240 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1241 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1242 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1243 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1244 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1245 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1246 | ResultStr += "\n"; |
| 1247 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1248 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1249 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1250 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1251 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1252 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1253 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1254 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1255 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1256 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
| 1258 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1259 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1260 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1262 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1263 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1264 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1266 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1267 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1268 | RewriteMethodDeclaration(*I); |
| 1269 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1270 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1271 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1272 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1275 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1276 | SourceRange SrcRange) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1277 | ObjCMethodDecl *OMD = 0; |
| 1278 | QualType Ty; |
| 1279 | Selector Sel; |
Duncan Sands | 54bd457 | 2010-10-20 08:21:16 +0000 | [diff] [blame] | 1280 | Stmt *Receiver = 0; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1281 | bool Super = false; |
| 1282 | QualType SuperTy; |
| 1283 | SourceLocation SuperLocation; |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1284 | SourceLocation SelectorLoc; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1285 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ObjCImplicitSetterGetterRefExpr. |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1286 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1287 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1288 | dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) { |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1289 | SelectorLoc = PropRefExpr->getLocation(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1290 | if (PropRefExpr->isExplicitProperty()) { |
| 1291 | ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty(); |
| 1292 | OMD = PDecl->getSetterMethodDecl(); |
| 1293 | Ty = PDecl->getType(); |
| 1294 | Sel = PDecl->getSetterName(); |
| 1295 | } else { |
| 1296 | OMD = PropRefExpr->getImplicitPropertySetter(); |
| 1297 | Sel = OMD->getSelector(); |
| 1298 | Ty = PropRefExpr->getType(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1299 | } |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1300 | Super = PropRefExpr->isSuperReceiver(); |
| 1301 | if (!Super) { |
| 1302 | Receiver = PropRefExpr->getBase(); |
| 1303 | } else { |
| 1304 | SuperTy = PropRefExpr->getSuperReceiverType(); |
| 1305 | SuperLocation = PropRefExpr->getReceiverLocation(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1306 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | assert(OMD && "RewritePropertyOrImplicitSetter - null OMD"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1310 | SmallVector<Expr *, 1> ExprVec; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1311 | ExprVec.push_back(newStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1313 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1314 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1315 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1316 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1317 | Expr::getValueKindForType(Ty), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1318 | /*FIXME?*/SourceLocation(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1319 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1320 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1321 | SuperTy, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1322 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1323 | &ExprVec[0], 1, |
| 1324 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1325 | else { |
| 1326 | // FIXME. Refactor this into common code with that in |
| 1327 | // RewritePropertyOrImplicitGetter |
| 1328 | assert(Receiver && "RewritePropertyOrImplicitSetter - null Receiver"); |
| 1329 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1330 | if (PropGetters[Exp]) |
| 1331 | // This allows us to handle chain/nested property/implicit getters. |
| 1332 | Receiver = PropGetters[Exp]; |
| 1333 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1334 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1335 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1336 | Expr::getValueKindForType(Ty), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1337 | /*FIXME: */SourceLocation(), |
| 1338 | cast<Expr>(Receiver), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1339 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1340 | &ExprVec[0], 1, |
| 1341 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1342 | } |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1343 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1345 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1346 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1347 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1348 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1349 | // to things that stay around. |
| 1350 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1351 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1354 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr) { |
| 1355 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ImplicitGetter. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1356 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Ted Kremenek | 3b562af | 2010-10-19 23:10:22 +0000 | [diff] [blame] | 1357 | Stmt *Receiver = 0; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1358 | ObjCMethodDecl *OMD = 0; |
| 1359 | QualType Ty; |
| 1360 | Selector Sel; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1361 | bool Super = false; |
| 1362 | QualType SuperTy; |
| 1363 | SourceLocation SuperLocation; |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1364 | SourceLocation SelectorLoc; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1365 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1366 | dyn_cast<ObjCPropertyRefExpr>(PropOrGetterRefExpr)) { |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1367 | SelectorLoc = PropRefExpr->getLocation(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1368 | if (PropRefExpr->isExplicitProperty()) { |
| 1369 | ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty(); |
| 1370 | OMD = PDecl->getGetterMethodDecl(); |
| 1371 | Ty = PDecl->getType(); |
| 1372 | Sel = PDecl->getGetterName(); |
| 1373 | } else { |
| 1374 | OMD = PropRefExpr->getImplicitPropertyGetter(); |
| 1375 | Sel = OMD->getSelector(); |
| 1376 | Ty = PropRefExpr->getType(); |
| 1377 | } |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1378 | Super = PropRefExpr->isSuperReceiver(); |
| 1379 | if (!Super) |
| 1380 | Receiver = PropRefExpr->getBase(); |
| 1381 | else { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1382 | SuperTy = PropRefExpr->getSuperReceiverType(); |
| 1383 | SuperLocation = PropRefExpr->getReceiverLocation(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1384 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | assert (OMD && "RewritePropertyOrImplicitGetter - OMD is null"); |
| 1388 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1389 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1390 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1391 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1392 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1393 | Expr::getValueKindForType(Ty), |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1394 | PropOrGetterRefExpr->getLocStart(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1395 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1396 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1397 | SuperTy, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1398 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1399 | 0, 0, |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1400 | PropOrGetterRefExpr->getLocEnd()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1401 | else { |
| 1402 | assert (Receiver && "RewritePropertyOrImplicitGetter - Receiver is null"); |
| 1403 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1404 | if (PropGetters[Exp]) |
| 1405 | // This allows us to handle chain/nested property/implicit getters. |
| 1406 | Receiver = PropGetters[Exp]; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1407 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1408 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1409 | Expr::getValueKindForType(Ty), |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1410 | PropOrGetterRefExpr->getLocStart(), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1411 | cast<Expr>(Receiver), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1412 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1413 | 0, 0, |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1414 | PropOrGetterRefExpr->getLocEnd()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1415 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1416 | |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1417 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr, MsgExpr->getLocStart(), |
| 1418 | MsgExpr->getLocEnd()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1419 | |
| 1420 | if (!PropParentMap) |
| 1421 | PropParentMap = new ParentMap(CurrentBody); |
Fariborz Jahanian | 3cd1ea3 | 2010-12-04 21:22:13 +0000 | [diff] [blame] | 1422 | bool NestedPropertyRef = false; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1423 | Stmt *Parent = PropParentMap->getParent(PropOrGetterRefExpr); |
Fariborz Jahanian | 3cd1ea3 | 2010-12-04 21:22:13 +0000 | [diff] [blame] | 1424 | ImplicitCastExpr*ICE=0; |
| 1425 | if (Parent) |
| 1426 | if ((ICE = dyn_cast<ImplicitCastExpr>(Parent))) { |
| 1427 | assert((ICE->getCastKind() == CK_GetObjCProperty) |
| 1428 | && "RewritePropertyOrImplicitGetter"); |
| 1429 | Parent = PropParentMap->getParent(Parent); |
| 1430 | NestedPropertyRef = (Parent && isa<ObjCPropertyRefExpr>(Parent)); |
| 1431 | } |
| 1432 | if (NestedPropertyRef) { |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1433 | // We stash away the ReplacingStmt since actually doing the |
| 1434 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
Fariborz Jahanian | 3cd1ea3 | 2010-12-04 21:22:13 +0000 | [diff] [blame] | 1435 | PropGetters[ICE] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1436 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1437 | // to things that stay around. |
| 1438 | Context->Deallocate(MsgExpr); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1439 | return PropOrGetterRefExpr; // return the original... |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1440 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1441 | ReplaceStmt(PropOrGetterRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1443 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1444 | // to things that stay around. |
| 1445 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1446 | return ReplacingStmt; |
| 1447 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1451 | SourceLocation OrigStart, |
| 1452 | bool &replaced) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1453 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1454 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1455 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1456 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1457 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1458 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1459 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1460 | // lookup which class implements the instance variable. |
| 1461 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1463 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1464 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1466 | // Synthesize an explicit cast to gain access to the ivar. |
| 1467 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1468 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1469 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1470 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1471 | SourceLocation(), SourceLocation(), |
| 1472 | II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1473 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1474 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1475 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1476 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1477 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1478 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1479 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1480 | IV->getBase()->getLocEnd(), |
| 1481 | castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1482 | replaced = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1484 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1485 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1486 | IV->getLocation(), |
| 1487 | D->getType(), |
| 1488 | VK_LValue, OK_Ordinary); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1489 | // delete IV; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1490 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1491 | } |
Fariborz Jahanian | 7e20ffe | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1492 | // Get the new text |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1493 | // Cannot delete IV->getBase(), since PE points to it. |
| 1494 | // Replace the old base with the cast. This is important when doing |
| 1495 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1497 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1498 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1499 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1500 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1502 | // Explicit ivar refs need to have a cast inserted. |
| 1503 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1504 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1505 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1506 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1507 | // lookup which class implements the instance variable. |
| 1508 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1510 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1511 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1513 | // Synthesize an explicit cast to gain access to the ivar. |
| 1514 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1515 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1516 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1517 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1518 | SourceLocation(), SourceLocation(), |
| 1519 | II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1520 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1521 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1522 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1523 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1524 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1525 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1526 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1527 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1528 | replaced = true; |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1529 | // Cannot delete IV->getBase(), since PE points to it. |
| 1530 | // Replace the old base with the cast. This is important when doing |
| 1531 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1533 | return IV; |
| 1534 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1535 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1536 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1539 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1540 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1541 | if (*CI) { |
| 1542 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1543 | if (newStmt) |
| 1544 | *CI = newStmt; |
| 1545 | } |
| 1546 | } |
| 1547 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1548 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1549 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1550 | replaced); |
| 1551 | return newStmt; |
Fariborz Jahanian | 376338a | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1552 | } |
| 1553 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1554 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1555 | return newStmt; |
| 1556 | } |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1557 | return S; |
| 1558 | } |
| 1559 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1560 | /// SynthCountByEnumWithState - To print: |
| 1561 | /// ((unsigned int (*) |
| 1562 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1564 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | /// "countByEnumeratingWithState:objects:count:"), |
| 1566 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1567 | /// (id *)items, (unsigned int)16) |
| 1568 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1569 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1570 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1571 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1572 | buf += "\n\t\t"; |
| 1573 | buf += "((id)l_collection,\n\t\t"; |
| 1574 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1575 | buf += "\n\t\t"; |
| 1576 | buf += "&enumState, " |
| 1577 | "(id *)items, (unsigned int)16)"; |
| 1578 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1579 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1580 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1581 | /// statement to exit to its outer synthesized loop. |
| 1582 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1583 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1584 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1585 | return S; |
| 1586 | // replace break with goto __break_label |
| 1587 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1589 | SourceLocation startLoc = S->getLocStart(); |
| 1590 | buf = "goto __break_label_"; |
| 1591 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1592 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1598 | /// statement to continue with its inner synthesized loop. |
| 1599 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1600 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1601 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1602 | return S; |
| 1603 | // replace continue with goto __continue_label |
| 1604 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1606 | SourceLocation startLoc = S->getLocStart(); |
| 1607 | buf = "goto __continue_label_"; |
| 1608 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1609 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1611 | return 0; |
| 1612 | } |
| 1613 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1614 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1615 | /// It rewrites: |
| 1616 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1618 | /// Into: |
| 1619 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1621 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1622 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1623 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1625 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1626 | /// if (limit) { |
| 1627 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1628 | /// do { |
| 1629 | /// unsigned long counter = 0; |
| 1630 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1632 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1633 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1634 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1635 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1636 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1638 | /// objects:items count:16]); |
| 1639 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1640 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1641 | /// } |
| 1642 | /// else |
| 1643 | /// elem = nil; |
| 1644 | /// } |
| 1645 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1646 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1647 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1648 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1650 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1652 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1654 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1655 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1656 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1657 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1658 | std::string buf; |
| 1659 | buf = "\n{\n\t"; |
| 1660 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1661 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1662 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1663 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1664 | if (ElementType->isObjCQualifiedIdType() || |
| 1665 | ElementType->isObjCQualifiedInterfaceType()) |
| 1666 | // Simply use 'id' for all qualified types. |
| 1667 | elementTypeAsString = "id"; |
| 1668 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1669 | elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1670 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1671 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1672 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1673 | buf += elementName; |
| 1674 | buf += ";\n\t"; |
| 1675 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1676 | else { |
| 1677 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1678 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1679 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1680 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1681 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1682 | // Simply use 'id' for all qualified types. |
| 1683 | elementTypeAsString = "id"; |
| 1684 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1685 | elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1688 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1689 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1690 | // id items[16]; |
| 1691 | buf += "id items[16];\n\t"; |
| 1692 | // id l_collection = (id) |
| 1693 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1694 | // Find start location of 'collection' the hard way! |
| 1695 | const char *startCollectionBuf = startBuf; |
| 1696 | startCollectionBuf += 3; // skip 'for' |
| 1697 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1698 | startCollectionBuf++; // skip '(' |
| 1699 | // find 'in' and skip it. |
| 1700 | while (*startCollectionBuf != ' ' || |
| 1701 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1702 | (*(startCollectionBuf+3) != ' ' && |
| 1703 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1704 | startCollectionBuf++; |
| 1705 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
| 1707 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1708 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1709 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1710 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1711 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1712 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1713 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1715 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1716 | // objects:items count:16]; |
| 1717 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1719 | // ((unsigned int (*) |
| 1720 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1722 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | // "countByEnumeratingWithState:objects:count:"), |
| 1724 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1725 | // (id *)items, (unsigned int)16); |
| 1726 | buf += "unsigned long limit =\n\t\t"; |
| 1727 | SynthCountByEnumWithState(buf); |
| 1728 | buf += ";\n\t"; |
| 1729 | /// if (limit) { |
| 1730 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1731 | /// do { |
| 1732 | /// unsigned long counter = 0; |
| 1733 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1735 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1736 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1737 | buf += "if (limit) {\n\t"; |
| 1738 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1739 | buf += "do {\n\t\t"; |
| 1740 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1741 | buf += "do {\n\t\t\t"; |
| 1742 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1743 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1744 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1745 | buf += " = ("; |
| 1746 | buf += elementTypeAsString; |
| 1747 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1748 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1749 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1751 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1752 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1754 | /// objects:items count:16]); |
| 1755 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1756 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1757 | /// } |
| 1758 | /// else |
| 1759 | /// elem = nil; |
| 1760 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1762 | buf = ";\n\t"; |
| 1763 | buf += "__continue_label_"; |
| 1764 | buf += utostr(ObjCBcLabelNo.back()); |
| 1765 | buf += ": ;"; |
| 1766 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1767 | buf += "} while (counter < limit);\n\t"; |
| 1768 | buf += "} while (limit = "; |
| 1769 | SynthCountByEnumWithState(buf); |
| 1770 | buf += ");\n\t"; |
| 1771 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1772 | buf += " = (("; |
| 1773 | buf += elementTypeAsString; |
| 1774 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1775 | buf += "__break_label_"; |
| 1776 | buf += utostr(ObjCBcLabelNo.back()); |
| 1777 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1778 | buf += "}\n\t"; |
| 1779 | buf += "else\n\t\t"; |
| 1780 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1781 | buf += " = (("; |
| 1782 | buf += elementTypeAsString; |
| 1783 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1784 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1786 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1787 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1788 | if (isa<CompoundStmt>(S->getBody())) { |
| 1789 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1790 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1791 | } else { |
| 1792 | /* Need to treat single statements specially. For example: |
| 1793 | * |
| 1794 | * for (A *a in b) if (stuff()) break; |
| 1795 | * for (A *a in b) xxxyy; |
| 1796 | * |
| 1797 | * The following code simply scans ahead to the semi to find the actual end. |
| 1798 | */ |
| 1799 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1800 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1801 | assert(semiBuf && "Can't find ';'"); |
| 1802 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1803 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1804 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1805 | Stmts.pop_back(); |
| 1806 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1807 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1811 | /// This routine rewrites @synchronized(expr) stmt; |
| 1812 | /// into: |
| 1813 | /// objc_sync_enter(expr); |
| 1814 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1815 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1816 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1817 | // Get the start location and compute the semi location. |
| 1818 | SourceLocation startLoc = S->getLocStart(); |
| 1819 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1821 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
| 1823 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1824 | buf = "objc_sync_enter((id)"; |
| 1825 | const char *lparenBuf = startBuf; |
| 1826 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1827 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1829 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1830 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1831 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1832 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1833 | while (*endBuf != ')') endBuf--; |
| 1834 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1835 | buf = ");\n"; |
| 1836 | // declare a new scope with two variables, _stack and _rethrow. |
| 1837 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1838 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1839 | buf += "char *pointers[4];} _stack;\n"; |
| 1840 | buf += "id volatile _rethrow = 0;\n"; |
| 1841 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1842 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1843 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1844 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1845 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1847 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1848 | SourceLocation lastCurlyLoc = startLoc; |
| 1849 | buf = "}\nelse {\n"; |
| 1850 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1851 | buf += "}\n"; |
| 1852 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1853 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1854 | |
| 1855 | std::string syncBuf; |
| 1856 | syncBuf += " objc_sync_exit("; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1857 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1858 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1859 | S->getSynchExpr()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1860 | std::string syncExprBufS; |
| 1861 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1862 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1863 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1864 | syncBuf += syncExprBuf.str(); |
| 1865 | syncBuf += ");"; |
| 1866 | |
| 1867 | buf += syncBuf; |
| 1868 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1869 | buf += "}\n"; |
| 1870 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1872 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1873 | |
| 1874 | bool hasReturns = false; |
| 1875 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1876 | if (hasReturns) |
| 1877 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1878 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1879 | return 0; |
| 1880 | } |
| 1881 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1882 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1883 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1884 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1885 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1886 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1887 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1888 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1889 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1891 | TryFinallyContainsReturnDiag); |
| 1892 | } |
| 1893 | return; |
| 1894 | } |
| 1895 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1896 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1897 | { |
| 1898 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1899 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1900 | if (*CI) |
| 1901 | HasReturnStmts(*CI, hasReturns); |
| 1902 | |
| 1903 | if (isa<ReturnStmt>(S)) |
| 1904 | hasReturns = true; |
| 1905 | return; |
| 1906 | } |
| 1907 | |
| 1908 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1909 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1910 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1911 | if (*CI) { |
| 1912 | RewriteTryReturnStmts(*CI); |
| 1913 | } |
| 1914 | if (isa<ReturnStmt>(S)) { |
| 1915 | SourceLocation startLoc = S->getLocStart(); |
| 1916 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1917 | |
| 1918 | const char *semiBuf = strchr(startBuf, ';'); |
| 1919 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1920 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1921 | |
| 1922 | std::string buf; |
| 1923 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1924 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1925 | ReplaceText(startLoc, 6, buf); |
| 1926 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1927 | } |
| 1928 | return; |
| 1929 | } |
| 1930 | |
| 1931 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1932 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1933 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1934 | if (*CI) { |
| 1935 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1936 | } |
| 1937 | if (isa<ReturnStmt>(S)) { |
| 1938 | SourceLocation startLoc = S->getLocStart(); |
| 1939 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1940 | |
| 1941 | const char *semiBuf = strchr(startBuf, ';'); |
| 1942 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1943 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1944 | |
| 1945 | std::string buf; |
| 1946 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1947 | buf += syncExitBuf; |
| 1948 | buf += " return"; |
| 1949 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1950 | ReplaceText(startLoc, 6, buf); |
| 1951 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1952 | } |
| 1953 | return; |
| 1954 | } |
| 1955 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1956 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1957 | // Get the start location and compute the semi location. |
| 1958 | SourceLocation startLoc = S->getLocStart(); |
| 1959 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1961 | assert((*startBuf == '@') && "bogus @try location"); |
| 1962 | |
| 1963 | std::string buf; |
| 1964 | // declare a new scope with two variables, _stack and _rethrow. |
| 1965 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1966 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1967 | buf += "char *pointers[4];} _stack;\n"; |
| 1968 | buf += "id volatile _rethrow = 0;\n"; |
| 1969 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1970 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1971 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1972 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1974 | startLoc = S->getTryBody()->getLocEnd(); |
| 1975 | startBuf = SM->getCharacterData(startLoc); |
| 1976 | |
| 1977 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1979 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1980 | if (S->getNumCatchStmts()) { |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1981 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1982 | buf = " /* @catch begin */ else {\n"; |
| 1983 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1984 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1985 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1986 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1987 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1989 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1990 | } else { /* no catch list */ |
| 1991 | buf = "}\nelse {\n"; |
| 1992 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1993 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1994 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1995 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1996 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1997 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1998 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1999 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2001 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2002 | buf = "if ("; // we are generating code for the first catch clause |
| 2003 | else |
| 2004 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2005 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2006 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2008 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2010 | const char *lParenLoc = strchr(startBuf, '('); |
| 2011 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2012 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2013 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2014 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2015 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 2016 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2017 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2018 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2019 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2021 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 2022 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2023 | } else if (catchDecl) { |
| 2024 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2025 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2026 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2027 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2028 | } else if (const ObjCObjectPointerType *Ptr = |
| 2029 | t->getAs<ObjCObjectPointerType>()) { |
| 2030 | // Should be a pointer to a class. |
| 2031 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 2032 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2033 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2034 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2035 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2036 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2040 | lastCatchBody = Catch->getCatchBody(); |
| 2041 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2042 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 2043 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 2044 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 2045 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 2046 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2049 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2050 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2051 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2052 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2053 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2054 | } |
| 2055 | // Complete the catch list... |
| 2056 | if (lastCatchBody) { |
| 2057 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2058 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 2059 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2061 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 2062 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 2063 | buf = "} /* last catch end */\n"; |
| 2064 | buf += "else {\n"; |
| 2065 | buf += " _rethrow = _caught;\n"; |
| 2066 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 2067 | buf += "} } /* @catch end */\n"; |
| 2068 | if (!S->getFinallyStmt()) |
| 2069 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2070 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2072 | // Set lastCurlyLoc |
| 2073 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 2074 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2075 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2076 | startLoc = finalStmt->getLocStart(); |
| 2077 | startBuf = SM->getCharacterData(startLoc); |
| 2078 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2080 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2082 | Stmt *body = finalStmt->getFinallyBody(); |
| 2083 | SourceLocation startLoc = body->getLocStart(); |
| 2084 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2085 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 2086 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2088 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2090 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2091 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2092 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2093 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2095 | // Set lastCurlyLoc |
| 2096 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 2098 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2099 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2100 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 2101 | buf = "{ /* implicit finally clause */\n"; |
| 2102 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 2103 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 2104 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2105 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2106 | |
| 2107 | // Now check for any return/continue/go statements within the @try. |
| 2108 | // The implicit finally clause won't called if the @try contains any |
| 2109 | // jump statements. |
| 2110 | bool hasReturns = false; |
| 2111 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2112 | if (hasReturns) |
| 2113 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2114 | } |
| 2115 | // Now emit the final closing curly brace... |
| 2116 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2117 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2118 | return 0; |
| 2119 | } |
| 2120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2122 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2123 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2124 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2125 | // Get the start location and compute the semi location. |
| 2126 | SourceLocation startLoc = S->getLocStart(); |
| 2127 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2129 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2130 | |
| 2131 | std::string buf; |
| 2132 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2133 | if (S->getThrowExpr()) |
| 2134 | buf = "objc_exception_throw("; |
| 2135 | else // add an implicit argument |
| 2136 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2138 | // handle "@ throw" correctly. |
| 2139 | const char *wBuf = strchr(startBuf, 'w'); |
| 2140 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2141 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2143 | const char *semiBuf = strchr(startBuf, ';'); |
| 2144 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2145 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2146 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2147 | return 0; |
| 2148 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2149 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2150 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2151 | // Create a new string expression. |
| 2152 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2153 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2154 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2155 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2156 | StringLiteral::Ascii, false, |
| 2157 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2158 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2160 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2161 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2162 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2165 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2166 | if (!SelGetUidFunctionDecl) |
| 2167 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2168 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2169 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2170 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2171 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2173 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2174 | StringLiteral::Ascii, false, |
| 2175 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2176 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2177 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2178 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2179 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2180 | return SelExp; |
| 2181 | } |
| 2182 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2183 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2184 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2185 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2186 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2187 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2189 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2190 | DeclRefExpr *DRE = |
| 2191 | new (Context) DeclRefExpr(FD, msgSendType, VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2193 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 2194 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2195 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2196 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2197 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2199 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2201 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2202 | new (Context) CallExpr(*Context, ICE, args, nargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2203 | FT->getCallResultType(*Context), |
| 2204 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2205 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2208 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2209 | const char *&startRef, const char *&endRef) { |
| 2210 | while (startBuf < endBuf) { |
| 2211 | if (*startBuf == '<') |
| 2212 | startRef = startBuf; // mark the start. |
| 2213 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2214 | if (startRef && *startRef == '<') { |
| 2215 | endRef = startBuf; // mark the end. |
| 2216 | return true; |
| 2217 | } |
| 2218 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2219 | } |
| 2220 | startBuf++; |
| 2221 | } |
| 2222 | return false; |
| 2223 | } |
| 2224 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2225 | static void scanToNextArgument(const char *&argRef) { |
| 2226 | int angle = 0; |
| 2227 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2228 | if (*argRef == '<') |
| 2229 | angle++; |
| 2230 | else if (*argRef == '>') |
| 2231 | angle--; |
| 2232 | argRef++; |
| 2233 | } |
| 2234 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2235 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2236 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2237 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2238 | if (T->isObjCQualifiedIdType()) |
| 2239 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2240 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2241 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2242 | return true; |
| 2243 | } |
| 2244 | if (T->isObjCObjectPointerType()) { |
| 2245 | T = T->getPointeeType(); |
| 2246 | return T->isObjCQualifiedInterfaceType(); |
| 2247 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2248 | if (T->isArrayType()) { |
| 2249 | QualType ElemTy = Context->getBaseElementType(T); |
| 2250 | return needToScanForQualifiers(ElemTy); |
| 2251 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2252 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2255 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2256 | QualType Type = E->getType(); |
| 2257 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2258 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2260 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2261 | Loc = ECE->getLParenLoc(); |
| 2262 | EndLoc = ECE->getRParenLoc(); |
| 2263 | } else { |
| 2264 | Loc = E->getLocStart(); |
| 2265 | EndLoc = E->getLocEnd(); |
| 2266 | } |
| 2267 | // This will defend against trying to rewrite synthesized expressions. |
| 2268 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2269 | return; |
| 2270 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2271 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2272 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2273 | const char *startRef = 0, *endRef = 0; |
| 2274 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2275 | // Get the locations of the startRef, endRef. |
| 2276 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2277 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2278 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2279 | InsertText(LessLoc, "/*"); |
| 2280 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | } |
| 2284 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2285 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2286 | SourceLocation Loc; |
| 2287 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2288 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2289 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2290 | Loc = VD->getLocation(); |
| 2291 | Type = VD->getType(); |
| 2292 | } |
| 2293 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2294 | Loc = FD->getLocation(); |
| 2295 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2296 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2297 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2298 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2299 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2300 | if (!proto) |
| 2301 | return; |
| 2302 | Type = proto->getResultType(); |
| 2303 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2304 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2305 | Loc = FD->getLocation(); |
| 2306 | Type = FD->getType(); |
| 2307 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2308 | else |
| 2309 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2311 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2312 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2314 | const char *endBuf = SM->getCharacterData(Loc); |
| 2315 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2316 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2317 | startBuf--; // scan backward (from the decl location) for return type. |
| 2318 | const char *startRef = 0, *endRef = 0; |
| 2319 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2320 | // Get the locations of the startRef, endRef. |
| 2321 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2322 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2323 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2324 | InsertText(LessLoc, "/*"); |
| 2325 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2328 | if (!proto) |
| 2329 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2330 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2331 | const char *startBuf = SM->getCharacterData(Loc); |
| 2332 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2333 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2334 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2335 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2337 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2338 | // scan forward (from the decl location) for argument types. |
| 2339 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2340 | const char *startRef = 0, *endRef = 0; |
| 2341 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2342 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | SourceLocation LessLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2344 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2345 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2346 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2347 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2348 | InsertText(LessLoc, "/*"); |
| 2349 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2350 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2351 | startBuf = ++endBuf; |
| 2352 | } |
| 2353 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2354 | // If the function name is derived from a macro expansion, then the |
| 2355 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2356 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2357 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2358 | startBuf++; |
| 2359 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2360 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2363 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2364 | QualType QT = ND->getType(); |
| 2365 | const Type* TypePtr = QT->getAs<Type>(); |
| 2366 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2367 | return; |
| 2368 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2369 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2370 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2371 | TypePtr = QT->getAs<Type>(); |
| 2372 | } |
| 2373 | // FIXME. This will not work for multiple declarators; as in: |
| 2374 | // __typeof__(a) b,c,d; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2375 | std::string TypeAsString(QT.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2376 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2377 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2378 | if (ND->getInit()) { |
| 2379 | std::string Name(ND->getNameAsString()); |
| 2380 | TypeAsString += " " + Name + " = "; |
| 2381 | Expr *E = ND->getInit(); |
| 2382 | SourceLocation startLoc; |
| 2383 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2384 | startLoc = ECE->getLParenLoc(); |
| 2385 | else |
| 2386 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2387 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2388 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2389 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2390 | } |
| 2391 | else { |
| 2392 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2393 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2394 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2395 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2399 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2400 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2401 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2402 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2403 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2404 | QualType getFuncType = |
| 2405 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2406 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2408 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2409 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2410 | SC_Extern, |
| 2411 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2414 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2415 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2416 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2417 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2418 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2419 | return; |
| 2420 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2421 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2424 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2425 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2426 | const char *argPtr = TypeString.c_str(); |
| 2427 | if (!strchr(argPtr, '^')) { |
| 2428 | Str += TypeString; |
| 2429 | return; |
| 2430 | } |
| 2431 | while (*argPtr) { |
| 2432 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2433 | argPtr++; |
| 2434 | } |
| 2435 | } |
| 2436 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2437 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2438 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2439 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2440 | QualType Type = VD->getType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2441 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2442 | const char *argPtr = TypeString.c_str(); |
| 2443 | int paren = 0; |
| 2444 | while (*argPtr) { |
| 2445 | switch (*argPtr) { |
| 2446 | case '(': |
| 2447 | Str += *argPtr; |
| 2448 | paren++; |
| 2449 | break; |
| 2450 | case ')': |
| 2451 | Str += *argPtr; |
| 2452 | paren--; |
| 2453 | break; |
| 2454 | case '^': |
| 2455 | Str += '*'; |
| 2456 | if (paren == 1) |
| 2457 | Str += VD->getNameAsString(); |
| 2458 | break; |
| 2459 | default: |
| 2460 | Str += *argPtr; |
| 2461 | break; |
| 2462 | } |
| 2463 | argPtr++; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2468 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2469 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2470 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2471 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2472 | if (!proto) |
| 2473 | return; |
| 2474 | QualType Type = proto->getResultType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2475 | std::string FdStr = Type.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2476 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2477 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2478 | FdStr += "("; |
| 2479 | unsigned numArgs = proto->getNumArgs(); |
| 2480 | for (unsigned i = 0; i < numArgs; i++) { |
| 2481 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2482 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2483 | if (i+1 < numArgs) |
| 2484 | FdStr += ", "; |
| 2485 | } |
| 2486 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2487 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2488 | CurFunctionDeclToDeclareForBlock = 0; |
| 2489 | } |
| 2490 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2491 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2492 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2493 | if (SuperContructorFunctionDecl) |
| 2494 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2495 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2496 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2497 | QualType argT = Context->getObjCIdType(); |
| 2498 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2499 | ArgTys.push_back(argT); |
| 2500 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2501 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2502 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2503 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2505 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2506 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2507 | SC_Extern, |
| 2508 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2511 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2512 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2513 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2514 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2515 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2516 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2517 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2518 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2519 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2520 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2521 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2522 | &ArgTys[0], ArgTys.size(), |
| 2523 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2524 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2525 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2526 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2527 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2528 | SC_Extern, |
| 2529 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2532 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2533 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2534 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2535 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2536 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2537 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2538 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2539 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2540 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2541 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2542 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2543 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2544 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2545 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2546 | &ArgTys[0], ArgTys.size(), |
| 2547 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2548 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2550 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2551 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2552 | SC_Extern, |
| 2553 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2556 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2557 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2558 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2559 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2560 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2561 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2562 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2563 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2564 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2565 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2566 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2567 | &ArgTys[0], ArgTys.size(), |
| 2568 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2569 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2570 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2571 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2572 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2573 | SC_Extern, |
| 2574 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2579 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2581 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2582 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2583 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2584 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2585 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2586 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2587 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2588 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2589 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2590 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2591 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2592 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2593 | &ArgTys[0], ArgTys.size(), |
| 2594 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2595 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2597 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2598 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2599 | SC_Extern, |
| 2600 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2603 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2604 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2605 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2606 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2607 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2608 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2609 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2610 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2611 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2612 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2613 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2614 | &ArgTys[0], ArgTys.size(), |
| 2615 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2616 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2618 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2619 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2620 | SC_Extern, |
| 2621 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2624 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2625 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2626 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2627 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2628 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2629 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2630 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2631 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2633 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2634 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2635 | SC_Extern, |
| 2636 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2639 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2640 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2641 | IdentifierInfo *getSuperClassIdent = |
| 2642 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2643 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2644 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2645 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2646 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2647 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2648 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2649 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2650 | getSuperClassIdent, |
| 2651 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2652 | SC_Extern, |
| 2653 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2654 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2657 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2658 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2659 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2660 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2661 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2662 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2663 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2664 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2666 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2667 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2668 | SC_Extern, |
| 2669 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2672 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2673 | QualType strType = getConstantStringStructType(); |
| 2674 | |
| 2675 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2676 | |
| 2677 | std::string tmpName = InFileName; |
| 2678 | unsigned i; |
| 2679 | for (i=0; i < tmpName.length(); i++) { |
| 2680 | char c = tmpName.at(i); |
| 2681 | // replace any non alphanumeric characters with '_'. |
| 2682 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2683 | tmpName[i] = '_'; |
| 2684 | } |
| 2685 | S += tmpName; |
| 2686 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2687 | S += utostr(NumObjCStringLiterals++); |
| 2688 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2689 | Preamble += "static __NSConstantStringImpl " + S; |
| 2690 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2691 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2692 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2693 | std::string prettyBufS; |
| 2694 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2695 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2696 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2697 | Preamble += prettyBuf.str(); |
| 2698 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2699 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | |
| 2701 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2702 | SourceLocation(), &Context->Idents.get(S), |
| 2703 | strType, 0, SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2704 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, VK_LValue, |
| 2705 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2706 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2708 | VK_RValue, OK_Ordinary, |
| 2709 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2710 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2711 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2712 | CK_BitCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2713 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2714 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2715 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2718 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2719 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2720 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2721 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2722 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2723 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2724 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2726 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2728 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2730 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2731 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2732 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2734 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2736 | FieldTypes[i], 0, |
| 2737 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2738 | /*Mutable=*/false, |
| 2739 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2742 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2743 | } |
| 2744 | return Context->getTagDeclType(SuperStructDecl); |
| 2745 | } |
| 2746 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2747 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2748 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2749 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2750 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2751 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2752 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2754 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2756 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2758 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2760 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2762 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2763 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2764 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2766 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2767 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2768 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2769 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2770 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2771 | /*Mutable=*/true, |
| 2772 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2775 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2776 | } |
| 2777 | return Context->getTagDeclType(ConstantStringDecl); |
| 2778 | } |
| 2779 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2780 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2781 | SourceLocation StartLoc, |
| 2782 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2783 | if (!SelGetUidFunctionDecl) |
| 2784 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2785 | if (!MsgSendFunctionDecl) |
| 2786 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2787 | if (!MsgSendSuperFunctionDecl) |
| 2788 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2789 | if (!MsgSendStretFunctionDecl) |
| 2790 | SynthMsgSendStretFunctionDecl(); |
| 2791 | if (!MsgSendSuperStretFunctionDecl) |
| 2792 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2793 | if (!MsgSendFpretFunctionDecl) |
| 2794 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2795 | if (!GetClassFunctionDecl) |
| 2796 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2797 | if (!GetSuperClassFunctionDecl) |
| 2798 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2799 | if (!GetMetaClassFunctionDecl) |
| 2800 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2802 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2803 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2804 | // May need to use objc_msgSend_stret() as well. |
| 2805 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2806 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2807 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2808 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2809 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2810 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2811 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2814 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2815 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2816 | switch (Exp->getReceiverKind()) { |
| 2817 | case ObjCMessageExpr::SuperClass: { |
| 2818 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2819 | if (MsgSendStretFlavor) |
| 2820 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2821 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2823 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2825 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2827 | // set the receiver to self, the first argument to all methods. |
| 2828 | InitExprs.push_back( |
| 2829 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2830 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2831 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2832 | Context->getObjCIdType(), |
| 2833 | VK_RValue, |
| 2834 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2835 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2837 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2838 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2839 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2840 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2841 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2842 | StringLiteral::Ascii, false, |
| 2843 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2844 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2845 | &ClsExprs[0], |
| 2846 | ClsExprs.size(), |
| 2847 | StartLoc, |
| 2848 | EndLoc); |
| 2849 | // (Class)objc_getClass("CurrentClass") |
| 2850 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2851 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2852 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2853 | ClsExprs.clear(); |
| 2854 | ClsExprs.push_back(ArgExpr); |
| 2855 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2856 | &ClsExprs[0], ClsExprs.size(), |
| 2857 | StartLoc, EndLoc); |
| 2858 | |
| 2859 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2860 | // To turn off a warning, type-cast to 'id' |
| 2861 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2862 | NoTypeInfoCStyleCastExpr(Context, |
| 2863 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2864 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2865 | // struct objc_super |
| 2866 | QualType superType = getSuperStructType(); |
| 2867 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2869 | if (LangOpts.Microsoft) { |
| 2870 | SynthSuperContructorFunctionDecl(); |
| 2871 | // Simulate a contructor call... |
| 2872 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2873 | superType, VK_LValue, |
| 2874 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2875 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2876 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2877 | superType, VK_LValue, |
| 2878 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | // The code for super is a little tricky to prevent collision with |
| 2880 | // the structure definition in the header. The rewriter has it's own |
| 2881 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2882 | // we need the cast below. For example: |
| 2883 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2884 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2885 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2886 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2887 | VK_RValue, OK_Ordinary, |
| 2888 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2889 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2890 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2891 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2892 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2893 | // (struct objc_super) { <exprs from above> } |
| 2894 | InitListExpr *ILE = |
| 2895 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2896 | &InitExprs[0], InitExprs.size(), |
| 2897 | SourceLocation()); |
| 2898 | TypeSourceInfo *superTInfo |
| 2899 | = Context->getTrivialTypeSourceInfo(superType); |
| 2900 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2901 | superType, VK_LValue, |
| 2902 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2903 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2904 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2905 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2906 | VK_RValue, OK_Ordinary, |
| 2907 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2908 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2909 | MsgExprs.push_back(SuperRep); |
| 2910 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2911 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2912 | |
| 2913 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2914 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2915 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2916 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2917 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2918 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2919 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2920 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2921 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2922 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2923 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2924 | &ClsExprs[0], |
| 2925 | ClsExprs.size(), |
| 2926 | StartLoc, EndLoc); |
| 2927 | MsgExprs.push_back(Cls); |
| 2928 | break; |
| 2929 | } |
| 2930 | |
| 2931 | case ObjCMessageExpr::SuperInstance:{ |
| 2932 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2933 | if (MsgSendStretFlavor) |
| 2934 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2935 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2936 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2937 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2938 | |
| 2939 | InitExprs.push_back( |
| 2940 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2941 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2942 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2943 | Context->getObjCIdType(), |
| 2944 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2945 | ); // set the 'receiver'. |
| 2946 | |
| 2947 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2948 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2949 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2950 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2951 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2952 | StringLiteral::Ascii, false, argType, |
| 2953 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2954 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2955 | &ClsExprs[0], |
| 2956 | ClsExprs.size(), |
| 2957 | StartLoc, EndLoc); |
| 2958 | // (Class)objc_getClass("CurrentClass") |
| 2959 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2960 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2961 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2962 | ClsExprs.clear(); |
| 2963 | ClsExprs.push_back(ArgExpr); |
| 2964 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2965 | &ClsExprs[0], ClsExprs.size(), |
| 2966 | StartLoc, EndLoc); |
| 2967 | |
| 2968 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2969 | // To turn off a warning, type-cast to 'id' |
| 2970 | InitExprs.push_back( |
| 2971 | // set 'super class', using class_getSuperclass(). |
| 2972 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2973 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2974 | // struct objc_super |
| 2975 | QualType superType = getSuperStructType(); |
| 2976 | Expr *SuperRep; |
| 2977 | |
| 2978 | if (LangOpts.Microsoft) { |
| 2979 | SynthSuperContructorFunctionDecl(); |
| 2980 | // Simulate a contructor call... |
| 2981 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2982 | superType, VK_LValue, |
| 2983 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2984 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2985 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2986 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2987 | // The code for super is a little tricky to prevent collision with |
| 2988 | // the structure definition in the header. The rewriter has it's own |
| 2989 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2990 | // we need the cast below. For example: |
| 2991 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2992 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2993 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2994 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2995 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2996 | SourceLocation()); |
| 2997 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2998 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2999 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3000 | } else { |
| 3001 | // (struct objc_super) { <exprs from above> } |
| 3002 | InitListExpr *ILE = |
| 3003 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3004 | &InitExprs[0], InitExprs.size(), |
| 3005 | SourceLocation()); |
| 3006 | TypeSourceInfo *superTInfo |
| 3007 | = Context->getTrivialTypeSourceInfo(superType); |
| 3008 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3009 | superType, VK_RValue, ILE, |
| 3010 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3011 | } |
| 3012 | MsgExprs.push_back(SuperRep); |
| 3013 | break; |
| 3014 | } |
| 3015 | |
| 3016 | case ObjCMessageExpr::Instance: { |
| 3017 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3018 | // Foo<Proto> *. |
| 3019 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3020 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3021 | recExpr = CE->getSubExpr(); |
| 3022 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3023 | CK_BitCast, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3024 | MsgExprs.push_back(recExpr); |
| 3025 | break; |
| 3026 | } |
| 3027 | } |
| 3028 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 3029 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3030 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3031 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 3033 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3034 | StringLiteral::Ascii, false, |
| 3035 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3036 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3037 | &SelExprs[0], SelExprs.size(), |
| 3038 | StartLoc, |
| 3039 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3040 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3042 | // Now push any user supplied arguments. |
| 3043 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3044 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 3045 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3046 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3047 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 3048 | QualType type = ICE->getType(); |
| 3049 | if (needToScanForQualifiers(type)) |
| 3050 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3051 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3052 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 3053 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 3054 | bool integral = SubExpr->getType()->isIntegralType(*Context); |
| 3055 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, |
| 3056 | (integral && type->isBooleanType()) |
| 3057 | ? CK_IntegralToBoolean : CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3058 | userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3059 | } |
| 3060 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3061 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3062 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3063 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3064 | userExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3065 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3066 | CK_BitCast, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3069 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3070 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3071 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3072 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3073 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3074 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3075 | // Generate the funky cast. |
| 3076 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3077 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3078 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3079 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3080 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 3081 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3082 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3083 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3084 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3085 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3086 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3087 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3088 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3089 | E = OMD->param_end(); PI != E; ++PI) { |
| 3090 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3092 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3093 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3094 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3095 | ArgTypes.push_back(t); |
| 3096 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3097 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 3098 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3099 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3100 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3101 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3102 | } |
| 3103 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 3104 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3106 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3108 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3109 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3111 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3112 | // xx.m:13: warning: function called through a non-compatible type |
| 3113 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3114 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3115 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3116 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3118 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3119 | QualType castType = |
| 3120 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3121 | // If we don't have a method decl, force a variadic cast. |
| 3122 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3123 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3124 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3125 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3126 | |
| 3127 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3128 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3130 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3131 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3133 | FT->getResultType(), VK_RValue, |
| 3134 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3135 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3136 | if (MsgSendStretFlavor) { |
| 3137 | // We have the method which returns a struct/union. Must also generate |
| 3138 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3139 | // expression which dictate which one to envoke depending on size of |
| 3140 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3142 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3144 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3145 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3146 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3147 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3148 | CK_BitCast, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3149 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3150 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3151 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3152 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3153 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3154 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3156 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3157 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3159 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3160 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3162 | FT->getResultType(), VK_RValue, |
| 3163 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3165 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3166 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3167 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3168 | Context->getTrivialTypeSourceInfo(returnType), |
| 3169 | Context->getSizeType(), SourceLocation(), |
| 3170 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3171 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3172 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3173 | // For X86 it is more complicated and some kind of target specific routine |
| 3174 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3176 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3177 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3178 | llvm::APInt(IntSize, 8), |
| 3179 | Context->IntTy, |
| 3180 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3181 | BinaryOperator *lessThanExpr = |
| 3182 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3183 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3184 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3186 | new (Context) ConditionalOperator(lessThanExpr, |
| 3187 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3188 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3189 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3190 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3191 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3192 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3193 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3194 | return ReplacingStmt; |
| 3195 | } |
| 3196 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3197 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3198 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3199 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3201 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3202 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3204 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3205 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3208 | // typedef struct objc_object Protocol; |
| 3209 | QualType RewriteObjC::getProtocolType() { |
| 3210 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3211 | TypeSourceInfo *TInfo |
| 3212 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3213 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3214 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3215 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3216 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3217 | } |
| 3218 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3219 | } |
| 3220 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3221 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3222 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3223 | /// The forward references (and metadata) are generated in |
| 3224 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3225 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3226 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3227 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3229 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3230 | SC_Extern, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3231 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), VK_LValue, |
| 3232 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3233 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3234 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3235 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3236 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3237 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3238 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3239 | ReplaceStmt(Exp, castExpr); |
| 3240 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3241 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3242 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3247 | const char *endBuf) { |
| 3248 | while (startBuf < endBuf) { |
| 3249 | if (*startBuf == '#') { |
| 3250 | // Skip whitespace. |
| 3251 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3252 | ; |
| 3253 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3254 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3255 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3256 | !strncmp(startBuf, "define", strlen("define")) || |
| 3257 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3258 | !strncmp(startBuf, "else", strlen("else")) || |
| 3259 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3260 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3261 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3262 | !strncmp(startBuf, "include", strlen("include")) || |
| 3263 | !strncmp(startBuf, "import", strlen("import")) || |
| 3264 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3265 | return true; |
| 3266 | } |
| 3267 | startBuf++; |
| 3268 | } |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3272 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3273 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3274 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3275 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3276 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3277 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3278 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3279 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3280 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3281 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3282 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3283 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3284 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3285 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3287 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3288 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3290 | // If no ivars and no root or if its root, directly or indirectly, |
| 3291 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3292 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3293 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3294 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3295 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3296 | return; |
| 3297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | |
| 3299 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3300 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3301 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3302 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3303 | if (LangOpts.Microsoft) |
| 3304 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3305 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3306 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3307 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3309 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3310 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3311 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3312 | // NSURL.h, for example): |
| 3313 | // |
| 3314 | // #ifdef XYZ |
| 3315 | // @interface Foo : NSObject |
| 3316 | // #else |
| 3317 | // @interface FooBar : NSObject |
| 3318 | // #endif |
| 3319 | // { |
| 3320 | // int i; |
| 3321 | // } |
| 3322 | // @end |
| 3323 | // |
| 3324 | // This clause is segregated to avoid breaking the common case. |
| 3325 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3327 | CDecl->getClassLoc(); |
| 3328 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3329 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3330 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3331 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3332 | // advance to the end of the referenced protocols. |
| 3333 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3334 | endHeader++; |
| 3335 | } |
| 3336 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3337 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3338 | } else { |
| 3339 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3340 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3341 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3342 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3343 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3344 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3345 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3346 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3347 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3349 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3350 | SourceLocation OnePastCurly = |
| 3351 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3352 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3353 | } |
| 3354 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3356 | // Now comment out any visibility specifiers. |
| 3357 | while (cursor < endBuf) { |
| 3358 | if (*cursor == '@') { |
| 3359 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3360 | // Skip whitespace. |
| 3361 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3362 | /*scan*/; |
| 3363 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3364 | // FIXME: presence of @public, etc. inside comment results in |
| 3365 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3366 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3367 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3368 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3369 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3370 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3371 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3372 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3373 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3374 | // for type checking. |
| 3375 | else if (*cursor == '<') { |
| 3376 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3377 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3378 | cursor = strchr(cursor, '>'); |
| 3379 | cursor++; |
| 3380 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3381 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3382 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3383 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3384 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3385 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3386 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3387 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3388 | // Don't forget to add a ';'!! |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3389 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3390 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3391 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3392 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3393 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3394 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3395 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3396 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3397 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3398 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3399 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3400 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3401 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3404 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3405 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3406 | template<typename MethodIterator> |
| 3407 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3408 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3409 | bool IsInstanceMethod, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3410 | StringRef prefix, |
| 3411 | StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3412 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3413 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3415 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3416 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3417 | SEL _cmd; |
| 3418 | char *method_types; |
| 3419 | void *_imp; |
| 3420 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3421 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3422 | Result += "\nstruct _objc_method {\n"; |
| 3423 | Result += "\tSEL _cmd;\n"; |
| 3424 | Result += "\tchar *method_types;\n"; |
| 3425 | Result += "\tvoid *_imp;\n"; |
| 3426 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3428 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3431 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3433 | /* struct { |
| 3434 | struct _objc_method_list *next_method; |
| 3435 | int method_count; |
| 3436 | struct _objc_method method_list[]; |
| 3437 | } |
| 3438 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3439 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3440 | Result += "\nstatic struct {\n"; |
| 3441 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3442 | Result += "\tint method_count;\n"; |
| 3443 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3444 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3445 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3446 | Result += prefix; |
| 3447 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3448 | Result += "_METHODS_"; |
| 3449 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3450 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3451 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3452 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3453 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3454 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3455 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3456 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3457 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3458 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3459 | Result += "\", \""; |
| 3460 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3461 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3462 | Result += MethodInternalNames[*MethodBegin]; |
| 3463 | Result += "}\n"; |
| 3464 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3465 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3466 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3467 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3468 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3469 | Result += "\", \""; |
| 3470 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3471 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3472 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3473 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3474 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3475 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3478 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3479 | void RewriteObjC:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3480 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, StringRef prefix, |
| 3481 | StringRef ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3482 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Output struct protocol_methods holder of method selector and type. |
| 3485 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3486 | /* struct protocol_methods { |
| 3487 | SEL _cmd; |
| 3488 | char *method_types; |
| 3489 | } |
| 3490 | */ |
| 3491 | Result += "\nstruct _protocol_methods {\n"; |
| 3492 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3493 | Result += "\tchar *method_types;\n"; |
| 3494 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3496 | objc_protocol_methods = true; |
| 3497 | } |
| 3498 | // Do not synthesize the protocol more than once. |
| 3499 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3500 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3502 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3503 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3504 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3505 | /* struct _objc_protocol_method_list { |
| 3506 | int protocol_method_count; |
| 3507 | struct protocol_methods protocols[]; |
| 3508 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3509 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3510 | Result += "\nstatic struct {\n"; |
| 3511 | Result += "\tint protocol_method_count;\n"; |
| 3512 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3513 | Result += utostr(NumMethods); |
| 3514 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3515 | Result += PDecl->getNameAsString(); |
| 3516 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3517 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3519 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | for (ObjCProtocolDecl::instmeth_iterator |
| 3521 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3522 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3523 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3524 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3525 | else |
| 3526 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3527 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3528 | std::string MethodTypeString; |
| 3529 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3530 | Result += "\", \""; |
| 3531 | Result += MethodTypeString; |
| 3532 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3533 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3534 | Result += "\t }\n};\n"; |
| 3535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3537 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3538 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3539 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3540 | if (NumMethods > 0) { |
| 3541 | /* struct _objc_protocol_method_list { |
| 3542 | int protocol_method_count; |
| 3543 | struct protocol_methods protocols[]; |
| 3544 | } |
| 3545 | */ |
| 3546 | Result += "\nstatic struct {\n"; |
| 3547 | Result += "\tint protocol_method_count;\n"; |
| 3548 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3549 | Result += utostr(NumMethods); |
| 3550 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3551 | Result += PDecl->getNameAsString(); |
| 3552 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3553 | "{\n\t"; |
| 3554 | Result += utostr(NumMethods); |
| 3555 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3557 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3559 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3560 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3561 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3562 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3563 | else |
| 3564 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3565 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3566 | std::string MethodTypeString; |
| 3567 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3568 | Result += "\", \""; |
| 3569 | Result += MethodTypeString; |
| 3570 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3571 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3572 | Result += "\t }\n};\n"; |
| 3573 | } |
| 3574 | |
| 3575 | // Output: |
| 3576 | /* struct _objc_protocol { |
| 3577 | // Objective-C 1.0 extensions |
| 3578 | struct _objc_protocol_extension *isa; |
| 3579 | char *protocol_name; |
| 3580 | struct _objc_protocol **protocol_list; |
| 3581 | struct _objc_protocol_method_list *instance_methods; |
| 3582 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3584 | */ |
| 3585 | static bool objc_protocol = false; |
| 3586 | if (!objc_protocol) { |
| 3587 | Result += "\nstruct _objc_protocol {\n"; |
| 3588 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3589 | Result += "\tchar *protocol_name;\n"; |
| 3590 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3591 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3592 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3593 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3595 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3596 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3597 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3598 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3599 | Result += PDecl->getNameAsString(); |
| 3600 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3601 | "{\n\t0, \""; |
| 3602 | Result += PDecl->getNameAsString(); |
| 3603 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3604 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3605 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3606 | Result += PDecl->getNameAsString(); |
| 3607 | Result += ", "; |
| 3608 | } |
| 3609 | else |
| 3610 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3611 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3612 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3613 | Result += PDecl->getNameAsString(); |
| 3614 | Result += "\n"; |
| 3615 | } |
| 3616 | else |
| 3617 | Result += "0\n"; |
| 3618 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3620 | // Mark this protocol as having been generated. |
| 3621 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3622 | assert(false && "protocol already synthesized"); |
| 3623 | |
| 3624 | } |
| 3625 | |
| 3626 | void RewriteObjC:: |
| 3627 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3628 | StringRef prefix, StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3629 | std::string &Result) { |
| 3630 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3631 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3632 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3633 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3634 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3635 | // Output the top lovel protocol meta-data for the class. |
| 3636 | /* struct _objc_protocol_list { |
| 3637 | struct _objc_protocol_list *next; |
| 3638 | int protocol_count; |
| 3639 | struct _objc_protocol *class_protocols[]; |
| 3640 | } |
| 3641 | */ |
| 3642 | Result += "\nstatic struct {\n"; |
| 3643 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3644 | Result += "\tint protocol_count;\n"; |
| 3645 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3646 | Result += utostr(Protocols.size()); |
| 3647 | Result += "];\n} _OBJC_"; |
| 3648 | Result += prefix; |
| 3649 | Result += "_PROTOCOLS_"; |
| 3650 | Result += ClassName; |
| 3651 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3652 | "{\n\t0, "; |
| 3653 | Result += utostr(Protocols.size()); |
| 3654 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3656 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3657 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3658 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3660 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3661 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3662 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3663 | Result += "\n"; |
| 3664 | } |
| 3665 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3668 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3670 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3671 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3672 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3673 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3674 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3675 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3677 | CDecl = CDecl->getNextClassCategory()) |
| 3678 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3679 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3681 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3682 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3683 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3685 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3686 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3687 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3688 | |
| 3689 | // If any of our property implementations have associated getters or |
| 3690 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3691 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3692 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3693 | Prop != PropEnd; ++Prop) { |
| 3694 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3695 | continue; |
| 3696 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3697 | continue; |
| 3698 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3699 | if (!PD) |
| 3700 | continue; |
| 3701 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3702 | InstanceMethods.push_back(Getter); |
| 3703 | if (PD->isReadOnly()) |
| 3704 | continue; |
| 3705 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3706 | InstanceMethods.push_back(Setter); |
| 3707 | } |
| 3708 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3709 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3710 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3712 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3713 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3714 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3715 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3717 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3718 | // Null CDecl is case of a category implementation with no category interface |
| 3719 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3720 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3721 | FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3722 | /* struct _objc_category { |
| 3723 | char *category_name; |
| 3724 | char *class_name; |
| 3725 | struct _objc_method_list *instance_methods; |
| 3726 | struct _objc_method_list *class_methods; |
| 3727 | struct _objc_protocol_list *protocols; |
| 3728 | // Objective-C 1.0 extensions |
| 3729 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3731 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3733 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3735 | static bool objc_category = false; |
| 3736 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3737 | Result += "\nstruct _objc_category {\n"; |
| 3738 | Result += "\tchar *category_name;\n"; |
| 3739 | Result += "\tchar *class_name;\n"; |
| 3740 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3741 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3742 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3744 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3745 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3746 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3747 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3748 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3749 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3750 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3751 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3752 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3753 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3754 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3756 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3757 | Result += "\t, (struct _objc_method_list *)" |
| 3758 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3759 | Result += FullCategoryName; |
| 3760 | Result += "\n"; |
| 3761 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3762 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3763 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3764 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3765 | Result += "\t, (struct _objc_method_list *)" |
| 3766 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3767 | Result += FullCategoryName; |
| 3768 | Result += "\n"; |
| 3769 | } |
| 3770 | else |
| 3771 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3773 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3775 | Result += FullCategoryName; |
| 3776 | Result += "\n"; |
| 3777 | } |
| 3778 | else |
| 3779 | Result += "\t, 0\n"; |
| 3780 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3783 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3784 | /// ivar offset. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3785 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3786 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3787 | if (ivar->isBitField()) { |
| 3788 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3789 | // place all bitfields at offset 0. |
| 3790 | Result += "0"; |
| 3791 | } else { |
| 3792 | Result += "__OFFSETOFIVAR__(struct "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3793 | Result += ivar->getContainingInterface()->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3794 | if (LangOpts.Microsoft) |
| 3795 | Result += "_IMPL"; |
| 3796 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3797 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3798 | Result += ")"; |
| 3799 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3802 | //===----------------------------------------------------------------------===// |
| 3803 | // Meta Data Emission |
| 3804 | //===----------------------------------------------------------------------===// |
| 3805 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3806 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3807 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3808 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3810 | // Explicitly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3811 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3813 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3814 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3817 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3818 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3820 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3821 | if (NumIvars > 0) { |
| 3822 | static bool objc_ivar = false; |
| 3823 | if (!objc_ivar) { |
| 3824 | /* struct _objc_ivar { |
| 3825 | char *ivar_name; |
| 3826 | char *ivar_type; |
| 3827 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3829 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3830 | Result += "\nstruct _objc_ivar {\n"; |
| 3831 | Result += "\tchar *ivar_name;\n"; |
| 3832 | Result += "\tchar *ivar_type;\n"; |
| 3833 | Result += "\tint ivar_offset;\n"; |
| 3834 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3836 | objc_ivar = true; |
| 3837 | } |
| 3838 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3839 | /* struct { |
| 3840 | int ivar_count; |
| 3841 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3843 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3845 | Result += "\tint ivar_count;\n"; |
| 3846 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3847 | Result += utostr(NumIvars); |
| 3848 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3849 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3850 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3851 | "{\n\t"; |
| 3852 | Result += utostr(NumIvars); |
| 3853 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3855 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3856 | SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3857 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3858 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3859 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3860 | IV != IVEnd; ++IV) |
| 3861 | IVars.push_back(*IV); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3862 | IVI = IDecl->ivar_begin(); |
| 3863 | IVE = IDecl->ivar_end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3864 | } else { |
| 3865 | IVI = CDecl->ivar_begin(); |
| 3866 | IVE = CDecl->ivar_end(); |
| 3867 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3868 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3869 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3870 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3871 | std::string TmpString, StrEncoding; |
| 3872 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3873 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3874 | Result += StrEncoding; |
| 3875 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3876 | SynthesizeIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3877 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3878 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3879 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3880 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3881 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3882 | std::string TmpString, StrEncoding; |
| 3883 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3884 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3885 | Result += StrEncoding; |
| 3886 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3887 | SynthesizeIvarOffsetComputation((*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3888 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3891 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3892 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3894 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3895 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3896 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3897 | |
| 3898 | // If any of our property implementations have associated getters or |
| 3899 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3900 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3901 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3902 | Prop != PropEnd; ++Prop) { |
| 3903 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3904 | continue; |
| 3905 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3906 | continue; |
| 3907 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3908 | if (!PD) |
| 3909 | continue; |
| 3910 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3911 | if (!Getter->isDefined()) |
| 3912 | InstanceMethods.push_back(Getter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3913 | if (PD->isReadOnly()) |
| 3914 | continue; |
| 3915 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3916 | if (!Setter->isDefined()) |
| 3917 | InstanceMethods.push_back(Setter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3918 | } |
| 3919 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3920 | true, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3922 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3923 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3924 | false, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3926 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3927 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3928 | "CLASS", CDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3930 | // Declaration of class/meta-class metadata |
| 3931 | /* struct _objc_class { |
| 3932 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3933 | const char *super_class_name; |
| 3934 | char *name; |
| 3935 | long version; |
| 3936 | long info; |
| 3937 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3938 | struct _objc_ivar_list *ivars; |
| 3939 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3940 | struct objc_cache *cache; |
| 3941 | struct objc_protocol_list *protocols; |
| 3942 | const char *ivar_layout; |
| 3943 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3944 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3945 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3946 | static bool objc_class = false; |
| 3947 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3948 | Result += "\nstruct _objc_class {\n"; |
| 3949 | Result += "\tstruct _objc_class *isa;\n"; |
| 3950 | Result += "\tconst char *super_class_name;\n"; |
| 3951 | Result += "\tchar *name;\n"; |
| 3952 | Result += "\tlong version;\n"; |
| 3953 | Result += "\tlong info;\n"; |
| 3954 | Result += "\tlong instance_size;\n"; |
| 3955 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3956 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3957 | Result += "\tstruct objc_cache *cache;\n"; |
| 3958 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3959 | Result += "\tconst char *ivar_layout;\n"; |
| 3960 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3961 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3962 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3964 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3965 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3966 | ObjCInterfaceDecl *RootClass = 0; |
| 3967 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3968 | while (SuperClass) { |
| 3969 | RootClass = SuperClass; |
| 3970 | SuperClass = SuperClass->getSuperClass(); |
| 3971 | } |
| 3972 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3974 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3975 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3976 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3977 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3978 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3979 | Result += "\""; |
| 3980 | |
| 3981 | if (SuperClass) { |
| 3982 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3983 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3984 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3985 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3986 | Result += "\""; |
| 3987 | } |
| 3988 | else { |
| 3989 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3990 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3991 | Result += "\""; |
| 3992 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3993 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3994 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3995 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3996 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3997 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3998 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4000 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4001 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4002 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4003 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4004 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4005 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4006 | Result += ",0,0\n"; |
| 4007 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 4008 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4009 | Result += "\t,0,0,0,0\n"; |
| 4010 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4012 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4013 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4014 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4015 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4016 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4017 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4018 | if (SuperClass) { |
| 4019 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4020 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4021 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4022 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4023 | Result += "\""; |
| 4024 | } |
| 4025 | else { |
| 4026 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4027 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4028 | Result += "\""; |
| 4029 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4030 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4031 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4032 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4033 | Result += ",0"; |
| 4034 | else { |
| 4035 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 4036 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4037 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 4038 | if (LangOpts.Microsoft) |
| 4039 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4040 | Result += ")"; |
| 4041 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4042 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 4043 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4044 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4045 | Result += "\n\t"; |
| 4046 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4047 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4048 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4049 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 4050 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4051 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4053 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4054 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4055 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4056 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4057 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4058 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4059 | Result += ", 0,0\n"; |
| 4060 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4061 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4062 | Result += ",0,0,0\n"; |
| 4063 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4064 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4065 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4066 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4067 | /// and emits meta-data. |
| 4068 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4069 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4070 | int ClsDefCount = ClassImplementation.size(); |
| 4071 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4073 | // Rewrite implemented methods |
| 4074 | for (int i = 0; i < ClsDefCount; i++) |
| 4075 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 4077 | for (int i = 0; i < CatDefCount; i++) |
| 4078 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4079 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4081 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 4082 | int ClsDefCount = ClassImplementation.size(); |
| 4083 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 4084 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4085 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4086 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4087 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4089 | // For each implemented category, write out all its meta data. |
| 4090 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4091 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4092 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4093 | // Write objc_symtab metadata |
| 4094 | /* |
| 4095 | struct _objc_symtab |
| 4096 | { |
| 4097 | long sel_ref_cnt; |
| 4098 | SEL *refs; |
| 4099 | short cls_def_cnt; |
| 4100 | short cat_def_cnt; |
| 4101 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4103 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4105 | Result += "\nstruct _objc_symtab {\n"; |
| 4106 | Result += "\tlong sel_ref_cnt;\n"; |
| 4107 | Result += "\tSEL *refs;\n"; |
| 4108 | Result += "\tshort cls_def_cnt;\n"; |
| 4109 | Result += "\tshort cat_def_cnt;\n"; |
| 4110 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4111 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4112 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4113 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4114 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4115 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4116 | + ", " + utostr(CatDefCount) + "\n"; |
| 4117 | for (int i = 0; i < ClsDefCount; i++) { |
| 4118 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4119 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4120 | Result += "\n"; |
| 4121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4123 | for (int i = 0; i < CatDefCount; i++) { |
| 4124 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4125 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4126 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4127 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4128 | Result += "\n"; |
| 4129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4131 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4133 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4135 | /* |
| 4136 | struct _objc_module { |
| 4137 | long version; |
| 4138 | long size; |
| 4139 | const char *name; |
| 4140 | struct _objc_symtab *symtab; |
| 4141 | } |
| 4142 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4144 | Result += "\nstruct _objc_module {\n"; |
| 4145 | Result += "\tlong version;\n"; |
| 4146 | Result += "\tlong size;\n"; |
| 4147 | Result += "\tconst char *name;\n"; |
| 4148 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4149 | Result += "};\n\n"; |
| 4150 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4151 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4153 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4154 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4155 | |
| 4156 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4157 | if (ProtocolExprDecls.size()) { |
| 4158 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4159 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4161 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4162 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4163 | Result += (*I)->getNameAsString(); |
| 4164 | Result += " = &_OBJC_PROTOCOL_"; |
| 4165 | Result += (*I)->getNameAsString(); |
| 4166 | Result += ";\n"; |
| 4167 | } |
| 4168 | Result += "#pragma data_seg(pop)\n\n"; |
| 4169 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4170 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4171 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4172 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4173 | Result += "&_OBJC_MODULES;\n"; |
| 4174 | Result += "#pragma data_seg(pop)\n\n"; |
| 4175 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4176 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4177 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4178 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4179 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4180 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4181 | assert(BlockByRefDeclNo.count(VD) && |
| 4182 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4183 | if (def) |
| 4184 | ResultStr += "struct "; |
| 4185 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4186 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4187 | } |
| 4188 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4189 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4190 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4191 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4192 | return false; |
| 4193 | } |
| 4194 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4195 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4196 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4197 | std::string Tag) { |
| 4198 | const FunctionType *AFT = CE->getFunctionType(); |
| 4199 | QualType RT = AFT->getResultType(); |
| 4200 | std::string StructRef = "struct " + Tag; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 4201 | std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4202 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4203 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4204 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4206 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4208 | // block (to reference imported block decl refs). |
| 4209 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4210 | } else if (BD->param_empty()) { |
| 4211 | S += "(" + StructRef + " *__cself)"; |
| 4212 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4213 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4214 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4215 | S += '('; |
| 4216 | // first add the implicit argument. |
| 4217 | S += StructRef + " *__cself, "; |
| 4218 | std::string ParamStr; |
| 4219 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4220 | E = BD->param_end(); AI != E; ++AI) { |
| 4221 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4222 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4223 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4224 | if (convertBlockPointerToFunctionPointer(QT)) |
| 4225 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4226 | else |
| 4227 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4228 | S += ParamStr; |
| 4229 | } |
| 4230 | if (FT->isVariadic()) { |
| 4231 | if (!BD->param_empty()) S += ", "; |
| 4232 | S += "..."; |
| 4233 | } |
| 4234 | S += ')'; |
| 4235 | } |
| 4236 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4238 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4239 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4240 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4241 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4242 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4243 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4244 | std::string TypeString; |
| 4245 | RewriteByRefString(TypeString, Name, (*I)); |
| 4246 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4247 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4248 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4249 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4250 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4251 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4252 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4253 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4254 | // Handle nested closure invocation. For example: |
| 4255 | // |
| 4256 | // void (^myImportedClosure)(void); |
| 4257 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4259 | // void (^anotherClosure)(void); |
| 4260 | // anotherClosure = ^(void) { |
| 4261 | // myImportedClosure(); // import and invoke the closure |
| 4262 | // }; |
| 4263 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4264 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4265 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4266 | S += " = ("; |
| 4267 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4268 | S += ")"; |
| 4269 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4270 | } |
| 4271 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4272 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4273 | QualType QT = (*I)->getType(); |
| 4274 | if (HasLocalVariableExternalStorage(*I)) |
| 4275 | QT = Context->getPointerType(QT); |
| 4276 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4277 | S += Name + " = __cself->" + |
| 4278 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4279 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4280 | } |
| 4281 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4282 | const char *cstr = RewrittenStr.c_str(); |
| 4283 | while (*cstr++ != '{') ; |
| 4284 | S += cstr; |
| 4285 | S += "\n"; |
| 4286 | return S; |
| 4287 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4288 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4289 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4290 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4291 | std::string Tag) { |
| 4292 | std::string StructRef = "struct " + Tag; |
| 4293 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4295 | S += funcName; |
| 4296 | S += "_block_copy_" + utostr(i); |
| 4297 | S += "(" + StructRef; |
| 4298 | S += "*dst, " + StructRef; |
| 4299 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4301 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4302 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4303 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4304 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4305 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4306 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4307 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4308 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4309 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4310 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4311 | else |
| 4312 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4313 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4314 | S += "}\n"; |
| 4315 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4316 | S += "\nstatic void __"; |
| 4317 | S += funcName; |
| 4318 | S += "_block_dispose_" + utostr(i); |
| 4319 | S += "(" + StructRef; |
| 4320 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4322 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4323 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4324 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4325 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4326 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4327 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4328 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4329 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4330 | else |
| 4331 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4334 | return S; |
| 4335 | } |
| 4336 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4337 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4338 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4339 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4340 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4341 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4342 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4343 | S += " struct " + Desc; |
| 4344 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4346 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4347 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4348 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4350 | if (BlockDeclRefs.size()) { |
| 4351 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4352 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4353 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4354 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4355 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4356 | std::string ArgName = "_" + FieldName; |
| 4357 | // Handle nested closure invocation. For example: |
| 4358 | // |
| 4359 | // void (^myImportedBlock)(void); |
| 4360 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4361 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4362 | // void (^anotherBlock)(void); |
| 4363 | // anotherBlock = ^(void) { |
| 4364 | // myImportedBlock(); // import and invoke the closure |
| 4365 | // }; |
| 4366 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4367 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4368 | S += "struct __block_impl *"; |
| 4369 | Constructor += ", void *" + ArgName; |
| 4370 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4371 | QualType QT = (*I)->getType(); |
| 4372 | if (HasLocalVariableExternalStorage(*I)) |
| 4373 | QT = Context->getPointerType(QT); |
| 4374 | QT.getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4375 | QT.getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4376 | Constructor += ", " + ArgName; |
| 4377 | } |
| 4378 | S += FieldName + ";\n"; |
| 4379 | } |
| 4380 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4381 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4382 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4383 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4384 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4385 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4386 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4387 | std::string TypeString; |
| 4388 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4389 | TypeString += " *"; |
| 4390 | FieldName = TypeString + FieldName; |
| 4391 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4392 | Constructor += ", " + ArgName; |
| 4393 | } |
| 4394 | S += FieldName + "; // by ref\n"; |
| 4395 | } |
| 4396 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4397 | Constructor += ", int flags=0)"; |
| 4398 | // Initialize all "by copy" arguments. |
| 4399 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4400 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4401 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4402 | std::string Name = (*I)->getNameAsString(); |
| 4403 | if (firsTime) { |
| 4404 | Constructor += " : "; |
| 4405 | firsTime = false; |
| 4406 | } |
| 4407 | else |
| 4408 | Constructor += ", "; |
| 4409 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4410 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4411 | else |
| 4412 | Constructor += Name + "(_" + Name + ")"; |
| 4413 | } |
| 4414 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4415 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4416 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4417 | std::string Name = (*I)->getNameAsString(); |
| 4418 | if (firsTime) { |
| 4419 | Constructor += " : "; |
| 4420 | firsTime = false; |
| 4421 | } |
| 4422 | else |
| 4423 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4424 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4428 | if (GlobalVarDecl) |
| 4429 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4430 | else |
| 4431 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4432 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4434 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4435 | } else { |
| 4436 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4437 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4438 | if (GlobalVarDecl) |
| 4439 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4440 | else |
| 4441 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4442 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4443 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4444 | } |
| 4445 | Constructor += " "; |
| 4446 | Constructor += "}\n"; |
| 4447 | S += Constructor; |
| 4448 | S += "};\n"; |
| 4449 | return S; |
| 4450 | } |
| 4451 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4452 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4453 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4454 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4455 | unsigned hasCopy) { |
| 4456 | std::string S = "\nstatic struct " + DescTag; |
| 4457 | |
| 4458 | S += " {\n unsigned long reserved;\n"; |
| 4459 | S += " unsigned long Block_size;\n"; |
| 4460 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4461 | S += " void (*copy)(struct "; |
| 4462 | S += ImplTag; S += "*, struct "; |
| 4463 | S += ImplTag; S += "*);\n"; |
| 4464 | |
| 4465 | S += " void (*dispose)(struct "; |
| 4466 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4467 | } |
| 4468 | S += "} "; |
| 4469 | |
| 4470 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4471 | S += ImplTag + ")"; |
| 4472 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4473 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4474 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4475 | } |
| 4476 | S += "};\n"; |
| 4477 | return S; |
| 4478 | } |
| 4479 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4480 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4481 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4482 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4483 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4484 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4485 | bool RewriteSC = (GlobalVarDecl && |
| 4486 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4487 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4488 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4489 | if (RewriteSC) { |
| 4490 | std::string SC(" void __"); |
| 4491 | SC += GlobalVarDecl->getNameAsString(); |
| 4492 | SC += "() {}"; |
| 4493 | InsertText(FunLocStart, SC); |
| 4494 | } |
| 4495 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4496 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4497 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4498 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4499 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4500 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4501 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4502 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4503 | ValueDecl *VD = Exp->getDecl(); |
| 4504 | BlockDeclRefs.push_back(Exp); |
| 4505 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4506 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4507 | BlockByCopyDecls.push_back(VD); |
| 4508 | } |
| 4509 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4510 | BlockByRefDeclsPtrSet.insert(VD); |
| 4511 | BlockByRefDecls.push_back(VD); |
| 4512 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 4513 | // imported objects in the inner blocks not used in the outer |
| 4514 | // blocks must be copied/disposed in the outer block as well. |
| 4515 | if (Exp->isByRef() || |
| 4516 | VD->getType()->isObjCObjectPointerType() || |
| 4517 | VD->getType()->isBlockPointerType()) |
| 4518 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4519 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4520 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4521 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4522 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4524 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4525 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4526 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4527 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4528 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4530 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4533 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4534 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4535 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4536 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4537 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4538 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4540 | BlockDeclRefs.clear(); |
| 4541 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4542 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4543 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4544 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4545 | ImportedBlockDecls.clear(); |
| 4546 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4547 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4548 | // Must insert any 'const/volatile/static here. Since it has been |
| 4549 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4550 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4551 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4552 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4553 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4554 | SC += "const "; |
| 4555 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4556 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4557 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4558 | SC += "restrict "; |
| 4559 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4562 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4563 | InnerDeclRefsCount.clear(); |
| 4564 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4565 | RewrittenBlockExprs.clear(); |
| 4566 | } |
| 4567 | |
| 4568 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4569 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4570 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4572 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4573 | } |
| 4574 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4575 | static void BuildUniqueMethodName(std::string &Name, |
| 4576 | ObjCMethodDecl *MD) { |
| 4577 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4578 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4579 | Name += "__" + MD->getSelector().getAsString(); |
| 4580 | // Convert colons to underscores. |
| 4581 | std::string::size_type loc = 0; |
| 4582 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4583 | Name.replace(loc, 1, "_"); |
| 4584 | } |
| 4585 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4586 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4587 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4588 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4589 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4590 | std::string FuncName; |
| 4591 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4592 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4596 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4597 | if (*CI) { |
| 4598 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4599 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4600 | else |
| 4601 | GetBlockDeclRefExprs(*CI); |
| 4602 | } |
| 4603 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4604 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4605 | // FIXME: Handle enums. |
| 4606 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4607 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4608 | } |
| 4609 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4610 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4611 | BlockDeclRefExpr *BDRE = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4612 | new (Context)BlockDeclRefExpr(cast<VarDecl>(DRE->getDecl()), |
| 4613 | DRE->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4614 | VK_LValue, DRE->getLocation(), false); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4615 | BlockDeclRefs.push_back(BDRE); |
| 4616 | } |
| 4617 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4618 | return; |
| 4619 | } |
| 4620 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4621 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4622 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4623 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4624 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4625 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4626 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4627 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4628 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4629 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4630 | InnerContexts); |
| 4631 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4632 | else |
| 4633 | GetInnerBlockDeclRefExprs(*CI, |
| 4634 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4635 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4636 | |
| 4637 | } |
| 4638 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4639 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4640 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4641 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4642 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4643 | } |
| 4644 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4645 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4646 | if (Var->isFunctionOrMethodVarDecl()) |
| 4647 | ImportedLocalExternalDecls.insert(Var); |
| 4648 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4649 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4650 | return; |
| 4651 | } |
| 4652 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4653 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4654 | /// whose result type may be a block pointer or whose argument type(s) |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 4655 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4656 | /// all block pointers to function pointers. |
| 4657 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4658 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4659 | // FTP will be null for closures that don't take arguments. |
| 4660 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4661 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4662 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4663 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4664 | |
| 4665 | if (FTP) { |
| 4666 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4667 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4668 | QualType t = *I; |
| 4669 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4670 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4671 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4672 | ArgTypes.push_back(t); |
| 4673 | } |
| 4674 | } |
| 4675 | QualType FuncType; |
| 4676 | // FIXME. Does this work if block takes no argument but has a return type |
| 4677 | // which is of block type? |
| 4678 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4679 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4680 | else FuncType = QualType(FT, 0); |
| 4681 | return FuncType; |
| 4682 | } |
| 4683 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4684 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4685 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4686 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4687 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4688 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4689 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4690 | } else if (const BlockDeclRefExpr *CDRE = |
| 4691 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4692 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4693 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4694 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4695 | } |
| 4696 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4697 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4698 | } |
| 4699 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4700 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4701 | else if (const ConditionalOperator *CEXPR = |
| 4702 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4703 | Expr *LHSExp = CEXPR->getLHS(); |
| 4704 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4705 | Expr *RHSExp = CEXPR->getRHS(); |
| 4706 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4707 | Expr *CONDExp = CEXPR->getCond(); |
| 4708 | ConditionalOperator *CondExpr = |
| 4709 | new (Context) ConditionalOperator(CONDExp, |
| 4710 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4711 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4712 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4713 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4714 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4715 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4716 | } else { |
| 4717 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4718 | } |
| 4719 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4720 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4721 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4722 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4723 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4725 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4726 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4727 | &Context->Idents.get("__block_impl")); |
| 4728 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4729 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4730 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4731 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4733 | // Push the block argument type. |
| 4734 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4735 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4737 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4738 | QualType t = *I; |
| 4739 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4740 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4741 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4742 | ArgTypes.push_back(t); |
| 4743 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4744 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4745 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4746 | QualType PtrToFuncCastType |
| 4747 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4749 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4751 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4752 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4753 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4754 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4755 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4756 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4757 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4759 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4760 | SourceLocation(), |
| 4761 | &Context->Idents.get("FuncPtr"), |
| 4762 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4763 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4764 | /*HasInit=*/false); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4765 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4766 | FD->getType(), VK_LValue, |
| 4767 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4769 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4770 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4771 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4772 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4774 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4775 | // Add the implicit argument. |
| 4776 | BlkExprs.push_back(BlkCast); |
| 4777 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4779 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4780 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4781 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4782 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4783 | BlkExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4784 | Exp->getType(), VK_RValue, |
| 4785 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4786 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4789 | // We need to return the rewritten expression to handle cases where the |
| 4790 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4791 | // For example: |
| 4792 | // |
| 4793 | // int main() { |
| 4794 | // __block Foo *f; |
| 4795 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4797 | // void (^myblock)() = ^() { |
| 4798 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4799 | // i = 77; |
| 4800 | // }; |
| 4801 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4802 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4803 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4804 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4805 | ValueDecl *VD; |
| 4806 | bool isArrow = true; |
| 4807 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4808 | VD = BDRE->getDecl(); |
| 4809 | else { |
| 4810 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4811 | isArrow = false; |
| 4812 | } |
| 4813 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4814 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4815 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4816 | &Context->Idents.get("__forwarding"), |
| 4817 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4818 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4819 | /*HasInit=*/false); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4820 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4821 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4822 | FD->getType(), VK_LValue, |
| 4823 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4824 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4825 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4826 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4827 | &Context->Idents.get(Name), |
| 4828 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4829 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4830 | /*HasInit=*/false); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4831 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4832 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4833 | |
| 4834 | |
| 4835 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4836 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 4837 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4838 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4839 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4840 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4841 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4844 | // Rewrites the imported local variable V with external storage |
| 4845 | // (static, extern, etc.) as *V |
| 4846 | // |
| 4847 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4848 | ValueDecl *VD = DRE->getDecl(); |
| 4849 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4850 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4851 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4852 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4853 | VK_LValue, OK_Ordinary, |
| 4854 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4855 | // Need parens to enforce precedence. |
| 4856 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4857 | Exp); |
| 4858 | ReplaceStmt(DRE, PE); |
| 4859 | return PE; |
| 4860 | } |
| 4861 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4862 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4863 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4864 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4865 | |
| 4866 | // Need to avoid trying to rewrite synthesized casts. |
| 4867 | if (LocStart.isInvalid()) |
| 4868 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4869 | // Need to avoid trying to rewrite casts contained in macros. |
| 4870 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4871 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4873 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4874 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4875 | QualType QT = CE->getType(); |
| 4876 | const Type* TypePtr = QT->getAs<Type>(); |
| 4877 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4878 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4879 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4880 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4881 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4882 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4883 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4884 | return; |
| 4885 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4886 | // advance the location to startArgList. |
| 4887 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4889 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4890 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4891 | case '^': |
| 4892 | // Replace the '^' with '*'. |
| 4893 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4894 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4895 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | return; |
| 4899 | } |
| 4900 | |
| 4901 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4902 | SourceLocation DeclLoc = FD->getLocation(); |
| 4903 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4905 | // We have 1 or more arguments that have closure pointers. |
| 4906 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4907 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4908 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4909 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4910 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4911 | parenCount++; |
| 4912 | // advance the location to startArgList. |
| 4913 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4914 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4916 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4918 | while (*argPtr++ && parenCount) { |
| 4919 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4920 | case '^': |
| 4921 | // Replace the '^' with '*'. |
| 4922 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4923 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4924 | break; |
| 4925 | case '(': |
| 4926 | parenCount++; |
| 4927 | break; |
| 4928 | case ')': |
| 4929 | parenCount--; |
| 4930 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4931 | } |
| 4932 | } |
| 4933 | return; |
| 4934 | } |
| 4935 | |
| 4936 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4937 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4938 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4939 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4940 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4941 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4942 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4943 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4944 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4945 | } |
| 4946 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4948 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4949 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4950 | return true; |
| 4951 | } |
| 4952 | return false; |
| 4953 | } |
| 4954 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4955 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4956 | const FunctionProtoType *FTP; |
| 4957 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4958 | if (PT) { |
| 4959 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4960 | } else { |
| 4961 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4962 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4963 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4964 | } |
| 4965 | if (FTP) { |
| 4966 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4967 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4968 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4969 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4970 | if ((*I)->isObjCObjectPointerType() && |
| 4971 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4972 | return true; |
| 4973 | } |
| 4974 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4975 | } |
| 4976 | return false; |
| 4977 | } |
| 4978 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4979 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4980 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4981 | const char *argPtr = strchr(Name, '('); |
| 4982 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4983 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4984 | LParen = argPtr; // output the start. |
| 4985 | argPtr++; // skip past the left paren. |
| 4986 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4988 | while (*argPtr && parenCount) { |
| 4989 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4990 | case '(': parenCount++; break; |
| 4991 | case ')': parenCount--; break; |
| 4992 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4993 | } |
| 4994 | if (parenCount) argPtr++; |
| 4995 | } |
| 4996 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4997 | RParen = argPtr; // output the end |
| 4998 | } |
| 4999 | |
| 5000 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 5001 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 5002 | RewriteBlockPointerFunctionArgs(FD); |
| 5003 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5004 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5005 | // Handle Variables and Typedefs. |
| 5006 | SourceLocation DeclLoc = ND->getLocation(); |
| 5007 | QualType DeclT; |
| 5008 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 5009 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5010 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5011 | DeclT = TDD->getUnderlyingType(); |
| 5012 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 5013 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | else |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5015 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5016 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5017 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 5018 | const char *endBuf = startBuf; |
| 5019 | // scan backward (from the decl location) for the end of the previous decl. |
| 5020 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 5021 | startBuf--; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5022 | SourceLocation Start = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 5023 | std::string buf; |
| 5024 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5025 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 5026 | // may take block argument types (which will be handled below). |
| 5027 | if (*startBuf == '^') { |
| 5028 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5029 | buf = '*'; |
| 5030 | startBuf++; |
| 5031 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5032 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5033 | while (*startBuf != ')') { |
| 5034 | buf += *startBuf; |
| 5035 | startBuf++; |
| 5036 | OrigLength++; |
| 5037 | } |
| 5038 | buf += ')'; |
| 5039 | OrigLength++; |
| 5040 | |
| 5041 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 5042 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5043 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5044 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5045 | DeclLoc = ND->getLocation(); |
| 5046 | startBuf = SM->getCharacterData(DeclLoc); |
| 5047 | const char *argListBegin, *argListEnd; |
| 5048 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 5049 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5050 | if (*argListBegin == '^') |
| 5051 | buf += '*'; |
| 5052 | else if (*argListBegin == '<') { |
| 5053 | buf += "/*"; |
| 5054 | buf += *argListBegin++; |
| 5055 | OrigLength++;; |
| 5056 | while (*argListBegin != '>') { |
| 5057 | buf += *argListBegin++; |
| 5058 | OrigLength++; |
| 5059 | } |
| 5060 | buf += *argListBegin; |
| 5061 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5062 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5063 | else |
| 5064 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5065 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5066 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5067 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5068 | buf += ')'; |
| 5069 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5070 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5071 | ReplaceText(Start, OrigLength, buf); |
| 5072 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5073 | return; |
| 5074 | } |
| 5075 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5076 | |
| 5077 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 5078 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 5079 | /// struct Block_byref_id_object *src) { |
| 5080 | /// _Block_object_assign (&_dest->object, _src->object, |
| 5081 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5082 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5083 | /// _Block_object_assign(&_dest->object, _src->object, |
| 5084 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5085 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5086 | /// } |
| 5087 | /// And: |
| 5088 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 5089 | /// _Block_object_dispose(_src->object, |
| 5090 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5091 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5092 | /// _Block_object_dispose(_src->object, |
| 5093 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5094 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5095 | /// } |
| 5096 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5097 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 5098 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5099 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 5100 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5101 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5102 | CopyDestroyCache.insert(flag); |
| 5103 | S = "static void __Block_byref_id_object_copy_"; |
| 5104 | S += utostr(flag); |
| 5105 | S += "(void *dst, void *src) {\n"; |
| 5106 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5107 | // offset into the object pointer is computed as: |
| 5108 | // void * + void* + int + int + void* + void * |
| 5109 | unsigned IntSize = |
| 5110 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5111 | unsigned VoidPtrSize = |
| 5112 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 5113 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 5114 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5115 | S += " _Block_object_assign((char*)dst + "; |
| 5116 | S += utostr(offset); |
| 5117 | S += ", *(void * *) ((char*)src + "; |
| 5118 | S += utostr(offset); |
| 5119 | S += "), "; |
| 5120 | S += utostr(flag); |
| 5121 | S += ");\n}\n"; |
| 5122 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5123 | S += "static void __Block_byref_id_object_dispose_"; |
| 5124 | S += utostr(flag); |
| 5125 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5126 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5127 | S += utostr(offset); |
| 5128 | S += "), "; |
| 5129 | S += utostr(flag); |
| 5130 | S += ");\n}\n"; |
| 5131 | return S; |
| 5132 | } |
| 5133 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5134 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5135 | /// the declaration into: |
| 5136 | /// struct __Block_byref_ND { |
| 5137 | /// void *__isa; // NULL for everything except __weak pointers |
| 5138 | /// struct __Block_byref_ND *__forwarding; |
| 5139 | /// int32_t __flags; |
| 5140 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5141 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5142 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5143 | /// typex ND; |
| 5144 | /// }; |
| 5145 | /// |
| 5146 | /// It then replaces declaration of ND variable with: |
| 5147 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5148 | /// __size=sizeof(struct __Block_byref_ND), |
| 5149 | /// ND=initializer-if-any}; |
| 5150 | /// |
| 5151 | /// |
| 5152 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5153 | // Insert declaration for the function in which block literal is |
| 5154 | // used. |
| 5155 | if (CurFunctionDeclToDeclareForBlock) |
| 5156 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5157 | int flag = 0; |
| 5158 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5159 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5160 | if (DeclLoc.isInvalid()) |
| 5161 | // If type location is missing, it is because of missing type (a warning). |
| 5162 | // Use variable's location which is good for this case. |
| 5163 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5164 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5165 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5166 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5167 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5168 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5169 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5170 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5171 | ByrefType += " {\n"; |
| 5172 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5173 | RewriteByRefString(ByrefType, Name, ND); |
| 5174 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5175 | ByrefType += " int __flags;\n"; |
| 5176 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5177 | // Add void *__Block_byref_id_object_copy; |
| 5178 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5179 | QualType Ty = ND->getType(); |
| 5180 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5181 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5182 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5183 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5184 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5185 | |
| 5186 | QualType T = Ty; |
| 5187 | (void)convertBlockPointerToFunctionPointer(T); |
| 5188 | T.getAsStringInternal(Name, Context->PrintingPolicy); |
| 5189 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5190 | ByrefType += " " + Name + ";\n"; |
| 5191 | ByrefType += "};\n"; |
| 5192 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5193 | SourceLocation FunLocStart; |
| 5194 | if (CurFunctionDef) |
| 5195 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5196 | else { |
| 5197 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5198 | FunLocStart = CurMethodDef->getLocStart(); |
| 5199 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5200 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5201 | if (Ty.isObjCGCWeak()) { |
| 5202 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5203 | isa = 1; |
| 5204 | } |
| 5205 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5206 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5207 | flag = BLOCK_BYREF_CALLER; |
| 5208 | QualType Ty = ND->getType(); |
| 5209 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5210 | if (Ty->isBlockPointerType()) |
| 5211 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5212 | else |
| 5213 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5214 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5215 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5216 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5217 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5218 | |
| 5219 | // struct __Block_byref_ND ND = |
| 5220 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5221 | // initializer-if-any}; |
| 5222 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5223 | unsigned flags = 0; |
| 5224 | if (HasCopyAndDispose) |
| 5225 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5226 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5227 | ByrefType.clear(); |
| 5228 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5229 | std::string ForwardingCastType("("); |
| 5230 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5231 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5232 | ByrefType += " " + Name + " = {(void*)"; |
| 5233 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5234 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5235 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5236 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5237 | ByrefType += "sizeof("; |
| 5238 | RewriteByRefString(ByrefType, Name, ND); |
| 5239 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5240 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5241 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5242 | ByrefType += utostr(flag); |
| 5243 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5244 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5245 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5246 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5247 | unsigned nameSize = Name.size(); |
| 5248 | // for block or function pointer declaration. Name is aleady |
| 5249 | // part of the declaration. |
| 5250 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5251 | nameSize = 1; |
| 5252 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5253 | } |
| 5254 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5255 | SourceLocation startLoc; |
| 5256 | Expr *E = ND->getInit(); |
| 5257 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5258 | startLoc = ECE->getLParenLoc(); |
| 5259 | else |
| 5260 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5261 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5262 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5263 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5264 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5265 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5266 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5267 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5268 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5269 | ByrefType += "sizeof("; |
| 5270 | RewriteByRefString(ByrefType, Name, ND); |
| 5271 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5272 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5273 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5274 | ByrefType += utostr(flag); |
| 5275 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5276 | ByrefType += utostr(flag); |
| 5277 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5278 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5279 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5280 | |
| 5281 | // Complete the newly synthesized compound expression by inserting a right |
| 5282 | // curly brace before the end of the declaration. |
| 5283 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5284 | // also assumes there is only one declarator. For example, the following |
| 5285 | // isn't currently supported by this routine (in general): |
| 5286 | // |
| 5287 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5288 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5289 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5290 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5291 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5292 | SourceLocation semiLoc = |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5293 | startLoc.getFileLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5294 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5295 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5296 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5297 | return; |
| 5298 | } |
| 5299 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5300 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5301 | // Add initializers for any closure decl refs. |
| 5302 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5303 | if (BlockDeclRefs.size()) { |
| 5304 | // Unique all "by copy" declarations. |
| 5305 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5306 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5307 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5308 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5309 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5310 | } |
| 5311 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5312 | // Unique all "by ref" declarations. |
| 5313 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5314 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5315 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5316 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5317 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5318 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5319 | } |
| 5320 | // Find any imported blocks...they will need special attention. |
| 5321 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5322 | if (BlockDeclRefs[i]->isByRef() || |
| 5323 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5324 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5325 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5329 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5330 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5331 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5332 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5333 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5334 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5337 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5338 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5339 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5340 | Blocks.push_back(Exp); |
| 5341 | |
| 5342 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5343 | |
| 5344 | // Add inner imported variables now used in current block. |
| 5345 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5346 | if (!InnerBlockDeclRefs.empty()) { |
| 5347 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5348 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5349 | ValueDecl *VD = Exp->getDecl(); |
| 5350 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5351 | // We need to save the copied-in variables in nested |
| 5352 | // blocks because it is needed at the end for some of the API generations. |
| 5353 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5354 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5355 | BlockDeclRefs.push_back(Exp); |
| 5356 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5357 | BlockByCopyDecls.push_back(VD); |
| 5358 | } |
| 5359 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5360 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5361 | BlockDeclRefs.push_back(Exp); |
| 5362 | BlockByRefDeclsPtrSet.insert(VD); |
| 5363 | BlockByRefDecls.push_back(VD); |
| 5364 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5365 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5366 | // Find any imported blocks...they will need special attention. |
| 5367 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5368 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5369 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5370 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5371 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5372 | } |
| 5373 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5374 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5375 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5376 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5377 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5378 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5379 | else if (CurMethodDef) |
| 5380 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5381 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5382 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5383 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5384 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5385 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5386 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5387 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5389 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 5390 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5391 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5392 | |
| 5393 | FunctionDecl *FD; |
| 5394 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5395 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5396 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5397 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5398 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, VK_RValue, |
| 5399 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5400 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5401 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5402 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5403 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5404 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5405 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5406 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5407 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5408 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | InitExprs.push_back(castExpr); |
| 5410 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5411 | // Initialize the block descriptor. |
| 5412 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5413 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5414 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5415 | SourceLocation(), SourceLocation(), |
| 5416 | &Context->Idents.get(DescData.c_str()), |
| 5417 | Context->VoidPtrTy, 0, |
| 5418 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5419 | UnaryOperator *DescRefExpr = |
| 5420 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, |
| 5421 | Context->VoidPtrTy, |
| 5422 | VK_LValue, |
| 5423 | SourceLocation()), |
| 5424 | UO_AddrOf, |
| 5425 | Context->getPointerType(Context->VoidPtrTy), |
| 5426 | VK_RValue, OK_Ordinary, |
| 5427 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5428 | InitExprs.push_back(DescRefExpr); |
| 5429 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5430 | // Add initializers for any closure decl refs. |
| 5431 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5432 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5433 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5434 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5435 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5436 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5437 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5438 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5439 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5440 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5441 | if (HasLocalVariableExternalStorage(*I)) { |
| 5442 | QualType QT = (*I)->getType(); |
| 5443 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5444 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5445 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5446 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5447 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5448 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5449 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5450 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5451 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5452 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5453 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5454 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5455 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5456 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5457 | if (HasLocalVariableExternalStorage(*I)) { |
| 5458 | QualType QT = (*I)->getType(); |
| 5459 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5460 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5461 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5464 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5466 | } |
| 5467 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5468 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5469 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5470 | ValueDecl *ND = (*I); |
| 5471 | std::string Name(ND->getNameAsString()); |
| 5472 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5473 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5474 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5475 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5476 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5477 | SourceLocation(), SourceLocation(), |
| 5478 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5479 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5480 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5481 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5482 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5483 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5484 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5485 | bool isNestedCapturedVar = false; |
| 5486 | if (block) |
| 5487 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5488 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5489 | const VarDecl *variable = ci->getVariable(); |
| 5490 | if (variable == ND && ci->isNested()) { |
| 5491 | assert (ci->isByRef() && |
| 5492 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5493 | isNestedCapturedVar = true; |
| 5494 | break; |
| 5495 | } |
| 5496 | } |
| 5497 | // captured nested byref variable has its address passed. Do not take |
| 5498 | // its address again. |
| 5499 | if (!isNestedCapturedVar) |
| 5500 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5501 | Context->getPointerType(Exp->getType()), |
| 5502 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5503 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5504 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5505 | } |
| 5506 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5507 | if (ImportedBlockDecls.size()) { |
| 5508 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5509 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5510 | unsigned IntSize = |
| 5511 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5512 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5513 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5514 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5515 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5516 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5517 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5518 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5519 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5520 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5521 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5522 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5523 | BlockDeclRefs.clear(); |
| 5524 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5525 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5526 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5527 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5528 | ImportedBlockDecls.clear(); |
| 5529 | return NewRep; |
| 5530 | } |
| 5531 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5532 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5533 | if (const ObjCForCollectionStmt * CS = |
| 5534 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5535 | return CS->getElement() == DS; |
| 5536 | return false; |
| 5537 | } |
| 5538 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5539 | //===----------------------------------------------------------------------===// |
| 5540 | // Function Body / Expression rewriting |
| 5541 | //===----------------------------------------------------------------------===// |
| 5542 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5543 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5544 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5545 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5546 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5547 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5548 | // we get this right. |
| 5549 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5550 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 5551 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5552 | if (*CI) |
| 5553 | CollectPropertySetters(*CI); |
| 5554 | |
| 5555 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5556 | if (BinOp->isAssignmentOp()) { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5557 | if (isa<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 5558 | PropSetters[BinOp->getLHS()] = BinOp; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | } |
| 5562 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5563 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5564 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5565 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5566 | Stmts.push_back(S); |
| 5567 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5568 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5569 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5571 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5572 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5573 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5574 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 5575 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5576 | if (*CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5577 | Stmt *newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5578 | Stmt *ChildStmt = (*CI); |
| 5579 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(ChildStmt)) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5580 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5581 | bool replaced = false; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5582 | newStmt = RewriteObjCNestedIvarRefExpr(ChildStmt, replaced); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5583 | if (replaced) { |
| 5584 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5585 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5586 | else |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5587 | ReplaceStmt(ChildStmt, newStmt); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | else |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5591 | newStmt = RewriteFunctionBodyOrGlobalInitializer(ChildStmt); |
| 5592 | if (newStmt) { |
| 5593 | if (Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(ChildStmt)) |
| 5594 | if (PropSetters[PropOrImplicitRefExpr] == S) { |
| 5595 | S = newStmt; |
| 5596 | newStmt = 0; |
| 5597 | } |
| 5598 | if (newStmt) |
| 5599 | *CI = newStmt; |
| 5600 | } |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5601 | // If dealing with an assignment with LHS being a property reference |
| 5602 | // expression, the entire assignment tree is rewritten into a property |
| 5603 | // setter messaging. This involvs the RHS too. Do not attempt to rewrite |
| 5604 | // RHS again. |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5605 | if (Expr *Exp = dyn_cast<Expr>(ChildStmt)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5606 | if (isa<ObjCPropertyRefExpr>(Exp)) { |
Fariborz Jahanian | 8537f7b | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5607 | if (PropSetters[Exp]) { |
| 5608 | ++CI; |
| 5609 | continue; |
| 5610 | } |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5611 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 5612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5614 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5615 | SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5616 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5617 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5618 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5619 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5620 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5621 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5622 | Stmt *SaveCurrentBody = CurrentBody; |
| 5623 | CurrentBody = BE->getBody(); |
Fariborz Jahanian | 3617809 | 2011-04-08 23:48:29 +0000 | [diff] [blame] | 5624 | CollectPropertySetters(CurrentBody); |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5625 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5626 | // block literal on rhs of a property-dot-sytax assignment |
| 5627 | // must be replaced by its synthesize ast so getRewrittenText |
| 5628 | // works as expected. In this case, what actually ends up on RHS |
| 5629 | // is the blockTranscribed which is the helper function for the |
| 5630 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5631 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5632 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5633 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5634 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5635 | CurrentBody = SaveCurrentBody; |
| 5636 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5637 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5638 | // Now we snarf the rewritten text and stash it away for later use. |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5639 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5640 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5642 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5643 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5644 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5645 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5646 | return blockTranscribed; |
| 5647 | } |
| 5648 | // Handle specific things. |
| 5649 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5650 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5651 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5652 | if (isa<ObjCPropertyRefExpr>(S)) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5653 | Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(S); |
| 5654 | assert(PropOrImplicitRefExpr && "Property or implicit setter/getter is null"); |
| 5655 | |
| 5656 | BinaryOperator *BinOp = PropSetters[PropOrImplicitRefExpr]; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5657 | if (BinOp) { |
| 5658 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5659 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5660 | DisableReplaceStmt = true; |
| 5661 | // Save the source range. Even if we disable the replacement, the |
| 5662 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5663 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5664 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5665 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5666 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5667 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Fariborz Jahanian | f2c6fa4 | 2010-10-14 23:31:39 +0000 | [diff] [blame] | 5668 | // Need to rewrite the ivar access expression if need be. |
| 5669 | if (isa<ObjCIvarRefExpr>(newStmt)) { |
| 5670 | bool replaced = false; |
| 5671 | newStmt = RewriteObjCNestedIvarRefExpr(newStmt, replaced); |
| 5672 | } |
| 5673 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5674 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5675 | // |
| 5676 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5677 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5678 | // to see the original expression). Consider this example: |
| 5679 | // |
| 5680 | // Foo *obj1, *obj2; |
| 5681 | // |
| 5682 | // obj1.i = [obj2 rrrr]; |
| 5683 | // |
| 5684 | // 'BinOp' for the previous expression looks like: |
| 5685 | // |
| 5686 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5687 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5688 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5689 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5690 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5691 | // |
| 5692 | // 'newStmt' represents the rewritten message expression. For example: |
| 5693 | // |
| 5694 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5695 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5696 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5697 | // (CStyleCastExpr 0x231d220 'void *' |
| 5698 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5699 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5700 | // Note that 'newStmt' is passed to RewritePropertyOrImplicitSetter so that it |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5701 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5702 | // the original RHS (since we haven't altered BinOp). |
| 5703 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5704 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5705 | // node. As a result, we now leak the original AST nodes. |
| 5706 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5707 | return RewritePropertyOrImplicitSetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5708 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5709 | return RewritePropertyOrImplicitGetter(PropOrImplicitRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5710 | } |
| 5711 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5712 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5713 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5714 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5716 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5717 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5718 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5719 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5720 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5721 | // Before we rewrite it, put the original message expression in a comment. |
| 5722 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5723 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5724 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5725 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5726 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5727 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5728 | std::string messString; |
| 5729 | messString += "// "; |
| 5730 | messString.append(startBuf, endBuf-startBuf+1); |
| 5731 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | |
| 5733 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5734 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5735 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5736 | // Tried this, but it didn't work either... |
| 5737 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5738 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5739 | return RewriteMessageExpr(MessExpr); |
| 5740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5741 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5742 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5743 | return RewriteObjCTryStmt(StmtTry); |
| 5744 | |
| 5745 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5746 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5747 | |
| 5748 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5749 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5750 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5751 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5752 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5753 | |
| 5754 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5755 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5756 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5757 | OrigStmtRange.getEnd()); |
| 5758 | if (BreakStmt *StmtBreakStmt = |
| 5759 | dyn_cast<BreakStmt>(S)) |
| 5760 | return RewriteBreakStmt(StmtBreakStmt); |
| 5761 | if (ContinueStmt *StmtContinueStmt = |
| 5762 | dyn_cast<ContinueStmt>(S)) |
| 5763 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5764 | |
| 5765 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5766 | // and cast exprs. |
| 5767 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5768 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5769 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5771 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5772 | // the context of an ObjCForCollectionStmt. For example: |
| 5773 | // NSArray *someArray; |
| 5774 | // for (id <FooProtocol> index in someArray) ; |
| 5775 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5776 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5777 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5778 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5779 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5780 | // Blocks rewrite rules. |
| 5781 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5782 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5783 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5784 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5785 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5786 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5787 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5788 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5789 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5790 | if (VD->hasAttr<BlocksAttr>()) { |
| 5791 | static unsigned uniqueByrefDeclCount = 0; |
| 5792 | assert(!BlockByRefDeclNo.count(ND) && |
| 5793 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5794 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5795 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5796 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5797 | else |
| 5798 | RewriteTypeOfDecl(VD); |
| 5799 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5800 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5801 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5802 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5803 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5804 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5805 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5806 | } |
| 5807 | } |
| 5808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5809 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5810 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5811 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | |
| 5813 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5814 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5815 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5816 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5817 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5818 | && "Statement stack mismatch"); |
| 5819 | Stmts.pop_back(); |
| 5820 | } |
| 5821 | // Handle blocks rewriting. |
| 5822 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5823 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5824 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5825 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5826 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5827 | ValueDecl *VD = DRE->getDecl(); |
| 5828 | if (VD->hasAttr<BlocksAttr>()) |
| 5829 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5830 | if (HasLocalVariableExternalStorage(VD)) |
| 5831 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5834 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5835 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5836 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5837 | ReplaceStmt(S, BlockCall); |
| 5838 | return BlockCall; |
| 5839 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5840 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5841 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5842 | RewriteCastExpr(CE); |
| 5843 | } |
| 5844 | #if 0 |
| 5845 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5846 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5847 | ICE->getSubExpr(), |
| 5848 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5849 | // Get the new text. |
| 5850 | std::string SStr; |
| 5851 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5852 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5853 | const std::string &Str = Buf.str(); |
| 5854 | |
| 5855 | printf("CAST = %s\n", &Str[0]); |
| 5856 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5857 | delete S; |
| 5858 | return Replacement; |
| 5859 | } |
| 5860 | #endif |
| 5861 | // Return this stmt unmodified. |
| 5862 | return S; |
| 5863 | } |
| 5864 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5865 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5866 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5867 | e = RD->field_end(); i != e; ++i) { |
| 5868 | FieldDecl *FD = *i; |
| 5869 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5870 | RewriteBlockPointerDecl(FD); |
| 5871 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5872 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5873 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5874 | } |
| 5875 | } |
| 5876 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5877 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5878 | /// main file of the input. |
| 5879 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5880 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5881 | if (FD->isOverloadedOperator()) |
| 5882 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5883 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5884 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5885 | // prototype. This enables us to rewrite function declarations and |
| 5886 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5887 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5888 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5889 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 5f1bfc1 | 2010-07-07 11:31:34 +0000 | [diff] [blame] | 5890 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5891 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5892 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5893 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5894 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5895 | Body = |
| 5896 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5897 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5898 | CurrentBody = 0; |
| 5899 | if (PropParentMap) { |
| 5900 | delete PropParentMap; |
| 5901 | PropParentMap = 0; |
| 5902 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5903 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5904 | // and any copy/dispose helper functions. |
| 5905 | InsertBlockLiteralsWithinFunction(FD); |
| 5906 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5907 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5909 | return; |
| 5910 | } |
| 5911 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5912 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5913 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5914 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5915 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5916 | Body = |
| 5917 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5918 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5919 | CurrentBody = 0; |
| 5920 | if (PropParentMap) { |
| 5921 | delete PropParentMap; |
| 5922 | PropParentMap = 0; |
| 5923 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5924 | InsertBlockLiteralsWithinMethod(MD); |
| 5925 | CurMethodDef = 0; |
| 5926 | } |
| 5927 | } |
| 5928 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5929 | ClassImplementation.push_back(CI); |
| 5930 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5931 | CategoryImplementation.push_back(CI); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 5932 | else if (isa<ObjCClassDecl>(D)) |
| 5933 | assert(false && "RewriteObjC::HandleDeclInMainFile - ObjCClassDecl"); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5934 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5935 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5936 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5937 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5938 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5939 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5940 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5941 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5942 | RewriteCastExpr(CE); |
| 5943 | } |
| 5944 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5945 | } else if (VD->getType()->isRecordType()) { |
| 5946 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5947 | if (RD->isDefinition()) |
| 5948 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5949 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5950 | if (VD->getInit()) { |
| 5951 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5952 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5953 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5954 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5955 | CurrentBody = 0; |
| 5956 | if (PropParentMap) { |
| 5957 | delete PropParentMap; |
| 5958 | PropParentMap = 0; |
| 5959 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5960 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5961 | VD->getName()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5962 | GlobalVarDecl = 0; |
| 5963 | |
| 5964 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5965 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5966 | RewriteCastExpr(CE); |
| 5967 | } |
| 5968 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5969 | return; |
| 5970 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5971 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5972 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5973 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5974 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5975 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5976 | return; |
| 5977 | } |
| 5978 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5979 | if (RD->isDefinition()) |
| 5980 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5981 | return; |
| 5982 | } |
| 5983 | // Nothing yet. |
| 5984 | } |
| 5985 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5986 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5987 | if (Diags.hasErrorOccurred()) |
| 5988 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5989 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5990 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5992 | // Here's a great place to add any extra declarations that may be needed. |
| 5993 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5994 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5995 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5996 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5997 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5998 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5999 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 6000 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6001 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6002 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 6003 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6005 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 6006 | //printf("Changed:\n"); |
| 6007 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 6008 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 6009 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6010 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 6011 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6012 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 6013 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 6014 | // Rewrite Objective-c meta data* |
| 6015 | std::string ResultStr; |
| 6016 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 6017 | // Emit metadata. |
| 6018 | *OutFile << ResultStr; |
| 6019 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6020 | OutFile->flush(); |
| 6021 | } |