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 { |
| 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 37 | block, ... */ |
| 38 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 39 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 40 | __block variable */ |
| 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 42 | helpers */ |
| 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 44 | support routines */ |
| 45 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 46 | }; |
| 47 | |
| 48 | enum { |
| 49 | BLOCK_NEEDS_FREE = (1 << 24), |
| 50 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 51 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 52 | BLOCK_IS_GC = (1 << 27), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 55 | }; |
| 56 | |
Chris Lattner | 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 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 70 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 71 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 72 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 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; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 76 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 77 | llvm::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; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 116 | llvm::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. |
| 124 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 125 | llvm::SmallVector<int, 32> InnerDeclRefsCount; |
| 126 | llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
| 127 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 128 | llvm::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. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 131 | llvm::SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 133 | llvm::SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 134 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | 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) { |
| 164 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 165 | HandleTopLevelSingleDecl(*I); |
| 166 | } |
| 167 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 168 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 169 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 170 | Diagnostic &D, const LangOptions &LOpts, |
| 171 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 172 | |
| 173 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 175 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 177 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 180 | if (ReplacingStmt) |
| 181 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 182 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 183 | if (DisableReplaceStmt) |
| 184 | return; // Used when rewriting the assignment of a property setter. |
| 185 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 186 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 187 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 188 | ReplacedNodes[Old] = New; |
| 189 | return; |
| 190 | } |
| 191 | if (SilenceRewriteMacroWarning) |
| 192 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 193 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 194 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 195 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 196 | |
| 197 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 198 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 199 | int Size = Rewrite.getRangeSize(SrcRange); |
| 200 | if (Size == -1) { |
| 201 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 202 | << Old->getSourceRange(); |
| 203 | return; |
| 204 | } |
| 205 | // Get the new text. |
| 206 | std::string SStr; |
| 207 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 208 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 209 | const std::string &Str = S.str(); |
| 210 | |
| 211 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 212 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 213 | ReplacedNodes[Old] = New; |
| 214 | return; |
| 215 | } |
| 216 | if (SilenceRewriteMacroWarning) |
| 217 | return; |
| 218 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 219 | << Old->getSourceRange(); |
| 220 | } |
| 221 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 222 | void InsertText(SourceLocation Loc, llvm::StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 223 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 224 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 225 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 226 | SilenceRewriteMacroWarning) |
| 227 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 229 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 230 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 232 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 233 | llvm::StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 234 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 235 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 236 | SilenceRewriteMacroWarning) |
| 237 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 239 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 242 | // Syntactic Rewriting. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 243 | void RewriteInclude(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 244 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 245 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 246 | ObjCImplementationDecl *IMD, |
| 247 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 249 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 250 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 251 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 252 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 253 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 254 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 255 | ValueDecl *VD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 256 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 257 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 258 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 259 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 260 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 261 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 262 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 263 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 264 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 266 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 267 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 268 | bool needToScanForQualifiers(QualType T); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 269 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 270 | QualType getConstantStringStructType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 271 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 272 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 274 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 275 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 276 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 278 | Stmt *CurrentBody; |
| 279 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 281 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 282 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 283 | bool &replaced); |
| 284 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 285 | Stmt *RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr); |
| 286 | Stmt *RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 287 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 288 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 289 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 291 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 292 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 293 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 294 | void RewriteTryReturnStmts(Stmt *S); |
| 295 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 297 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 298 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 299 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 300 | SourceLocation OrigEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 302 | Expr **args, unsigned nargs, |
| 303 | SourceLocation StartLoc=SourceLocation(), |
| 304 | SourceLocation EndLoc=SourceLocation()); |
| 305 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 306 | SourceLocation StartLoc=SourceLocation(), |
| 307 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 308 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 309 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 310 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 312 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 313 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 314 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 315 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 316 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 317 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 318 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 319 | void SynthGetSuperClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 320 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 321 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 323 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 324 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 325 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 327 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 328 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 330 | template<typename MethodIterator> |
| 331 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 332 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 333 | bool IsInstanceMethod, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 334 | llvm::StringRef prefix, |
| 335 | llvm::StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 336 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 338 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 339 | llvm::StringRef prefix, |
| 340 | llvm::StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 341 | std::string &Result); |
| 342 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 343 | llvm::StringRef prefix, |
| 344 | llvm::StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 345 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 346 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 347 | std::string &Result); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 348 | void SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 349 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 350 | void RewriteImplementations(); |
| 351 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 353 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 355 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 357 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 358 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
| 360 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 362 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 363 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 364 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 365 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 366 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
| 368 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 369 | llvm::StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 371 | llvm::StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 372 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 373 | std::string Tag, std::string Desc); |
| 374 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 375 | std::string ImplTag, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 376 | int i, llvm::StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 377 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 378 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 379 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 380 | llvm::StringRef FunName); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 381 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 383 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 385 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 386 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 387 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 389 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 390 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 391 | bool isTopLevelBlockPointerType(QualType T) { |
| 392 | return isa<BlockPointerType>(T); |
| 393 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 395 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 396 | /// to a function pointer type and upon success, returns true; false |
| 397 | /// otherwise. |
| 398 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 399 | if (isTopLevelBlockPointerType(T)) { |
| 400 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 401 | T = Context->getPointerType(BPT->getPointeeType()); |
| 402 | return true; |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 407 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 408 | bool isObjCType(QualType T) { |
| 409 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 410 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 412 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 414 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 415 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 416 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 418 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 420 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 426 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 427 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 428 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 429 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 431 | FunctionDecl *SynthBlockInitFunctionDecl(llvm::StringRef name); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 432 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 433 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 435 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 437 | if (From[i] == '"') |
| 438 | To += "\\\""; |
| 439 | else |
| 440 | To += From[i]; |
| 441 | } |
| 442 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 443 | }; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 444 | |
| 445 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 446 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 447 | CastKind Kind, Expr *E) { |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 448 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 449 | return CStyleCastExpr::Create(*Ctx, Ty, Kind, E, 0, TInfo, |
| 450 | SourceLocation(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 455 | NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 456 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 458 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 459 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 460 | // All the args are checked/rewritten. Don't call twice! |
| 461 | RewriteBlockPointerDecl(D); |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 468 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 469 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 470 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 473 | static bool IsHeaderFile(const std::string &Filename) { |
| 474 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 476 | if (DotPos == std::string::npos) { |
| 477 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 481 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 482 | // C header: .h |
| 483 | // C++ header: .hh or .H; |
| 484 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 486 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 487 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 488 | Diagnostic &D, const LangOptions &LOpts, |
| 489 | bool silenceMacroWarn) |
| 490 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 491 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 492 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 494 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 496 | "rewriter doesn't support user-specified control flow semantics " |
| 497 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 500 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 501 | llvm::raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 503 | const LangOptions &LOpts, |
| 504 | bool SilenceRewriteMacroWarning) { |
| 505 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 507 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 508 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 509 | Context = &context; |
| 510 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 511 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 512 | MsgSendFunctionDecl = 0; |
| 513 | MsgSendSuperFunctionDecl = 0; |
| 514 | MsgSendStretFunctionDecl = 0; |
| 515 | MsgSendSuperStretFunctionDecl = 0; |
| 516 | MsgSendFpretFunctionDecl = 0; |
| 517 | GetClassFunctionDecl = 0; |
| 518 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 519 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 520 | SelGetUidFunctionDecl = 0; |
| 521 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 522 | ConstantStringClassReference = 0; |
| 523 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 524 | CurMethodDef = 0; |
| 525 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 526 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 527 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 528 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 529 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 530 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 531 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 532 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 533 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 534 | PropParentMap = 0; |
| 535 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 536 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 537 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 539 | // Get the ID and start/end of the main file. |
| 540 | MainFileID = SM->getMainFileID(); |
| 541 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 542 | MainFileStart = MainBuf->getBufferStart(); |
| 543 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 545 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 547 | // declaring objc_selector outside the parameter list removes a silly |
| 548 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 549 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 550 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 551 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 552 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 553 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 554 | if (LangOpts.Microsoft) { |
| 555 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 556 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 557 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 558 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 559 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 560 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 561 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 562 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 563 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 564 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 565 | if (LangOpts.Microsoft) { |
| 566 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 567 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 568 | } else |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 569 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 570 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 571 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 572 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 573 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 574 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 575 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 576 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 577 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 578 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 579 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 580 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 581 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 582 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 583 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 584 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 585 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 586 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 587 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 588 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 589 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 590 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 591 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 592 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 593 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 594 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 595 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 596 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 597 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 598 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 599 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 600 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 601 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 602 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 603 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 604 | Preamble += "#endif\n"; |
| 605 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 606 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 607 | Preamble += " int *isa;\n"; |
| 608 | Preamble += " int flags;\n"; |
| 609 | Preamble += " char *str;\n"; |
| 610 | Preamble += " long length;\n"; |
| 611 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 612 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 613 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 614 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 615 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 616 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 617 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 618 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 619 | // Blocks preamble. |
| 620 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 621 | Preamble += "#define BLOCK_IMPL\n"; |
| 622 | Preamble += "struct __block_impl {\n"; |
| 623 | Preamble += " void *isa;\n"; |
| 624 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 625 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 626 | Preamble += " void *FuncPtr;\n"; |
| 627 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 628 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 629 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 630 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 631 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 632 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 633 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 634 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 635 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 636 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 637 | 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] | 638 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 639 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 640 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 641 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 642 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 643 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 644 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 645 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 646 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 647 | Preamble += "#endif\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 648 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 649 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 650 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 651 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 652 | Preamble += "#define __weak\n"; |
| 653 | } |
Fariborz Jahanian | df49652 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 654 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 655 | // as this avoids warning in any 64bit/32bit compilation model. |
| 656 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 660 | //===----------------------------------------------------------------------===// |
| 661 | // Top Level Driver Code |
| 662 | //===----------------------------------------------------------------------===// |
| 663 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 664 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 665 | if (Diags.hasErrorOccurred()) |
| 666 | return; |
| 667 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 668 | // Two cases: either the decl could be in the main file, or it could be in a |
| 669 | // #included file. If the former, rewrite it now. If the later, check to see |
| 670 | // if we rewrote the #include/#import. |
| 671 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 672 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 674 | // If this is for a builtin, ignore it. |
| 675 | if (Loc.isInvalid()) return; |
| 676 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 677 | // Look for built-in declarations that we need to refer during the rewrite. |
| 678 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 679 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 680 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 681 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 682 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 683 | ConstantStringClassReference = FVD; |
| 684 | return; |
| 685 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 686 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 687 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 688 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 689 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 690 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 691 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 693 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 694 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 695 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 696 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 697 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 698 | DIEnd = LSD->decls_end(); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 699 | DI != DIEnd; ++DI) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 700 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 701 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 702 | // 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] | 703 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 704 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
| 708 | // Syntactic (non-AST) Rewriting Code |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 711 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 712 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 713 | llvm::StringRef MainBuf = SM->getBufferData(MainFileID); |
| 714 | const char *MainBufStart = MainBuf.begin(); |
| 715 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 716 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 718 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 719 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 720 | if (*BufPtr == '#') { |
| 721 | if (++BufPtr == MainBufEnd) |
| 722 | return; |
| 723 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 724 | if (++BufPtr == MainBufEnd) |
| 725 | return; |
| 726 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 727 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | SourceLocation ImportLoc = |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 729 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 730 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 731 | BufPtr += ImportLen; |
| 732 | } |
| 733 | } |
| 734 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 737 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 738 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 739 | std::string S; |
| 740 | S = "((struct "; |
| 741 | S += ClassDecl->getIdentifier()->getName(); |
| 742 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 743 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 744 | return S; |
| 745 | } |
| 746 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 747 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 748 | ObjCImplementationDecl *IMD, |
| 749 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 750 | static bool objcGetPropertyDefined = false; |
| 751 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 752 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 753 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 754 | const char *startBuf = SM->getCharacterData(startLoc); |
| 755 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 756 | const char *semiBuf = strchr(startBuf, ';'); |
| 757 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 758 | SourceLocation onePastSemiLoc = |
| 759 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 760 | |
| 761 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 762 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 764 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 765 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 768 | if (!OID) |
| 769 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 770 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 771 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 772 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 773 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 774 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 775 | std::string Getr; |
| 776 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 777 | objcGetPropertyDefined = true; |
| 778 | // FIXME. Is this attribute correct in all cases? |
| 779 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 780 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 781 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 782 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 783 | PD->getGetterMethodDecl(), Getr); |
| 784 | Getr += "{ "; |
| 785 | // Synthesize an explicit cast to gain access to the ivar. |
| 786 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 787 | if (GenGetProperty) { |
| 788 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 789 | Getr += "typedef "; |
| 790 | const FunctionType *FPRetType = 0; |
| 791 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 792 | FPRetType); |
| 793 | Getr += " _TYPE"; |
| 794 | if (FPRetType) { |
| 795 | Getr += ")"; // close the precedence "scope" for "*". |
| 796 | |
| 797 | // Now, emit the argument types (if any). |
| 798 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 799 | Getr += "("; |
| 800 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 801 | if (i) Getr += ", "; |
| 802 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 803 | Context->PrintingPolicy); |
| 804 | Getr += ParamStr; |
| 805 | } |
| 806 | if (FT->isVariadic()) { |
| 807 | if (FT->getNumArgs()) Getr += ", "; |
| 808 | Getr += "..."; |
| 809 | } |
| 810 | Getr += ")"; |
| 811 | } else |
| 812 | Getr += "()"; |
| 813 | } |
| 814 | Getr += ";\n"; |
| 815 | Getr += "return (_TYPE)"; |
| 816 | Getr += "objc_getProperty(self, _cmd, "; |
| 817 | SynthesizeIvarOffsetComputation(OID, Getr); |
| 818 | Getr += ", 1)"; |
| 819 | } |
| 820 | else |
| 821 | Getr += "return " + getIvarAccessString(OID); |
| 822 | Getr += "; }"; |
| 823 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 824 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 825 | |
| 826 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 827 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 829 | // Generate the 'setter' function. |
| 830 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 831 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 832 | ObjCPropertyDecl::OBJC_PR_copy); |
| 833 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 834 | objcSetPropertyDefined = true; |
| 835 | // FIXME. Is this attribute correct in all cases? |
| 836 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 837 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 838 | } |
| 839 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 840 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 841 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 842 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 843 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 844 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 845 | if (GenSetProperty) { |
| 846 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 847 | SynthesizeIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 848 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 849 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 850 | Setr += ", "; |
| 851 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 852 | Setr += "0, "; |
| 853 | else |
| 854 | Setr += "1, "; |
| 855 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 856 | Setr += "1)"; |
| 857 | else |
| 858 | Setr += "0)"; |
| 859 | } |
| 860 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 861 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 862 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 863 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 864 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 865 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 866 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 867 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 868 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 869 | // Get the start location and compute the semi location. |
| 870 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 871 | const char *startBuf = SM->getCharacterData(startLoc); |
| 872 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 874 | // Translate to typedef's that forward reference structs with the same name |
| 875 | // as the class. As a convenience, we include the original declaration |
| 876 | // as a comment. |
| 877 | std::string typedefString; |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 878 | typedefString += "// @class "; |
| 879 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 880 | I != E; ++I) { |
| 881 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 882 | typedefString += ForwardDecl->getNameAsString(); |
| 883 | if (I+1 != E) |
| 884 | typedefString += ", "; |
| 885 | else |
| 886 | typedefString += ";\n"; |
| 887 | } |
| 888 | |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 889 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 890 | I != E; ++I) { |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 891 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 892 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 893 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 894 | typedefString += "\n"; |
| 895 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 896 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 897 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 898 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 899 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 900 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 901 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 903 | // Replace the @class with typedefs corresponding to the classes. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 904 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 907 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 908 | // When method is a synthesized one, such as a getter/setter there is |
| 909 | // nothing to rewrite. |
| 910 | if (Method->isSynthesized()) |
| 911 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 912 | SourceLocation LocStart = Method->getLocStart(); |
| 913 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 915 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 916 | SM->getInstantiationLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 917 | InsertText(LocStart, "#if 0\n"); |
| 918 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 919 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 920 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 925 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 927 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 928 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 931 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 932 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 934 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 935 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 937 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 938 | E = CatDecl->prop_end(); I != E; ++I) |
| 939 | RewriteProperty(*I); |
| 940 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | for (ObjCCategoryDecl::instmeth_iterator |
| 942 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 943 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 944 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 946 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 947 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 948 | RewriteMethodDeclaration(*I); |
| 949 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 950 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 951 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 952 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 955 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 956 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 958 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 959 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
| 961 | for (ObjCProtocolDecl::instmeth_iterator |
| 962 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 963 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 964 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 965 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 966 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 967 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 968 | RewriteMethodDeclaration(*I); |
| 969 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 970 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 971 | E = PDecl->prop_end(); I != E; ++I) |
| 972 | RewriteProperty(*I); |
| 973 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 974 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 975 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 976 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 977 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 978 | // Must comment out @optional/@required |
| 979 | const char *startBuf = SM->getCharacterData(LocStart); |
| 980 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 981 | for (const char *p = startBuf; p < endBuf; p++) { |
| 982 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 983 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 984 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 986 | } |
| 987 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 988 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 989 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 995 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 996 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 997 | if (LocStart.isInvalid()) |
| 998 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 999 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1000 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1003 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1004 | const FunctionType *&FPRetType) { |
| 1005 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1006 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1007 | else if (T->isFunctionPointerType() || |
| 1008 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1009 | // needs special handling, since pointer-to-functions have special |
| 1010 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1011 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1012 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1013 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1014 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1015 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1016 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1017 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1018 | ResultStr += FPRetType->getResultType().getAsString( |
| 1019 | Context->PrintingPolicy); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1020 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1023 | ResultStr += T.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1026 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1027 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1028 | std::string &ResultStr) { |
| 1029 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1030 | const FunctionType *FPRetType = 0; |
| 1031 | ResultStr += "\nstatic "; |
| 1032 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1033 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1035 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1036 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1038 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1039 | NameStr += "_I_"; |
| 1040 | else |
| 1041 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1043 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1044 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1047 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1048 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1049 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1052 | { |
| 1053 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1054 | int len = selString.size(); |
| 1055 | for (int i = 0; i < len; i++) |
| 1056 | if (selString[i] == ':') |
| 1057 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1058 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1059 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1060 | // Remember this name for metadata emission |
| 1061 | MethodInternalNames[OMD] = NameStr; |
| 1062 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1064 | // Rewrite arguments |
| 1065 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1067 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1068 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1069 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1070 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1071 | if (!LangOpts.Microsoft) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1072 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1073 | ResultStr += "struct "; |
| 1074 | } |
| 1075 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1076 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1077 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1078 | } |
| 1079 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1080 | ResultStr += Context->getObjCClassType().getAsString( |
| 1081 | Context->PrintingPolicy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1083 | ResultStr += " self, "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1084 | ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1085 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1087 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1088 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1089 | E = OMD->param_end(); PI != E; ++PI) { |
| 1090 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1091 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1092 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1093 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1094 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1095 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1096 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1097 | QualType QT = PDecl->getType(); |
| 1098 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1099 | if (convertBlockPointerToFunctionPointer(QT)) |
| 1100 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
| 1101 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1102 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1103 | ResultStr += Name; |
| 1104 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1105 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1106 | if (OMD->isVariadic()) |
| 1107 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1108 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1110 | if (FPRetType) { |
| 1111 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1113 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1114 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1115 | ResultStr += "("; |
| 1116 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1117 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1118 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1119 | Context->PrintingPolicy); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1120 | ResultStr += ParamStr; |
| 1121 | } |
| 1122 | if (FT->isVariadic()) { |
| 1123 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1124 | ResultStr += "..."; |
| 1125 | } |
| 1126 | ResultStr += ")"; |
| 1127 | } else { |
| 1128 | ResultStr += "()"; |
| 1129 | } |
| 1130 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1131 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1132 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1133 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1134 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1136 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1138 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1139 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1140 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1141 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1142 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1143 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1144 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1145 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1146 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1147 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1148 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1149 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1150 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1153 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1154 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1155 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1156 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1157 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1158 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1159 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1160 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1161 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1163 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1164 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1165 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1166 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1167 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1168 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1170 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1171 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1174 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1177 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1178 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1179 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1180 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1181 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1182 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1183 | ResultStr += "\n"; |
| 1184 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1185 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1186 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1187 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1188 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1189 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1190 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1191 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1192 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1193 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
| 1195 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1196 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1197 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1199 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1200 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1201 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1203 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1204 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1205 | RewriteMethodDeclaration(*I); |
| 1206 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1207 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1208 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1209 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1212 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1213 | SourceRange SrcRange) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1214 | ObjCMethodDecl *OMD = 0; |
| 1215 | QualType Ty; |
| 1216 | Selector Sel; |
Duncan Sands | 54bd457 | 2010-10-20 08:21:16 +0000 | [diff] [blame] | 1217 | Stmt *Receiver = 0; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1218 | bool Super = false; |
| 1219 | QualType SuperTy; |
| 1220 | SourceLocation SuperLocation; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1221 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ObjCImplicitSetterGetterRefExpr. |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1222 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1223 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) { |
| 1224 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1225 | OMD = PDecl->getSetterMethodDecl(); |
| 1226 | Ty = PDecl->getType(); |
| 1227 | Sel = PDecl->getSetterName(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1228 | Super = PropRefExpr->isSuperReceiver(); |
| 1229 | if (!Super) |
| 1230 | Receiver = PropRefExpr->getBase(); |
| 1231 | else { |
| 1232 | SuperTy = PropRefExpr->getSuperType(); |
| 1233 | SuperLocation = PropRefExpr->getSuperLocation(); |
| 1234 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1235 | } |
| 1236 | else if (ObjCImplicitSetterGetterRefExpr *ImplicitRefExpr = |
| 1237 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(BinOp->getLHS())) { |
| 1238 | OMD = ImplicitRefExpr->getSetterMethod(); |
| 1239 | Sel = OMD->getSelector(); |
| 1240 | Ty = ImplicitRefExpr->getType(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1241 | Super = ImplicitRefExpr->isSuperReceiver(); |
| 1242 | if (!Super) |
| 1243 | Receiver = ImplicitRefExpr->getBase(); |
| 1244 | else { |
| 1245 | SuperTy = ImplicitRefExpr->getSuperType(); |
| 1246 | SuperLocation = ImplicitRefExpr->getSuperLocation(); |
| 1247 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | assert(OMD && "RewritePropertyOrImplicitSetter - null OMD"); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1251 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1252 | ExprVec.push_back(newStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1254 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1255 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1256 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1257 | Ty.getNonReferenceType(), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1258 | /*FIXME?*/SourceLocation(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1259 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1260 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1261 | SuperTy, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1262 | Sel, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1263 | &ExprVec[0], 1, |
| 1264 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1265 | else { |
| 1266 | // FIXME. Refactor this into common code with that in |
| 1267 | // RewritePropertyOrImplicitGetter |
| 1268 | assert(Receiver && "RewritePropertyOrImplicitSetter - null Receiver"); |
| 1269 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1270 | if (PropGetters[Exp]) |
| 1271 | // This allows us to handle chain/nested property/implicit getters. |
| 1272 | Receiver = PropGetters[Exp]; |
| 1273 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1274 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1275 | Ty.getNonReferenceType(), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1276 | /*FIXME: */SourceLocation(), |
| 1277 | cast<Expr>(Receiver), |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1278 | Sel, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1279 | &ExprVec[0], 1, |
| 1280 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1281 | } |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1282 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1284 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1285 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1286 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1287 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1288 | // to things that stay around. |
| 1289 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1290 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1293 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr) { |
| 1294 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ImplicitGetter. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1295 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
Ted Kremenek | 3b562af | 2010-10-19 23:10:22 +0000 | [diff] [blame] | 1296 | Stmt *Receiver = 0; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1297 | ObjCMethodDecl *OMD = 0; |
| 1298 | QualType Ty; |
| 1299 | Selector Sel; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1300 | bool Super = false; |
| 1301 | QualType SuperTy; |
| 1302 | SourceLocation SuperLocation; |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1303 | if (ObjCPropertyRefExpr *PropRefExpr = |
| 1304 | dyn_cast<ObjCPropertyRefExpr>(PropOrGetterRefExpr)) { |
| 1305 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1306 | OMD = PDecl->getGetterMethodDecl(); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1307 | Ty = PDecl->getType(); |
| 1308 | Sel = PDecl->getGetterName(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1309 | Super = PropRefExpr->isSuperReceiver(); |
| 1310 | if (!Super) |
| 1311 | Receiver = PropRefExpr->getBase(); |
| 1312 | else { |
| 1313 | SuperTy = PropRefExpr->getSuperType(); |
| 1314 | SuperLocation = PropRefExpr->getSuperLocation(); |
| 1315 | } |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1316 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1317 | else if (ObjCImplicitSetterGetterRefExpr *ImplicitRefExpr = |
| 1318 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(PropOrGetterRefExpr)) { |
| 1319 | OMD = ImplicitRefExpr->getGetterMethod(); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1320 | Sel = OMD->getSelector(); |
| 1321 | Ty = ImplicitRefExpr->getType(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1322 | Super = ImplicitRefExpr->isSuperReceiver(); |
| 1323 | if (!Super) |
| 1324 | Receiver = ImplicitRefExpr->getBase(); |
| 1325 | else { |
| 1326 | SuperTy = ImplicitRefExpr->getSuperType(); |
| 1327 | SuperLocation = ImplicitRefExpr->getSuperLocation(); |
| 1328 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | assert (OMD && "RewritePropertyOrImplicitGetter - OMD is null"); |
| 1332 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1333 | ObjCMessageExpr *MsgExpr; |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1334 | if (Super) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1335 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1336 | Ty.getNonReferenceType(), |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1337 | /*FIXME?*/SourceLocation(), |
| 1338 | SuperLocation, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1339 | /*IsInstanceSuper=*/true, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1340 | SuperTy, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1341 | Sel, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1342 | 0, 0, |
| 1343 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1344 | else { |
| 1345 | assert (Receiver && "RewritePropertyOrImplicitGetter - Receiver is null"); |
| 1346 | if (Expr *Exp = dyn_cast<Expr>(Receiver)) |
| 1347 | if (PropGetters[Exp]) |
| 1348 | // This allows us to handle chain/nested property/implicit getters. |
| 1349 | Receiver = PropGetters[Exp]; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1350 | MsgExpr = ObjCMessageExpr::Create(*Context, |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1351 | Ty.getNonReferenceType(), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1352 | /*FIXME:*/SourceLocation(), |
| 1353 | cast<Expr>(Receiver), |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1354 | Sel, OMD, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1355 | 0, 0, |
| 1356 | /*FIXME:*/SourceLocation()); |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1357 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1358 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1359 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (!PropParentMap) |
| 1362 | PropParentMap = new ParentMap(CurrentBody); |
| 1363 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1364 | Stmt *Parent = PropParentMap->getParent(PropOrGetterRefExpr); |
| 1365 | if (Parent && (isa<ObjCPropertyRefExpr>(Parent) || |
| 1366 | isa<ObjCImplicitSetterGetterRefExpr>(Parent))) { |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1367 | // We stash away the ReplacingStmt since actually doing the |
| 1368 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1369 | PropGetters[PropOrGetterRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1370 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1371 | // to things that stay around. |
| 1372 | Context->Deallocate(MsgExpr); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1373 | return PropOrGetterRefExpr; // return the original... |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1374 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1375 | ReplaceStmt(PropOrGetterRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1377 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1378 | // to things that stay around. |
| 1379 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1380 | return ReplacingStmt; |
| 1381 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1385 | SourceLocation OrigStart, |
| 1386 | bool &replaced) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1387 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1388 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1389 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1390 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1391 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1392 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1393 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1394 | // lookup which class implements the instance variable. |
| 1395 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1397 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1398 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1400 | // Synthesize an explicit cast to gain access to the ivar. |
| 1401 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1402 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1403 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1404 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1405 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1406 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1407 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1408 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1409 | CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1410 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1411 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1412 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1413 | IV->getBase()->getLocEnd(), |
| 1414 | castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1415 | replaced = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1417 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1418 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1419 | IV->getLocation(), |
| 1420 | D->getType()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1421 | // delete IV; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1422 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1423 | } |
Fariborz Jahanian | 7e20ffe | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1424 | // Get the new text |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1425 | // Cannot delete IV->getBase(), since PE points to it. |
| 1426 | // Replace the old base with the cast. This is important when doing |
| 1427 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1429 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1430 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1431 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1432 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1434 | // Explicit ivar refs need to have a cast inserted. |
| 1435 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1436 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1437 | ObjCInterfaceType *iFaceDecl = |
| 1438 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1439 | // lookup which class implements the instance variable. |
| 1440 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1442 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1443 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1445 | // Synthesize an explicit cast to gain access to the ivar. |
| 1446 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1447 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1448 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1449 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1450 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1451 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1452 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1453 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1454 | CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1455 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1456 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1457 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1458 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1459 | replaced = true; |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1460 | // Cannot delete IV->getBase(), since PE points to it. |
| 1461 | // Replace the old base with the cast. This is important when doing |
| 1462 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1464 | return IV; |
| 1465 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1466 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1467 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1470 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1471 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1472 | CI != E; ++CI) { |
| 1473 | if (*CI) { |
| 1474 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1475 | if (newStmt) |
| 1476 | *CI = newStmt; |
| 1477 | } |
| 1478 | } |
| 1479 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1480 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1481 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1482 | replaced); |
| 1483 | return newStmt; |
Fariborz Jahanian | 376338a | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1484 | } |
| 1485 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1486 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1487 | return newStmt; |
| 1488 | } |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1489 | return S; |
| 1490 | } |
| 1491 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1492 | /// SynthCountByEnumWithState - To print: |
| 1493 | /// ((unsigned int (*) |
| 1494 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1496 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | /// "countByEnumeratingWithState:objects:count:"), |
| 1498 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1499 | /// (id *)items, (unsigned int)16) |
| 1500 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1501 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1502 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1503 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1504 | buf += "\n\t\t"; |
| 1505 | buf += "((id)l_collection,\n\t\t"; |
| 1506 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1507 | buf += "\n\t\t"; |
| 1508 | buf += "&enumState, " |
| 1509 | "(id *)items, (unsigned int)16)"; |
| 1510 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1511 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1512 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1513 | /// statement to exit to its outer synthesized loop. |
| 1514 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1515 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1516 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1517 | return S; |
| 1518 | // replace break with goto __break_label |
| 1519 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1521 | SourceLocation startLoc = S->getLocStart(); |
| 1522 | buf = "goto __break_label_"; |
| 1523 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1524 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1530 | /// statement to continue with its inner synthesized loop. |
| 1531 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1532 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1533 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1534 | return S; |
| 1535 | // replace continue with goto __continue_label |
| 1536 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1538 | SourceLocation startLoc = S->getLocStart(); |
| 1539 | buf = "goto __continue_label_"; |
| 1540 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1541 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1543 | return 0; |
| 1544 | } |
| 1545 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1546 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1547 | /// It rewrites: |
| 1548 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1550 | /// Into: |
| 1551 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1553 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1554 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1555 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1557 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1558 | /// if (limit) { |
| 1559 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1560 | /// do { |
| 1561 | /// unsigned long counter = 0; |
| 1562 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1564 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1565 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1566 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1567 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1568 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1570 | /// objects:items count:16]); |
| 1571 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1572 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1573 | /// } |
| 1574 | /// else |
| 1575 | /// elem = nil; |
| 1576 | /// } |
| 1577 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1578 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1579 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1580 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1582 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1584 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1586 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1587 | const char *startBuf = SM->getCharacterData(startLoc); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1588 | llvm::StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1589 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1590 | std::string buf; |
| 1591 | buf = "\n{\n\t"; |
| 1592 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1593 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1594 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1595 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1596 | if (ElementType->isObjCQualifiedIdType() || |
| 1597 | ElementType->isObjCQualifiedInterfaceType()) |
| 1598 | // Simply use 'id' for all qualified types. |
| 1599 | elementTypeAsString = "id"; |
| 1600 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1601 | elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1602 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1603 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1604 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1605 | buf += elementName; |
| 1606 | buf += ";\n\t"; |
| 1607 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1608 | else { |
| 1609 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1610 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1611 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1612 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1613 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1614 | // Simply use 'id' for all qualified types. |
| 1615 | elementTypeAsString = "id"; |
| 1616 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1617 | elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1620 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1621 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1622 | // id items[16]; |
| 1623 | buf += "id items[16];\n\t"; |
| 1624 | // id l_collection = (id) |
| 1625 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1626 | // Find start location of 'collection' the hard way! |
| 1627 | const char *startCollectionBuf = startBuf; |
| 1628 | startCollectionBuf += 3; // skip 'for' |
| 1629 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1630 | startCollectionBuf++; // skip '(' |
| 1631 | // find 'in' and skip it. |
| 1632 | while (*startCollectionBuf != ' ' || |
| 1633 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1634 | (*(startCollectionBuf+3) != ' ' && |
| 1635 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1636 | startCollectionBuf++; |
| 1637 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
| 1639 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1640 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1641 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1642 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1643 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1644 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1645 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1647 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1648 | // objects:items count:16]; |
| 1649 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1651 | // ((unsigned int (*) |
| 1652 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1654 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | // "countByEnumeratingWithState:objects:count:"), |
| 1656 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1657 | // (id *)items, (unsigned int)16); |
| 1658 | buf += "unsigned long limit =\n\t\t"; |
| 1659 | SynthCountByEnumWithState(buf); |
| 1660 | buf += ";\n\t"; |
| 1661 | /// if (limit) { |
| 1662 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1663 | /// do { |
| 1664 | /// unsigned long counter = 0; |
| 1665 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1667 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1668 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1669 | buf += "if (limit) {\n\t"; |
| 1670 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1671 | buf += "do {\n\t\t"; |
| 1672 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1673 | buf += "do {\n\t\t\t"; |
| 1674 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1675 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1676 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1677 | buf += " = ("; |
| 1678 | buf += elementTypeAsString; |
| 1679 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1680 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1681 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1683 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1684 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1686 | /// objects:items count:16]); |
| 1687 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1688 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1689 | /// } |
| 1690 | /// else |
| 1691 | /// elem = nil; |
| 1692 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1694 | buf = ";\n\t"; |
| 1695 | buf += "__continue_label_"; |
| 1696 | buf += utostr(ObjCBcLabelNo.back()); |
| 1697 | buf += ": ;"; |
| 1698 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1699 | buf += "} while (counter < limit);\n\t"; |
| 1700 | buf += "} while (limit = "; |
| 1701 | SynthCountByEnumWithState(buf); |
| 1702 | buf += ");\n\t"; |
| 1703 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1704 | buf += " = (("; |
| 1705 | buf += elementTypeAsString; |
| 1706 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1707 | buf += "__break_label_"; |
| 1708 | buf += utostr(ObjCBcLabelNo.back()); |
| 1709 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1710 | buf += "}\n\t"; |
| 1711 | buf += "else\n\t\t"; |
| 1712 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1713 | buf += " = (("; |
| 1714 | buf += elementTypeAsString; |
| 1715 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1716 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1718 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1719 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1720 | if (isa<CompoundStmt>(S->getBody())) { |
| 1721 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1722 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1723 | } else { |
| 1724 | /* Need to treat single statements specially. For example: |
| 1725 | * |
| 1726 | * for (A *a in b) if (stuff()) break; |
| 1727 | * for (A *a in b) xxxyy; |
| 1728 | * |
| 1729 | * The following code simply scans ahead to the semi to find the actual end. |
| 1730 | */ |
| 1731 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1732 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1733 | assert(semiBuf && "Can't find ';'"); |
| 1734 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1735 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1736 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1737 | Stmts.pop_back(); |
| 1738 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1739 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1743 | /// This routine rewrites @synchronized(expr) stmt; |
| 1744 | /// into: |
| 1745 | /// objc_sync_enter(expr); |
| 1746 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1747 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1748 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1749 | // Get the start location and compute the semi location. |
| 1750 | SourceLocation startLoc = S->getLocStart(); |
| 1751 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1753 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
| 1755 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1756 | buf = "objc_sync_enter((id)"; |
| 1757 | const char *lparenBuf = startBuf; |
| 1758 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1759 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1761 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1762 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1763 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1764 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1765 | while (*endBuf != ')') endBuf--; |
| 1766 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1767 | buf = ");\n"; |
| 1768 | // declare a new scope with two variables, _stack and _rethrow. |
| 1769 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1770 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1771 | buf += "char *pointers[4];} _stack;\n"; |
| 1772 | buf += "id volatile _rethrow = 0;\n"; |
| 1773 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1774 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1775 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1776 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1777 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1779 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1780 | SourceLocation lastCurlyLoc = startLoc; |
| 1781 | buf = "}\nelse {\n"; |
| 1782 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1783 | buf += "}\n"; |
| 1784 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1785 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1786 | |
| 1787 | std::string syncBuf; |
| 1788 | syncBuf += " objc_sync_exit("; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1789 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1790 | CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1791 | S->getSynchExpr()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1792 | std::string syncExprBufS; |
| 1793 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1794 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1795 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1796 | syncBuf += syncExprBuf.str(); |
| 1797 | syncBuf += ");"; |
| 1798 | |
| 1799 | buf += syncBuf; |
| 1800 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1801 | buf += "}\n"; |
| 1802 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1804 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1805 | |
| 1806 | bool hasReturns = false; |
| 1807 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1808 | if (hasReturns) |
| 1809 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1810 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1814 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1815 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1816 | // Perform a bottom up traversal of all children. |
| 1817 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1818 | CI != E; ++CI) |
| 1819 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1820 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1821 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1822 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1824 | TryFinallyContainsReturnDiag); |
| 1825 | } |
| 1826 | return; |
| 1827 | } |
| 1828 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1829 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1830 | { |
| 1831 | // Perform a bottom up traversal of all children. |
| 1832 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1833 | CI != E; ++CI) |
| 1834 | if (*CI) |
| 1835 | HasReturnStmts(*CI, hasReturns); |
| 1836 | |
| 1837 | if (isa<ReturnStmt>(S)) |
| 1838 | hasReturns = true; |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1843 | // Perform a bottom up traversal of all children. |
| 1844 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1845 | CI != E; ++CI) |
| 1846 | if (*CI) { |
| 1847 | RewriteTryReturnStmts(*CI); |
| 1848 | } |
| 1849 | if (isa<ReturnStmt>(S)) { |
| 1850 | SourceLocation startLoc = S->getLocStart(); |
| 1851 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1852 | |
| 1853 | const char *semiBuf = strchr(startBuf, ';'); |
| 1854 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1855 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1856 | |
| 1857 | std::string buf; |
| 1858 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1859 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1860 | ReplaceText(startLoc, 6, buf); |
| 1861 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1862 | } |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1867 | // Perform a bottom up traversal of all children. |
| 1868 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1869 | CI != E; ++CI) |
| 1870 | if (*CI) { |
| 1871 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1872 | } |
| 1873 | if (isa<ReturnStmt>(S)) { |
| 1874 | SourceLocation startLoc = S->getLocStart(); |
| 1875 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1876 | |
| 1877 | const char *semiBuf = strchr(startBuf, ';'); |
| 1878 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1879 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1880 | |
| 1881 | std::string buf; |
| 1882 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1883 | buf += syncExitBuf; |
| 1884 | buf += " return"; |
| 1885 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1886 | ReplaceText(startLoc, 6, buf); |
| 1887 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1888 | } |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1892 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1893 | // Get the start location and compute the semi location. |
| 1894 | SourceLocation startLoc = S->getLocStart(); |
| 1895 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1897 | assert((*startBuf == '@') && "bogus @try location"); |
| 1898 | |
| 1899 | std::string buf; |
| 1900 | // declare a new scope with two variables, _stack and _rethrow. |
| 1901 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1902 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1903 | buf += "char *pointers[4];} _stack;\n"; |
| 1904 | buf += "id volatile _rethrow = 0;\n"; |
| 1905 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1906 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1907 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1908 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1910 | startLoc = S->getTryBody()->getLocEnd(); |
| 1911 | startBuf = SM->getCharacterData(startLoc); |
| 1912 | |
| 1913 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1915 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1916 | if (S->getNumCatchStmts()) { |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1917 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1918 | buf = " /* @catch begin */ else {\n"; |
| 1919 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1920 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1921 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1922 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1923 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1925 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1926 | } else { /* no catch list */ |
| 1927 | buf = "}\nelse {\n"; |
| 1928 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1929 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1930 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1931 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1932 | bool sawIdTypedCatch = false; |
| 1933 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1934 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1935 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1936 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1938 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1939 | buf = "if ("; // we are generating code for the first catch clause |
| 1940 | else |
| 1941 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1942 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1943 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1945 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1947 | const char *lParenLoc = strchr(startBuf, '('); |
| 1948 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1949 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1950 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1951 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1952 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1953 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1954 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1955 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1956 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1958 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1959 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1960 | } else if (catchDecl) { |
| 1961 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1962 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1963 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1964 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1965 | sawIdTypedCatch = true; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1966 | } else if (const ObjCObjectPointerType *Ptr = |
| 1967 | t->getAs<ObjCObjectPointerType>()) { |
| 1968 | // Should be a pointer to a class. |
| 1969 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1970 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1971 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1972 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1973 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1974 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1978 | lastCatchBody = Catch->getCatchBody(); |
| 1979 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1980 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1981 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1982 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1983 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1984 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1987 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1988 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1989 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1990 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1991 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1992 | } |
| 1993 | // Complete the catch list... |
| 1994 | if (lastCatchBody) { |
| 1995 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1996 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1997 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1999 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 2000 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 2001 | buf = "} /* last catch end */\n"; |
| 2002 | buf += "else {\n"; |
| 2003 | buf += " _rethrow = _caught;\n"; |
| 2004 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 2005 | buf += "} } /* @catch end */\n"; |
| 2006 | if (!S->getFinallyStmt()) |
| 2007 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2008 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2010 | // Set lastCurlyLoc |
| 2011 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 2012 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2013 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2014 | startLoc = finalStmt->getLocStart(); |
| 2015 | startBuf = SM->getCharacterData(startLoc); |
| 2016 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2018 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2020 | Stmt *body = finalStmt->getFinallyBody(); |
| 2021 | SourceLocation startLoc = body->getLocStart(); |
| 2022 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2023 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 2024 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2026 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2028 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2029 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2030 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2031 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2033 | // Set lastCurlyLoc |
| 2034 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 2036 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2037 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2038 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 2039 | buf = "{ /* implicit finally clause */\n"; |
| 2040 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 2041 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 2042 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2043 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Now check for any return/continue/go statements within the @try. |
| 2046 | // The implicit finally clause won't called if the @try contains any |
| 2047 | // jump statements. |
| 2048 | bool hasReturns = false; |
| 2049 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2050 | if (hasReturns) |
| 2051 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2052 | } |
| 2053 | // Now emit the final closing curly brace... |
| 2054 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2055 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2056 | return 0; |
| 2057 | } |
| 2058 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2060 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2061 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2062 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2063 | // Get the start location and compute the semi location. |
| 2064 | SourceLocation startLoc = S->getLocStart(); |
| 2065 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2067 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2068 | |
| 2069 | std::string buf; |
| 2070 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2071 | if (S->getThrowExpr()) |
| 2072 | buf = "objc_exception_throw("; |
| 2073 | else // add an implicit argument |
| 2074 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2076 | // handle "@ throw" correctly. |
| 2077 | const char *wBuf = strchr(startBuf, 'w'); |
| 2078 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2079 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2081 | const char *semiBuf = strchr(startBuf, ';'); |
| 2082 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2083 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2084 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2085 | return 0; |
| 2086 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2087 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2088 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2089 | // Create a new string expression. |
| 2090 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2091 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2092 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2093 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 2094 | StrEncoding.length(), false,StrType, |
| 2095 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2096 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2098 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2099 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2100 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2103 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2104 | if (!SelGetUidFunctionDecl) |
| 2105 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2106 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2107 | // Create a call to sel_registerName("selName"). |
| 2108 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2109 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2111 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2112 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2113 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2114 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2115 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2116 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2117 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2118 | return SelExp; |
| 2119 | } |
| 2120 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2121 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2122 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2123 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2124 | // 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] | 2125 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2127 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2128 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2130 | // 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] | 2131 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2132 | ImplicitCastExpr *ICE = |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2133 | ImplicitCastExpr::Create(*Context, pToFunc, CK_Unknown, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2134 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2136 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2138 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2139 | new (Context) CallExpr(*Context, ICE, args, nargs, |
| 2140 | FT->getCallResultType(*Context), EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2141 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2144 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2145 | const char *&startRef, const char *&endRef) { |
| 2146 | while (startBuf < endBuf) { |
| 2147 | if (*startBuf == '<') |
| 2148 | startRef = startBuf; // mark the start. |
| 2149 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2150 | if (startRef && *startRef == '<') { |
| 2151 | endRef = startBuf; // mark the end. |
| 2152 | return true; |
| 2153 | } |
| 2154 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2155 | } |
| 2156 | startBuf++; |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2161 | static void scanToNextArgument(const char *&argRef) { |
| 2162 | int angle = 0; |
| 2163 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2164 | if (*argRef == '<') |
| 2165 | angle++; |
| 2166 | else if (*argRef == '>') |
| 2167 | angle--; |
| 2168 | argRef++; |
| 2169 | } |
| 2170 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2171 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2172 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2173 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2174 | if (T->isObjCQualifiedIdType()) |
| 2175 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2176 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2177 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2178 | return true; |
| 2179 | } |
| 2180 | if (T->isObjCObjectPointerType()) { |
| 2181 | T = T->getPointeeType(); |
| 2182 | return T->isObjCQualifiedInterfaceType(); |
| 2183 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2184 | if (T->isArrayType()) { |
| 2185 | QualType ElemTy = Context->getBaseElementType(T); |
| 2186 | return needToScanForQualifiers(ElemTy); |
| 2187 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2188 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2191 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2192 | QualType Type = E->getType(); |
| 2193 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2194 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2196 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2197 | Loc = ECE->getLParenLoc(); |
| 2198 | EndLoc = ECE->getRParenLoc(); |
| 2199 | } else { |
| 2200 | Loc = E->getLocStart(); |
| 2201 | EndLoc = E->getLocEnd(); |
| 2202 | } |
| 2203 | // This will defend against trying to rewrite synthesized expressions. |
| 2204 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2205 | return; |
| 2206 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2207 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2208 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2209 | const char *startRef = 0, *endRef = 0; |
| 2210 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2211 | // Get the locations of the startRef, endRef. |
| 2212 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2213 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2214 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2215 | InsertText(LessLoc, "/*"); |
| 2216 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2221 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2222 | SourceLocation Loc; |
| 2223 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2224 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2225 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2226 | Loc = VD->getLocation(); |
| 2227 | Type = VD->getType(); |
| 2228 | } |
| 2229 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2230 | Loc = FD->getLocation(); |
| 2231 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2232 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2233 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2234 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2235 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2236 | if (!proto) |
| 2237 | return; |
| 2238 | Type = proto->getResultType(); |
| 2239 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2240 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2241 | Loc = FD->getLocation(); |
| 2242 | Type = FD->getType(); |
| 2243 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2244 | else |
| 2245 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2247 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2248 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2250 | const char *endBuf = SM->getCharacterData(Loc); |
| 2251 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2252 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2253 | startBuf--; // scan backward (from the decl location) for return type. |
| 2254 | const char *startRef = 0, *endRef = 0; |
| 2255 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2256 | // Get the locations of the startRef, endRef. |
| 2257 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2258 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2259 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2260 | InsertText(LessLoc, "/*"); |
| 2261 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2264 | if (!proto) |
| 2265 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2266 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2267 | const char *startBuf = SM->getCharacterData(Loc); |
| 2268 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2269 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2270 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2271 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2273 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2274 | // scan forward (from the decl location) for argument types. |
| 2275 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2276 | const char *startRef = 0, *endRef = 0; |
| 2277 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2278 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | SourceLocation LessLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2280 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2282 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2283 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2284 | InsertText(LessLoc, "/*"); |
| 2285 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2286 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2287 | startBuf = ++endBuf; |
| 2288 | } |
| 2289 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2290 | // If the function name is derived from a macro expansion, then the |
| 2291 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2292 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2293 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2294 | startBuf++; |
| 2295 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2296 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2299 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2300 | QualType QT = ND->getType(); |
| 2301 | const Type* TypePtr = QT->getAs<Type>(); |
| 2302 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2303 | return; |
| 2304 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2305 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2306 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2307 | TypePtr = QT->getAs<Type>(); |
| 2308 | } |
| 2309 | // FIXME. This will not work for multiple declarators; as in: |
| 2310 | // __typeof__(a) b,c,d; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2311 | std::string TypeAsString(QT.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2312 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2313 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2314 | if (ND->getInit()) { |
| 2315 | std::string Name(ND->getNameAsString()); |
| 2316 | TypeAsString += " " + Name + " = "; |
| 2317 | Expr *E = ND->getInit(); |
| 2318 | SourceLocation startLoc; |
| 2319 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2320 | startLoc = ECE->getLParenLoc(); |
| 2321 | else |
| 2322 | startLoc = E->getLocStart(); |
| 2323 | startLoc = SM->getInstantiationLoc(startLoc); |
| 2324 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2325 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2326 | } |
| 2327 | else { |
| 2328 | SourceLocation X = ND->getLocEnd(); |
| 2329 | X = SM->getInstantiationLoc(X); |
| 2330 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2331 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2335 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2336 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2337 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2338 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2339 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2340 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2341 | &ArgTys[0], ArgTys.size(), |
| 2342 | false /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2343 | false, false, 0, 0, |
| 2344 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2345 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2347 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2348 | SC_Extern, |
| 2349 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2352 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2353 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2354 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2355 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2356 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2357 | return; |
| 2358 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2359 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2362 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2363 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2364 | const char *argPtr = TypeString.c_str(); |
| 2365 | if (!strchr(argPtr, '^')) { |
| 2366 | Str += TypeString; |
| 2367 | return; |
| 2368 | } |
| 2369 | while (*argPtr) { |
| 2370 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2371 | argPtr++; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2375 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2376 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2377 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2378 | QualType Type = VD->getType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2379 | std::string TypeString(Type.getAsString(Context->PrintingPolicy)); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2380 | const char *argPtr = TypeString.c_str(); |
| 2381 | int paren = 0; |
| 2382 | while (*argPtr) { |
| 2383 | switch (*argPtr) { |
| 2384 | case '(': |
| 2385 | Str += *argPtr; |
| 2386 | paren++; |
| 2387 | break; |
| 2388 | case ')': |
| 2389 | Str += *argPtr; |
| 2390 | paren--; |
| 2391 | break; |
| 2392 | case '^': |
| 2393 | Str += '*'; |
| 2394 | if (paren == 1) |
| 2395 | Str += VD->getNameAsString(); |
| 2396 | break; |
| 2397 | default: |
| 2398 | Str += *argPtr; |
| 2399 | break; |
| 2400 | } |
| 2401 | argPtr++; |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2406 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2407 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2408 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2409 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2410 | if (!proto) |
| 2411 | return; |
| 2412 | QualType Type = proto->getResultType(); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2413 | std::string FdStr = Type.getAsString(Context->PrintingPolicy); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2414 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2415 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2416 | FdStr += "("; |
| 2417 | unsigned numArgs = proto->getNumArgs(); |
| 2418 | for (unsigned i = 0; i < numArgs; i++) { |
| 2419 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2420 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2421 | if (i+1 < numArgs) |
| 2422 | FdStr += ", "; |
| 2423 | } |
| 2424 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2425 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2426 | CurFunctionDeclToDeclareForBlock = 0; |
| 2427 | } |
| 2428 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2429 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2430 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2431 | if (SuperContructorFunctionDecl) |
| 2432 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2433 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2434 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2435 | QualType argT = Context->getObjCIdType(); |
| 2436 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2437 | ArgTys.push_back(argT); |
| 2438 | ArgTys.push_back(argT); |
| 2439 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2440 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2441 | false, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2442 | false, false, 0, 0, |
| 2443 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2444 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2446 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2447 | SC_Extern, |
| 2448 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2451 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2452 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2453 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2454 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2455 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2456 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2457 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2458 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2459 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2460 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2461 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2462 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2463 | true /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2464 | false, false, 0, 0, |
| 2465 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2466 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2467 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2468 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2469 | SC_Extern, |
| 2470 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2473 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2474 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2475 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2476 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2477 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2478 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2479 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2480 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2481 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2482 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2483 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2484 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2485 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2486 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2487 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2488 | true /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2489 | false, false, 0, 0, |
| 2490 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2491 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2493 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2494 | SC_Extern, |
| 2495 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2498 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2499 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2500 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2501 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2502 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2503 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2504 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2505 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2506 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2507 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2508 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2509 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2510 | true /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2511 | false, false, 0, 0, |
| 2512 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2513 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2515 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2516 | SC_Extern, |
| 2517 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2521 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2522 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2524 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2525 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2526 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2527 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2528 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2529 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2530 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2531 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2532 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2533 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2534 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2535 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2536 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2537 | true /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2538 | false, false, 0, 0, |
| 2539 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2540 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2542 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2543 | SC_Extern, |
| 2544 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2547 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2548 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2549 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2550 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2551 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2552 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2553 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2554 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2555 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2556 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2557 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2558 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2559 | true /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2560 | false, false, 0, 0, |
| 2561 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2562 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2564 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2565 | SC_Extern, |
| 2566 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2569 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2570 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2571 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2572 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2573 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2574 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2575 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2576 | false /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2577 | false, false, 0, 0, |
| 2578 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2579 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2581 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2582 | SC_Extern, |
| 2583 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2586 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2587 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2588 | IdentifierInfo *getSuperClassIdent = |
| 2589 | &Context->Idents.get("class_getSuperclass"); |
| 2590 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2591 | ArgTys.push_back(Context->getObjCClassType()); |
| 2592 | QualType getClassType = Context->getFunctionType(Context->getObjCClassType(), |
| 2593 | &ArgTys[0], ArgTys.size(), |
| 2594 | false /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2595 | false, false, 0, 0, |
| 2596 | FunctionType::ExtInfo()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2597 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2598 | SourceLocation(), |
| 2599 | getSuperClassIdent, |
| 2600 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2601 | SC_Extern, |
| 2602 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2603 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2606 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2607 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2608 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2609 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2610 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2611 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2612 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2613 | false /*isVariadic*/, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2614 | false, false, 0, 0, |
| 2615 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2616 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2618 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2619 | SC_Extern, |
| 2620 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2623 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2624 | QualType strType = getConstantStringStructType(); |
| 2625 | |
| 2626 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2627 | |
| 2628 | std::string tmpName = InFileName; |
| 2629 | unsigned i; |
| 2630 | for (i=0; i < tmpName.length(); i++) { |
| 2631 | char c = tmpName.at(i); |
| 2632 | // replace any non alphanumeric characters with '_'. |
| 2633 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2634 | tmpName[i] = '_'; |
| 2635 | } |
| 2636 | S += tmpName; |
| 2637 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2638 | S += utostr(NumObjCStringLiterals++); |
| 2639 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2640 | Preamble += "static __NSConstantStringImpl " + S; |
| 2641 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2642 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2643 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2644 | std::string prettyBufS; |
| 2645 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2646 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2647 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2648 | Preamble += prettyBuf.str(); |
| 2649 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2650 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | |
| 2652 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2653 | &Context->Idents.get(S), strType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2654 | SC_Static, SC_None); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2655 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2656 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | Context->getPointerType(DRE->getType()), |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2658 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2659 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2660 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2661 | CK_Unknown, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2662 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2663 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2664 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2667 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2668 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2669 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2670 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2672 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2673 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2675 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2677 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2679 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2680 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2681 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2683 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2684 | FieldTypes[i], 0, |
| 2685 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2686 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2689 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2690 | } |
| 2691 | return Context->getTagDeclType(SuperStructDecl); |
| 2692 | } |
| 2693 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2694 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2695 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2696 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2698 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2699 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2701 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2703 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2705 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2706 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2707 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2709 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2710 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2711 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2713 | ConstantStringDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2714 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2715 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2716 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2717 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2720 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2721 | } |
| 2722 | return Context->getTagDeclType(ConstantStringDecl); |
| 2723 | } |
| 2724 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2725 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2726 | SourceLocation StartLoc, |
| 2727 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2728 | if (!SelGetUidFunctionDecl) |
| 2729 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2730 | if (!MsgSendFunctionDecl) |
| 2731 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2732 | if (!MsgSendSuperFunctionDecl) |
| 2733 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2734 | if (!MsgSendStretFunctionDecl) |
| 2735 | SynthMsgSendStretFunctionDecl(); |
| 2736 | if (!MsgSendSuperStretFunctionDecl) |
| 2737 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2738 | if (!MsgSendFpretFunctionDecl) |
| 2739 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2740 | if (!GetClassFunctionDecl) |
| 2741 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2742 | if (!GetSuperClassFunctionDecl) |
| 2743 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2744 | if (!GetMetaClassFunctionDecl) |
| 2745 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2747 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2748 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2749 | // May need to use objc_msgSend_stret() as well. |
| 2750 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2751 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2752 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2753 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2754 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2755 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2756 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2759 | // Synthesize a call to objc_msgSend(). |
| 2760 | llvm::SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2761 | switch (Exp->getReceiverKind()) { |
| 2762 | case ObjCMessageExpr::SuperClass: { |
| 2763 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2764 | if (MsgSendStretFlavor) |
| 2765 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2766 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2768 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2770 | llvm::SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2772 | // set the receiver to self, the first argument to all methods. |
| 2773 | InitExprs.push_back( |
| 2774 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2775 | CK_Unknown, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2776 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2777 | Context->getObjCIdType(), |
| 2778 | SourceLocation())) |
| 2779 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2781 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2782 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2783 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2784 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2785 | ClassDecl->getIdentifier()->getNameStart(), |
| 2786 | ClassDecl->getIdentifier()->getLength(), |
| 2787 | false, argType, SourceLocation())); |
| 2788 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2789 | &ClsExprs[0], |
| 2790 | ClsExprs.size(), |
| 2791 | StartLoc, |
| 2792 | EndLoc); |
| 2793 | // (Class)objc_getClass("CurrentClass") |
| 2794 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2795 | Context->getObjCClassType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2796 | CK_Unknown, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2797 | ClsExprs.clear(); |
| 2798 | ClsExprs.push_back(ArgExpr); |
| 2799 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2800 | &ClsExprs[0], ClsExprs.size(), |
| 2801 | StartLoc, EndLoc); |
| 2802 | |
| 2803 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2804 | // To turn off a warning, type-cast to 'id' |
| 2805 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2806 | NoTypeInfoCStyleCastExpr(Context, |
| 2807 | Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2808 | CK_Unknown, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2809 | // struct objc_super |
| 2810 | QualType superType = getSuperStructType(); |
| 2811 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2813 | if (LangOpts.Microsoft) { |
| 2814 | SynthSuperContructorFunctionDecl(); |
| 2815 | // Simulate a contructor call... |
| 2816 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2817 | superType, SourceLocation()); |
| 2818 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2819 | InitExprs.size(), |
| 2820 | superType, SourceLocation()); |
| 2821 | // The code for super is a little tricky to prevent collision with |
| 2822 | // the structure definition in the header. The rewriter has it's own |
| 2823 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2824 | // we need the cast below. For example: |
| 2825 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2826 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2827 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2828 | Context->getPointerType(SuperRep->getType()), |
| 2829 | SourceLocation()); |
| 2830 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2831 | Context->getPointerType(superType), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2832 | CK_Unknown, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2833 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2834 | // (struct objc_super) { <exprs from above> } |
| 2835 | InitListExpr *ILE = |
| 2836 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2837 | &InitExprs[0], InitExprs.size(), |
| 2838 | SourceLocation()); |
| 2839 | TypeSourceInfo *superTInfo |
| 2840 | = Context->getTrivialTypeSourceInfo(superType); |
| 2841 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2842 | superType, ILE, false); |
| 2843 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2844 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2845 | Context->getPointerType(SuperRep->getType()), |
| 2846 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2847 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2848 | MsgExprs.push_back(SuperRep); |
| 2849 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2850 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2851 | |
| 2852 | case ObjCMessageExpr::Class: { |
| 2853 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2854 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2855 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2856 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2857 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2858 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2859 | clsName->getNameStart(), |
| 2860 | clsName->getLength(), |
| 2861 | false, argType, |
| 2862 | SourceLocation())); |
| 2863 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2864 | &ClsExprs[0], |
| 2865 | ClsExprs.size(), |
| 2866 | StartLoc, EndLoc); |
| 2867 | MsgExprs.push_back(Cls); |
| 2868 | break; |
| 2869 | } |
| 2870 | |
| 2871 | case ObjCMessageExpr::SuperInstance:{ |
| 2872 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2873 | if (MsgSendStretFlavor) |
| 2874 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2875 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2876 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 2877 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2878 | |
| 2879 | InitExprs.push_back( |
| 2880 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2881 | CK_Unknown, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2882 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2883 | Context->getObjCIdType(), |
| 2884 | SourceLocation())) |
| 2885 | ); // set the 'receiver'. |
| 2886 | |
| 2887 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2888 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2889 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2890 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2891 | ClassDecl->getIdentifier()->getNameStart(), |
| 2892 | ClassDecl->getIdentifier()->getLength(), |
| 2893 | false, argType, SourceLocation())); |
| 2894 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2895 | &ClsExprs[0], |
| 2896 | ClsExprs.size(), |
| 2897 | StartLoc, EndLoc); |
| 2898 | // (Class)objc_getClass("CurrentClass") |
| 2899 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2900 | Context->getObjCClassType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2901 | CK_Unknown, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2902 | ClsExprs.clear(); |
| 2903 | ClsExprs.push_back(ArgExpr); |
| 2904 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2905 | &ClsExprs[0], ClsExprs.size(), |
| 2906 | StartLoc, EndLoc); |
| 2907 | |
| 2908 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2909 | // To turn off a warning, type-cast to 'id' |
| 2910 | InitExprs.push_back( |
| 2911 | // set 'super class', using class_getSuperclass(). |
| 2912 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2913 | CK_Unknown, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2914 | // struct objc_super |
| 2915 | QualType superType = getSuperStructType(); |
| 2916 | Expr *SuperRep; |
| 2917 | |
| 2918 | if (LangOpts.Microsoft) { |
| 2919 | SynthSuperContructorFunctionDecl(); |
| 2920 | // Simulate a contructor call... |
| 2921 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2922 | superType, SourceLocation()); |
| 2923 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2924 | InitExprs.size(), |
| 2925 | superType, SourceLocation()); |
| 2926 | // The code for super is a little tricky to prevent collision with |
| 2927 | // the structure definition in the header. The rewriter has it's own |
| 2928 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2929 | // we need the cast below. For example: |
| 2930 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2931 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2932 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2933 | Context->getPointerType(SuperRep->getType()), |
| 2934 | SourceLocation()); |
| 2935 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2936 | Context->getPointerType(superType), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2937 | CK_Unknown, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2938 | } else { |
| 2939 | // (struct objc_super) { <exprs from above> } |
| 2940 | InitListExpr *ILE = |
| 2941 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2942 | &InitExprs[0], InitExprs.size(), |
| 2943 | SourceLocation()); |
| 2944 | TypeSourceInfo *superTInfo |
| 2945 | = Context->getTrivialTypeSourceInfo(superType); |
| 2946 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2947 | superType, ILE, false); |
| 2948 | } |
| 2949 | MsgExprs.push_back(SuperRep); |
| 2950 | break; |
| 2951 | } |
| 2952 | |
| 2953 | case ObjCMessageExpr::Instance: { |
| 2954 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2955 | // Foo<Proto> *. |
| 2956 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2957 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2958 | recExpr = CE->getSubExpr(); |
| 2959 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2960 | CK_Unknown, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2961 | MsgExprs.push_back(recExpr); |
| 2962 | break; |
| 2963 | } |
| 2964 | } |
| 2965 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2966 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2967 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2968 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2970 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2971 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2972 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2973 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2974 | &SelExprs[0], SelExprs.size(), |
| 2975 | StartLoc, |
| 2976 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2977 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2979 | // Now push any user supplied arguments. |
| 2980 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2981 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2982 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2983 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2984 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2985 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2986 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2987 | : ICE->getType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2988 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2989 | (void)convertBlockPointerToFunctionPointer(type); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2990 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2991 | userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2992 | } |
| 2993 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2994 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2995 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2996 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2997 | userExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2998 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2999 | CK_Unknown, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3002 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3003 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3004 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3005 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3006 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3007 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3008 | // Generate the funky cast. |
| 3009 | CastExpr *cast; |
| 3010 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3011 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3013 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 3014 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3015 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3016 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3017 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3018 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3019 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3020 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3021 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3022 | E = OMD->param_end(); PI != E; ++PI) { |
| 3023 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3025 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3026 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3027 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3028 | ArgTypes.push_back(t); |
| 3029 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3030 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 3031 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3032 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3033 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3034 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3035 | } |
| 3036 | // 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] | 3037 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3039 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3041 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3042 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3044 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3045 | // xx.m:13: warning: function called through a non-compatible type |
| 3046 | // 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] | 3047 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3048 | Context->getPointerType(Context->VoidTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3049 | CK_Unknown, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3051 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3053 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 3054 | // If we don't have a method decl, force a variadic cast. |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3055 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3056 | false, false, 0, 0, |
| 3057 | FunctionType::ExtInfo()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3058 | castType = Context->getPointerType(castType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3059 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3060 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3061 | |
| 3062 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3063 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3065 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3066 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | MsgExprs.size(), |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3068 | FT->getResultType(), EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3069 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3070 | if (MsgSendStretFlavor) { |
| 3071 | // We have the method which returns a struct/union. Must also generate |
| 3072 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3073 | // expression which dictate which one to envoke depending on size of |
| 3074 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3076 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3078 | SourceLocation()); |
| 3079 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3080 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3081 | Context->getPointerType(Context->VoidTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3082 | CK_Unknown, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3083 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3085 | &ArgTypes[0], ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3086 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3087 | false, false, 0, 0, |
| 3088 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3089 | castType = Context->getPointerType(castType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3090 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3091 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3093 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3094 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3096 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3097 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3099 | FT->getResultType(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3101 | // Build sizeof(returnType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3102 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3103 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3104 | Context->getSizeType(), |
| 3105 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3106 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3107 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3108 | // For X86 it is more complicated and some kind of target specific routine |
| 3109 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3111 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3112 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3113 | llvm::APInt(IntSize, 8), |
| 3114 | Context->IntTy, |
| 3115 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3117 | BO_LE, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | Context->IntTy, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3119 | SourceLocation()); |
| 3120 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3121 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3122 | new (Context) ConditionalOperator(lessThanExpr, |
| 3123 | SourceLocation(), CE, |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3124 | SourceLocation(), STCE, (Expr*)0, |
| 3125 | returnType); |
| 3126 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3127 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3128 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3129 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3130 | return ReplacingStmt; |
| 3131 | } |
| 3132 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3133 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3134 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3135 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3137 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3138 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3140 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3141 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3144 | // typedef struct objc_object Protocol; |
| 3145 | QualType RewriteObjC::getProtocolType() { |
| 3146 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3147 | TypeSourceInfo *TInfo |
| 3148 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3149 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3150 | SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3151 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3152 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3153 | } |
| 3154 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3155 | } |
| 3156 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3157 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3158 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3159 | /// The forward references (and metadata) are generated in |
| 3160 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3161 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3162 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3163 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3165 | ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3166 | SC_Extern, SC_None); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3167 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3168 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3169 | Context->getPointerType(DRE->getType()), |
| 3170 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3171 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3172 | CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3173 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3174 | ReplaceStmt(Exp, castExpr); |
| 3175 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3176 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3177 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3182 | const char *endBuf) { |
| 3183 | while (startBuf < endBuf) { |
| 3184 | if (*startBuf == '#') { |
| 3185 | // Skip whitespace. |
| 3186 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3187 | ; |
| 3188 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3189 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3190 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3191 | !strncmp(startBuf, "define", strlen("define")) || |
| 3192 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3193 | !strncmp(startBuf, "else", strlen("else")) || |
| 3194 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3195 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3196 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3197 | !strncmp(startBuf, "include", strlen("include")) || |
| 3198 | !strncmp(startBuf, "import", strlen("import")) || |
| 3199 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3200 | return true; |
| 3201 | } |
| 3202 | startBuf++; |
| 3203 | } |
| 3204 | return false; |
| 3205 | } |
| 3206 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3207 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3208 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3209 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3210 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3211 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3212 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3213 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3214 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3215 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3216 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3217 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3218 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3219 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3220 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3222 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3223 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3225 | // If no ivars and no root or if its root, directly or indirectly, |
| 3226 | // 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] | 3227 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3228 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3229 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3230 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3231 | return; |
| 3232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | |
| 3234 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3235 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3236 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3237 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3238 | if (LangOpts.Microsoft) |
| 3239 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3240 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3241 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3242 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3244 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3245 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3246 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3247 | // NSURL.h, for example): |
| 3248 | // |
| 3249 | // #ifdef XYZ |
| 3250 | // @interface Foo : NSObject |
| 3251 | // #else |
| 3252 | // @interface FooBar : NSObject |
| 3253 | // #endif |
| 3254 | // { |
| 3255 | // int i; |
| 3256 | // } |
| 3257 | // @end |
| 3258 | // |
| 3259 | // This clause is segregated to avoid breaking the common case. |
| 3260 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3262 | CDecl->getClassLoc(); |
| 3263 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3264 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3266 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3267 | // advance to the end of the referenced protocols. |
| 3268 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3269 | endHeader++; |
| 3270 | } |
| 3271 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3272 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3273 | } else { |
| 3274 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3275 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3276 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3277 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3278 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3279 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3280 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3281 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3282 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3284 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3285 | SourceLocation OnePastCurly = |
| 3286 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3287 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3288 | } |
| 3289 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3291 | // Now comment out any visibility specifiers. |
| 3292 | while (cursor < endBuf) { |
| 3293 | if (*cursor == '@') { |
| 3294 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3295 | // Skip whitespace. |
| 3296 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3297 | /*scan*/; |
| 3298 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3299 | // FIXME: presence of @public, etc. inside comment results in |
| 3300 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3301 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3302 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3303 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3304 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3305 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3306 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3307 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3308 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3309 | // for type checking. |
| 3310 | else if (*cursor == '<') { |
| 3311 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3312 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3313 | cursor = strchr(cursor, '>'); |
| 3314 | cursor++; |
| 3315 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3316 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3317 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3318 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3319 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3320 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3321 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3322 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3323 | // Don't forget to add a ';'!! |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3324 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3325 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3326 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3327 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3328 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3329 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3330 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3331 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3332 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3333 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3334 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3335 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3336 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3339 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3340 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3341 | template<typename MethodIterator> |
| 3342 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3343 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3344 | bool IsInstanceMethod, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3345 | llvm::StringRef prefix, |
| 3346 | llvm::StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3347 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3348 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3350 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3351 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3352 | SEL _cmd; |
| 3353 | char *method_types; |
| 3354 | void *_imp; |
| 3355 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3356 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3357 | Result += "\nstruct _objc_method {\n"; |
| 3358 | Result += "\tSEL _cmd;\n"; |
| 3359 | Result += "\tchar *method_types;\n"; |
| 3360 | Result += "\tvoid *_imp;\n"; |
| 3361 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3363 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3366 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3368 | /* struct { |
| 3369 | struct _objc_method_list *next_method; |
| 3370 | int method_count; |
| 3371 | struct _objc_method method_list[]; |
| 3372 | } |
| 3373 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3374 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3375 | Result += "\nstatic struct {\n"; |
| 3376 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3377 | Result += "\tint method_count;\n"; |
| 3378 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3379 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3380 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3381 | Result += prefix; |
| 3382 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3383 | Result += "_METHODS_"; |
| 3384 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3385 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3386 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3387 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3388 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3389 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3390 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3391 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3392 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3393 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3394 | Result += "\", \""; |
| 3395 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3396 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3397 | Result += MethodInternalNames[*MethodBegin]; |
| 3398 | Result += "}\n"; |
| 3399 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3400 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3401 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3402 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3403 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3404 | Result += "\", \""; |
| 3405 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3406 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3407 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3408 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3409 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3410 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3413 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3414 | void RewriteObjC:: |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3415 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, llvm::StringRef prefix, |
| 3416 | llvm::StringRef ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3417 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Output struct protocol_methods holder of method selector and type. |
| 3420 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3421 | /* struct protocol_methods { |
| 3422 | SEL _cmd; |
| 3423 | char *method_types; |
| 3424 | } |
| 3425 | */ |
| 3426 | Result += "\nstruct _protocol_methods {\n"; |
| 3427 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3428 | Result += "\tchar *method_types;\n"; |
| 3429 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3431 | objc_protocol_methods = true; |
| 3432 | } |
| 3433 | // Do not synthesize the protocol more than once. |
| 3434 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3435 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3437 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3438 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3439 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3440 | /* struct _objc_protocol_method_list { |
| 3441 | int protocol_method_count; |
| 3442 | struct protocol_methods protocols[]; |
| 3443 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3444 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3445 | Result += "\nstatic struct {\n"; |
| 3446 | Result += "\tint protocol_method_count;\n"; |
| 3447 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3448 | Result += utostr(NumMethods); |
| 3449 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3450 | Result += PDecl->getNameAsString(); |
| 3451 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3452 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3454 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | for (ObjCProtocolDecl::instmeth_iterator |
| 3456 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3457 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3458 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3459 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3460 | else |
| 3461 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3462 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3463 | std::string MethodTypeString; |
| 3464 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3465 | Result += "\", \""; |
| 3466 | Result += MethodTypeString; |
| 3467 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3468 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3469 | Result += "\t }\n};\n"; |
| 3470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3472 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3473 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3474 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3475 | if (NumMethods > 0) { |
| 3476 | /* struct _objc_protocol_method_list { |
| 3477 | int protocol_method_count; |
| 3478 | struct protocol_methods protocols[]; |
| 3479 | } |
| 3480 | */ |
| 3481 | Result += "\nstatic struct {\n"; |
| 3482 | Result += "\tint protocol_method_count;\n"; |
| 3483 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3484 | Result += utostr(NumMethods); |
| 3485 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3486 | Result += PDecl->getNameAsString(); |
| 3487 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3488 | "{\n\t"; |
| 3489 | Result += utostr(NumMethods); |
| 3490 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3491 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3492 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3494 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3495 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3496 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3497 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3498 | else |
| 3499 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3500 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3501 | std::string MethodTypeString; |
| 3502 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3503 | Result += "\", \""; |
| 3504 | Result += MethodTypeString; |
| 3505 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3506 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3507 | Result += "\t }\n};\n"; |
| 3508 | } |
| 3509 | |
| 3510 | // Output: |
| 3511 | /* struct _objc_protocol { |
| 3512 | // Objective-C 1.0 extensions |
| 3513 | struct _objc_protocol_extension *isa; |
| 3514 | char *protocol_name; |
| 3515 | struct _objc_protocol **protocol_list; |
| 3516 | struct _objc_protocol_method_list *instance_methods; |
| 3517 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3519 | */ |
| 3520 | static bool objc_protocol = false; |
| 3521 | if (!objc_protocol) { |
| 3522 | Result += "\nstruct _objc_protocol {\n"; |
| 3523 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3524 | Result += "\tchar *protocol_name;\n"; |
| 3525 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3526 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3527 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3528 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3530 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3533 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3534 | Result += PDecl->getNameAsString(); |
| 3535 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3536 | "{\n\t0, \""; |
| 3537 | Result += PDecl->getNameAsString(); |
| 3538 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3539 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3540 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3541 | Result += PDecl->getNameAsString(); |
| 3542 | Result += ", "; |
| 3543 | } |
| 3544 | else |
| 3545 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3546 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3547 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3548 | Result += PDecl->getNameAsString(); |
| 3549 | Result += "\n"; |
| 3550 | } |
| 3551 | else |
| 3552 | Result += "0\n"; |
| 3553 | Result += "};\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 | // Mark this protocol as having been generated. |
| 3556 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3557 | assert(false && "protocol already synthesized"); |
| 3558 | |
| 3559 | } |
| 3560 | |
| 3561 | void RewriteObjC:: |
| 3562 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3563 | llvm::StringRef prefix, llvm::StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3564 | std::string &Result) { |
| 3565 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3567 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3568 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3569 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3570 | // Output the top lovel protocol meta-data for the class. |
| 3571 | /* struct _objc_protocol_list { |
| 3572 | struct _objc_protocol_list *next; |
| 3573 | int protocol_count; |
| 3574 | struct _objc_protocol *class_protocols[]; |
| 3575 | } |
| 3576 | */ |
| 3577 | Result += "\nstatic struct {\n"; |
| 3578 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3579 | Result += "\tint protocol_count;\n"; |
| 3580 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3581 | Result += utostr(Protocols.size()); |
| 3582 | Result += "];\n} _OBJC_"; |
| 3583 | Result += prefix; |
| 3584 | Result += "_PROTOCOLS_"; |
| 3585 | Result += ClassName; |
| 3586 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3587 | "{\n\t0, "; |
| 3588 | Result += utostr(Protocols.size()); |
| 3589 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3590 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3591 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3592 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3593 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3595 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3596 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3597 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3598 | Result += "\n"; |
| 3599 | } |
| 3600 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3603 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3605 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3606 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3607 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3608 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3609 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3610 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3612 | CDecl = CDecl->getNextClassCategory()) |
| 3613 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3614 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3615 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3616 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3617 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3618 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3620 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3622 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3623 | |
| 3624 | // If any of our property implementations have associated getters or |
| 3625 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3626 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3627 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3628 | Prop != PropEnd; ++Prop) { |
| 3629 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3630 | continue; |
| 3631 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3632 | continue; |
| 3633 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3634 | if (!PD) |
| 3635 | continue; |
| 3636 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3637 | InstanceMethods.push_back(Getter); |
| 3638 | if (PD->isReadOnly()) |
| 3639 | continue; |
| 3640 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3641 | InstanceMethods.push_back(Setter); |
| 3642 | } |
| 3643 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3644 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3645 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3647 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3648 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3649 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3650 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3652 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3653 | // Null CDecl is case of a category implementation with no category interface |
| 3654 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3655 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3656 | FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3657 | /* struct _objc_category { |
| 3658 | char *category_name; |
| 3659 | char *class_name; |
| 3660 | struct _objc_method_list *instance_methods; |
| 3661 | struct _objc_method_list *class_methods; |
| 3662 | struct _objc_protocol_list *protocols; |
| 3663 | // Objective-C 1.0 extensions |
| 3664 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3666 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3668 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3670 | static bool objc_category = false; |
| 3671 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3672 | Result += "\nstruct _objc_category {\n"; |
| 3673 | Result += "\tchar *category_name;\n"; |
| 3674 | Result += "\tchar *class_name;\n"; |
| 3675 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3676 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3677 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3679 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3680 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3681 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3682 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3683 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3684 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3685 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3686 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3687 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3688 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3689 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3691 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3692 | Result += "\t, (struct _objc_method_list *)" |
| 3693 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3694 | Result += FullCategoryName; |
| 3695 | Result += "\n"; |
| 3696 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3697 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3698 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3699 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3700 | Result += "\t, (struct _objc_method_list *)" |
| 3701 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3702 | Result += FullCategoryName; |
| 3703 | Result += "\n"; |
| 3704 | } |
| 3705 | else |
| 3706 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3708 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3710 | Result += FullCategoryName; |
| 3711 | Result += "\n"; |
| 3712 | } |
| 3713 | else |
| 3714 | Result += "\t, 0\n"; |
| 3715 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3718 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3719 | /// ivar offset. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3720 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3721 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3722 | if (ivar->isBitField()) { |
| 3723 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3724 | // place all bitfields at offset 0. |
| 3725 | Result += "0"; |
| 3726 | } else { |
| 3727 | Result += "__OFFSETOFIVAR__(struct "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3728 | Result += ivar->getContainingInterface()->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3729 | if (LangOpts.Microsoft) |
| 3730 | Result += "_IMPL"; |
| 3731 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3732 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3733 | Result += ")"; |
| 3734 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3737 | //===----------------------------------------------------------------------===// |
| 3738 | // Meta Data Emission |
| 3739 | //===----------------------------------------------------------------------===// |
| 3740 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3741 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3742 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3743 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3745 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3746 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3748 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3749 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3750 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3751 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3752 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3753 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3755 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3756 | if (NumIvars > 0) { |
| 3757 | static bool objc_ivar = false; |
| 3758 | if (!objc_ivar) { |
| 3759 | /* struct _objc_ivar { |
| 3760 | char *ivar_name; |
| 3761 | char *ivar_type; |
| 3762 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3763 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3764 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3765 | Result += "\nstruct _objc_ivar {\n"; |
| 3766 | Result += "\tchar *ivar_name;\n"; |
| 3767 | Result += "\tchar *ivar_type;\n"; |
| 3768 | Result += "\tint ivar_offset;\n"; |
| 3769 | Result += "};\n"; |
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 | objc_ivar = true; |
| 3772 | } |
| 3773 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3774 | /* struct { |
| 3775 | int ivar_count; |
| 3776 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3778 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3780 | Result += "\tint ivar_count;\n"; |
| 3781 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3782 | Result += utostr(NumIvars); |
| 3783 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3784 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3785 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3786 | "{\n\t"; |
| 3787 | Result += utostr(NumIvars); |
| 3788 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3790 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3791 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3792 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3793 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3794 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3795 | IV != IVEnd; ++IV) |
| 3796 | IVars.push_back(*IV); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3797 | IVI = IDecl->ivar_begin(); |
| 3798 | IVE = IDecl->ivar_end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3799 | } else { |
| 3800 | IVI = CDecl->ivar_begin(); |
| 3801 | IVE = CDecl->ivar_end(); |
| 3802 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3803 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3804 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3805 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3806 | std::string TmpString, StrEncoding; |
| 3807 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3808 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3809 | Result += StrEncoding; |
| 3810 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3811 | SynthesizeIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3812 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3813 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3814 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3815 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3816 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3817 | std::string TmpString, StrEncoding; |
| 3818 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3819 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3820 | Result += StrEncoding; |
| 3821 | Result += "\", "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3822 | SynthesizeIvarOffsetComputation((*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3823 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3825 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3826 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3829 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3831 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3832 | |
| 3833 | // If any of our property implementations have associated getters or |
| 3834 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3835 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3836 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3837 | Prop != PropEnd; ++Prop) { |
| 3838 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3839 | continue; |
| 3840 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3841 | continue; |
| 3842 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3843 | if (!PD) |
| 3844 | continue; |
| 3845 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3846 | if (!Getter->isDefined()) |
| 3847 | InstanceMethods.push_back(Getter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3848 | if (PD->isReadOnly()) |
| 3849 | continue; |
| 3850 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3851 | if (!Setter->isDefined()) |
| 3852 | InstanceMethods.push_back(Setter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3853 | } |
| 3854 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3855 | true, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3857 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3858 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3859 | false, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3861 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3862 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3863 | "CLASS", CDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3865 | // Declaration of class/meta-class metadata |
| 3866 | /* struct _objc_class { |
| 3867 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3868 | const char *super_class_name; |
| 3869 | char *name; |
| 3870 | long version; |
| 3871 | long info; |
| 3872 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3873 | struct _objc_ivar_list *ivars; |
| 3874 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3875 | struct objc_cache *cache; |
| 3876 | struct objc_protocol_list *protocols; |
| 3877 | const char *ivar_layout; |
| 3878 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3880 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3881 | static bool objc_class = false; |
| 3882 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3883 | Result += "\nstruct _objc_class {\n"; |
| 3884 | Result += "\tstruct _objc_class *isa;\n"; |
| 3885 | Result += "\tconst char *super_class_name;\n"; |
| 3886 | Result += "\tchar *name;\n"; |
| 3887 | Result += "\tlong version;\n"; |
| 3888 | Result += "\tlong info;\n"; |
| 3889 | Result += "\tlong instance_size;\n"; |
| 3890 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3891 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3892 | Result += "\tstruct objc_cache *cache;\n"; |
| 3893 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3894 | Result += "\tconst char *ivar_layout;\n"; |
| 3895 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3896 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3897 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3900 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3901 | ObjCInterfaceDecl *RootClass = 0; |
| 3902 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3903 | while (SuperClass) { |
| 3904 | RootClass = SuperClass; |
| 3905 | SuperClass = SuperClass->getSuperClass(); |
| 3906 | } |
| 3907 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3909 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3910 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3911 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3912 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3913 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3914 | Result += "\""; |
| 3915 | |
| 3916 | if (SuperClass) { |
| 3917 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3918 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3919 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3920 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3921 | Result += "\""; |
| 3922 | } |
| 3923 | else { |
| 3924 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3925 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3926 | Result += "\""; |
| 3927 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3928 | // 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] | 3929 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3930 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3931 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3932 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3933 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3935 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3936 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3937 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3938 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3939 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3940 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3941 | Result += ",0,0\n"; |
| 3942 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3943 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3944 | Result += "\t,0,0,0,0\n"; |
| 3945 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3947 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3948 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3949 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3950 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3951 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3952 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3953 | if (SuperClass) { |
| 3954 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3955 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3956 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3957 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3958 | Result += "\""; |
| 3959 | } |
| 3960 | else { |
| 3961 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3962 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3963 | Result += "\""; |
| 3964 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3965 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3966 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3967 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3968 | Result += ",0"; |
| 3969 | else { |
| 3970 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3971 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3972 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3973 | if (LangOpts.Microsoft) |
| 3974 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3975 | Result += ")"; |
| 3976 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3977 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3978 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3979 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3980 | Result += "\n\t"; |
| 3981 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3982 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3983 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3984 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3985 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3986 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3988 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3989 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3990 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3991 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3992 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3993 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3994 | Result += ", 0,0\n"; |
| 3995 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3996 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3997 | Result += ",0,0,0\n"; |
| 3998 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3999 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4000 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4001 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4002 | /// and emits meta-data. |
| 4003 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4004 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4005 | int ClsDefCount = ClassImplementation.size(); |
| 4006 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4007 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4008 | // Rewrite implemented methods |
| 4009 | for (int i = 0; i < ClsDefCount; i++) |
| 4010 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 4012 | for (int i = 0; i < CatDefCount; i++) |
| 4013 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4014 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4015 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4016 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 4017 | int ClsDefCount = ClassImplementation.size(); |
| 4018 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 4019 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4020 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4021 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4022 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4023 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4024 | // For each implemented category, write out all its meta data. |
| 4025 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4026 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4027 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4028 | // Write objc_symtab metadata |
| 4029 | /* |
| 4030 | struct _objc_symtab |
| 4031 | { |
| 4032 | long sel_ref_cnt; |
| 4033 | SEL *refs; |
| 4034 | short cls_def_cnt; |
| 4035 | short cat_def_cnt; |
| 4036 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4038 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4040 | Result += "\nstruct _objc_symtab {\n"; |
| 4041 | Result += "\tlong sel_ref_cnt;\n"; |
| 4042 | Result += "\tSEL *refs;\n"; |
| 4043 | Result += "\tshort cls_def_cnt;\n"; |
| 4044 | Result += "\tshort cat_def_cnt;\n"; |
| 4045 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4046 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4048 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4049 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4050 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4051 | + ", " + utostr(CatDefCount) + "\n"; |
| 4052 | for (int i = 0; i < ClsDefCount; i++) { |
| 4053 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4054 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4055 | Result += "\n"; |
| 4056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4058 | for (int i = 0; i < CatDefCount; i++) { |
| 4059 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4060 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4061 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4062 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4063 | Result += "\n"; |
| 4064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4066 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4068 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4070 | /* |
| 4071 | struct _objc_module { |
| 4072 | long version; |
| 4073 | long size; |
| 4074 | const char *name; |
| 4075 | struct _objc_symtab *symtab; |
| 4076 | } |
| 4077 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4079 | Result += "\nstruct _objc_module {\n"; |
| 4080 | Result += "\tlong version;\n"; |
| 4081 | Result += "\tlong size;\n"; |
| 4082 | Result += "\tconst char *name;\n"; |
| 4083 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4084 | Result += "};\n\n"; |
| 4085 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4086 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4088 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4089 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4092 | if (ProtocolExprDecls.size()) { |
| 4093 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4094 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4096 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4097 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4098 | Result += (*I)->getNameAsString(); |
| 4099 | Result += " = &_OBJC_PROTOCOL_"; |
| 4100 | Result += (*I)->getNameAsString(); |
| 4101 | Result += ";\n"; |
| 4102 | } |
| 4103 | Result += "#pragma data_seg(pop)\n\n"; |
| 4104 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4105 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4106 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4107 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4108 | Result += "&_OBJC_MODULES;\n"; |
| 4109 | Result += "#pragma data_seg(pop)\n\n"; |
| 4110 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4111 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4112 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4113 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4114 | const std::string &Name, |
| 4115 | ValueDecl *VD) { |
| 4116 | assert(BlockByRefDeclNo.count(VD) && |
| 4117 | "RewriteByRefString: ByRef decl missing"); |
| 4118 | ResultStr += "struct __Block_byref_" + Name + |
| 4119 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4120 | } |
| 4121 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4122 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4123 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4124 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4128 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4129 | llvm::StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4130 | std::string Tag) { |
| 4131 | const FunctionType *AFT = CE->getFunctionType(); |
| 4132 | QualType RT = AFT->getResultType(); |
| 4133 | std::string StructRef = "struct " + Tag; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 4134 | std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4135 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4136 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4137 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4139 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | // 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] | 4141 | // block (to reference imported block decl refs). |
| 4142 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4143 | } else if (BD->param_empty()) { |
| 4144 | S += "(" + StructRef + " *__cself)"; |
| 4145 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4146 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4147 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4148 | S += '('; |
| 4149 | // first add the implicit argument. |
| 4150 | S += StructRef + " *__cself, "; |
| 4151 | std::string ParamStr; |
| 4152 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4153 | E = BD->param_end(); AI != E; ++AI) { |
| 4154 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4155 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4156 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4157 | if (convertBlockPointerToFunctionPointer(QT)) |
| 4158 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4159 | else |
| 4160 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4161 | S += ParamStr; |
| 4162 | } |
| 4163 | if (FT->isVariadic()) { |
| 4164 | if (!BD->param_empty()) S += ", "; |
| 4165 | S += "..."; |
| 4166 | } |
| 4167 | S += ')'; |
| 4168 | } |
| 4169 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4171 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4172 | // First, emit a declaration for all "by ref" decls. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4173 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4174 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4175 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4176 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4177 | std::string TypeString; |
| 4178 | RewriteByRefString(TypeString, Name, (*I)); |
| 4179 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4180 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4181 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4182 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4183 | // Next, emit a declaration for all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4184 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4185 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4186 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4187 | // Handle nested closure invocation. For example: |
| 4188 | // |
| 4189 | // void (^myImportedClosure)(void); |
| 4190 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4192 | // void (^anotherClosure)(void); |
| 4193 | // anotherClosure = ^(void) { |
| 4194 | // myImportedClosure(); // import and invoke the closure |
| 4195 | // }; |
| 4196 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4197 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4198 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4199 | S += " = ("; |
| 4200 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4201 | S += ")"; |
| 4202 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4203 | } |
| 4204 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4205 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4206 | QualType QT = (*I)->getType(); |
| 4207 | if (HasLocalVariableExternalStorage(*I)) |
| 4208 | QT = Context->getPointerType(QT); |
| 4209 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4210 | S += Name + " = __cself->" + |
| 4211 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4212 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4213 | } |
| 4214 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4215 | const char *cstr = RewrittenStr.c_str(); |
| 4216 | while (*cstr++ != '{') ; |
| 4217 | S += cstr; |
| 4218 | S += "\n"; |
| 4219 | return S; |
| 4220 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4221 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4222 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4223 | llvm::StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4224 | std::string Tag) { |
| 4225 | std::string StructRef = "struct " + Tag; |
| 4226 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4228 | S += funcName; |
| 4229 | S += "_block_copy_" + utostr(i); |
| 4230 | S += "(" + StructRef; |
| 4231 | S += "*dst, " + StructRef; |
| 4232 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4234 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4235 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4236 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4237 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4238 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4239 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4240 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4241 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4242 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4243 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4244 | S += "}\n"; |
| 4245 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4246 | S += "\nstatic void __"; |
| 4247 | S += funcName; |
| 4248 | S += "_block_dispose_" + utostr(i); |
| 4249 | S += "(" + StructRef; |
| 4250 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4252 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4253 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4254 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4255 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4256 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4257 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4258 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4259 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4261 | return S; |
| 4262 | } |
| 4263 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4264 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4265 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4266 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4267 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4268 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4269 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4270 | S += " struct " + Desc; |
| 4271 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4273 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4274 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4275 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4277 | if (BlockDeclRefs.size()) { |
| 4278 | // Output all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4279 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4280 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4281 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4282 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4283 | std::string ArgName = "_" + FieldName; |
| 4284 | // Handle nested closure invocation. For example: |
| 4285 | // |
| 4286 | // void (^myImportedBlock)(void); |
| 4287 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4289 | // void (^anotherBlock)(void); |
| 4290 | // anotherBlock = ^(void) { |
| 4291 | // myImportedBlock(); // import and invoke the closure |
| 4292 | // }; |
| 4293 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4294 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4295 | S += "struct __block_impl *"; |
| 4296 | Constructor += ", void *" + ArgName; |
| 4297 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4298 | QualType QT = (*I)->getType(); |
| 4299 | if (HasLocalVariableExternalStorage(*I)) |
| 4300 | QT = Context->getPointerType(QT); |
| 4301 | QT.getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4302 | QT.getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4303 | Constructor += ", " + ArgName; |
| 4304 | } |
| 4305 | S += FieldName + ";\n"; |
| 4306 | } |
| 4307 | // Output all "by ref" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4308 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4309 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4310 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4311 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4312 | std::string ArgName = "_" + FieldName; |
| 4313 | // Handle nested closure invocation. For example: |
| 4314 | // |
| 4315 | // void (^myImportedBlock)(void); |
| 4316 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4318 | // void (^anotherBlock)(void); |
| 4319 | // anotherBlock = ^(void) { |
| 4320 | // myImportedBlock(); // import and invoke the closure |
| 4321 | // }; |
| 4322 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4323 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4324 | S += "struct __block_impl *"; |
| 4325 | Constructor += ", void *" + ArgName; |
| 4326 | } else { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4327 | std::string TypeString; |
| 4328 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4329 | TypeString += " *"; |
| 4330 | FieldName = TypeString + FieldName; |
| 4331 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4332 | Constructor += ", " + ArgName; |
| 4333 | } |
| 4334 | S += FieldName + "; // by ref\n"; |
| 4335 | } |
| 4336 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4337 | Constructor += ", int flags=0)"; |
| 4338 | // Initialize all "by copy" arguments. |
| 4339 | bool firsTime = true; |
| 4340 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4341 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4342 | std::string Name = (*I)->getNameAsString(); |
| 4343 | if (firsTime) { |
| 4344 | Constructor += " : "; |
| 4345 | firsTime = false; |
| 4346 | } |
| 4347 | else |
| 4348 | Constructor += ", "; |
| 4349 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4350 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4351 | else |
| 4352 | Constructor += Name + "(_" + Name + ")"; |
| 4353 | } |
| 4354 | // Initialize all "by ref" arguments. |
| 4355 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4356 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4357 | std::string Name = (*I)->getNameAsString(); |
| 4358 | if (firsTime) { |
| 4359 | Constructor += " : "; |
| 4360 | firsTime = false; |
| 4361 | } |
| 4362 | else |
| 4363 | Constructor += ", "; |
| 4364 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4365 | Constructor += Name + "((struct __block_impl *)_" |
| 4366 | + Name + "->__forwarding)"; |
| 4367 | else |
| 4368 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 4369 | } |
| 4370 | |
| 4371 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4372 | if (GlobalVarDecl) |
| 4373 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4374 | else |
| 4375 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4376 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4378 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4379 | } else { |
| 4380 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4381 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4382 | if (GlobalVarDecl) |
| 4383 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4384 | else |
| 4385 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4386 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4387 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4388 | } |
| 4389 | Constructor += " "; |
| 4390 | Constructor += "}\n"; |
| 4391 | S += Constructor; |
| 4392 | S += "};\n"; |
| 4393 | return S; |
| 4394 | } |
| 4395 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4396 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4397 | std::string ImplTag, int i, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4398 | llvm::StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4399 | unsigned hasCopy) { |
| 4400 | std::string S = "\nstatic struct " + DescTag; |
| 4401 | |
| 4402 | S += " {\n unsigned long reserved;\n"; |
| 4403 | S += " unsigned long Block_size;\n"; |
| 4404 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4405 | S += " void (*copy)(struct "; |
| 4406 | S += ImplTag; S += "*, struct "; |
| 4407 | S += ImplTag; S += "*);\n"; |
| 4408 | |
| 4409 | S += " void (*dispose)(struct "; |
| 4410 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4411 | } |
| 4412 | S += "} "; |
| 4413 | |
| 4414 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4415 | S += ImplTag + ")"; |
| 4416 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4417 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4418 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4419 | } |
| 4420 | S += "};\n"; |
| 4421 | return S; |
| 4422 | } |
| 4423 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4424 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4425 | llvm::StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4426 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4427 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4428 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4429 | bool RewriteSC = (GlobalVarDecl && |
| 4430 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4431 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4432 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4433 | if (RewriteSC) { |
| 4434 | std::string SC(" void __"); |
| 4435 | SC += GlobalVarDecl->getNameAsString(); |
| 4436 | SC += "() {}"; |
| 4437 | InsertText(FunLocStart, SC); |
| 4438 | } |
| 4439 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4440 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4441 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4442 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4443 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4444 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4445 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4446 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4447 | ValueDecl *VD = Exp->getDecl(); |
| 4448 | BlockDeclRefs.push_back(Exp); |
| 4449 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4450 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4451 | BlockByCopyDecls.push_back(VD); |
| 4452 | } |
| 4453 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4454 | BlockByRefDeclsPtrSet.insert(VD); |
| 4455 | BlockByRefDecls.push_back(VD); |
| 4456 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 4457 | // imported objects in the inner blocks not used in the outer |
| 4458 | // blocks must be copied/disposed in the outer block as well. |
| 4459 | if (Exp->isByRef() || |
| 4460 | VD->getType()->isObjCObjectPointerType() || |
| 4461 | VD->getType()->isBlockPointerType()) |
| 4462 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4463 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4464 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4465 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4466 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4468 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4469 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4470 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4471 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4472 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4473 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4474 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4475 | |
| 4476 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4477 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4478 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4479 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4480 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4481 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4482 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4483 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4484 | BlockDeclRefs.clear(); |
| 4485 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4486 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4487 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4488 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4489 | ImportedBlockDecls.clear(); |
| 4490 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4491 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4492 | // Must insert any 'const/volatile/static here. Since it has been |
| 4493 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4494 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4495 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4496 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4497 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4498 | SC += "const "; |
| 4499 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4500 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4501 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4502 | SC += "restrict "; |
| 4503 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4506 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4507 | InnerDeclRefsCount.clear(); |
| 4508 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4509 | RewrittenBlockExprs.clear(); |
| 4510 | } |
| 4511 | |
| 4512 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4513 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4514 | llvm::StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4515 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4516 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4517 | } |
| 4518 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4519 | static void BuildUniqueMethodName(std::string &Name, |
| 4520 | ObjCMethodDecl *MD) { |
| 4521 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4522 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4523 | Name += "__" + MD->getSelector().getAsString(); |
| 4524 | // Convert colons to underscores. |
| 4525 | std::string::size_type loc = 0; |
| 4526 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4527 | Name.replace(loc, 1, "_"); |
| 4528 | } |
| 4529 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4530 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4531 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4532 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4533 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4534 | std::string FuncName; |
| 4535 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4536 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4540 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4541 | CI != E; ++CI) |
| 4542 | if (*CI) { |
| 4543 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4544 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4545 | else |
| 4546 | GetBlockDeclRefExprs(*CI); |
| 4547 | } |
| 4548 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4549 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4550 | // FIXME: Handle enums. |
| 4551 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4552 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4553 | } |
| 4554 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4555 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4556 | BlockDeclRefExpr *BDRE = |
| 4557 | new (Context)BlockDeclRefExpr(DRE->getDecl(), DRE->getType(), |
| 4558 | DRE->getLocation(), false); |
| 4559 | BlockDeclRefs.push_back(BDRE); |
| 4560 | } |
| 4561 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4562 | return; |
| 4563 | } |
| 4564 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4565 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4566 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4567 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4568 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4569 | CI != E; ++CI) |
| 4570 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4571 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4572 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4573 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4574 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4575 | InnerContexts); |
| 4576 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4577 | else |
| 4578 | GetInnerBlockDeclRefExprs(*CI, |
| 4579 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4580 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4581 | |
| 4582 | } |
| 4583 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4584 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4585 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4586 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4587 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4588 | } |
| 4589 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4590 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4591 | if (Var->isFunctionOrMethodVarDecl()) |
| 4592 | ImportedLocalExternalDecls.insert(Var); |
| 4593 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4594 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | } |
| 4597 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4598 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4599 | /// whose result type may be a block pointer or whose argument type(s) |
| 4600 | /// might be block pointers to an equivalent funtion type replacing |
| 4601 | /// all block pointers to function pointers. |
| 4602 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4603 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4604 | // FTP will be null for closures that don't take arguments. |
| 4605 | // Generate a funky cast. |
| 4606 | llvm::SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4607 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4608 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4609 | |
| 4610 | if (FTP) { |
| 4611 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4612 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4613 | QualType t = *I; |
| 4614 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4615 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4616 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4617 | ArgTypes.push_back(t); |
| 4618 | } |
| 4619 | } |
| 4620 | QualType FuncType; |
| 4621 | // FIXME. Does this work if block takes no argument but has a return type |
| 4622 | // which is of block type? |
| 4623 | if (HasBlockType) |
| 4624 | FuncType = Context->getFunctionType(Res, |
| 4625 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4626 | false, false, 0, 0, FunctionType::ExtInfo()); |
| 4627 | else FuncType = QualType(FT, 0); |
| 4628 | return FuncType; |
| 4629 | } |
| 4630 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4631 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4632 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4633 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4635 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4636 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4637 | } else if (const BlockDeclRefExpr *CDRE = |
| 4638 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4639 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4640 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4641 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4642 | } |
| 4643 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4644 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4645 | } |
| 4646 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4647 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4648 | else if (const ConditionalOperator *CEXPR = |
| 4649 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4650 | Expr *LHSExp = CEXPR->getLHS(); |
| 4651 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4652 | Expr *RHSExp = CEXPR->getRHS(); |
| 4653 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4654 | Expr *CONDExp = CEXPR->getCond(); |
| 4655 | ConditionalOperator *CondExpr = |
| 4656 | new (Context) ConditionalOperator(CONDExp, |
| 4657 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4658 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4659 | (Expr*)0, |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4660 | Exp->getType()); |
| 4661 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4662 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4663 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4664 | } else { |
| 4665 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4666 | } |
| 4667 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4668 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4669 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4670 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4671 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4673 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4674 | SourceLocation(), |
| 4675 | &Context->Idents.get("__block_impl")); |
| 4676 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4677 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4678 | // Generate a funky cast. |
| 4679 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4681 | // Push the block argument type. |
| 4682 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4683 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4685 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4686 | QualType t = *I; |
| 4687 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4688 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4689 | ArgTypes.push_back(t); |
| 4690 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4691 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4692 | // Now do the pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4694 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4695 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4696 | FunctionType::ExtInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4698 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4699 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4700 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4701 | CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4702 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4703 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4704 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4705 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4706 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4708 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4710 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4711 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4712 | FD->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4713 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4714 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4715 | CK_Unknown, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4716 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4718 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4719 | // Add the implicit argument. |
| 4720 | BlkExprs.push_back(BlkCast); |
| 4721 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4722 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4723 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4724 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4725 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4726 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4727 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4728 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4729 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4732 | // We need to return the rewritten expression to handle cases where the |
| 4733 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4734 | // For example: |
| 4735 | // |
| 4736 | // int main() { |
| 4737 | // __block Foo *f; |
| 4738 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4740 | // void (^myblock)() = ^() { |
| 4741 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4742 | // i = 77; |
| 4743 | // }; |
| 4744 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4745 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4746 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4747 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4748 | ValueDecl *VD; |
| 4749 | bool isArrow = true; |
| 4750 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4751 | VD = BDRE->getDecl(); |
| 4752 | else { |
| 4753 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4754 | isArrow = false; |
| 4755 | } |
| 4756 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4757 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4758 | &Context->Idents.get("__forwarding"), |
| 4759 | Context->VoidPtrTy, 0, |
| 4760 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4761 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4762 | FD, SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4763 | FD->getType()); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4764 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4765 | llvm::StringRef Name = VD->getName(); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4766 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4767 | &Context->Idents.get(Name), |
| 4768 | Context->VoidPtrTy, 0, |
| 4769 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4770 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4771 | DeclRefExp->getType()); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4772 | |
| 4773 | |
| 4774 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4775 | // Need parens to enforce precedence. |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4776 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4777 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4778 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4779 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4782 | // Rewrites the imported local variable V with external storage |
| 4783 | // (static, extern, etc.) as *V |
| 4784 | // |
| 4785 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4786 | ValueDecl *VD = DRE->getDecl(); |
| 4787 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4788 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4789 | return DRE; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4790 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4791 | DRE->getType(), DRE->getLocation()); |
| 4792 | // Need parens to enforce precedence. |
| 4793 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4794 | Exp); |
| 4795 | ReplaceStmt(DRE, PE); |
| 4796 | return PE; |
| 4797 | } |
| 4798 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4799 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4800 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4801 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4802 | |
| 4803 | // Need to avoid trying to rewrite synthesized casts. |
| 4804 | if (LocStart.isInvalid()) |
| 4805 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4806 | // Need to avoid trying to rewrite casts contained in macros. |
| 4807 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4808 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4810 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4811 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4812 | QualType QT = CE->getType(); |
| 4813 | const Type* TypePtr = QT->getAs<Type>(); |
| 4814 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4815 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4816 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4817 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4818 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4819 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4820 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4821 | return; |
| 4822 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4823 | // advance the location to startArgList. |
| 4824 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4826 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4827 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4828 | case '^': |
| 4829 | // Replace the '^' with '*'. |
| 4830 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4831 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4832 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4833 | } |
| 4834 | } |
| 4835 | return; |
| 4836 | } |
| 4837 | |
| 4838 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4839 | SourceLocation DeclLoc = FD->getLocation(); |
| 4840 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4841 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4842 | // We have 1 or more arguments that have closure pointers. |
| 4843 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4844 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4846 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4848 | parenCount++; |
| 4849 | // advance the location to startArgList. |
| 4850 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4851 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4852 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4853 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4854 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4855 | while (*argPtr++ && parenCount) { |
| 4856 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4857 | case '^': |
| 4858 | // Replace the '^' with '*'. |
| 4859 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4860 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4861 | break; |
| 4862 | case '(': |
| 4863 | parenCount++; |
| 4864 | break; |
| 4865 | case ')': |
| 4866 | parenCount--; |
| 4867 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4868 | } |
| 4869 | } |
| 4870 | return; |
| 4871 | } |
| 4872 | |
| 4873 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4874 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4875 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4876 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4877 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4878 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4879 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4880 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4881 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4882 | } |
| 4883 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4885 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4886 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4887 | return true; |
| 4888 | } |
| 4889 | return false; |
| 4890 | } |
| 4891 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4892 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4893 | const FunctionProtoType *FTP; |
| 4894 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4895 | if (PT) { |
| 4896 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4897 | } else { |
| 4898 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4899 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4900 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4901 | } |
| 4902 | if (FTP) { |
| 4903 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4904 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4905 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4906 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4907 | if ((*I)->isObjCObjectPointerType() && |
| 4908 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4909 | return true; |
| 4910 | } |
| 4911 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4912 | } |
| 4913 | return false; |
| 4914 | } |
| 4915 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4916 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4917 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4918 | const char *argPtr = strchr(Name, '('); |
| 4919 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4920 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4921 | LParen = argPtr; // output the start. |
| 4922 | argPtr++; // skip past the left paren. |
| 4923 | unsigned parenCount = 1; |
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 && parenCount) { |
| 4926 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4927 | case '(': parenCount++; break; |
| 4928 | case ')': parenCount--; break; |
| 4929 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4930 | } |
| 4931 | if (parenCount) argPtr++; |
| 4932 | } |
| 4933 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4934 | RParen = argPtr; // output the end |
| 4935 | } |
| 4936 | |
| 4937 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4938 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4939 | RewriteBlockPointerFunctionArgs(FD); |
| 4940 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4942 | // Handle Variables and Typedefs. |
| 4943 | SourceLocation DeclLoc = ND->getLocation(); |
| 4944 | QualType DeclT; |
| 4945 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4946 | DeclT = VD->getType(); |
| 4947 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4948 | DeclT = TDD->getUnderlyingType(); |
| 4949 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4950 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | else |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4952 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
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 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4955 | const char *endBuf = startBuf; |
| 4956 | // scan backward (from the decl location) for the end of the previous decl. |
| 4957 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4958 | startBuf--; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4959 | SourceLocation Start = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4960 | std::string buf; |
| 4961 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4962 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4963 | // may take block argument types (which will be handled below). |
| 4964 | if (*startBuf == '^') { |
| 4965 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4966 | buf = '*'; |
| 4967 | startBuf++; |
| 4968 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4969 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4970 | while (*startBuf != ')') { |
| 4971 | buf += *startBuf; |
| 4972 | startBuf++; |
| 4973 | OrigLength++; |
| 4974 | } |
| 4975 | buf += ')'; |
| 4976 | OrigLength++; |
| 4977 | |
| 4978 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4979 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4980 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4981 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4982 | DeclLoc = ND->getLocation(); |
| 4983 | startBuf = SM->getCharacterData(DeclLoc); |
| 4984 | const char *argListBegin, *argListEnd; |
| 4985 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4986 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4987 | if (*argListBegin == '^') |
| 4988 | buf += '*'; |
| 4989 | else if (*argListBegin == '<') { |
| 4990 | buf += "/*"; |
| 4991 | buf += *argListBegin++; |
| 4992 | OrigLength++;; |
| 4993 | while (*argListBegin != '>') { |
| 4994 | buf += *argListBegin++; |
| 4995 | OrigLength++; |
| 4996 | } |
| 4997 | buf += *argListBegin; |
| 4998 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4999 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5000 | else |
| 5001 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5002 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5003 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5004 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5005 | buf += ')'; |
| 5006 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5007 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5008 | ReplaceText(Start, OrigLength, buf); |
| 5009 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5010 | return; |
| 5011 | } |
| 5012 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5013 | |
| 5014 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 5015 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 5016 | /// struct Block_byref_id_object *src) { |
| 5017 | /// _Block_object_assign (&_dest->object, _src->object, |
| 5018 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5019 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5020 | /// _Block_object_assign(&_dest->object, _src->object, |
| 5021 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5022 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5023 | /// } |
| 5024 | /// And: |
| 5025 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 5026 | /// _Block_object_dispose(_src->object, |
| 5027 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5028 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5029 | /// _Block_object_dispose(_src->object, |
| 5030 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5031 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5032 | /// } |
| 5033 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5034 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 5035 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5036 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 5037 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5038 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5039 | CopyDestroyCache.insert(flag); |
| 5040 | S = "static void __Block_byref_id_object_copy_"; |
| 5041 | S += utostr(flag); |
| 5042 | S += "(void *dst, void *src) {\n"; |
| 5043 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5044 | // offset into the object pointer is computed as: |
| 5045 | // void * + void* + int + int + void* + void * |
| 5046 | unsigned IntSize = |
| 5047 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5048 | unsigned VoidPtrSize = |
| 5049 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 5050 | |
| 5051 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 5052 | S += " _Block_object_assign((char*)dst + "; |
| 5053 | S += utostr(offset); |
| 5054 | S += ", *(void * *) ((char*)src + "; |
| 5055 | S += utostr(offset); |
| 5056 | S += "), "; |
| 5057 | S += utostr(flag); |
| 5058 | S += ");\n}\n"; |
| 5059 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5060 | S += "static void __Block_byref_id_object_dispose_"; |
| 5061 | S += utostr(flag); |
| 5062 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5063 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5064 | S += utostr(offset); |
| 5065 | S += "), "; |
| 5066 | S += utostr(flag); |
| 5067 | S += ");\n}\n"; |
| 5068 | return S; |
| 5069 | } |
| 5070 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5071 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5072 | /// the declaration into: |
| 5073 | /// struct __Block_byref_ND { |
| 5074 | /// void *__isa; // NULL for everything except __weak pointers |
| 5075 | /// struct __Block_byref_ND *__forwarding; |
| 5076 | /// int32_t __flags; |
| 5077 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5078 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5079 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5080 | /// typex ND; |
| 5081 | /// }; |
| 5082 | /// |
| 5083 | /// It then replaces declaration of ND variable with: |
| 5084 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5085 | /// __size=sizeof(struct __Block_byref_ND), |
| 5086 | /// ND=initializer-if-any}; |
| 5087 | /// |
| 5088 | /// |
| 5089 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5090 | // Insert declaration for the function in which block literal is |
| 5091 | // used. |
| 5092 | if (CurFunctionDeclToDeclareForBlock) |
| 5093 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5094 | int flag = 0; |
| 5095 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5096 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5097 | if (DeclLoc.isInvalid()) |
| 5098 | // If type location is missing, it is because of missing type (a warning). |
| 5099 | // Use variable's location which is good for this case. |
| 5100 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5101 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5102 | SourceLocation X = ND->getLocEnd(); |
| 5103 | X = SM->getInstantiationLoc(X); |
| 5104 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5105 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5106 | std::string ByrefType; |
| 5107 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5108 | ByrefType += " {\n"; |
| 5109 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5110 | RewriteByRefString(ByrefType, Name, ND); |
| 5111 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5112 | ByrefType += " int __flags;\n"; |
| 5113 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5114 | // Add void *__Block_byref_id_object_copy; |
| 5115 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5116 | QualType Ty = ND->getType(); |
| 5117 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5118 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5119 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5120 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
| 5123 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5124 | ByrefType += " " + Name + ";\n"; |
| 5125 | ByrefType += "};\n"; |
| 5126 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5127 | SourceLocation FunLocStart; |
| 5128 | if (CurFunctionDef) |
| 5129 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5130 | else { |
| 5131 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5132 | FunLocStart = CurMethodDef->getLocStart(); |
| 5133 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5134 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5135 | if (Ty.isObjCGCWeak()) { |
| 5136 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5137 | isa = 1; |
| 5138 | } |
| 5139 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5140 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5141 | flag = BLOCK_BYREF_CALLER; |
| 5142 | QualType Ty = ND->getType(); |
| 5143 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5144 | if (Ty->isBlockPointerType()) |
| 5145 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5146 | else |
| 5147 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5148 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5149 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5150 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5151 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5152 | |
| 5153 | // struct __Block_byref_ND ND = |
| 5154 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5155 | // initializer-if-any}; |
| 5156 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5157 | unsigned flags = 0; |
| 5158 | if (HasCopyAndDispose) |
| 5159 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5160 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5161 | ByrefType.clear(); |
| 5162 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5163 | std::string ForwardingCastType("("); |
| 5164 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5165 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5166 | ByrefType += " " + Name + " = {(void*)"; |
| 5167 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5168 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5169 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5170 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5171 | ByrefType += "sizeof("; |
| 5172 | RewriteByRefString(ByrefType, Name, ND); |
| 5173 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5174 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5175 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5176 | ByrefType += utostr(flag); |
| 5177 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5178 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5179 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5180 | ByrefType += "};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5181 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5182 | } |
| 5183 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5184 | SourceLocation startLoc; |
| 5185 | Expr *E = ND->getInit(); |
| 5186 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5187 | startLoc = ECE->getLParenLoc(); |
| 5188 | else |
| 5189 | startLoc = E->getLocStart(); |
Fariborz Jahanian | 791b10d | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 5190 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5191 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5192 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5193 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5194 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5195 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5196 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5197 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5198 | ByrefType += "sizeof("; |
| 5199 | RewriteByRefString(ByrefType, Name, ND); |
| 5200 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5201 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5202 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5203 | ByrefType += utostr(flag); |
| 5204 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5205 | ByrefType += utostr(flag); |
| 5206 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5207 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5208 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5209 | |
| 5210 | // Complete the newly synthesized compound expression by inserting a right |
| 5211 | // curly brace before the end of the declaration. |
| 5212 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5213 | // also assumes there is only one declarator. For example, the following |
| 5214 | // isn't currently supported by this routine (in general): |
| 5215 | // |
| 5216 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5217 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5218 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5219 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5220 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5221 | SourceLocation semiLoc = |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5222 | startLoc.getFileLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5223 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5224 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5225 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5226 | return; |
| 5227 | } |
| 5228 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5229 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5230 | // Add initializers for any closure decl refs. |
| 5231 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5232 | if (BlockDeclRefs.size()) { |
| 5233 | // Unique all "by copy" declarations. |
| 5234 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5235 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5236 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5237 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5238 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5239 | } |
| 5240 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5241 | // Unique all "by ref" declarations. |
| 5242 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5243 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5244 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5245 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5246 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5247 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5248 | } |
| 5249 | // Find any imported blocks...they will need special attention. |
| 5250 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5251 | if (BlockDeclRefs[i]->isByRef() || |
| 5252 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5253 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5254 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5258 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(llvm::StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5259 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5260 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5262 | ID, FType, 0, SC_Extern, |
| 5263 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5264 | } |
| 5265 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5266 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
| 5267 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5268 | Blocks.push_back(Exp); |
| 5269 | |
| 5270 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5271 | |
| 5272 | // Add inner imported variables now used in current block. |
| 5273 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5274 | if (!InnerBlockDeclRefs.empty()) { |
| 5275 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5276 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5277 | ValueDecl *VD = Exp->getDecl(); |
| 5278 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5279 | // We need to save the copied-in variables in nested |
| 5280 | // blocks because it is needed at the end for some of the API generations. |
| 5281 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5282 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5283 | BlockDeclRefs.push_back(Exp); |
| 5284 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5285 | BlockByCopyDecls.push_back(VD); |
| 5286 | } |
| 5287 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5288 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5289 | BlockDeclRefs.push_back(Exp); |
| 5290 | BlockByRefDeclsPtrSet.insert(VD); |
| 5291 | BlockByRefDecls.push_back(VD); |
| 5292 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5293 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5294 | // Find any imported blocks...they will need special attention. |
| 5295 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5296 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5297 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5298 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5299 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5300 | } |
| 5301 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5302 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5303 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5305 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5306 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5307 | else if (CurMethodDef) |
| 5308 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5309 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5310 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5311 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5312 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5313 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5314 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5315 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5317 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 5318 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5319 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5320 | |
| 5321 | FunctionDecl *FD; |
| 5322 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5324 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5325 | FD = SynthBlockInitFunctionDecl(Tag); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5326 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5328 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5329 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5330 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5331 | FD = SynthBlockInitFunctionDecl(Func); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5332 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 5333 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5334 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5335 | CK_Unknown, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | InitExprs.push_back(castExpr); |
| 5337 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5338 | // Initialize the block descriptor. |
| 5339 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5340 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5341 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5342 | &Context->Idents.get(DescData.c_str()), |
| 5343 | Context->VoidPtrTy, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5344 | SC_Static, SC_None); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5345 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 5346 | new (Context) DeclRefExpr(NewVD, |
| 5347 | Context->VoidPtrTy, SourceLocation()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5348 | UO_AddrOf, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5349 | Context->getPointerType(Context->VoidPtrTy), |
| 5350 | SourceLocation()); |
| 5351 | InitExprs.push_back(DescRefExpr); |
| 5352 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5353 | // Add initializers for any closure decl refs. |
| 5354 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5355 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5356 | // Output all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5357 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5358 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5359 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5360 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5361 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5362 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5363 | if (HasLocalVariableExternalStorage(*I)) { |
| 5364 | QualType QT = (*I)->getType(); |
| 5365 | QT = Context->getPointerType(QT); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5366 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5367 | SourceLocation()); |
| 5368 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5369 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5370 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5371 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5372 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5373 | CK_Unknown, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5374 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5375 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5376 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5377 | if (HasLocalVariableExternalStorage(*I)) { |
| 5378 | QualType QT = (*I)->getType(); |
| 5379 | QT = Context->getPointerType(QT); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5380 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5381 | SourceLocation()); |
| 5382 | } |
| 5383 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5384 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5385 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5386 | } |
| 5387 | // Output all "by ref" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5388 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5389 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5390 | ValueDecl *ND = (*I); |
| 5391 | std::string Name(ND->getNameAsString()); |
| 5392 | std::string RecName; |
| 5393 | RewriteByRefString(RecName, Name, ND); |
| 5394 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5395 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5396 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5397 | SourceLocation(), II); |
| 5398 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5399 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5400 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5401 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5402 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5403 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | Context->getPointerType(Exp->getType()), |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5405 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5406 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_Unknown, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5407 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5408 | } |
| 5409 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5410 | if (ImportedBlockDecls.size()) { |
| 5411 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5412 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5413 | unsigned IntSize = |
| 5414 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5415 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5416 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5417 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5418 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5419 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5420 | FType, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5421 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5423 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5424 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_Unknown, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5425 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5426 | BlockDeclRefs.clear(); |
| 5427 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5428 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5429 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5430 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5431 | ImportedBlockDecls.clear(); |
| 5432 | return NewRep; |
| 5433 | } |
| 5434 | |
| 5435 | //===----------------------------------------------------------------------===// |
| 5436 | // Function Body / Expression rewriting |
| 5437 | //===----------------------------------------------------------------------===// |
| 5438 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5439 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5440 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5441 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5442 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5443 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5444 | // we get this right. |
| 5445 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5446 | // Perform a bottom up traversal of all children. |
| 5447 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5448 | CI != E; ++CI) |
| 5449 | if (*CI) |
| 5450 | CollectPropertySetters(*CI); |
| 5451 | |
| 5452 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5453 | if (BinOp->isAssignmentOp()) { |
Fariborz Jahanian | 8537f7b | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5454 | if (isa<ObjCPropertyRefExpr>(BinOp->getLHS()) || |
| 5455 | isa<ObjCImplicitSetterGetterRefExpr>(BinOp->getLHS())) |
| 5456 | PropSetters[BinOp->getLHS()] = BinOp; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5461 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5463 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5464 | Stmts.push_back(S); |
| 5465 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5466 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5467 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5469 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5470 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5472 | // Perform a bottom up rewrite of all children. |
| 5473 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5474 | CI != E; ++CI) |
| 5475 | if (*CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5476 | Stmt *newStmt; |
| 5477 | Stmt *S = (*CI); |
| 5478 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5479 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5480 | bool replaced = false; |
| 5481 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 5482 | if (replaced) { |
| 5483 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5484 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5485 | else |
| 5486 | ReplaceStmt(S, newStmt); |
| 5487 | } |
| 5488 | } |
| 5489 | else |
| 5490 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5491 | if (newStmt) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5492 | *CI = newStmt; |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5493 | // If dealing with an assignment with LHS being a property reference |
| 5494 | // expression, the entire assignment tree is rewritten into a property |
| 5495 | // setter messaging. This involvs the RHS too. Do not attempt to rewrite |
| 5496 | // RHS again. |
Fariborz Jahanian | 8537f7b | 2010-10-11 22:21:03 +0000 | [diff] [blame] | 5497 | if (Expr *Exp = dyn_cast<Expr>(S)) |
| 5498 | if (isa<ObjCPropertyRefExpr>(Exp) || |
| 5499 | isa<ObjCImplicitSetterGetterRefExpr>(Exp)) { |
| 5500 | if (PropSetters[Exp]) { |
| 5501 | ++CI; |
| 5502 | continue; |
| 5503 | } |
Fariborz Jahanian | 90fe4bc | 2010-10-08 21:12:22 +0000 | [diff] [blame] | 5504 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 5505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5506 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5507 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5508 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5509 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5510 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5511 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5512 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5513 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5514 | // Rewrite the block body in place. |
| 5515 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5516 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5517 | // Now we snarf the rewritten text and stash it away for later use. |
Ted Kremenek | 6a12a14 | 2010-01-07 18:00:35 +0000 | [diff] [blame] | 5518 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5519 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5520 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5521 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5522 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5523 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5524 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5525 | return blockTranscribed; |
| 5526 | } |
| 5527 | // Handle specific things. |
| 5528 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5529 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5530 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5531 | if (isa<ObjCPropertyRefExpr>(S) || isa<ObjCImplicitSetterGetterRefExpr>(S)) { |
| 5532 | Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(S); |
| 5533 | assert(PropOrImplicitRefExpr && "Property or implicit setter/getter is null"); |
| 5534 | |
| 5535 | BinaryOperator *BinOp = PropSetters[PropOrImplicitRefExpr]; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5536 | if (BinOp) { |
| 5537 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5538 | // 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] | 5539 | DisableReplaceStmt = true; |
| 5540 | // Save the source range. Even if we disable the replacement, the |
| 5541 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5542 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5543 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5544 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5545 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5546 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Fariborz Jahanian | f2c6fa4 | 2010-10-14 23:31:39 +0000 | [diff] [blame] | 5547 | // Need to rewrite the ivar access expression if need be. |
| 5548 | if (isa<ObjCIvarRefExpr>(newStmt)) { |
| 5549 | bool replaced = false; |
| 5550 | newStmt = RewriteObjCNestedIvarRefExpr(newStmt, replaced); |
| 5551 | } |
| 5552 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5553 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5554 | // |
| 5555 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5556 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5557 | // to see the original expression). Consider this example: |
| 5558 | // |
| 5559 | // Foo *obj1, *obj2; |
| 5560 | // |
| 5561 | // obj1.i = [obj2 rrrr]; |
| 5562 | // |
| 5563 | // 'BinOp' for the previous expression looks like: |
| 5564 | // |
| 5565 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5566 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5567 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5568 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5569 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5570 | // |
| 5571 | // 'newStmt' represents the rewritten message expression. For example: |
| 5572 | // |
| 5573 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5574 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5575 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5576 | // (CStyleCastExpr 0x231d220 'void *' |
| 5577 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5578 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5579 | // Note that 'newStmt' is passed to RewritePropertyOrImplicitSetter so that it |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5580 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5581 | // the original RHS (since we haven't altered BinOp). |
| 5582 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5583 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5584 | // node. As a result, we now leak the original AST nodes. |
| 5585 | // |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5586 | return RewritePropertyOrImplicitSetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5587 | } else { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5588 | return RewritePropertyOrImplicitGetter(PropOrImplicitRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5589 | } |
| 5590 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 5591 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5592 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5593 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5594 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5595 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5596 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5597 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5598 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5599 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5600 | // Before we rewrite it, put the original message expression in a comment. |
| 5601 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5602 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5603 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5604 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5605 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5606 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5607 | std::string messString; |
| 5608 | messString += "// "; |
| 5609 | messString.append(startBuf, endBuf-startBuf+1); |
| 5610 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5611 | |
| 5612 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5613 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5614 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5615 | // Tried this, but it didn't work either... |
| 5616 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5617 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5618 | return RewriteMessageExpr(MessExpr); |
| 5619 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5621 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5622 | return RewriteObjCTryStmt(StmtTry); |
| 5623 | |
| 5624 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5625 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5626 | |
| 5627 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5628 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5629 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5630 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5631 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5632 | |
| 5633 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5634 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5636 | OrigStmtRange.getEnd()); |
| 5637 | if (BreakStmt *StmtBreakStmt = |
| 5638 | dyn_cast<BreakStmt>(S)) |
| 5639 | return RewriteBreakStmt(StmtBreakStmt); |
| 5640 | if (ContinueStmt *StmtContinueStmt = |
| 5641 | dyn_cast<ContinueStmt>(S)) |
| 5642 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5643 | |
| 5644 | // 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] | 5645 | // and cast exprs. |
| 5646 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5647 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5648 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5649 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5650 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5651 | // the context of an ObjCForCollectionStmt. For example: |
| 5652 | // NSArray *someArray; |
| 5653 | // for (id <FooProtocol> index in someArray) ; |
| 5654 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5655 | // and it depends on the original text locations/positions. |
Benjamin Kramer | b2041de | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5656 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5657 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5658 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5659 | // Blocks rewrite rules. |
| 5660 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5661 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5662 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5663 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5664 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5665 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5666 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5667 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5668 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5669 | if (VD->hasAttr<BlocksAttr>()) { |
| 5670 | static unsigned uniqueByrefDeclCount = 0; |
| 5671 | assert(!BlockByRefDeclNo.count(ND) && |
| 5672 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5673 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5674 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5675 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5676 | else |
| 5677 | RewriteTypeOfDecl(VD); |
| 5678 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5679 | } |
| 5680 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5681 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5682 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5683 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5684 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5685 | } |
| 5686 | } |
| 5687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5688 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5689 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5690 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | |
| 5692 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5693 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5694 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5695 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5696 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5697 | && "Statement stack mismatch"); |
| 5698 | Stmts.pop_back(); |
| 5699 | } |
| 5700 | // Handle blocks rewriting. |
| 5701 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5702 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5703 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5704 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5705 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5706 | ValueDecl *VD = DRE->getDecl(); |
| 5707 | if (VD->hasAttr<BlocksAttr>()) |
| 5708 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5709 | if (HasLocalVariableExternalStorage(VD)) |
| 5710 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5713 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5714 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5715 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5716 | ReplaceStmt(S, BlockCall); |
| 5717 | return BlockCall; |
| 5718 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5719 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5720 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5721 | RewriteCastExpr(CE); |
| 5722 | } |
| 5723 | #if 0 |
| 5724 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5725 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5726 | ICE->getSubExpr(), |
| 5727 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5728 | // Get the new text. |
| 5729 | std::string SStr; |
| 5730 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5731 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5732 | const std::string &Str = Buf.str(); |
| 5733 | |
| 5734 | printf("CAST = %s\n", &Str[0]); |
| 5735 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5736 | delete S; |
| 5737 | return Replacement; |
| 5738 | } |
| 5739 | #endif |
| 5740 | // Return this stmt unmodified. |
| 5741 | return S; |
| 5742 | } |
| 5743 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5744 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5745 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5746 | e = RD->field_end(); i != e; ++i) { |
| 5747 | FieldDecl *FD = *i; |
| 5748 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5749 | RewriteBlockPointerDecl(FD); |
| 5750 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5751 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5752 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5753 | } |
| 5754 | } |
| 5755 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5756 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5757 | /// main file of the input. |
| 5758 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5759 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5760 | if (FD->isOverloadedOperator()) |
| 5761 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5763 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5764 | // prototype. This enables us to rewrite function declarations and |
| 5765 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5766 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5767 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5768 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 5f1bfc1 | 2010-07-07 11:31:34 +0000 | [diff] [blame] | 5769 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5770 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5771 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5772 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5773 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5774 | Body = |
| 5775 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5776 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5777 | CurrentBody = 0; |
| 5778 | if (PropParentMap) { |
| 5779 | delete PropParentMap; |
| 5780 | PropParentMap = 0; |
| 5781 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5782 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5783 | // and any copy/dispose helper functions. |
| 5784 | InsertBlockLiteralsWithinFunction(FD); |
| 5785 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5786 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5787 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5788 | return; |
| 5789 | } |
| 5790 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5791 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5792 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5793 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5794 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5795 | Body = |
| 5796 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5797 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5798 | CurrentBody = 0; |
| 5799 | if (PropParentMap) { |
| 5800 | delete PropParentMap; |
| 5801 | PropParentMap = 0; |
| 5802 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5803 | InsertBlockLiteralsWithinMethod(MD); |
| 5804 | CurMethodDef = 0; |
| 5805 | } |
| 5806 | } |
| 5807 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5808 | ClassImplementation.push_back(CI); |
| 5809 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5810 | CategoryImplementation.push_back(CI); |
| 5811 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5812 | RewriteForwardClassDecl(CD); |
| 5813 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5814 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5815 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5816 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5817 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5818 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5819 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5820 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5821 | RewriteCastExpr(CE); |
| 5822 | } |
| 5823 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5824 | } else if (VD->getType()->isRecordType()) { |
| 5825 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5826 | if (RD->isDefinition()) |
| 5827 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5828 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5829 | if (VD->getInit()) { |
| 5830 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5831 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5832 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5833 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5834 | CurrentBody = 0; |
| 5835 | if (PropParentMap) { |
| 5836 | delete PropParentMap; |
| 5837 | PropParentMap = 0; |
| 5838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5839 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5840 | VD->getName()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5841 | GlobalVarDecl = 0; |
| 5842 | |
| 5843 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5844 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5845 | RewriteCastExpr(CE); |
| 5846 | } |
| 5847 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5848 | return; |
| 5849 | } |
| 5850 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5851 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5852 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5853 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5854 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5855 | return; |
| 5856 | } |
| 5857 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5858 | if (RD->isDefinition()) |
| 5859 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5860 | return; |
| 5861 | } |
| 5862 | // Nothing yet. |
| 5863 | } |
| 5864 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5865 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5866 | if (Diags.hasErrorOccurred()) |
| 5867 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5868 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5869 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5870 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5871 | // Here's a great place to add any extra declarations that may be needed. |
| 5872 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5873 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5874 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5875 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5876 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5877 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5878 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5879 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5880 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5881 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5882 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5883 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5884 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5885 | //printf("Changed:\n"); |
| 5886 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5887 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5888 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5889 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5890 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5891 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5892 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5893 | // Rewrite Objective-c meta data* |
| 5894 | std::string ResultStr; |
| 5895 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5896 | // Emit metadata. |
| 5897 | *OutFile << ResultStr; |
| 5898 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5899 | OutFile->flush(); |
| 5900 | } |