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() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 423 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 424 | if (const ObjCObjectPointerType * OBJPT = |
| 425 | T->getAsObjCInterfacePointerType()) { |
| 426 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 427 | T = QualType(IFaceT, 0); |
| 428 | T = Context->getPointerType(T); |
| 429 | } |
| 430 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 433 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 434 | bool isObjCType(QualType T) { |
| 435 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 436 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 438 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 440 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 441 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 442 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 444 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 446 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 452 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 453 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 454 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 455 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 458 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 459 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 461 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 463 | if (From[i] == '"') |
| 464 | To += "\\\""; |
| 465 | else |
| 466 | To += From[i]; |
| 467 | } |
| 468 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 469 | |
| 470 | QualType getSimpleFunctionType(QualType result, |
| 471 | const QualType *args, |
| 472 | unsigned numArgs, |
| 473 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 474 | if (result == Context->getObjCInstanceType()) |
| 475 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 476 | FunctionProtoType::ExtProtoInfo fpi; |
| 477 | fpi.Variadic = variadic; |
| 478 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 479 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 480 | }; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 481 | |
| 482 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 483 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 484 | CastKind Kind, Expr *E) { |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 485 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 486 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 487 | SourceLocation(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 492 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 493 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 494 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 496 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 497 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 498 | // All the args are checked/rewritten. Don't call twice! |
| 499 | RewriteBlockPointerDecl(D); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 506 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 507 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 508 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 511 | static bool IsHeaderFile(const std::string &Filename) { |
| 512 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 514 | if (DotPos == std::string::npos) { |
| 515 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 519 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 520 | // C header: .h |
| 521 | // C++ header: .hh or .H; |
| 522 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 525 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 526 | Diagnostic &D, const LangOptions &LOpts, |
| 527 | bool silenceMacroWarn) |
| 528 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 529 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 530 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 532 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 534 | "rewriter doesn't support user-specified control flow semantics " |
| 535 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 538 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 539 | raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 541 | const LangOptions &LOpts, |
| 542 | bool SilenceRewriteMacroWarning) { |
| 543 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 545 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 546 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 547 | Context = &context; |
| 548 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 549 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 550 | MsgSendFunctionDecl = 0; |
| 551 | MsgSendSuperFunctionDecl = 0; |
| 552 | MsgSendStretFunctionDecl = 0; |
| 553 | MsgSendSuperStretFunctionDecl = 0; |
| 554 | MsgSendFpretFunctionDecl = 0; |
| 555 | GetClassFunctionDecl = 0; |
| 556 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 557 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 558 | SelGetUidFunctionDecl = 0; |
| 559 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 560 | ConstantStringClassReference = 0; |
| 561 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 562 | CurMethodDef = 0; |
| 563 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 564 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 565 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 566 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 567 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 568 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 569 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 570 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 571 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 572 | PropParentMap = 0; |
| 573 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 574 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 575 | objc_impl_method = false; |
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 | // Get the ID and start/end of the main file. |
| 578 | MainFileID = SM->getMainFileID(); |
| 579 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 580 | MainFileStart = MainBuf->getBufferStart(); |
| 581 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 583 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 585 | // declaring objc_selector outside the parameter list removes a silly |
| 586 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 587 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 588 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 589 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 590 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 591 | Preamble += "struct objc_object *superClass; "; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 592 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 593 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 594 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 595 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 596 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 597 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 598 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 599 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 600 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 601 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 602 | Preamble += "#endif\n"; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 603 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 604 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 605 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 606 | } else |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 607 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 608 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
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_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 611 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 612 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 613 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 614 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 615 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 616 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 617 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 618 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 619 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 620 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 621 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 622 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 623 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 624 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 625 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 626 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 627 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 628 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 629 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 630 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 631 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 632 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 633 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 634 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 635 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 636 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 637 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 638 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 639 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 640 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 641 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 642 | Preamble += "#endif\n"; |
| 643 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 644 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 645 | Preamble += " int *isa;\n"; |
| 646 | Preamble += " int flags;\n"; |
| 647 | Preamble += " char *str;\n"; |
| 648 | Preamble += " long length;\n"; |
| 649 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 650 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 651 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 652 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 653 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 654 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 655 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 656 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 657 | // Blocks preamble. |
| 658 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 659 | Preamble += "#define BLOCK_IMPL\n"; |
| 660 | Preamble += "struct __block_impl {\n"; |
| 661 | Preamble += " void *isa;\n"; |
| 662 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 663 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 664 | Preamble += " void *FuncPtr;\n"; |
| 665 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 666 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 667 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 668 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 669 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 670 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 671 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 672 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 673 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 674 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 675 | 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] | 676 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 677 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 678 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 679 | Preamble += "#endif\n"; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 680 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 681 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 682 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 683 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 684 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 685 | Preamble += "#endif\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 686 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 687 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 688 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 689 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 690 | Preamble += "#define __weak\n"; |
| 691 | } |
Fariborz Jahanian | df49652 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 692 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 693 | // as this avoids warning in any 64bit/32bit compilation model. |
| 694 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 698 | //===----------------------------------------------------------------------===// |
| 699 | // Top Level Driver Code |
| 700 | //===----------------------------------------------------------------------===// |
| 701 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 702 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 703 | if (Diags.hasErrorOccurred()) |
| 704 | return; |
| 705 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 706 | // Two cases: either the decl could be in the main file, or it could be in a |
| 707 | // #included file. If the former, rewrite it now. If the later, check to see |
| 708 | // if we rewrote the #include/#import. |
| 709 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 710 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 712 | // If this is for a builtin, ignore it. |
| 713 | if (Loc.isInvalid()) return; |
| 714 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 715 | // Look for built-in declarations that we need to refer during the rewrite. |
| 716 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 717 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 718 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 719 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 720 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 721 | ConstantStringClassReference = FVD; |
| 722 | return; |
| 723 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 724 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 725 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 726 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 727 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 728 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 729 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 731 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 732 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 733 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 734 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 735 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 736 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 737 | DI != DIEnd; ) { |
| 738 | if (isa<ObjCClassDecl>((*DI))) { |
| 739 | SmallVector<Decl *, 8> DG; |
| 740 | Decl *D = (*DI); |
| 741 | SourceLocation Loc = D->getLocation(); |
| 742 | while (DI != DIEnd && |
| 743 | isa<ObjCClassDecl>(D) && D->getLocation() == Loc) { |
| 744 | DG.push_back(D); |
| 745 | ++DI; |
| 746 | D = (*DI); |
| 747 | } |
| 748 | RewriteForwardClassDecl(DG); |
| 749 | continue; |
| 750 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 751 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 752 | ++DI; |
| 753 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 755 | // 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] | 756 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 757 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 760 | //===----------------------------------------------------------------------===// |
| 761 | // Syntactic (non-AST) Rewriting Code |
| 762 | //===----------------------------------------------------------------------===// |
| 763 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 764 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 765 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 766 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 767 | const char *MainBufStart = MainBuf.begin(); |
| 768 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 769 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 771 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 772 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 773 | if (*BufPtr == '#') { |
| 774 | if (++BufPtr == MainBufEnd) |
| 775 | return; |
| 776 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 777 | if (++BufPtr == MainBufEnd) |
| 778 | return; |
| 779 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 780 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 782 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 783 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 784 | BufPtr += ImportLen; |
| 785 | } |
| 786 | } |
| 787 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 790 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 791 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 792 | std::string S; |
| 793 | S = "((struct "; |
| 794 | S += ClassDecl->getIdentifier()->getName(); |
| 795 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 796 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 797 | return S; |
| 798 | } |
| 799 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 800 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 801 | ObjCImplementationDecl *IMD, |
| 802 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 803 | static bool objcGetPropertyDefined = false; |
| 804 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 805 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 806 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 807 | const char *startBuf = SM->getCharacterData(startLoc); |
| 808 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 809 | const char *semiBuf = strchr(startBuf, ';'); |
| 810 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 811 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 812 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 813 | |
| 814 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 815 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 817 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 818 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 819 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 821 | if (!OID) |
| 822 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 823 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 824 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 825 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 826 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 827 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 828 | std::string Getr; |
| 829 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 830 | objcGetPropertyDefined = true; |
| 831 | // FIXME. Is this attribute correct in all cases? |
| 832 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 833 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 834 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 835 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 836 | PD->getGetterMethodDecl(), Getr); |
| 837 | Getr += "{ "; |
| 838 | // Synthesize an explicit cast to gain access to the ivar. |
| 839 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 840 | if (GenGetProperty) { |
| 841 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 842 | Getr += "typedef "; |
| 843 | const FunctionType *FPRetType = 0; |
| 844 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 845 | FPRetType); |
| 846 | Getr += " _TYPE"; |
| 847 | if (FPRetType) { |
| 848 | Getr += ")"; // close the precedence "scope" for "*". |
| 849 | |
| 850 | // Now, emit the argument types (if any). |
| 851 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 852 | Getr += "("; |
| 853 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 854 | if (i) Getr += ", "; |
| 855 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 856 | Context->PrintingPolicy); |
| 857 | Getr += ParamStr; |
| 858 | } |
| 859 | if (FT->isVariadic()) { |
| 860 | if (FT->getNumArgs()) Getr += ", "; |
| 861 | Getr += "..."; |
| 862 | } |
| 863 | Getr += ")"; |
| 864 | } else |
| 865 | Getr += "()"; |
| 866 | } |
| 867 | Getr += ";\n"; |
| 868 | Getr += "return (_TYPE)"; |
| 869 | Getr += "objc_getProperty(self, _cmd, "; |
| 870 | SynthesizeIvarOffsetComputation(OID, Getr); |
| 871 | Getr += ", 1)"; |
| 872 | } |
| 873 | else |
| 874 | Getr += "return " + getIvarAccessString(OID); |
| 875 | Getr += "; }"; |
| 876 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 877 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 878 | |
| 879 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 880 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 882 | // Generate the 'setter' function. |
| 883 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 884 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 885 | ObjCPropertyDecl::OBJC_PR_copy); |
| 886 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 887 | objcSetPropertyDefined = true; |
| 888 | // FIXME. Is this attribute correct in all cases? |
| 889 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 890 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 891 | } |
| 892 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 893 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 894 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 895 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 896 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 897 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 898 | if (GenSetProperty) { |
| 899 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 900 | SynthesizeIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 901 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 902 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 903 | Setr += ", "; |
| 904 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 905 | Setr += "0, "; |
| 906 | else |
| 907 | Setr += "1, "; |
| 908 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 909 | Setr += "1)"; |
| 910 | else |
| 911 | Setr += "0)"; |
| 912 | } |
| 913 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 914 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 915 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 916 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 917 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 918 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 919 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 920 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 921 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 922 | std::string &typedefString) { |
| 923 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 924 | typedefString += ForwardDecl->getNameAsString(); |
| 925 | typedefString += "\n"; |
| 926 | typedefString += "#define _REWRITER_typedef_"; |
| 927 | typedefString += ForwardDecl->getNameAsString(); |
| 928 | typedefString += "\n"; |
| 929 | typedefString += "typedef struct objc_object "; |
| 930 | typedefString += ForwardDecl->getNameAsString(); |
| 931 | typedefString += ";\n#endif\n"; |
| 932 | } |
| 933 | |
| 934 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl, |
| 935 | const std::string &typedefString) { |
| 936 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 937 | const char *startBuf = SM->getCharacterData(startLoc); |
| 938 | const char *semiPtr = strchr(startBuf, ';'); |
| 939 | // Replace the @class with typedefs corresponding to the classes. |
| 940 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 941 | } |
| 942 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 943 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 944 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 945 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 946 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(*I); |
| 947 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 948 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 949 | // Translate to typedef's that forward reference structs with the same name |
| 950 | // as the class. As a convenience, we include the original declaration |
| 951 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 952 | typedefString += "// @class "; |
| 953 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 954 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 955 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 956 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 957 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 958 | DeclGroupRef::iterator I = D.begin(); |
| 959 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(*I), typedefString); |
| 960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 962 | void RewriteObjC::RewriteForwardClassDecl( |
| 963 | const llvm::SmallVector<Decl*, 8> &D) { |
| 964 | std::string typedefString; |
| 965 | for (unsigned i = 0; i < D.size(); i++) { |
| 966 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(D[i]); |
| 967 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 968 | if (i == 0) { |
| 969 | typedefString += "// @class "; |
| 970 | typedefString += ForwardDecl->getNameAsString(); |
| 971 | typedefString += ";\n"; |
| 972 | } |
| 973 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 974 | } |
| 975 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 978 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 979 | // When method is a synthesized one, such as a getter/setter there is |
| 980 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 981 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 982 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 983 | SourceLocation LocStart = Method->getLocStart(); |
| 984 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 986 | if (SM->getExpansionLineNumber(LocEnd) > |
| 987 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 988 | InsertText(LocStart, "#if 0\n"); |
| 989 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 990 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 991 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 996 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 998 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 999 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1002 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1003 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1005 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1006 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 1008 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1009 | E = CatDecl->prop_end(); I != E; ++I) |
| 1010 | RewriteProperty(*I); |
| 1011 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | for (ObjCCategoryDecl::instmeth_iterator |
| 1013 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1014 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1015 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1017 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1018 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1019 | RewriteMethodDeclaration(*I); |
| 1020 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1021 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1022 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1023 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1026 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1027 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1029 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1030 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
| 1032 | for (ObjCProtocolDecl::instmeth_iterator |
| 1033 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1034 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1035 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1036 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1037 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1038 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1039 | RewriteMethodDeclaration(*I); |
| 1040 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1041 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1042 | E = PDecl->prop_end(); I != E; ++I) |
| 1043 | RewriteProperty(*I); |
| 1044 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1045 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1046 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1047 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1048 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1049 | // Must comment out @optional/@required |
| 1050 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1051 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1052 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1053 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1054 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1055 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1057 | } |
| 1058 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1059 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1060 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1066 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1067 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1068 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 1069 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1070 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1071 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1074 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1075 | const FunctionType *&FPRetType) { |
| 1076 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1077 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1078 | else if (T->isFunctionPointerType() || |
| 1079 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1080 | // needs special handling, since pointer-to-functions have special |
| 1081 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1082 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1083 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1084 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1085 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1086 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1087 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1088 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1089 | ResultStr += FPRetType->getResultType().getAsString( |
| 1090 | Context->PrintingPolicy); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1091 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1094 | ResultStr += T.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1097 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1098 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1099 | std::string &ResultStr) { |
| 1100 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1101 | const FunctionType *FPRetType = 0; |
| 1102 | ResultStr += "\nstatic "; |
| 1103 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1104 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1106 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1107 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1109 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1110 | NameStr += "_I_"; |
| 1111 | else |
| 1112 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1114 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1115 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1118 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1119 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1120 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1123 | { |
| 1124 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1125 | int len = selString.size(); |
| 1126 | for (int i = 0; i < len; i++) |
| 1127 | if (selString[i] == ':') |
| 1128 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1129 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1130 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1131 | // Remember this name for metadata emission |
| 1132 | MethodInternalNames[OMD] = NameStr; |
| 1133 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1135 | // Rewrite arguments |
| 1136 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1138 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1139 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1140 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1141 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1142 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1143 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1144 | ResultStr += "struct "; |
| 1145 | } |
| 1146 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1147 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1148 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1149 | } |
| 1150 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1151 | ResultStr += Context->getObjCClassType().getAsString( |
| 1152 | Context->PrintingPolicy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1154 | ResultStr += " self, "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1155 | ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1156 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1158 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1159 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1160 | E = OMD->param_end(); PI != E; ++PI) { |
| 1161 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1162 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1163 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1164 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1165 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1166 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1167 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1168 | QualType QT = PDecl->getType(); |
| 1169 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1170 | if (convertBlockPointerToFunctionPointer(QT)) |
| 1171 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
| 1172 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1173 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1174 | ResultStr += Name; |
| 1175 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1176 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1177 | if (OMD->isVariadic()) |
| 1178 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1179 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1181 | if (FPRetType) { |
| 1182 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1184 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1185 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1186 | ResultStr += "("; |
| 1187 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1188 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1189 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1190 | Context->PrintingPolicy); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1191 | ResultStr += ParamStr; |
| 1192 | } |
| 1193 | if (FT->isVariadic()) { |
| 1194 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1195 | ResultStr += "..."; |
| 1196 | } |
| 1197 | ResultStr += ")"; |
| 1198 | } else { |
| 1199 | ResultStr += "()"; |
| 1200 | } |
| 1201 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1202 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1203 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1204 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1205 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1207 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1209 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1210 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1211 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1212 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1213 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1214 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1215 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1216 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1217 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1218 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1219 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1220 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1221 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1224 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1225 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1226 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1227 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1228 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1229 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1230 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1231 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1232 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1234 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1235 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1236 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1237 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1238 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1239 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1241 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1242 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1245 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1248 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1249 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1250 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1251 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1252 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1253 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1254 | ResultStr += "\n"; |
| 1255 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1256 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1257 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1258 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1259 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1260 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1261 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1262 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1263 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1264 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
| 1266 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1267 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1268 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1270 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1271 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1272 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1274 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1275 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1276 | RewriteMethodDeclaration(*I); |
| 1277 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1278 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1279 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1280 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1283 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1284 | SourceRange SrcRange) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1285 | ObjCMethodDecl *OMD = 0; |
| 1286 | QualType Ty; |
| 1287 | Selector Sel; |
Duncan Sands | 54bd457 | 2010-10-20 08:21:16 +0000 | [diff] [blame] | 1288 | Stmt *Receiver = 0; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1289 | bool Super = false; |
| 1290 | QualType SuperTy; |
| 1291 | SourceLocation SuperLocation; |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1292 | SourceLocation SelectorLoc; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1293 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ObjCImplicitSetterGetterRefExpr. |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1294 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1295 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1296 | dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) { |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1297 | SelectorLoc = PropRefExpr->getLocation(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1298 | if (PropRefExpr->isExplicitProperty()) { |
| 1299 | ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty(); |
| 1300 | OMD = PDecl->getSetterMethodDecl(); |
| 1301 | Ty = PDecl->getType(); |
| 1302 | Sel = PDecl->getSetterName(); |
| 1303 | } else { |
| 1304 | OMD = PropRefExpr->getImplicitPropertySetter(); |
| 1305 | Sel = OMD->getSelector(); |
| 1306 | Ty = PropRefExpr->getType(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1307 | } |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1308 | Super = PropRefExpr->isSuperReceiver(); |
| 1309 | if (!Super) { |
| 1310 | Receiver = PropRefExpr->getBase(); |
| 1311 | } else { |
| 1312 | SuperTy = PropRefExpr->getSuperReceiverType(); |
| 1313 | SuperLocation = PropRefExpr->getReceiverLocation(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1314 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | assert(OMD && "RewritePropertyOrImplicitSetter - null OMD"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1318 | SmallVector<Expr *, 1> ExprVec; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1319 | ExprVec.push_back(newStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1321 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1322 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1323 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1324 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1325 | Expr::getValueKindForType(Ty), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1326 | /*FIXME?*/SourceLocation(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1327 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1328 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1329 | SuperTy, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1330 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1331 | &ExprVec[0], 1, |
| 1332 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1333 | else { |
| 1334 | // FIXME. Refactor this into common code with that in |
| 1335 | // RewritePropertyOrImplicitGetter |
| 1336 | assert(Receiver && "RewritePropertyOrImplicitSetter - null Receiver"); |
| 1337 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1338 | if (PropGetters[Exp]) |
| 1339 | // This allows us to handle chain/nested property/implicit getters. |
| 1340 | Receiver = PropGetters[Exp]; |
| 1341 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1342 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1343 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1344 | Expr::getValueKindForType(Ty), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1345 | /*FIXME: */SourceLocation(), |
| 1346 | cast<Expr>(Receiver), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1347 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1348 | &ExprVec[0], 1, |
| 1349 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1350 | } |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1351 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1353 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1354 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1355 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1356 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1357 | // to things that stay around. |
| 1358 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1359 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1362 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr) { |
| 1363 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ImplicitGetter. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1364 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Ted Kremenek | 3b562af | 2010-10-19 23:10:22 +0000 | [diff] [blame] | 1365 | Stmt *Receiver = 0; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1366 | ObjCMethodDecl *OMD = 0; |
| 1367 | QualType Ty; |
| 1368 | Selector Sel; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1369 | bool Super = false; |
| 1370 | QualType SuperTy; |
| 1371 | SourceLocation SuperLocation; |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1372 | SourceLocation SelectorLoc; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1373 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1374 | dyn_cast<ObjCPropertyRefExpr>(PropOrGetterRefExpr)) { |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1375 | SelectorLoc = PropRefExpr->getLocation(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1376 | if (PropRefExpr->isExplicitProperty()) { |
| 1377 | ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty(); |
| 1378 | OMD = PDecl->getGetterMethodDecl(); |
| 1379 | Ty = PDecl->getType(); |
| 1380 | Sel = PDecl->getGetterName(); |
| 1381 | } else { |
| 1382 | OMD = PropRefExpr->getImplicitPropertyGetter(); |
| 1383 | Sel = OMD->getSelector(); |
| 1384 | Ty = PropRefExpr->getType(); |
| 1385 | } |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1386 | Super = PropRefExpr->isSuperReceiver(); |
| 1387 | if (!Super) |
| 1388 | Receiver = PropRefExpr->getBase(); |
| 1389 | else { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1390 | SuperTy = PropRefExpr->getSuperReceiverType(); |
| 1391 | SuperLocation = PropRefExpr->getReceiverLocation(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1392 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | assert (OMD && "RewritePropertyOrImplicitGetter - OMD is null"); |
| 1396 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1397 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1398 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1399 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1400 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1401 | Expr::getValueKindForType(Ty), |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1402 | PropOrGetterRefExpr->getLocStart(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1403 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1404 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1405 | SuperTy, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1406 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1407 | 0, 0, |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1408 | PropOrGetterRefExpr->getLocEnd()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1409 | else { |
| 1410 | assert (Receiver && "RewritePropertyOrImplicitGetter - Receiver is null"); |
| 1411 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1412 | if (PropGetters[Exp]) |
| 1413 | // This allows us to handle chain/nested property/implicit getters. |
| 1414 | Receiver = PropGetters[Exp]; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1415 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1416 | Ty.getNonReferenceType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1417 | Expr::getValueKindForType(Ty), |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1418 | PropOrGetterRefExpr->getLocStart(), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1419 | cast<Expr>(Receiver), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1420 | Sel, SelectorLoc, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1421 | 0, 0, |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1422 | PropOrGetterRefExpr->getLocEnd()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1423 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1424 | |
Fariborz Jahanian | baac58d | 2011-02-26 00:33:41 +0000 | [diff] [blame] | 1425 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr, MsgExpr->getLocStart(), |
| 1426 | MsgExpr->getLocEnd()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1427 | |
| 1428 | if (!PropParentMap) |
| 1429 | PropParentMap = new ParentMap(CurrentBody); |
Fariborz Jahanian | 3cd1ea3 | 2010-12-04 21:22:13 +0000 | [diff] [blame] | 1430 | bool NestedPropertyRef = false; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1431 | Stmt *Parent = PropParentMap->getParent(PropOrGetterRefExpr); |
Fariborz Jahanian | 3cd1ea3 | 2010-12-04 21:22:13 +0000 | [diff] [blame] | 1432 | ImplicitCastExpr*ICE=0; |
| 1433 | if (Parent) |
| 1434 | if ((ICE = dyn_cast<ImplicitCastExpr>(Parent))) { |
| 1435 | assert((ICE->getCastKind() == CK_GetObjCProperty) |
| 1436 | && "RewritePropertyOrImplicitGetter"); |
| 1437 | Parent = PropParentMap->getParent(Parent); |
| 1438 | NestedPropertyRef = (Parent && isa<ObjCPropertyRefExpr>(Parent)); |
| 1439 | } |
| 1440 | if (NestedPropertyRef) { |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1441 | // We stash away the ReplacingStmt since actually doing the |
| 1442 | // 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] | 1443 | PropGetters[ICE] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1444 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1445 | // to things that stay around. |
| 1446 | Context->Deallocate(MsgExpr); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1447 | return PropOrGetterRefExpr; // return the original... |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1448 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1449 | ReplaceStmt(PropOrGetterRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1451 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1452 | // to things that stay around. |
| 1453 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1454 | return ReplacingStmt; |
| 1455 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1459 | SourceLocation OrigStart, |
| 1460 | bool &replaced) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1461 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1462 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1463 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1464 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1465 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1466 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1467 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1468 | // lookup which class implements the instance variable. |
| 1469 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1471 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1472 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1474 | // Synthesize an explicit cast to gain access to the ivar. |
| 1475 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1476 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1477 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1478 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1479 | SourceLocation(), SourceLocation(), |
| 1480 | II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1481 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1482 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1483 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1484 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1485 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1486 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1487 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1488 | IV->getBase()->getLocEnd(), |
| 1489 | castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1490 | replaced = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1492 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1493 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1494 | IV->getLocation(), |
| 1495 | D->getType(), |
| 1496 | VK_LValue, OK_Ordinary); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1497 | // delete IV; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1498 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1499 | } |
Fariborz Jahanian | 7e20ffe | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1500 | // Get the new text |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1501 | // Cannot delete IV->getBase(), since PE points to it. |
| 1502 | // Replace the old base with the cast. This is important when doing |
| 1503 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1505 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1506 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1507 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1508 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1510 | // Explicit ivar refs need to have a cast inserted. |
| 1511 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1512 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1513 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1514 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1515 | // lookup which class implements the instance variable. |
| 1516 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1518 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1519 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1521 | // Synthesize an explicit cast to gain access to the ivar. |
| 1522 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1523 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1524 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1525 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1526 | SourceLocation(), SourceLocation(), |
| 1527 | II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1528 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1529 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1530 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1531 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1532 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1533 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1534 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1535 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1536 | replaced = true; |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1537 | // Cannot delete IV->getBase(), since PE points to it. |
| 1538 | // Replace the old base with the cast. This is important when doing |
| 1539 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1541 | return IV; |
| 1542 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1543 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1544 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1547 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1548 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1549 | if (*CI) { |
| 1550 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1551 | if (newStmt) |
| 1552 | *CI = newStmt; |
| 1553 | } |
| 1554 | } |
| 1555 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1556 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1557 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1558 | replaced); |
| 1559 | return newStmt; |
Fariborz Jahanian | 376338a | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1560 | } |
| 1561 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1562 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1563 | return newStmt; |
| 1564 | } |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1565 | return S; |
| 1566 | } |
| 1567 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1568 | /// SynthCountByEnumWithState - To print: |
| 1569 | /// ((unsigned int (*) |
| 1570 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1572 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | /// "countByEnumeratingWithState:objects:count:"), |
| 1574 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1575 | /// (id *)items, (unsigned int)16) |
| 1576 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1577 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1578 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1579 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1580 | buf += "\n\t\t"; |
| 1581 | buf += "((id)l_collection,\n\t\t"; |
| 1582 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1583 | buf += "\n\t\t"; |
| 1584 | buf += "&enumState, " |
| 1585 | "(id *)items, (unsigned int)16)"; |
| 1586 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1587 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1588 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1589 | /// statement to exit to its outer synthesized loop. |
| 1590 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1591 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1592 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1593 | return S; |
| 1594 | // replace break with goto __break_label |
| 1595 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1597 | SourceLocation startLoc = S->getLocStart(); |
| 1598 | buf = "goto __break_label_"; |
| 1599 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1600 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1606 | /// statement to continue with its inner synthesized loop. |
| 1607 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1608 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1609 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1610 | return S; |
| 1611 | // replace continue with goto __continue_label |
| 1612 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1614 | SourceLocation startLoc = S->getLocStart(); |
| 1615 | buf = "goto __continue_label_"; |
| 1616 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1617 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1622 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1623 | /// It rewrites: |
| 1624 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1626 | /// Into: |
| 1627 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1629 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1630 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1631 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1633 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1634 | /// if (limit) { |
| 1635 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1636 | /// do { |
| 1637 | /// unsigned long counter = 0; |
| 1638 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1640 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1641 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1642 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1643 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1644 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1646 | /// objects:items count:16]); |
| 1647 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1648 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1649 | /// } |
| 1650 | /// else |
| 1651 | /// elem = nil; |
| 1652 | /// } |
| 1653 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1654 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1655 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1656 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1658 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1660 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1662 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1663 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1664 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1665 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1666 | std::string buf; |
| 1667 | buf = "\n{\n\t"; |
| 1668 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1669 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1670 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1671 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1672 | if (ElementType->isObjCQualifiedIdType() || |
| 1673 | ElementType->isObjCQualifiedInterfaceType()) |
| 1674 | // Simply use 'id' for all qualified types. |
| 1675 | elementTypeAsString = "id"; |
| 1676 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1677 | elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1678 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1679 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1680 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1681 | buf += elementName; |
| 1682 | buf += ";\n\t"; |
| 1683 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1684 | else { |
| 1685 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1686 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1687 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1688 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1689 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1690 | // Simply use 'id' for all qualified types. |
| 1691 | elementTypeAsString = "id"; |
| 1692 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1693 | elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1696 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1697 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1698 | // id items[16]; |
| 1699 | buf += "id items[16];\n\t"; |
| 1700 | // id l_collection = (id) |
| 1701 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1702 | // Find start location of 'collection' the hard way! |
| 1703 | const char *startCollectionBuf = startBuf; |
| 1704 | startCollectionBuf += 3; // skip 'for' |
| 1705 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1706 | startCollectionBuf++; // skip '(' |
| 1707 | // find 'in' and skip it. |
| 1708 | while (*startCollectionBuf != ' ' || |
| 1709 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1710 | (*(startCollectionBuf+3) != ' ' && |
| 1711 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1712 | startCollectionBuf++; |
| 1713 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
| 1715 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1716 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1717 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1718 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1719 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1720 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1721 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1723 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1724 | // objects:items count:16]; |
| 1725 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1727 | // ((unsigned int (*) |
| 1728 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1730 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | // "countByEnumeratingWithState:objects:count:"), |
| 1732 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1733 | // (id *)items, (unsigned int)16); |
| 1734 | buf += "unsigned long limit =\n\t\t"; |
| 1735 | SynthCountByEnumWithState(buf); |
| 1736 | buf += ";\n\t"; |
| 1737 | /// if (limit) { |
| 1738 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1739 | /// do { |
| 1740 | /// unsigned long counter = 0; |
| 1741 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1743 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1744 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1745 | buf += "if (limit) {\n\t"; |
| 1746 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1747 | buf += "do {\n\t\t"; |
| 1748 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1749 | buf += "do {\n\t\t\t"; |
| 1750 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1751 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1752 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1753 | buf += " = ("; |
| 1754 | buf += elementTypeAsString; |
| 1755 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1756 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1757 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1759 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1760 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1762 | /// objects:items count:16]); |
| 1763 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1764 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1765 | /// } |
| 1766 | /// else |
| 1767 | /// elem = nil; |
| 1768 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1770 | buf = ";\n\t"; |
| 1771 | buf += "__continue_label_"; |
| 1772 | buf += utostr(ObjCBcLabelNo.back()); |
| 1773 | buf += ": ;"; |
| 1774 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1775 | buf += "} while (counter < limit);\n\t"; |
| 1776 | buf += "} while (limit = "; |
| 1777 | SynthCountByEnumWithState(buf); |
| 1778 | buf += ");\n\t"; |
| 1779 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1780 | buf += " = (("; |
| 1781 | buf += elementTypeAsString; |
| 1782 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1783 | buf += "__break_label_"; |
| 1784 | buf += utostr(ObjCBcLabelNo.back()); |
| 1785 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1786 | buf += "}\n\t"; |
| 1787 | buf += "else\n\t\t"; |
| 1788 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1789 | buf += " = (("; |
| 1790 | buf += elementTypeAsString; |
| 1791 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1792 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1794 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1795 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1796 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1797 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1798 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1799 | } else { |
| 1800 | /* Need to treat single statements specially. For example: |
| 1801 | * |
| 1802 | * for (A *a in b) if (stuff()) break; |
| 1803 | * for (A *a in b) xxxyy; |
| 1804 | * |
| 1805 | * The following code simply scans ahead to the semi to find the actual end. |
| 1806 | */ |
| 1807 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1808 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1809 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1810 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1811 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1812 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1813 | Stmts.pop_back(); |
| 1814 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1815 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1819 | /// This routine rewrites @synchronized(expr) stmt; |
| 1820 | /// into: |
| 1821 | /// objc_sync_enter(expr); |
| 1822 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1823 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1824 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1825 | // Get the start location and compute the semi location. |
| 1826 | SourceLocation startLoc = S->getLocStart(); |
| 1827 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1829 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | |
| 1831 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1832 | buf = "objc_sync_enter((id)"; |
| 1833 | const char *lparenBuf = startBuf; |
| 1834 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1835 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1837 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1838 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1839 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1840 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1841 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1842 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1843 | buf = ");\n"; |
| 1844 | // declare a new scope with two variables, _stack and _rethrow. |
| 1845 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1846 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1847 | buf += "char *pointers[4];} _stack;\n"; |
| 1848 | buf += "id volatile _rethrow = 0;\n"; |
| 1849 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1850 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1851 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1852 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1853 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1855 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1856 | SourceLocation lastCurlyLoc = startLoc; |
| 1857 | buf = "}\nelse {\n"; |
| 1858 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1859 | buf += "}\n"; |
| 1860 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1861 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1862 | |
| 1863 | std::string syncBuf; |
| 1864 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1865 | |
| 1866 | Expr *syncExpr = S->getSynchExpr(); |
| 1867 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1868 | ? CK_BitCast : |
| 1869 | syncExpr->getType()->isBlockPointerType() |
| 1870 | ? CK_BlockPointerToObjCPointerCast |
| 1871 | : CK_CPointerToObjCPointerCast; |
| 1872 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1873 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1874 | std::string syncExprBufS; |
| 1875 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1876 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1877 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1878 | syncBuf += syncExprBuf.str(); |
| 1879 | syncBuf += ");"; |
| 1880 | |
| 1881 | buf += syncBuf; |
| 1882 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1883 | buf += "}\n"; |
| 1884 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1886 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1887 | |
| 1888 | bool hasReturns = false; |
| 1889 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1890 | if (hasReturns) |
| 1891 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1892 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1893 | return 0; |
| 1894 | } |
| 1895 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1896 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1897 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 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 | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1900 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1901 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1902 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1903 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1905 | TryFinallyContainsReturnDiag); |
| 1906 | } |
| 1907 | return; |
| 1908 | } |
| 1909 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1910 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1911 | { |
| 1912 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1913 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1914 | if (*CI) |
| 1915 | HasReturnStmts(*CI, hasReturns); |
| 1916 | |
| 1917 | if (isa<ReturnStmt>(S)) |
| 1918 | hasReturns = true; |
| 1919 | return; |
| 1920 | } |
| 1921 | |
| 1922 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1923 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1924 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1925 | if (*CI) { |
| 1926 | RewriteTryReturnStmts(*CI); |
| 1927 | } |
| 1928 | if (isa<ReturnStmt>(S)) { |
| 1929 | SourceLocation startLoc = S->getLocStart(); |
| 1930 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1931 | |
| 1932 | const char *semiBuf = strchr(startBuf, ';'); |
| 1933 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1934 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1935 | |
| 1936 | std::string buf; |
| 1937 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1938 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1939 | ReplaceText(startLoc, 6, buf); |
| 1940 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1941 | } |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1946 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1947 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1948 | if (*CI) { |
| 1949 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1950 | } |
| 1951 | if (isa<ReturnStmt>(S)) { |
| 1952 | SourceLocation startLoc = S->getLocStart(); |
| 1953 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1954 | |
| 1955 | const char *semiBuf = strchr(startBuf, ';'); |
| 1956 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1957 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1958 | |
| 1959 | std::string buf; |
| 1960 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1961 | buf += syncExitBuf; |
| 1962 | buf += " return"; |
| 1963 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1964 | ReplaceText(startLoc, 6, buf); |
| 1965 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1966 | } |
| 1967 | return; |
| 1968 | } |
| 1969 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1970 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1971 | // Get the start location and compute the semi location. |
| 1972 | SourceLocation startLoc = S->getLocStart(); |
| 1973 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1975 | assert((*startBuf == '@') && "bogus @try location"); |
| 1976 | |
| 1977 | std::string buf; |
| 1978 | // declare a new scope with two variables, _stack and _rethrow. |
| 1979 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1980 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1981 | buf += "char *pointers[4];} _stack;\n"; |
| 1982 | buf += "id volatile _rethrow = 0;\n"; |
| 1983 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1984 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1985 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1986 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1988 | startLoc = S->getTryBody()->getLocEnd(); |
| 1989 | startBuf = SM->getCharacterData(startLoc); |
| 1990 | |
| 1991 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1993 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1994 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1995 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1996 | buf = " /* @catch begin */ else {\n"; |
| 1997 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1998 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1999 | buf += " if (_setjmp(_stack.buf))\n"; |
| 2000 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 2001 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2003 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 2004 | } else { /* no catch list */ |
| 2005 | buf = "}\nelse {\n"; |
| 2006 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 2007 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2008 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 2009 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2010 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2011 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 2012 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 2013 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2014 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2015 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2016 | buf = "if ("; // we are generating code for the first catch clause |
| 2017 | else |
| 2018 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2019 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2020 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2022 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2024 | const char *lParenLoc = strchr(startBuf, '('); |
| 2025 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2026 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2027 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2028 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2029 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 2030 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2031 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2032 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2033 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 2035 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 2036 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2037 | } else if (catchDecl) { |
| 2038 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2039 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2040 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2041 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2042 | } else if (const ObjCObjectPointerType *Ptr = |
| 2043 | t->getAs<ObjCObjectPointerType>()) { |
| 2044 | // Should be a pointer to a class. |
| 2045 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 2046 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2047 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2048 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2049 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2050 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2054 | lastCatchBody = Catch->getCatchBody(); |
| 2055 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2056 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 2057 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 2058 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 2059 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 2060 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2063 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2064 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2065 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 2066 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2067 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2068 | } |
| 2069 | // Complete the catch list... |
| 2070 | if (lastCatchBody) { |
| 2071 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2072 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 2073 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2075 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2076 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2077 | buf = "} /* last catch end */\n"; |
| 2078 | buf += "else {\n"; |
| 2079 | buf += " _rethrow = _caught;\n"; |
| 2080 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 2081 | buf += "} } /* @catch end */\n"; |
| 2082 | if (!S->getFinallyStmt()) |
| 2083 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2084 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2086 | // Set lastCurlyLoc |
| 2087 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 2088 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2089 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2090 | startLoc = finalStmt->getLocStart(); |
| 2091 | startBuf = SM->getCharacterData(startLoc); |
| 2092 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2094 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2096 | Stmt *body = finalStmt->getFinallyBody(); |
| 2097 | SourceLocation startLoc = body->getLocStart(); |
| 2098 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2099 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 2100 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2102 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2104 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2105 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2106 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2107 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2109 | // Set lastCurlyLoc |
| 2110 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 2112 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2113 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2114 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 2115 | buf = "{ /* implicit finally clause */\n"; |
| 2116 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 2117 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 2118 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2119 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2120 | |
| 2121 | // Now check for any return/continue/go statements within the @try. |
| 2122 | // The implicit finally clause won't called if the @try contains any |
| 2123 | // jump statements. |
| 2124 | bool hasReturns = false; |
| 2125 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2126 | if (hasReturns) |
| 2127 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2128 | } |
| 2129 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2130 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2131 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2136 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2137 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2138 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2139 | // Get the start location and compute the semi location. |
| 2140 | SourceLocation startLoc = S->getLocStart(); |
| 2141 | const char *startBuf = SM->getCharacterData(startLoc); |
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 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2144 | |
| 2145 | std::string buf; |
| 2146 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2147 | if (S->getThrowExpr()) |
| 2148 | buf = "objc_exception_throw("; |
| 2149 | else // add an implicit argument |
| 2150 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2152 | // handle "@ throw" correctly. |
| 2153 | const char *wBuf = strchr(startBuf, 'w'); |
| 2154 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2155 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2157 | const char *semiBuf = strchr(startBuf, ';'); |
| 2158 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2159 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2160 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2161 | return 0; |
| 2162 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2163 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2164 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2165 | // Create a new string expression. |
| 2166 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2167 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2168 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2169 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2170 | StringLiteral::Ascii, false, |
| 2171 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2172 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2174 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2175 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2176 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2179 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2180 | if (!SelGetUidFunctionDecl) |
| 2181 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2182 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2183 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2184 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2185 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2187 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2188 | StringLiteral::Ascii, false, |
| 2189 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2190 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2191 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2192 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2193 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2194 | return SelExp; |
| 2195 | } |
| 2196 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2197 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2198 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2199 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2200 | // 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] | 2201 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2203 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2204 | DeclRefExpr *DRE = |
| 2205 | new (Context) DeclRefExpr(FD, msgSendType, VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2207 | // 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] | 2208 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2209 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2210 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2211 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2213 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2215 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2216 | new (Context) CallExpr(*Context, ICE, args, nargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2217 | FT->getCallResultType(*Context), |
| 2218 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2219 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2222 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2223 | const char *&startRef, const char *&endRef) { |
| 2224 | while (startBuf < endBuf) { |
| 2225 | if (*startBuf == '<') |
| 2226 | startRef = startBuf; // mark the start. |
| 2227 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2228 | if (startRef && *startRef == '<') { |
| 2229 | endRef = startBuf; // mark the end. |
| 2230 | return true; |
| 2231 | } |
| 2232 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2233 | } |
| 2234 | startBuf++; |
| 2235 | } |
| 2236 | return false; |
| 2237 | } |
| 2238 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2239 | static void scanToNextArgument(const char *&argRef) { |
| 2240 | int angle = 0; |
| 2241 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2242 | if (*argRef == '<') |
| 2243 | angle++; |
| 2244 | else if (*argRef == '>') |
| 2245 | angle--; |
| 2246 | argRef++; |
| 2247 | } |
| 2248 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2249 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2250 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2251 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2252 | if (T->isObjCQualifiedIdType()) |
| 2253 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2254 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2255 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2256 | return true; |
| 2257 | } |
| 2258 | if (T->isObjCObjectPointerType()) { |
| 2259 | T = T->getPointeeType(); |
| 2260 | return T->isObjCQualifiedInterfaceType(); |
| 2261 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2262 | if (T->isArrayType()) { |
| 2263 | QualType ElemTy = Context->getBaseElementType(T); |
| 2264 | return needToScanForQualifiers(ElemTy); |
| 2265 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2266 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2269 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2270 | QualType Type = E->getType(); |
| 2271 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2272 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2274 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2275 | Loc = ECE->getLParenLoc(); |
| 2276 | EndLoc = ECE->getRParenLoc(); |
| 2277 | } else { |
| 2278 | Loc = E->getLocStart(); |
| 2279 | EndLoc = E->getLocEnd(); |
| 2280 | } |
| 2281 | // This will defend against trying to rewrite synthesized expressions. |
| 2282 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2283 | return; |
| 2284 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2285 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2286 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2287 | const char *startRef = 0, *endRef = 0; |
| 2288 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2289 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2290 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2291 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2292 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2293 | InsertText(LessLoc, "/*"); |
| 2294 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2299 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2300 | SourceLocation Loc; |
| 2301 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2302 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2303 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2304 | Loc = VD->getLocation(); |
| 2305 | Type = VD->getType(); |
| 2306 | } |
| 2307 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2308 | Loc = FD->getLocation(); |
| 2309 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2310 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2311 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2312 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2313 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2314 | if (!proto) |
| 2315 | return; |
| 2316 | Type = proto->getResultType(); |
| 2317 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2318 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2319 | Loc = FD->getLocation(); |
| 2320 | Type = FD->getType(); |
| 2321 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2322 | else |
| 2323 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2325 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2326 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2328 | const char *endBuf = SM->getCharacterData(Loc); |
| 2329 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2330 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2331 | startBuf--; // scan backward (from the decl location) for return type. |
| 2332 | const char *startRef = 0, *endRef = 0; |
| 2333 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2334 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2335 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2336 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2337 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2338 | InsertText(LessLoc, "/*"); |
| 2339 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2340 | } |
| 2341 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2342 | if (!proto) |
| 2343 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2344 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2345 | const char *startBuf = SM->getCharacterData(Loc); |
| 2346 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2347 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2348 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2349 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2351 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2352 | // scan forward (from the decl location) for argument types. |
| 2353 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2354 | const char *startRef = 0, *endRef = 0; |
| 2355 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2356 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2358 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2359 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2360 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2361 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2362 | InsertText(LessLoc, "/*"); |
| 2363 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2364 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2365 | startBuf = ++endBuf; |
| 2366 | } |
| 2367 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2368 | // If the function name is derived from a macro expansion, then the |
| 2369 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2370 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2371 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2372 | startBuf++; |
| 2373 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2374 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2377 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2378 | QualType QT = ND->getType(); |
| 2379 | const Type* TypePtr = QT->getAs<Type>(); |
| 2380 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2381 | return; |
| 2382 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2383 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2384 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2385 | TypePtr = QT->getAs<Type>(); |
| 2386 | } |
| 2387 | // FIXME. This will not work for multiple declarators; as in: |
| 2388 | // __typeof__(a) b,c,d; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2389 | std::string TypeAsString(QT.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2390 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2391 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2392 | if (ND->getInit()) { |
| 2393 | std::string Name(ND->getNameAsString()); |
| 2394 | TypeAsString += " " + Name + " = "; |
| 2395 | Expr *E = ND->getInit(); |
| 2396 | SourceLocation startLoc; |
| 2397 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2398 | startLoc = ECE->getLParenLoc(); |
| 2399 | else |
| 2400 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2401 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2402 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2403 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2404 | } |
| 2405 | else { |
| 2406 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2407 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2408 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2409 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2413 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2414 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2415 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2416 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2417 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2418 | QualType getFuncType = |
| 2419 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2420 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2422 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2423 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2424 | SC_Extern, |
| 2425 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2428 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2429 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2430 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2431 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2432 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2435 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2438 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2439 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2440 | const char *argPtr = TypeString.c_str(); |
| 2441 | if (!strchr(argPtr, '^')) { |
| 2442 | Str += TypeString; |
| 2443 | return; |
| 2444 | } |
| 2445 | while (*argPtr) { |
| 2446 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2447 | argPtr++; |
| 2448 | } |
| 2449 | } |
| 2450 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2451 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2452 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2453 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2454 | QualType Type = VD->getType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2455 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2456 | const char *argPtr = TypeString.c_str(); |
| 2457 | int paren = 0; |
| 2458 | while (*argPtr) { |
| 2459 | switch (*argPtr) { |
| 2460 | case '(': |
| 2461 | Str += *argPtr; |
| 2462 | paren++; |
| 2463 | break; |
| 2464 | case ')': |
| 2465 | Str += *argPtr; |
| 2466 | paren--; |
| 2467 | break; |
| 2468 | case '^': |
| 2469 | Str += '*'; |
| 2470 | if (paren == 1) |
| 2471 | Str += VD->getNameAsString(); |
| 2472 | break; |
| 2473 | default: |
| 2474 | Str += *argPtr; |
| 2475 | break; |
| 2476 | } |
| 2477 | argPtr++; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2482 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2483 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2484 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2485 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2486 | if (!proto) |
| 2487 | return; |
| 2488 | QualType Type = proto->getResultType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2489 | std::string FdStr = Type.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2490 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2491 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2492 | FdStr += "("; |
| 2493 | unsigned numArgs = proto->getNumArgs(); |
| 2494 | for (unsigned i = 0; i < numArgs; i++) { |
| 2495 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2496 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2497 | if (i+1 < numArgs) |
| 2498 | FdStr += ", "; |
| 2499 | } |
| 2500 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2501 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2502 | CurFunctionDeclToDeclareForBlock = 0; |
| 2503 | } |
| 2504 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2505 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2506 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2507 | if (SuperContructorFunctionDecl) |
| 2508 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2509 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2510 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2511 | QualType argT = Context->getObjCIdType(); |
| 2512 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2513 | ArgTys.push_back(argT); |
| 2514 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2515 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2516 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2517 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2519 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2520 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2521 | SC_Extern, |
| 2522 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2525 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2526 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2527 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2528 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2529 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2530 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2531 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2532 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2533 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2534 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2535 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2536 | &ArgTys[0], ArgTys.size(), |
| 2537 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2538 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2539 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2540 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2541 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2542 | SC_Extern, |
| 2543 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2546 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2547 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2548 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2549 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2550 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2551 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2552 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2553 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2554 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2555 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2556 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2557 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2558 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2559 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2560 | &ArgTys[0], ArgTys.size(), |
| 2561 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2562 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2564 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2565 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2566 | SC_Extern, |
| 2567 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2570 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2571 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2572 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2573 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2574 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2575 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2576 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2577 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2579 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2580 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2581 | &ArgTys[0], ArgTys.size(), |
| 2582 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2583 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2585 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2586 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2587 | SC_Extern, |
| 2588 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2592 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2593 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2595 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2596 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2597 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2598 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2599 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2600 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2601 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2602 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2603 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2604 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2605 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2606 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2607 | &ArgTys[0], ArgTys.size(), |
| 2608 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2609 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2611 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2612 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2613 | SC_Extern, |
| 2614 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2617 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2618 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2619 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2620 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2621 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2622 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2623 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2624 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2625 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2626 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2627 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2628 | &ArgTys[0], ArgTys.size(), |
| 2629 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2630 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2632 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2633 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2634 | SC_Extern, |
| 2635 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2638 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2639 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2640 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2641 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2642 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2643 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2644 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2645 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2647 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2648 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2649 | SC_Extern, |
| 2650 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2653 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2654 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2655 | IdentifierInfo *getSuperClassIdent = |
| 2656 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2657 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2658 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2659 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2660 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2661 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2662 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2663 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2664 | getSuperClassIdent, |
| 2665 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2666 | SC_Extern, |
| 2667 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2668 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2671 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2672 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2673 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2674 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2675 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2676 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2677 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2678 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2680 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2681 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2682 | SC_Extern, |
| 2683 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2686 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2687 | QualType strType = getConstantStringStructType(); |
| 2688 | |
| 2689 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2690 | |
| 2691 | std::string tmpName = InFileName; |
| 2692 | unsigned i; |
| 2693 | for (i=0; i < tmpName.length(); i++) { |
| 2694 | char c = tmpName.at(i); |
| 2695 | // replace any non alphanumeric characters with '_'. |
| 2696 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2697 | tmpName[i] = '_'; |
| 2698 | } |
| 2699 | S += tmpName; |
| 2700 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2701 | S += utostr(NumObjCStringLiterals++); |
| 2702 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2703 | Preamble += "static __NSConstantStringImpl " + S; |
| 2704 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2705 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2706 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2707 | std::string prettyBufS; |
| 2708 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2709 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2710 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2711 | Preamble += prettyBuf.str(); |
| 2712 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2713 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
| 2715 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2716 | SourceLocation(), &Context->Idents.get(S), |
| 2717 | strType, 0, SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2718 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, VK_LValue, |
| 2719 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2720 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2722 | VK_RValue, OK_Ordinary, |
| 2723 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2724 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2725 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2726 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2727 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2728 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2729 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2732 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2733 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2734 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2735 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2736 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2737 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2738 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2740 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2742 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2744 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2745 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2746 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2748 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2750 | FieldTypes[i], 0, |
| 2751 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2752 | /*Mutable=*/false, |
| 2753 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2756 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2757 | } |
| 2758 | return Context->getTagDeclType(SuperStructDecl); |
| 2759 | } |
| 2760 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2761 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2762 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2763 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2764 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2765 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2766 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2768 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2770 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2772 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2774 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2776 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2777 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2778 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2780 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2781 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2782 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2783 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2784 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2785 | /*Mutable=*/true, |
| 2786 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2789 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2790 | } |
| 2791 | return Context->getTagDeclType(ConstantStringDecl); |
| 2792 | } |
| 2793 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2794 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2795 | SourceLocation StartLoc, |
| 2796 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2797 | if (!SelGetUidFunctionDecl) |
| 2798 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2799 | if (!MsgSendFunctionDecl) |
| 2800 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2801 | if (!MsgSendSuperFunctionDecl) |
| 2802 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2803 | if (!MsgSendStretFunctionDecl) |
| 2804 | SynthMsgSendStretFunctionDecl(); |
| 2805 | if (!MsgSendSuperStretFunctionDecl) |
| 2806 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2807 | if (!MsgSendFpretFunctionDecl) |
| 2808 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2809 | if (!GetClassFunctionDecl) |
| 2810 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2811 | if (!GetSuperClassFunctionDecl) |
| 2812 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2813 | if (!GetMetaClassFunctionDecl) |
| 2814 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2816 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2817 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2818 | // May need to use objc_msgSend_stret() as well. |
| 2819 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2820 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2821 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2822 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2823 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2824 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2825 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2828 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2829 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2830 | switch (Exp->getReceiverKind()) { |
| 2831 | case ObjCMessageExpr::SuperClass: { |
| 2832 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2833 | if (MsgSendStretFlavor) |
| 2834 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2835 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
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 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2839 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2841 | // set the receiver to self, the first argument to all methods. |
| 2842 | InitExprs.push_back( |
| 2843 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2844 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2845 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2846 | Context->getObjCIdType(), |
| 2847 | VK_RValue, |
| 2848 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2849 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2851 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2852 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2853 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2854 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2855 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2856 | StringLiteral::Ascii, false, |
| 2857 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2858 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2859 | &ClsExprs[0], |
| 2860 | ClsExprs.size(), |
| 2861 | StartLoc, |
| 2862 | EndLoc); |
| 2863 | // (Class)objc_getClass("CurrentClass") |
| 2864 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2865 | Context->getObjCClassType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2866 | CK_CPointerToObjCPointerCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2867 | ClsExprs.clear(); |
| 2868 | ClsExprs.push_back(ArgExpr); |
| 2869 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2870 | &ClsExprs[0], ClsExprs.size(), |
| 2871 | StartLoc, EndLoc); |
| 2872 | |
| 2873 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2874 | // To turn off a warning, type-cast to 'id' |
| 2875 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2876 | NoTypeInfoCStyleCastExpr(Context, |
| 2877 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2878 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | // struct objc_super |
| 2880 | QualType superType = getSuperStructType(); |
| 2881 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2882 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2883 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2884 | SynthSuperContructorFunctionDecl(); |
| 2885 | // Simulate a contructor call... |
| 2886 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2887 | superType, VK_LValue, |
| 2888 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2889 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2890 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2891 | superType, VK_LValue, |
| 2892 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2893 | // The code for super is a little tricky to prevent collision with |
| 2894 | // the structure definition in the header. The rewriter has it's own |
| 2895 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2896 | // we need the cast below. For example: |
| 2897 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2898 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2899 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2900 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2901 | VK_RValue, OK_Ordinary, |
| 2902 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2903 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2904 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2905 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2906 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2907 | // (struct objc_super) { <exprs from above> } |
| 2908 | InitListExpr *ILE = |
| 2909 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2910 | &InitExprs[0], InitExprs.size(), |
| 2911 | SourceLocation()); |
| 2912 | TypeSourceInfo *superTInfo |
| 2913 | = Context->getTrivialTypeSourceInfo(superType); |
| 2914 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2915 | superType, VK_LValue, |
| 2916 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2917 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2918 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2919 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2920 | VK_RValue, OK_Ordinary, |
| 2921 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2922 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2923 | MsgExprs.push_back(SuperRep); |
| 2924 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2925 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2926 | |
| 2927 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2928 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2929 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2930 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2931 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2932 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2933 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2934 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2935 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2936 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2937 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2938 | &ClsExprs[0], |
| 2939 | ClsExprs.size(), |
| 2940 | StartLoc, EndLoc); |
| 2941 | MsgExprs.push_back(Cls); |
| 2942 | break; |
| 2943 | } |
| 2944 | |
| 2945 | case ObjCMessageExpr::SuperInstance:{ |
| 2946 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2947 | if (MsgSendStretFlavor) |
| 2948 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2949 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2950 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2951 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2952 | |
| 2953 | InitExprs.push_back( |
| 2954 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2955 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2956 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2957 | Context->getObjCIdType(), |
| 2958 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2959 | ); // set the 'receiver'. |
| 2960 | |
| 2961 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2962 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2963 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2964 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2965 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2966 | StringLiteral::Ascii, false, argType, |
| 2967 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2968 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2969 | &ClsExprs[0], |
| 2970 | ClsExprs.size(), |
| 2971 | StartLoc, EndLoc); |
| 2972 | // (Class)objc_getClass("CurrentClass") |
| 2973 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2974 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2975 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2976 | ClsExprs.clear(); |
| 2977 | ClsExprs.push_back(ArgExpr); |
| 2978 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2979 | &ClsExprs[0], ClsExprs.size(), |
| 2980 | StartLoc, EndLoc); |
| 2981 | |
| 2982 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2983 | // To turn off a warning, type-cast to 'id' |
| 2984 | InitExprs.push_back( |
| 2985 | // set 'super class', using class_getSuperclass(). |
| 2986 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2987 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2988 | // struct objc_super |
| 2989 | QualType superType = getSuperStructType(); |
| 2990 | Expr *SuperRep; |
| 2991 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2992 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2993 | SynthSuperContructorFunctionDecl(); |
| 2994 | // Simulate a contructor call... |
| 2995 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2996 | superType, VK_LValue, |
| 2997 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2998 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2999 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3000 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3001 | // The code for super is a little tricky to prevent collision with |
| 3002 | // the structure definition in the header. The rewriter has it's own |
| 3003 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3004 | // we need the cast below. For example: |
| 3005 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 3006 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3007 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3008 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3009 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3010 | SourceLocation()); |
| 3011 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3012 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3013 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3014 | } else { |
| 3015 | // (struct objc_super) { <exprs from above> } |
| 3016 | InitListExpr *ILE = |
| 3017 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3018 | &InitExprs[0], InitExprs.size(), |
| 3019 | SourceLocation()); |
| 3020 | TypeSourceInfo *superTInfo |
| 3021 | = Context->getTrivialTypeSourceInfo(superType); |
| 3022 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3023 | superType, VK_RValue, ILE, |
| 3024 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3025 | } |
| 3026 | MsgExprs.push_back(SuperRep); |
| 3027 | break; |
| 3028 | } |
| 3029 | |
| 3030 | case ObjCMessageExpr::Instance: { |
| 3031 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3032 | // Foo<Proto> *. |
| 3033 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3034 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3035 | recExpr = CE->getSubExpr(); |
| 3036 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3037 | CK_BitCast, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3038 | MsgExprs.push_back(recExpr); |
| 3039 | break; |
| 3040 | } |
| 3041 | } |
| 3042 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 3043 | // 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] | 3044 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3045 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 3047 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3048 | StringLiteral::Ascii, false, |
| 3049 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3050 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3051 | &SelExprs[0], SelExprs.size(), |
| 3052 | StartLoc, |
| 3053 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3054 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3056 | // Now push any user supplied arguments. |
| 3057 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3058 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 3059 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3060 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3061 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 3062 | QualType type = ICE->getType(); |
| 3063 | if (needToScanForQualifiers(type)) |
| 3064 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3065 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3066 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 3067 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3068 | CastKind CK; |
| 3069 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 3070 | type->isBooleanType()) { |
| 3071 | CK = CK_IntegralToBoolean; |
| 3072 | } else if (type->isObjCObjectPointerType()) { |
| 3073 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3074 | CK = CK_BlockPointerToObjCPointerCast; |
| 3075 | } else if (SubExpr->getType()->isPointerType()) { |
| 3076 | CK = CK_CPointerToObjCPointerCast; |
| 3077 | } else { |
| 3078 | CK = CK_BitCast; |
| 3079 | } |
| 3080 | } else { |
| 3081 | CK = CK_BitCast; |
| 3082 | } |
| 3083 | |
| 3084 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3085 | } |
| 3086 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3087 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3088 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3089 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3090 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3091 | CastKind CK; |
| 3092 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3093 | CK = CK_IntegralToPointer; |
| 3094 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3095 | CK = CK_BlockPointerToObjCPointerCast; |
| 3096 | } else if (userExpr->getType()->isPointerType()) { |
| 3097 | CK = CK_CPointerToObjCPointerCast; |
| 3098 | } else { |
| 3099 | CK = CK_BitCast; |
| 3100 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3101 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3102 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3105 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3106 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3107 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3108 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3109 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3110 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3111 | // Generate the funky cast. |
| 3112 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3113 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3114 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3116 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 3117 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3118 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3119 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3120 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3121 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3122 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3123 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3124 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3125 | E = OMD->param_end(); PI != E; ++PI) { |
| 3126 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3128 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3129 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3130 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3131 | ArgTypes.push_back(t); |
| 3132 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 3133 | returnType = Exp->getType(); |
| 3134 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3135 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3136 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3137 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3138 | } |
| 3139 | // 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] | 3140 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3142 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3144 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3145 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3147 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3148 | // xx.m:13: warning: function called through a non-compatible type |
| 3149 | // 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] | 3150 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3151 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3152 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3154 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3155 | QualType castType = |
| 3156 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3157 | // If we don't have a method decl, force a variadic cast. |
| 3158 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3159 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3160 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3161 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3164 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3166 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3167 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3169 | FT->getResultType(), VK_RValue, |
| 3170 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3171 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3172 | if (MsgSendStretFlavor) { |
| 3173 | // We have the method which returns a struct/union. Must also generate |
| 3174 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3175 | // expression which dictate which one to envoke depending on size of |
| 3176 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3178 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3180 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3181 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3182 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3183 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3184 | CK_BitCast, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3185 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3186 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3187 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3188 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3189 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3190 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3192 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3193 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3195 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3196 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3198 | FT->getResultType(), VK_RValue, |
| 3199 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3201 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3202 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3203 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3204 | Context->getTrivialTypeSourceInfo(returnType), |
| 3205 | Context->getSizeType(), SourceLocation(), |
| 3206 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3207 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3208 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3209 | // For X86 it is more complicated and some kind of target specific routine |
| 3210 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3212 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3213 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3214 | llvm::APInt(IntSize, 8), |
| 3215 | Context->IntTy, |
| 3216 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3217 | BinaryOperator *lessThanExpr = |
| 3218 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3219 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3220 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3222 | new (Context) ConditionalOperator(lessThanExpr, |
| 3223 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3224 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3225 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3226 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3227 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3228 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3229 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3230 | return ReplacingStmt; |
| 3231 | } |
| 3232 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3233 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3234 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3235 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3237 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3238 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3240 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3241 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3244 | // typedef struct objc_object Protocol; |
| 3245 | QualType RewriteObjC::getProtocolType() { |
| 3246 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3247 | TypeSourceInfo *TInfo |
| 3248 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3249 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3250 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3251 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3252 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3253 | } |
| 3254 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3255 | } |
| 3256 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3257 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3258 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3259 | /// The forward references (and metadata) are generated in |
| 3260 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3261 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3262 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3263 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3265 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3266 | SC_Extern, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3267 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), VK_LValue, |
| 3268 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3269 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3270 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3271 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3272 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3273 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3274 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3275 | ReplaceStmt(Exp, castExpr); |
| 3276 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3277 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3278 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3283 | const char *endBuf) { |
| 3284 | while (startBuf < endBuf) { |
| 3285 | if (*startBuf == '#') { |
| 3286 | // Skip whitespace. |
| 3287 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3288 | ; |
| 3289 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3290 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3291 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3292 | !strncmp(startBuf, "define", strlen("define")) || |
| 3293 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3294 | !strncmp(startBuf, "else", strlen("else")) || |
| 3295 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3296 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3297 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3298 | !strncmp(startBuf, "include", strlen("include")) || |
| 3299 | !strncmp(startBuf, "import", strlen("import")) || |
| 3300 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3301 | return true; |
| 3302 | } |
| 3303 | startBuf++; |
| 3304 | } |
| 3305 | return false; |
| 3306 | } |
| 3307 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3308 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3309 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3310 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3311 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3312 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3313 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3314 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3315 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3316 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3317 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3318 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3319 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3320 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3321 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3323 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3324 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3326 | // If no ivars and no root or if its root, directly or indirectly, |
| 3327 | // 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] | 3328 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3329 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3330 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3331 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3332 | return; |
| 3333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | |
| 3335 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3336 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3337 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3338 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3339 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3340 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3341 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3342 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3343 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3345 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3346 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3347 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3348 | // NSURL.h, for example): |
| 3349 | // |
| 3350 | // #ifdef XYZ |
| 3351 | // @interface Foo : NSObject |
| 3352 | // #else |
| 3353 | // @interface FooBar : NSObject |
| 3354 | // #endif |
| 3355 | // { |
| 3356 | // int i; |
| 3357 | // } |
| 3358 | // @end |
| 3359 | // |
| 3360 | // This clause is segregated to avoid breaking the common case. |
| 3361 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3363 | CDecl->getClassLoc(); |
| 3364 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3365 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3367 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3368 | // advance to the end of the referenced protocols. |
| 3369 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3370 | endHeader++; |
| 3371 | } |
| 3372 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3373 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3374 | } else { |
| 3375 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3376 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3377 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3378 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3379 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3380 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3381 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3382 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3383 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3385 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3386 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3387 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3388 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3389 | } |
| 3390 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3392 | // Now comment out any visibility specifiers. |
| 3393 | while (cursor < endBuf) { |
| 3394 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3395 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3396 | // Skip whitespace. |
| 3397 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3398 | /*scan*/; |
| 3399 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3400 | // FIXME: presence of @public, etc. inside comment results in |
| 3401 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3402 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3403 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3404 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3405 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3406 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3407 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3408 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3409 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3410 | // for type checking. |
| 3411 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3412 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3413 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3414 | cursor = strchr(cursor, '>'); |
| 3415 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3416 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3417 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3418 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3419 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3420 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3421 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3422 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3423 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3424 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3425 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3426 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3427 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3428 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3429 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3430 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3431 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3432 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3433 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3434 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3435 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3436 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 3437 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3440 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3441 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3442 | template<typename MethodIterator> |
| 3443 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3444 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3445 | bool IsInstanceMethod, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3446 | StringRef prefix, |
| 3447 | StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3448 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3449 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3451 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3452 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3453 | SEL _cmd; |
| 3454 | char *method_types; |
| 3455 | void *_imp; |
| 3456 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3457 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3458 | Result += "\nstruct _objc_method {\n"; |
| 3459 | Result += "\tSEL _cmd;\n"; |
| 3460 | Result += "\tchar *method_types;\n"; |
| 3461 | Result += "\tvoid *_imp;\n"; |
| 3462 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3464 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3467 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3469 | /* struct { |
| 3470 | struct _objc_method_list *next_method; |
| 3471 | int method_count; |
| 3472 | struct _objc_method method_list[]; |
| 3473 | } |
| 3474 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3475 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3476 | Result += "\nstatic struct {\n"; |
| 3477 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3478 | Result += "\tint method_count;\n"; |
| 3479 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3480 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3481 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3482 | Result += prefix; |
| 3483 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3484 | Result += "_METHODS_"; |
| 3485 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3486 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3487 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3488 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3489 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3490 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3491 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3492 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3493 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3494 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3495 | Result += "\", \""; |
| 3496 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3497 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3498 | Result += MethodInternalNames[*MethodBegin]; |
| 3499 | Result += "}\n"; |
| 3500 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3501 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3502 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3503 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3504 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3505 | Result += "\", \""; |
| 3506 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3507 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3508 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3509 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3510 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3511 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3514 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3515 | void RewriteObjC:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3516 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, StringRef prefix, |
| 3517 | StringRef ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3518 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3519 | |
| 3520 | // Output struct protocol_methods holder of method selector and type. |
| 3521 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3522 | /* struct protocol_methods { |
| 3523 | SEL _cmd; |
| 3524 | char *method_types; |
| 3525 | } |
| 3526 | */ |
| 3527 | Result += "\nstruct _protocol_methods {\n"; |
| 3528 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3529 | Result += "\tchar *method_types;\n"; |
| 3530 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3531 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3532 | objc_protocol_methods = true; |
| 3533 | } |
| 3534 | // Do not synthesize the protocol more than once. |
| 3535 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3536 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3538 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3539 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3540 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3541 | /* struct _objc_protocol_method_list { |
| 3542 | int protocol_method_count; |
| 3543 | struct protocol_methods protocols[]; |
| 3544 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3545 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 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_INSTANCE_METHODS_"; |
| 3551 | Result += PDecl->getNameAsString(); |
| 3552 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3553 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3555 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | for (ObjCProtocolDecl::instmeth_iterator |
| 3557 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3558 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3559 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3560 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3561 | else |
| 3562 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3563 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3564 | std::string MethodTypeString; |
| 3565 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3566 | Result += "\", \""; |
| 3567 | Result += MethodTypeString; |
| 3568 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3569 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3570 | Result += "\t }\n};\n"; |
| 3571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3573 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3574 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3575 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3576 | if (NumMethods > 0) { |
| 3577 | /* struct _objc_protocol_method_list { |
| 3578 | int protocol_method_count; |
| 3579 | struct protocol_methods protocols[]; |
| 3580 | } |
| 3581 | */ |
| 3582 | Result += "\nstatic struct {\n"; |
| 3583 | Result += "\tint protocol_method_count;\n"; |
| 3584 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3585 | Result += utostr(NumMethods); |
| 3586 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3587 | Result += PDecl->getNameAsString(); |
| 3588 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3589 | "{\n\t"; |
| 3590 | Result += utostr(NumMethods); |
| 3591 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3593 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3595 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3596 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3597 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3598 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3599 | else |
| 3600 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3601 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3602 | std::string MethodTypeString; |
| 3603 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3604 | Result += "\", \""; |
| 3605 | Result += MethodTypeString; |
| 3606 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3607 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3608 | Result += "\t }\n};\n"; |
| 3609 | } |
| 3610 | |
| 3611 | // Output: |
| 3612 | /* struct _objc_protocol { |
| 3613 | // Objective-C 1.0 extensions |
| 3614 | struct _objc_protocol_extension *isa; |
| 3615 | char *protocol_name; |
| 3616 | struct _objc_protocol **protocol_list; |
| 3617 | struct _objc_protocol_method_list *instance_methods; |
| 3618 | struct _objc_protocol_method_list *class_methods; |
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 | */ |
| 3621 | static bool objc_protocol = false; |
| 3622 | if (!objc_protocol) { |
| 3623 | Result += "\nstruct _objc_protocol {\n"; |
| 3624 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3625 | Result += "\tchar *protocol_name;\n"; |
| 3626 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3627 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3628 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3629 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3630 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3631 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3632 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3634 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3635 | Result += PDecl->getNameAsString(); |
| 3636 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3637 | "{\n\t0, \""; |
| 3638 | Result += PDecl->getNameAsString(); |
| 3639 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3640 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3641 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3642 | Result += PDecl->getNameAsString(); |
| 3643 | Result += ", "; |
| 3644 | } |
| 3645 | else |
| 3646 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3647 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3648 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3649 | Result += PDecl->getNameAsString(); |
| 3650 | Result += "\n"; |
| 3651 | } |
| 3652 | else |
| 3653 | Result += "0\n"; |
| 3654 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3656 | // Mark this protocol as having been generated. |
| 3657 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 3658 | llvm_unreachable("protocol already synthesized"); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3659 | |
| 3660 | } |
| 3661 | |
| 3662 | void RewriteObjC:: |
| 3663 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3664 | StringRef prefix, StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3665 | std::string &Result) { |
| 3666 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3668 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3669 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3670 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3671 | // Output the top lovel protocol meta-data for the class. |
| 3672 | /* struct _objc_protocol_list { |
| 3673 | struct _objc_protocol_list *next; |
| 3674 | int protocol_count; |
| 3675 | struct _objc_protocol *class_protocols[]; |
| 3676 | } |
| 3677 | */ |
| 3678 | Result += "\nstatic struct {\n"; |
| 3679 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3680 | Result += "\tint protocol_count;\n"; |
| 3681 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3682 | Result += utostr(Protocols.size()); |
| 3683 | Result += "];\n} _OBJC_"; |
| 3684 | Result += prefix; |
| 3685 | Result += "_PROTOCOLS_"; |
| 3686 | Result += ClassName; |
| 3687 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3688 | "{\n\t0, "; |
| 3689 | Result += utostr(Protocols.size()); |
| 3690 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3692 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3693 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3694 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3695 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3696 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3697 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3698 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3699 | Result += "\n"; |
| 3700 | } |
| 3701 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3704 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3706 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3707 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3708 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3709 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3710 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3711 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3712 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3713 | CDecl = CDecl->getNextClassCategory()) |
| 3714 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3715 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3717 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3718 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3719 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3721 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3722 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3723 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3724 | |
| 3725 | // If any of our property implementations have associated getters or |
| 3726 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3727 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3728 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3729 | Prop != PropEnd; ++Prop) { |
| 3730 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3731 | continue; |
| 3732 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3733 | continue; |
| 3734 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3735 | if (!PD) |
| 3736 | continue; |
| 3737 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3738 | InstanceMethods.push_back(Getter); |
| 3739 | if (PD->isReadOnly()) |
| 3740 | continue; |
| 3741 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3742 | InstanceMethods.push_back(Setter); |
| 3743 | } |
| 3744 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3745 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3746 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3748 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3749 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3750 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3751 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3753 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3754 | // Null CDecl is case of a category implementation with no category interface |
| 3755 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3756 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3757 | FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3758 | /* struct _objc_category { |
| 3759 | char *category_name; |
| 3760 | char *class_name; |
| 3761 | struct _objc_method_list *instance_methods; |
| 3762 | struct _objc_method_list *class_methods; |
| 3763 | struct _objc_protocol_list *protocols; |
| 3764 | // Objective-C 1.0 extensions |
| 3765 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3767 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3769 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3771 | static bool objc_category = false; |
| 3772 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3773 | Result += "\nstruct _objc_category {\n"; |
| 3774 | Result += "\tchar *category_name;\n"; |
| 3775 | Result += "\tchar *class_name;\n"; |
| 3776 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3777 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3778 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3780 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3781 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3782 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3783 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3784 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3785 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3786 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3787 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3788 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3789 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3790 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3792 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3793 | Result += "\t, (struct _objc_method_list *)" |
| 3794 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3795 | Result += FullCategoryName; |
| 3796 | Result += "\n"; |
| 3797 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3798 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3799 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3800 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3801 | Result += "\t, (struct _objc_method_list *)" |
| 3802 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3803 | Result += FullCategoryName; |
| 3804 | Result += "\n"; |
| 3805 | } |
| 3806 | else |
| 3807 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3809 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3810 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3811 | Result += FullCategoryName; |
| 3812 | Result += "\n"; |
| 3813 | } |
| 3814 | else |
| 3815 | Result += "\t, 0\n"; |
| 3816 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3819 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3820 | /// ivar offset. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3821 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3822 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3823 | if (ivar->isBitField()) { |
| 3824 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3825 | // place all bitfields at offset 0. |
| 3826 | Result += "0"; |
| 3827 | } else { |
| 3828 | Result += "__OFFSETOFIVAR__(struct "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3829 | Result += ivar->getContainingInterface()->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3830 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3831 | Result += "_IMPL"; |
| 3832 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3833 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3834 | Result += ")"; |
| 3835 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3838 | //===----------------------------------------------------------------------===// |
| 3839 | // Meta Data Emission |
| 3840 | //===----------------------------------------------------------------------===// |
| 3841 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3842 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3843 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3844 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3846 | // Explicitly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3847 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3849 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3850 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3853 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3854 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3856 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3857 | if (NumIvars > 0) { |
| 3858 | static bool objc_ivar = false; |
| 3859 | if (!objc_ivar) { |
| 3860 | /* struct _objc_ivar { |
| 3861 | char *ivar_name; |
| 3862 | char *ivar_type; |
| 3863 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3865 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3866 | Result += "\nstruct _objc_ivar {\n"; |
| 3867 | Result += "\tchar *ivar_name;\n"; |
| 3868 | Result += "\tchar *ivar_type;\n"; |
| 3869 | Result += "\tint ivar_offset;\n"; |
| 3870 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3872 | objc_ivar = true; |
| 3873 | } |
| 3874 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3875 | /* struct { |
| 3876 | int ivar_count; |
| 3877 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3879 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3881 | Result += "\tint ivar_count;\n"; |
| 3882 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3883 | Result += utostr(NumIvars); |
| 3884 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3885 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3886 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3887 | "{\n\t"; |
| 3888 | Result += utostr(NumIvars); |
| 3889 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3891 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3892 | SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3893 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3894 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3895 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3896 | IV != IVEnd; ++IV) |
| 3897 | IVars.push_back(*IV); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3898 | IVI = IDecl->ivar_begin(); |
| 3899 | IVE = IDecl->ivar_end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3900 | } else { |
| 3901 | IVI = CDecl->ivar_begin(); |
| 3902 | IVE = CDecl->ivar_end(); |
| 3903 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3904 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3905 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3906 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3907 | std::string TmpString, StrEncoding; |
| 3908 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3909 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3910 | Result += StrEncoding; |
| 3911 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3912 | SynthesizeIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3913 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3914 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3915 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3916 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3917 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3918 | std::string TmpString, StrEncoding; |
| 3919 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3920 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3921 | Result += StrEncoding; |
| 3922 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3923 | SynthesizeIvarOffsetComputation((*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3924 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3927 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3930 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3931 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3932 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3933 | |
| 3934 | // If any of our property implementations have associated getters or |
| 3935 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3936 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3937 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3938 | Prop != PropEnd; ++Prop) { |
| 3939 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3940 | continue; |
| 3941 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3942 | continue; |
| 3943 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3944 | if (!PD) |
| 3945 | continue; |
| 3946 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3947 | if (!Getter->isDefined()) |
| 3948 | InstanceMethods.push_back(Getter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3949 | if (PD->isReadOnly()) |
| 3950 | continue; |
| 3951 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3952 | if (!Setter->isDefined()) |
| 3953 | InstanceMethods.push_back(Setter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3954 | } |
| 3955 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3956 | true, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3958 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3959 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3960 | false, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3962 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3963 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3964 | "CLASS", CDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3965 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3966 | // Declaration of class/meta-class metadata |
| 3967 | /* struct _objc_class { |
| 3968 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3969 | const char *super_class_name; |
| 3970 | char *name; |
| 3971 | long version; |
| 3972 | long info; |
| 3973 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3974 | struct _objc_ivar_list *ivars; |
| 3975 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3976 | struct objc_cache *cache; |
| 3977 | struct objc_protocol_list *protocols; |
| 3978 | const char *ivar_layout; |
| 3979 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3981 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3982 | static bool objc_class = false; |
| 3983 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3984 | Result += "\nstruct _objc_class {\n"; |
| 3985 | Result += "\tstruct _objc_class *isa;\n"; |
| 3986 | Result += "\tconst char *super_class_name;\n"; |
| 3987 | Result += "\tchar *name;\n"; |
| 3988 | Result += "\tlong version;\n"; |
| 3989 | Result += "\tlong info;\n"; |
| 3990 | Result += "\tlong instance_size;\n"; |
| 3991 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3992 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3993 | Result += "\tstruct objc_cache *cache;\n"; |
| 3994 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3995 | Result += "\tconst char *ivar_layout;\n"; |
| 3996 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3997 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3998 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3999 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4001 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4002 | ObjCInterfaceDecl *RootClass = 0; |
| 4003 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4004 | while (SuperClass) { |
| 4005 | RootClass = SuperClass; |
| 4006 | SuperClass = SuperClass->getSuperClass(); |
| 4007 | } |
| 4008 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4009 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4010 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4011 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4012 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4013 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4014 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4015 | Result += "\""; |
| 4016 | |
| 4017 | if (SuperClass) { |
| 4018 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4019 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4020 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4021 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4022 | Result += "\""; |
| 4023 | } |
| 4024 | else { |
| 4025 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4026 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4027 | Result += "\""; |
| 4028 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4029 | // 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] | 4030 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4031 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4032 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 4033 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4034 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4036 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4037 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4038 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4039 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4040 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4041 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4042 | Result += ",0,0\n"; |
| 4043 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 4044 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4045 | Result += "\t,0,0,0,0\n"; |
| 4046 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4048 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4049 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4050 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4051 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4052 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4053 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4054 | if (SuperClass) { |
| 4055 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4056 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4057 | Result += "\", \""; |
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 += "\""; |
| 4060 | } |
| 4061 | else { |
| 4062 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4063 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4064 | Result += "\""; |
| 4065 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4066 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4067 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4068 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4069 | Result += ",0"; |
| 4070 | else { |
| 4071 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 4072 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4073 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4074 | if (LangOpts.MicrosoftExt) |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 4075 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4076 | Result += ")"; |
| 4077 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4078 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 4079 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4080 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4081 | Result += "\n\t"; |
| 4082 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4083 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4084 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4085 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 4086 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4087 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4089 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4090 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4091 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4092 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4093 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4094 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4095 | Result += ", 0,0\n"; |
| 4096 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4097 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4098 | Result += ",0,0,0\n"; |
| 4099 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4100 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4101 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4102 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4103 | /// and emits meta-data. |
| 4104 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4105 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4106 | int ClsDefCount = ClassImplementation.size(); |
| 4107 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4109 | // Rewrite implemented methods |
| 4110 | for (int i = 0; i < ClsDefCount; i++) |
| 4111 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4112 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 4113 | for (int i = 0; i < CatDefCount; i++) |
| 4114 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4116 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4117 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 4118 | int ClsDefCount = ClassImplementation.size(); |
| 4119 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 4120 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4121 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4122 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4123 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4125 | // For each implemented category, write out all its meta data. |
| 4126 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4127 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4128 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4129 | // Write objc_symtab metadata |
| 4130 | /* |
| 4131 | struct _objc_symtab |
| 4132 | { |
| 4133 | long sel_ref_cnt; |
| 4134 | SEL *refs; |
| 4135 | short cls_def_cnt; |
| 4136 | short cat_def_cnt; |
| 4137 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4139 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4141 | Result += "\nstruct _objc_symtab {\n"; |
| 4142 | Result += "\tlong sel_ref_cnt;\n"; |
| 4143 | Result += "\tSEL *refs;\n"; |
| 4144 | Result += "\tshort cls_def_cnt;\n"; |
| 4145 | Result += "\tshort cat_def_cnt;\n"; |
| 4146 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4147 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4149 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4150 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4152 | + ", " + utostr(CatDefCount) + "\n"; |
| 4153 | for (int i = 0; i < ClsDefCount; i++) { |
| 4154 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4155 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4156 | Result += "\n"; |
| 4157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4159 | for (int i = 0; i < CatDefCount; i++) { |
| 4160 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4161 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4162 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4163 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4164 | Result += "\n"; |
| 4165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4166 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4167 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4169 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4171 | /* |
| 4172 | struct _objc_module { |
| 4173 | long version; |
| 4174 | long size; |
| 4175 | const char *name; |
| 4176 | struct _objc_symtab *symtab; |
| 4177 | } |
| 4178 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4180 | Result += "\nstruct _objc_module {\n"; |
| 4181 | Result += "\tlong version;\n"; |
| 4182 | Result += "\tlong size;\n"; |
| 4183 | Result += "\tconst char *name;\n"; |
| 4184 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4185 | Result += "};\n\n"; |
| 4186 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4187 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4189 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4190 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4191 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4192 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4193 | if (ProtocolExprDecls.size()) { |
| 4194 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4195 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4197 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4198 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4199 | Result += (*I)->getNameAsString(); |
| 4200 | Result += " = &_OBJC_PROTOCOL_"; |
| 4201 | Result += (*I)->getNameAsString(); |
| 4202 | Result += ";\n"; |
| 4203 | } |
| 4204 | Result += "#pragma data_seg(pop)\n\n"; |
| 4205 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4206 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4207 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4208 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4209 | Result += "&_OBJC_MODULES;\n"; |
| 4210 | Result += "#pragma data_seg(pop)\n\n"; |
| 4211 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4212 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4213 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4214 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4215 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4216 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4217 | assert(BlockByRefDeclNo.count(VD) && |
| 4218 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4219 | if (def) |
| 4220 | ResultStr += "struct "; |
| 4221 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4222 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4223 | } |
| 4224 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4225 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4226 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4227 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4228 | return false; |
| 4229 | } |
| 4230 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4231 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4232 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4233 | std::string Tag) { |
| 4234 | const FunctionType *AFT = CE->getFunctionType(); |
| 4235 | QualType RT = AFT->getResultType(); |
| 4236 | std::string StructRef = "struct " + Tag; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 4237 | std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4238 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4239 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4240 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4242 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4243 | // 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] | 4244 | // block (to reference imported block decl refs). |
| 4245 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4246 | } else if (BD->param_empty()) { |
| 4247 | S += "(" + StructRef + " *__cself)"; |
| 4248 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4249 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4250 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4251 | S += '('; |
| 4252 | // first add the implicit argument. |
| 4253 | S += StructRef + " *__cself, "; |
| 4254 | std::string ParamStr; |
| 4255 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4256 | E = BD->param_end(); AI != E; ++AI) { |
| 4257 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4258 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4259 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4260 | if (convertBlockPointerToFunctionPointer(QT)) |
| 4261 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4262 | else |
| 4263 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4264 | S += ParamStr; |
| 4265 | } |
| 4266 | if (FT->isVariadic()) { |
| 4267 | if (!BD->param_empty()) S += ", "; |
| 4268 | S += "..."; |
| 4269 | } |
| 4270 | S += ')'; |
| 4271 | } |
| 4272 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4274 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4275 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4276 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4277 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4278 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4279 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4280 | std::string TypeString; |
| 4281 | RewriteByRefString(TypeString, Name, (*I)); |
| 4282 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4283 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4284 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4285 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4286 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4287 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4288 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4289 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4290 | // Handle nested closure invocation. For example: |
| 4291 | // |
| 4292 | // void (^myImportedClosure)(void); |
| 4293 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
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 | // void (^anotherClosure)(void); |
| 4296 | // anotherClosure = ^(void) { |
| 4297 | // myImportedClosure(); // import and invoke the closure |
| 4298 | // }; |
| 4299 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4300 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4301 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4302 | S += " = ("; |
| 4303 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4304 | S += ")"; |
| 4305 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4306 | } |
| 4307 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4308 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4309 | QualType QT = (*I)->getType(); |
| 4310 | if (HasLocalVariableExternalStorage(*I)) |
| 4311 | QT = Context->getPointerType(QT); |
| 4312 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4313 | S += Name + " = __cself->" + |
| 4314 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4315 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4316 | } |
| 4317 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4318 | const char *cstr = RewrittenStr.c_str(); |
| 4319 | while (*cstr++ != '{') ; |
| 4320 | S += cstr; |
| 4321 | S += "\n"; |
| 4322 | return S; |
| 4323 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4324 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4325 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4326 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4327 | std::string Tag) { |
| 4328 | std::string StructRef = "struct " + Tag; |
| 4329 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4330 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4331 | S += funcName; |
| 4332 | S += "_block_copy_" + utostr(i); |
| 4333 | S += "(" + StructRef; |
| 4334 | S += "*dst, " + StructRef; |
| 4335 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4337 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4338 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4339 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4340 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4341 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4342 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4343 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4344 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4345 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4346 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4347 | else |
| 4348 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4349 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4350 | S += "}\n"; |
| 4351 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4352 | S += "\nstatic void __"; |
| 4353 | S += funcName; |
| 4354 | S += "_block_dispose_" + utostr(i); |
| 4355 | S += "(" + StructRef; |
| 4356 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4358 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4359 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4360 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4361 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4362 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4363 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4364 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4365 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4366 | else |
| 4367 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4370 | return S; |
| 4371 | } |
| 4372 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4373 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4374 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4375 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4376 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4378 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4379 | S += " struct " + Desc; |
| 4380 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4382 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4383 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4384 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4386 | if (BlockDeclRefs.size()) { |
| 4387 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4388 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4389 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4390 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4391 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4392 | std::string ArgName = "_" + FieldName; |
| 4393 | // Handle nested closure invocation. For example: |
| 4394 | // |
| 4395 | // void (^myImportedBlock)(void); |
| 4396 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4398 | // void (^anotherBlock)(void); |
| 4399 | // anotherBlock = ^(void) { |
| 4400 | // myImportedBlock(); // import and invoke the closure |
| 4401 | // }; |
| 4402 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4403 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4404 | S += "struct __block_impl *"; |
| 4405 | Constructor += ", void *" + ArgName; |
| 4406 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4407 | QualType QT = (*I)->getType(); |
| 4408 | if (HasLocalVariableExternalStorage(*I)) |
| 4409 | QT = Context->getPointerType(QT); |
| 4410 | QT.getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4411 | QT.getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4412 | Constructor += ", " + ArgName; |
| 4413 | } |
| 4414 | S += FieldName + ";\n"; |
| 4415 | } |
| 4416 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4417 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4418 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4419 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4420 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4421 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4422 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4423 | std::string TypeString; |
| 4424 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4425 | TypeString += " *"; |
| 4426 | FieldName = TypeString + FieldName; |
| 4427 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4428 | Constructor += ", " + ArgName; |
| 4429 | } |
| 4430 | S += FieldName + "; // by ref\n"; |
| 4431 | } |
| 4432 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4433 | Constructor += ", int flags=0)"; |
| 4434 | // Initialize all "by copy" arguments. |
| 4435 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4436 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4437 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4438 | std::string Name = (*I)->getNameAsString(); |
| 4439 | if (firsTime) { |
| 4440 | Constructor += " : "; |
| 4441 | firsTime = false; |
| 4442 | } |
| 4443 | else |
| 4444 | Constructor += ", "; |
| 4445 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4446 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4447 | else |
| 4448 | Constructor += Name + "(_" + Name + ")"; |
| 4449 | } |
| 4450 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4451 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4452 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4453 | std::string Name = (*I)->getNameAsString(); |
| 4454 | if (firsTime) { |
| 4455 | Constructor += " : "; |
| 4456 | firsTime = false; |
| 4457 | } |
| 4458 | else |
| 4459 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4460 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4464 | if (GlobalVarDecl) |
| 4465 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4466 | else |
| 4467 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4468 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4470 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4471 | } else { |
| 4472 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4473 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4474 | if (GlobalVarDecl) |
| 4475 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4476 | else |
| 4477 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4478 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4479 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4480 | } |
| 4481 | Constructor += " "; |
| 4482 | Constructor += "}\n"; |
| 4483 | S += Constructor; |
| 4484 | S += "};\n"; |
| 4485 | return S; |
| 4486 | } |
| 4487 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4488 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4489 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4490 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4491 | unsigned hasCopy) { |
| 4492 | std::string S = "\nstatic struct " + DescTag; |
| 4493 | |
| 4494 | S += " {\n unsigned long reserved;\n"; |
| 4495 | S += " unsigned long Block_size;\n"; |
| 4496 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4497 | S += " void (*copy)(struct "; |
| 4498 | S += ImplTag; S += "*, struct "; |
| 4499 | S += ImplTag; S += "*);\n"; |
| 4500 | |
| 4501 | S += " void (*dispose)(struct "; |
| 4502 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4503 | } |
| 4504 | S += "} "; |
| 4505 | |
| 4506 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4507 | S += ImplTag + ")"; |
| 4508 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4509 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4510 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4511 | } |
| 4512 | S += "};\n"; |
| 4513 | return S; |
| 4514 | } |
| 4515 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4516 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4517 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4518 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4519 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4520 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4521 | bool RewriteSC = (GlobalVarDecl && |
| 4522 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4523 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4524 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4525 | if (RewriteSC) { |
| 4526 | std::string SC(" void __"); |
| 4527 | SC += GlobalVarDecl->getNameAsString(); |
| 4528 | SC += "() {}"; |
| 4529 | InsertText(FunLocStart, SC); |
| 4530 | } |
| 4531 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4532 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4533 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4534 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4535 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4536 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4537 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4538 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4539 | ValueDecl *VD = Exp->getDecl(); |
| 4540 | BlockDeclRefs.push_back(Exp); |
| 4541 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4542 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4543 | BlockByCopyDecls.push_back(VD); |
| 4544 | } |
| 4545 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4546 | BlockByRefDeclsPtrSet.insert(VD); |
| 4547 | BlockByRefDecls.push_back(VD); |
| 4548 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 4549 | // imported objects in the inner blocks not used in the outer |
| 4550 | // blocks must be copied/disposed in the outer block as well. |
| 4551 | if (Exp->isByRef() || |
| 4552 | VD->getType()->isObjCObjectPointerType() || |
| 4553 | VD->getType()->isBlockPointerType()) |
| 4554 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4555 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4556 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4557 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4558 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4560 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4561 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4562 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4563 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4564 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4566 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4567 | |
| 4568 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4569 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4570 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4571 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4572 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4573 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4574 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4576 | BlockDeclRefs.clear(); |
| 4577 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4578 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4579 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4580 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4581 | ImportedBlockDecls.clear(); |
| 4582 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4583 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4584 | // Must insert any 'const/volatile/static here. Since it has been |
| 4585 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4586 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4587 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4588 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4589 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4590 | SC += "const "; |
| 4591 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4592 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4593 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4594 | SC += "restrict "; |
| 4595 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4598 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4599 | InnerDeclRefsCount.clear(); |
| 4600 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4601 | RewrittenBlockExprs.clear(); |
| 4602 | } |
| 4603 | |
| 4604 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4605 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4606 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4608 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4609 | } |
| 4610 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4611 | static void BuildUniqueMethodName(std::string &Name, |
| 4612 | ObjCMethodDecl *MD) { |
| 4613 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4614 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4615 | Name += "__" + MD->getSelector().getAsString(); |
| 4616 | // Convert colons to underscores. |
| 4617 | std::string::size_type loc = 0; |
| 4618 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4619 | Name.replace(loc, 1, "_"); |
| 4620 | } |
| 4621 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4622 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4623 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4624 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4625 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4626 | std::string FuncName; |
| 4627 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4628 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4632 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4633 | if (*CI) { |
| 4634 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4635 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4636 | else |
| 4637 | GetBlockDeclRefExprs(*CI); |
| 4638 | } |
| 4639 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4640 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4641 | // FIXME: Handle enums. |
| 4642 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4643 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4644 | } |
| 4645 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4646 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4647 | BlockDeclRefExpr *BDRE = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4648 | new (Context)BlockDeclRefExpr(cast<VarDecl>(DRE->getDecl()), |
| 4649 | DRE->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4650 | VK_LValue, DRE->getLocation(), false); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4651 | BlockDeclRefs.push_back(BDRE); |
| 4652 | } |
| 4653 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4654 | return; |
| 4655 | } |
| 4656 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4657 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4658 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4659 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4660 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4661 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4662 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4663 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4664 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4665 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4666 | InnerContexts); |
| 4667 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4668 | else |
| 4669 | GetInnerBlockDeclRefExprs(*CI, |
| 4670 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4671 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4672 | |
| 4673 | } |
| 4674 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4675 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4676 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4677 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4678 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4679 | } |
| 4680 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4681 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4682 | if (Var->isFunctionOrMethodVarDecl()) |
| 4683 | ImportedLocalExternalDecls.insert(Var); |
| 4684 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4685 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4686 | return; |
| 4687 | } |
| 4688 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4689 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4690 | /// 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] | 4691 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4692 | /// all block pointers to function pointers. |
| 4693 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4694 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4695 | // FTP will be null for closures that don't take arguments. |
| 4696 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4697 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4698 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4699 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4700 | |
| 4701 | if (FTP) { |
| 4702 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4703 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4704 | QualType t = *I; |
| 4705 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4706 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4707 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4708 | ArgTypes.push_back(t); |
| 4709 | } |
| 4710 | } |
| 4711 | QualType FuncType; |
| 4712 | // FIXME. Does this work if block takes no argument but has a return type |
| 4713 | // which is of block type? |
| 4714 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4715 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4716 | else FuncType = QualType(FT, 0); |
| 4717 | return FuncType; |
| 4718 | } |
| 4719 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4720 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4721 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4722 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4724 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4725 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4726 | } else if (const BlockDeclRefExpr *CDRE = |
| 4727 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4728 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4729 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4730 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4731 | } |
| 4732 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4733 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4734 | } |
| 4735 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4736 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4737 | else if (const ConditionalOperator *CEXPR = |
| 4738 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4739 | Expr *LHSExp = CEXPR->getLHS(); |
| 4740 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4741 | Expr *RHSExp = CEXPR->getRHS(); |
| 4742 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4743 | Expr *CONDExp = CEXPR->getCond(); |
| 4744 | ConditionalOperator *CondExpr = |
| 4745 | new (Context) ConditionalOperator(CONDExp, |
| 4746 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4747 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4748 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4749 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4750 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4751 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4752 | } else { |
| 4753 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4754 | } |
| 4755 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4756 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4757 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4758 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4759 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4760 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4761 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4762 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4763 | &Context->Idents.get("__block_impl")); |
| 4764 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4765 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4766 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4767 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4769 | // Push the block argument type. |
| 4770 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4771 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4773 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4774 | QualType t = *I; |
| 4775 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4776 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4777 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4778 | ArgTypes.push_back(t); |
| 4779 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4780 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4781 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4782 | QualType PtrToFuncCastType |
| 4783 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4785 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4787 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4788 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4789 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4790 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4791 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4792 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4793 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4795 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4796 | SourceLocation(), |
| 4797 | &Context->Idents.get("FuncPtr"), |
| 4798 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4799 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4800 | /*HasInit=*/false); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4801 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4802 | FD->getType(), VK_LValue, |
| 4803 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4805 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4806 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4807 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4808 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4810 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4811 | // Add the implicit argument. |
| 4812 | BlkExprs.push_back(BlkCast); |
| 4813 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4815 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4816 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4817 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4818 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4819 | BlkExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4820 | Exp->getType(), VK_RValue, |
| 4821 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4822 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4825 | // We need to return the rewritten expression to handle cases where the |
| 4826 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4827 | // For example: |
| 4828 | // |
| 4829 | // int main() { |
| 4830 | // __block Foo *f; |
| 4831 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4832 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4833 | // void (^myblock)() = ^() { |
| 4834 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4835 | // i = 77; |
| 4836 | // }; |
| 4837 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4838 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4839 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4840 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4841 | ValueDecl *VD; |
| 4842 | bool isArrow = true; |
| 4843 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4844 | VD = BDRE->getDecl(); |
| 4845 | else { |
| 4846 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4847 | isArrow = false; |
| 4848 | } |
| 4849 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4850 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4851 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4852 | &Context->Idents.get("__forwarding"), |
| 4853 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4854 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4855 | /*HasInit=*/false); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4856 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4857 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4858 | FD->getType(), VK_LValue, |
| 4859 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4860 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4861 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4862 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4863 | &Context->Idents.get(Name), |
| 4864 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4865 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4866 | /*HasInit=*/false); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4867 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4868 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4869 | |
| 4870 | |
| 4871 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4872 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 4873 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4874 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4875 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4876 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4877 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4880 | // Rewrites the imported local variable V with external storage |
| 4881 | // (static, extern, etc.) as *V |
| 4882 | // |
| 4883 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4884 | ValueDecl *VD = DRE->getDecl(); |
| 4885 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4886 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4887 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4888 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4889 | VK_LValue, OK_Ordinary, |
| 4890 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4891 | // Need parens to enforce precedence. |
| 4892 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4893 | Exp); |
| 4894 | ReplaceStmt(DRE, PE); |
| 4895 | return PE; |
| 4896 | } |
| 4897 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4898 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4899 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4900 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4901 | |
| 4902 | // Need to avoid trying to rewrite synthesized casts. |
| 4903 | if (LocStart.isInvalid()) |
| 4904 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4905 | // Need to avoid trying to rewrite casts contained in macros. |
| 4906 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4907 | return; |
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 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4910 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4911 | QualType QT = CE->getType(); |
| 4912 | const Type* TypePtr = QT->getAs<Type>(); |
| 4913 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4914 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4915 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4916 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4917 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4918 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4919 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4920 | return; |
| 4921 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4922 | // advance the location to startArgList. |
| 4923 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4925 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4926 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4927 | case '^': |
| 4928 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4929 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4930 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4931 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4932 | } |
| 4933 | } |
| 4934 | return; |
| 4935 | } |
| 4936 | |
| 4937 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4938 | SourceLocation DeclLoc = FD->getLocation(); |
| 4939 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4941 | // We have 1 or more arguments that have closure pointers. |
| 4942 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4943 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4945 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4947 | parenCount++; |
| 4948 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4949 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4950 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4952 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4954 | while (*argPtr++ && parenCount) { |
| 4955 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4956 | case '^': |
| 4957 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4958 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4959 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4960 | break; |
| 4961 | case '(': |
| 4962 | parenCount++; |
| 4963 | break; |
| 4964 | case ')': |
| 4965 | parenCount--; |
| 4966 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | return; |
| 4970 | } |
| 4971 | |
| 4972 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4973 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4974 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4975 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4976 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4977 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4978 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4979 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4980 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4981 | } |
| 4982 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4983 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4984 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4985 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4986 | return true; |
| 4987 | } |
| 4988 | return false; |
| 4989 | } |
| 4990 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4991 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4992 | const FunctionProtoType *FTP; |
| 4993 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4994 | if (PT) { |
| 4995 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4996 | } else { |
| 4997 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4998 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4999 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 5000 | } |
| 5001 | if (FTP) { |
| 5002 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 5003 | E = FTP->arg_type_end(); I != E; ++I) { |
| 5004 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5005 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 5006 | if ((*I)->isObjCObjectPointerType() && |
| 5007 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 5008 | return true; |
| 5009 | } |
| 5010 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5011 | } |
| 5012 | return false; |
| 5013 | } |
| 5014 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5015 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 5016 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5017 | const char *argPtr = strchr(Name, '('); |
| 5018 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5020 | LParen = argPtr; // output the start. |
| 5021 | argPtr++; // skip past the left paren. |
| 5022 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5024 | while (*argPtr && parenCount) { |
| 5025 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 5026 | case '(': parenCount++; break; |
| 5027 | case ')': parenCount--; break; |
| 5028 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5029 | } |
| 5030 | if (parenCount) argPtr++; |
| 5031 | } |
| 5032 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 5033 | RParen = argPtr; // output the end |
| 5034 | } |
| 5035 | |
| 5036 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 5037 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 5038 | RewriteBlockPointerFunctionArgs(FD); |
| 5039 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5040 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5041 | // Handle Variables and Typedefs. |
| 5042 | SourceLocation DeclLoc = ND->getLocation(); |
| 5043 | QualType DeclT; |
| 5044 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 5045 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5046 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5047 | DeclT = TDD->getUnderlyingType(); |
| 5048 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 5049 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5050 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 5051 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5052 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5053 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 5054 | const char *endBuf = startBuf; |
| 5055 | // scan backward (from the decl location) for the end of the previous decl. |
| 5056 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 5057 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 5058 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5059 | std::string buf; |
| 5060 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5061 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 5062 | // may take block argument types (which will be handled below). |
| 5063 | if (*startBuf == '^') { |
| 5064 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5065 | buf = '*'; |
| 5066 | startBuf++; |
| 5067 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5068 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5069 | while (*startBuf != ')') { |
| 5070 | buf += *startBuf; |
| 5071 | startBuf++; |
| 5072 | OrigLength++; |
| 5073 | } |
| 5074 | buf += ')'; |
| 5075 | OrigLength++; |
| 5076 | |
| 5077 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 5078 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5079 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5080 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5081 | DeclLoc = ND->getLocation(); |
| 5082 | startBuf = SM->getCharacterData(DeclLoc); |
| 5083 | const char *argListBegin, *argListEnd; |
| 5084 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 5085 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5086 | if (*argListBegin == '^') |
| 5087 | buf += '*'; |
| 5088 | else if (*argListBegin == '<') { |
| 5089 | buf += "/*"; |
| 5090 | buf += *argListBegin++; |
| 5091 | OrigLength++;; |
| 5092 | while (*argListBegin != '>') { |
| 5093 | buf += *argListBegin++; |
| 5094 | OrigLength++; |
| 5095 | } |
| 5096 | buf += *argListBegin; |
| 5097 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5098 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5099 | else |
| 5100 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5101 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5102 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5103 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5104 | buf += ')'; |
| 5105 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5106 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5107 | ReplaceText(Start, OrigLength, buf); |
| 5108 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5109 | return; |
| 5110 | } |
| 5111 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5112 | |
| 5113 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 5114 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 5115 | /// struct Block_byref_id_object *src) { |
| 5116 | /// _Block_object_assign (&_dest->object, _src->object, |
| 5117 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5118 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5119 | /// _Block_object_assign(&_dest->object, _src->object, |
| 5120 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5121 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5122 | /// } |
| 5123 | /// And: |
| 5124 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 5125 | /// _Block_object_dispose(_src->object, |
| 5126 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5127 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5128 | /// _Block_object_dispose(_src->object, |
| 5129 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5130 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5131 | /// } |
| 5132 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5133 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 5134 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5135 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 5136 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5137 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5138 | CopyDestroyCache.insert(flag); |
| 5139 | S = "static void __Block_byref_id_object_copy_"; |
| 5140 | S += utostr(flag); |
| 5141 | S += "(void *dst, void *src) {\n"; |
| 5142 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5143 | // offset into the object pointer is computed as: |
| 5144 | // void * + void* + int + int + void* + void * |
| 5145 | unsigned IntSize = |
| 5146 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5147 | unsigned VoidPtrSize = |
| 5148 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 5149 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 5150 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5151 | S += " _Block_object_assign((char*)dst + "; |
| 5152 | S += utostr(offset); |
| 5153 | S += ", *(void * *) ((char*)src + "; |
| 5154 | S += utostr(offset); |
| 5155 | S += "), "; |
| 5156 | S += utostr(flag); |
| 5157 | S += ");\n}\n"; |
| 5158 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5159 | S += "static void __Block_byref_id_object_dispose_"; |
| 5160 | S += utostr(flag); |
| 5161 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5162 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5163 | S += utostr(offset); |
| 5164 | S += "), "; |
| 5165 | S += utostr(flag); |
| 5166 | S += ");\n}\n"; |
| 5167 | return S; |
| 5168 | } |
| 5169 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5170 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5171 | /// the declaration into: |
| 5172 | /// struct __Block_byref_ND { |
| 5173 | /// void *__isa; // NULL for everything except __weak pointers |
| 5174 | /// struct __Block_byref_ND *__forwarding; |
| 5175 | /// int32_t __flags; |
| 5176 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5177 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5178 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5179 | /// typex ND; |
| 5180 | /// }; |
| 5181 | /// |
| 5182 | /// It then replaces declaration of ND variable with: |
| 5183 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5184 | /// __size=sizeof(struct __Block_byref_ND), |
| 5185 | /// ND=initializer-if-any}; |
| 5186 | /// |
| 5187 | /// |
| 5188 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5189 | // Insert declaration for the function in which block literal is |
| 5190 | // used. |
| 5191 | if (CurFunctionDeclToDeclareForBlock) |
| 5192 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5193 | int flag = 0; |
| 5194 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5195 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5196 | if (DeclLoc.isInvalid()) |
| 5197 | // If type location is missing, it is because of missing type (a warning). |
| 5198 | // Use variable's location which is good for this case. |
| 5199 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5200 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5201 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5202 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5203 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5204 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5205 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5206 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5207 | ByrefType += " {\n"; |
| 5208 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5209 | RewriteByRefString(ByrefType, Name, ND); |
| 5210 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5211 | ByrefType += " int __flags;\n"; |
| 5212 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5213 | // Add void *__Block_byref_id_object_copy; |
| 5214 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5215 | QualType Ty = ND->getType(); |
| 5216 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5217 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5218 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5219 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5220 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5221 | |
| 5222 | QualType T = Ty; |
| 5223 | (void)convertBlockPointerToFunctionPointer(T); |
| 5224 | T.getAsStringInternal(Name, Context->PrintingPolicy); |
| 5225 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5226 | ByrefType += " " + Name + ";\n"; |
| 5227 | ByrefType += "};\n"; |
| 5228 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5229 | SourceLocation FunLocStart; |
| 5230 | if (CurFunctionDef) |
| 5231 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5232 | else { |
| 5233 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5234 | FunLocStart = CurMethodDef->getLocStart(); |
| 5235 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5236 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5237 | if (Ty.isObjCGCWeak()) { |
| 5238 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5239 | isa = 1; |
| 5240 | } |
| 5241 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5242 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5243 | flag = BLOCK_BYREF_CALLER; |
| 5244 | QualType Ty = ND->getType(); |
| 5245 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5246 | if (Ty->isBlockPointerType()) |
| 5247 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5248 | else |
| 5249 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5250 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5251 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5252 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5253 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5254 | |
| 5255 | // struct __Block_byref_ND ND = |
| 5256 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5257 | // initializer-if-any}; |
| 5258 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5259 | unsigned flags = 0; |
| 5260 | if (HasCopyAndDispose) |
| 5261 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5262 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5263 | ByrefType.clear(); |
| 5264 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5265 | std::string ForwardingCastType("("); |
| 5266 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5267 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5268 | ByrefType += " " + Name + " = {(void*)"; |
| 5269 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5270 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5271 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5272 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5273 | ByrefType += "sizeof("; |
| 5274 | RewriteByRefString(ByrefType, Name, ND); |
| 5275 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5276 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5277 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5278 | ByrefType += utostr(flag); |
| 5279 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5280 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5281 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5282 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5283 | unsigned nameSize = Name.size(); |
| 5284 | // for block or function pointer declaration. Name is aleady |
| 5285 | // part of the declaration. |
| 5286 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5287 | nameSize = 1; |
| 5288 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5289 | } |
| 5290 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5291 | SourceLocation startLoc; |
| 5292 | Expr *E = ND->getInit(); |
| 5293 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5294 | startLoc = ECE->getLParenLoc(); |
| 5295 | else |
| 5296 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5297 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5298 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5299 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5300 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5301 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5302 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5303 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5304 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5305 | ByrefType += "sizeof("; |
| 5306 | RewriteByRefString(ByrefType, Name, ND); |
| 5307 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5308 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5309 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5310 | ByrefType += utostr(flag); |
| 5311 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5312 | ByrefType += utostr(flag); |
| 5313 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5314 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5315 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5316 | |
| 5317 | // Complete the newly synthesized compound expression by inserting a right |
| 5318 | // curly brace before the end of the declaration. |
| 5319 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5320 | // also assumes there is only one declarator. For example, the following |
| 5321 | // isn't currently supported by this routine (in general): |
| 5322 | // |
| 5323 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5324 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5325 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5326 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5327 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5328 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 5329 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5330 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5331 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5332 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5333 | return; |
| 5334 | } |
| 5335 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5337 | // Add initializers for any closure decl refs. |
| 5338 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5339 | if (BlockDeclRefs.size()) { |
| 5340 | // Unique all "by copy" declarations. |
| 5341 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5342 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5343 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5344 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5345 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5346 | } |
| 5347 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5348 | // Unique all "by ref" declarations. |
| 5349 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5350 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5351 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5352 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5353 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5354 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5355 | } |
| 5356 | // Find any imported blocks...they will need special attention. |
| 5357 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5358 | if (BlockDeclRefs[i]->isByRef() || |
| 5359 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5360 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5361 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5362 | } |
| 5363 | } |
| 5364 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5365 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5366 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5367 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5368 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5369 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5370 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5373 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5374 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5375 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5376 | Blocks.push_back(Exp); |
| 5377 | |
| 5378 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5379 | |
| 5380 | // Add inner imported variables now used in current block. |
| 5381 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5382 | if (!InnerBlockDeclRefs.empty()) { |
| 5383 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5384 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5385 | ValueDecl *VD = Exp->getDecl(); |
| 5386 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5387 | // We need to save the copied-in variables in nested |
| 5388 | // blocks because it is needed at the end for some of the API generations. |
| 5389 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5390 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5391 | BlockDeclRefs.push_back(Exp); |
| 5392 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5393 | BlockByCopyDecls.push_back(VD); |
| 5394 | } |
| 5395 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5396 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5397 | BlockDeclRefs.push_back(Exp); |
| 5398 | BlockByRefDeclsPtrSet.insert(VD); |
| 5399 | BlockByRefDecls.push_back(VD); |
| 5400 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5401 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5402 | // Find any imported blocks...they will need special attention. |
| 5403 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5404 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5405 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5406 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5407 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5408 | } |
| 5409 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5410 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5411 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5412 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5413 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5414 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5415 | else if (CurMethodDef) |
| 5416 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5417 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5418 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5420 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5422 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5423 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5425 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 5426 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5427 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5428 | |
| 5429 | FunctionDecl *FD; |
| 5430 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5431 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5432 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5433 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5434 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, VK_RValue, |
| 5435 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5436 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5437 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5438 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5439 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5440 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5441 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5442 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5443 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5444 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5445 | InitExprs.push_back(castExpr); |
| 5446 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5447 | // Initialize the block descriptor. |
| 5448 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5450 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5451 | SourceLocation(), SourceLocation(), |
| 5452 | &Context->Idents.get(DescData.c_str()), |
| 5453 | Context->VoidPtrTy, 0, |
| 5454 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5455 | UnaryOperator *DescRefExpr = |
| 5456 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, |
| 5457 | Context->VoidPtrTy, |
| 5458 | VK_LValue, |
| 5459 | SourceLocation()), |
| 5460 | UO_AddrOf, |
| 5461 | Context->getPointerType(Context->VoidPtrTy), |
| 5462 | VK_RValue, OK_Ordinary, |
| 5463 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5464 | InitExprs.push_back(DescRefExpr); |
| 5465 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5466 | // Add initializers for any closure decl refs. |
| 5467 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5468 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5469 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5470 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5471 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5472 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5473 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5474 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5475 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5476 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5477 | if (HasLocalVariableExternalStorage(*I)) { |
| 5478 | QualType QT = (*I)->getType(); |
| 5479 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5480 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5481 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5482 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5483 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5484 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5485 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5486 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5487 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5488 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5489 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5490 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5491 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5492 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5493 | if (HasLocalVariableExternalStorage(*I)) { |
| 5494 | QualType QT = (*I)->getType(); |
| 5495 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5496 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5497 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5501 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5502 | } |
| 5503 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5504 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5505 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5506 | ValueDecl *ND = (*I); |
| 5507 | std::string Name(ND->getNameAsString()); |
| 5508 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5509 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5510 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5511 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5512 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5513 | SourceLocation(), SourceLocation(), |
| 5514 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5515 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5516 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5517 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5518 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5519 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5520 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5521 | bool isNestedCapturedVar = false; |
| 5522 | if (block) |
| 5523 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5524 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5525 | const VarDecl *variable = ci->getVariable(); |
| 5526 | if (variable == ND && ci->isNested()) { |
| 5527 | assert (ci->isByRef() && |
| 5528 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5529 | isNestedCapturedVar = true; |
| 5530 | break; |
| 5531 | } |
| 5532 | } |
| 5533 | // captured nested byref variable has its address passed. Do not take |
| 5534 | // its address again. |
| 5535 | if (!isNestedCapturedVar) |
| 5536 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5537 | Context->getPointerType(Exp->getType()), |
| 5538 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5539 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5540 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5541 | } |
| 5542 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5543 | if (ImportedBlockDecls.size()) { |
| 5544 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5545 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5546 | unsigned IntSize = |
| 5547 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5548 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5549 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5550 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5551 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5552 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5553 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5554 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5556 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5557 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5558 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5559 | BlockDeclRefs.clear(); |
| 5560 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5561 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5562 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5563 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5564 | ImportedBlockDecls.clear(); |
| 5565 | return NewRep; |
| 5566 | } |
| 5567 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5568 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5569 | if (const ObjCForCollectionStmt * CS = |
| 5570 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5571 | return CS->getElement() == DS; |
| 5572 | return false; |
| 5573 | } |
| 5574 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5575 | //===----------------------------------------------------------------------===// |
| 5576 | // Function Body / Expression rewriting |
| 5577 | //===----------------------------------------------------------------------===// |
| 5578 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5579 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5580 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5581 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5582 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5583 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5584 | // we get this right. |
| 5585 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5586 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 5587 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5588 | if (*CI) |
| 5589 | CollectPropertySetters(*CI); |
| 5590 | |
| 5591 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5592 | if (BinOp->isAssignmentOp()) { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5593 | if (isa<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 5594 | PropSetters[BinOp->getLHS()] = BinOp; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5595 | } |
| 5596 | } |
| 5597 | } |
| 5598 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5599 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5601 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5602 | Stmts.push_back(S); |
| 5603 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5604 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5605 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5607 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5608 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5609 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5610 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 5611 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5612 | if (*CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5613 | Stmt *newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5614 | Stmt *ChildStmt = (*CI); |
| 5615 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(ChildStmt)) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5616 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5617 | bool replaced = false; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5618 | newStmt = RewriteObjCNestedIvarRefExpr(ChildStmt, replaced); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5619 | if (replaced) { |
| 5620 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5621 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5622 | else |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5623 | ReplaceStmt(ChildStmt, newStmt); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5624 | } |
| 5625 | } |
| 5626 | else |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5627 | newStmt = RewriteFunctionBodyOrGlobalInitializer(ChildStmt); |
| 5628 | if (newStmt) { |
| 5629 | if (Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(ChildStmt)) |
| 5630 | if (PropSetters[PropOrImplicitRefExpr] == S) { |
| 5631 | S = newStmt; |
| 5632 | newStmt = 0; |
| 5633 | } |
| 5634 | if (newStmt) |
| 5635 | *CI = newStmt; |
| 5636 | } |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5637 | // If dealing with an assignment with LHS being a property reference |
| 5638 | // expression, the entire assignment tree is rewritten into a property |
| 5639 | // setter messaging. This involvs the RHS too. Do not attempt to rewrite |
| 5640 | // RHS again. |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5641 | if (Expr *Exp = dyn_cast<Expr>(ChildStmt)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5642 | if (isa<ObjCPropertyRefExpr>(Exp)) { |
Fariborz Jahanian | 8537f7b | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5643 | if (PropSetters[Exp]) { |
| 5644 | ++CI; |
| 5645 | continue; |
| 5646 | } |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5647 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 5648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5649 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5650 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5651 | SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5652 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5653 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5654 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5655 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5656 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5657 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5658 | Stmt *SaveCurrentBody = CurrentBody; |
| 5659 | CurrentBody = BE->getBody(); |
Fariborz Jahanian | 3617809 | 2011-04-08 23:48:29 +0000 | [diff] [blame] | 5660 | CollectPropertySetters(CurrentBody); |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5661 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5662 | // block literal on rhs of a property-dot-sytax assignment |
| 5663 | // must be replaced by its synthesize ast so getRewrittenText |
| 5664 | // works as expected. In this case, what actually ends up on RHS |
| 5665 | // is the blockTranscribed which is the helper function for the |
| 5666 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5667 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5668 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5669 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5670 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5671 | CurrentBody = SaveCurrentBody; |
| 5672 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5673 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5674 | // 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] | 5675 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5676 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5678 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5679 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5680 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5681 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5682 | return blockTranscribed; |
| 5683 | } |
| 5684 | // Handle specific things. |
| 5685 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5686 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5687 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 5688 | if (isa<ObjCPropertyRefExpr>(S)) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5689 | Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(S); |
| 5690 | assert(PropOrImplicitRefExpr && "Property or implicit setter/getter is null"); |
| 5691 | |
| 5692 | BinaryOperator *BinOp = PropSetters[PropOrImplicitRefExpr]; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5693 | if (BinOp) { |
| 5694 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5695 | // 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] | 5696 | DisableReplaceStmt = true; |
| 5697 | // Save the source range. Even if we disable the replacement, the |
| 5698 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5699 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5700 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5701 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5702 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5703 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Fariborz Jahanian | f2c6fa4 | 2010-10-14 23:31:39 +0000 | [diff] [blame] | 5704 | // Need to rewrite the ivar access expression if need be. |
| 5705 | if (isa<ObjCIvarRefExpr>(newStmt)) { |
| 5706 | bool replaced = false; |
| 5707 | newStmt = RewriteObjCNestedIvarRefExpr(newStmt, replaced); |
| 5708 | } |
| 5709 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5710 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5711 | // |
| 5712 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5713 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5714 | // to see the original expression). Consider this example: |
| 5715 | // |
| 5716 | // Foo *obj1, *obj2; |
| 5717 | // |
| 5718 | // obj1.i = [obj2 rrrr]; |
| 5719 | // |
| 5720 | // 'BinOp' for the previous expression looks like: |
| 5721 | // |
| 5722 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5723 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5724 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5725 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5726 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5727 | // |
| 5728 | // 'newStmt' represents the rewritten message expression. For example: |
| 5729 | // |
| 5730 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5731 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5732 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5733 | // (CStyleCastExpr 0x231d220 'void *' |
| 5734 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5735 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5736 | // Note that 'newStmt' is passed to RewritePropertyOrImplicitSetter so that it |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5737 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5738 | // the original RHS (since we haven't altered BinOp). |
| 5739 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5740 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5741 | // node. As a result, we now leak the original AST nodes. |
| 5742 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5743 | return RewritePropertyOrImplicitSetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5744 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5745 | return RewritePropertyOrImplicitGetter(PropOrImplicitRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5746 | } |
| 5747 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5748 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5749 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5750 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5751 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5752 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5753 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5754 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5755 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5756 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5757 | // Before we rewrite it, put the original message expression in a comment. |
| 5758 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5759 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5760 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5761 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5762 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5763 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5764 | std::string messString; |
| 5765 | messString += "// "; |
| 5766 | messString.append(startBuf, endBuf-startBuf+1); |
| 5767 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5768 | |
| 5769 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5770 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5771 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5772 | // Tried this, but it didn't work either... |
| 5773 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5774 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5775 | return RewriteMessageExpr(MessExpr); |
| 5776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5777 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5778 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5779 | return RewriteObjCTryStmt(StmtTry); |
| 5780 | |
| 5781 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5782 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5783 | |
| 5784 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5785 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5787 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5788 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5789 | |
| 5790 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5791 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5793 | OrigStmtRange.getEnd()); |
| 5794 | if (BreakStmt *StmtBreakStmt = |
| 5795 | dyn_cast<BreakStmt>(S)) |
| 5796 | return RewriteBreakStmt(StmtBreakStmt); |
| 5797 | if (ContinueStmt *StmtContinueStmt = |
| 5798 | dyn_cast<ContinueStmt>(S)) |
| 5799 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5800 | |
| 5801 | // 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] | 5802 | // and cast exprs. |
| 5803 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5804 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5805 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5807 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5808 | // the context of an ObjCForCollectionStmt. For example: |
| 5809 | // NSArray *someArray; |
| 5810 | // for (id <FooProtocol> index in someArray) ; |
| 5811 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5812 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5813 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5814 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5815 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5816 | // Blocks rewrite rules. |
| 5817 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5818 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5819 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5820 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5821 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5822 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5823 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5824 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5825 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5826 | if (VD->hasAttr<BlocksAttr>()) { |
| 5827 | static unsigned uniqueByrefDeclCount = 0; |
| 5828 | assert(!BlockByRefDeclNo.count(ND) && |
| 5829 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5830 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5831 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5832 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5833 | else |
| 5834 | RewriteTypeOfDecl(VD); |
| 5835 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5836 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5837 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5838 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5839 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5840 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5841 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5842 | } |
| 5843 | } |
| 5844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5845 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5846 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5847 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5848 | |
| 5849 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5850 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5851 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5852 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5853 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5854 | && "Statement stack mismatch"); |
| 5855 | Stmts.pop_back(); |
| 5856 | } |
| 5857 | // Handle blocks rewriting. |
| 5858 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5859 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5860 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5861 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5862 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5863 | ValueDecl *VD = DRE->getDecl(); |
| 5864 | if (VD->hasAttr<BlocksAttr>()) |
| 5865 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5866 | if (HasLocalVariableExternalStorage(VD)) |
| 5867 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5870 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5871 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5872 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5873 | ReplaceStmt(S, BlockCall); |
| 5874 | return BlockCall; |
| 5875 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5876 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5877 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5878 | RewriteCastExpr(CE); |
| 5879 | } |
| 5880 | #if 0 |
| 5881 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5882 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5883 | ICE->getSubExpr(), |
| 5884 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5885 | // Get the new text. |
| 5886 | std::string SStr; |
| 5887 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5888 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5889 | const std::string &Str = Buf.str(); |
| 5890 | |
| 5891 | printf("CAST = %s\n", &Str[0]); |
| 5892 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5893 | delete S; |
| 5894 | return Replacement; |
| 5895 | } |
| 5896 | #endif |
| 5897 | // Return this stmt unmodified. |
| 5898 | return S; |
| 5899 | } |
| 5900 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5901 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5902 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5903 | e = RD->field_end(); i != e; ++i) { |
| 5904 | FieldDecl *FD = *i; |
| 5905 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5906 | RewriteBlockPointerDecl(FD); |
| 5907 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5908 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5909 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5910 | } |
| 5911 | } |
| 5912 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5913 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5914 | /// main file of the input. |
| 5915 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5916 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5917 | if (FD->isOverloadedOperator()) |
| 5918 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5919 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5920 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5921 | // prototype. This enables us to rewrite function declarations and |
| 5922 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5923 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5924 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5925 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 5f1bfc1 | 2010-07-07 11:31:34 +0000 | [diff] [blame] | 5926 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5927 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5928 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5929 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5930 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5931 | Body = |
| 5932 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5933 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5934 | CurrentBody = 0; |
| 5935 | if (PropParentMap) { |
| 5936 | delete PropParentMap; |
| 5937 | PropParentMap = 0; |
| 5938 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5939 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5940 | // and any copy/dispose helper functions. |
| 5941 | InsertBlockLiteralsWithinFunction(FD); |
| 5942 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5943 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5944 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5945 | return; |
| 5946 | } |
| 5947 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5948 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5949 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5950 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5951 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5952 | Body = |
| 5953 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5954 | MD->setBody(Body); |
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 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5960 | InsertBlockLiteralsWithinMethod(MD); |
| 5961 | CurMethodDef = 0; |
| 5962 | } |
| 5963 | } |
| 5964 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5965 | ClassImplementation.push_back(CI); |
| 5966 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5967 | CategoryImplementation.push_back(CI); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 5968 | else if (isa<ObjCClassDecl>(D)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 5969 | llvm_unreachable("RewriteObjC::HandleDeclInMainFile - ObjCClassDecl"); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5970 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5971 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5972 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5973 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5974 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5975 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5976 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5977 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5978 | RewriteCastExpr(CE); |
| 5979 | } |
| 5980 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5981 | } else if (VD->getType()->isRecordType()) { |
| 5982 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5983 | if (RD->isDefinition()) |
| 5984 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5985 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5986 | if (VD->getInit()) { |
| 5987 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5988 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5989 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5990 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5991 | CurrentBody = 0; |
| 5992 | if (PropParentMap) { |
| 5993 | delete PropParentMap; |
| 5994 | PropParentMap = 0; |
| 5995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5997 | VD->getName()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5998 | GlobalVarDecl = 0; |
| 5999 | |
| 6000 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 6001 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 6002 | RewriteCastExpr(CE); |
| 6003 | } |
| 6004 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6005 | return; |
| 6006 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6007 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 6008 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6009 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6010 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6011 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 6012 | return; |
| 6013 | } |
| 6014 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 6015 | if (RD->isDefinition()) |
| 6016 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6017 | return; |
| 6018 | } |
| 6019 | // Nothing yet. |
| 6020 | } |
| 6021 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 6022 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6023 | if (Diags.hasErrorOccurred()) |
| 6024 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6025 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6026 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6027 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6028 | // Here's a great place to add any extra declarations that may be needed. |
| 6029 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6030 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6031 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 6032 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 6033 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 6034 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 6035 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 6036 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6037 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6038 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 6039 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6040 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6041 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 6042 | //printf("Changed:\n"); |
| 6043 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 6044 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 6045 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6046 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 6047 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 6048 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 6049 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 6050 | // Rewrite Objective-c meta data* |
| 6051 | std::string ResultStr; |
| 6052 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 6053 | // Emit metadata. |
| 6054 | *OutFile << ResultStr; |
| 6055 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 6056 | OutFile->flush(); |
| 6057 | } |