Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 9f30fc3 | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 35 | enum { |
| 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 37 | block, ... */ |
| 38 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 39 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 40 | __block variable */ |
| 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 42 | helpers */ |
| 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 44 | support routines */ |
| 45 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 46 | }; |
| 47 | |
| 48 | enum { |
| 49 | BLOCK_NEEDS_FREE = (1 << 24), |
| 50 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 51 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 52 | BLOCK_IS_GC = (1 << 27), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 55 | }; |
| 56 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 57 | Rewriter Rewrite; |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 58 | Diagnostic &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 59 | const LangOptions &LangOpts; |
Steve Naroff | 7b3579b | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 60 | unsigned RewriteFailedDiag; |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 61 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 63 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 64 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 65 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 66 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 67 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 68 | SourceLocation LastIncLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 70 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 71 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 72 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 74 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 75 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 76 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 77 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 78 | // Remember all the @protocol(<expr>) expressions. |
| 79 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 80 | |
| 81 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 82 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 83 | unsigned NumObjCStringLiterals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 87 | FunctionDecl *MsgSendStretFunctionDecl; |
| 88 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 89 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 90 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 91 | FunctionDecl *GetMetaClassFunctionDecl; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 92 | FunctionDecl *GetSuperClassFunctionDecl; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 93 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 94 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 95 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 97 | // ObjC string constant support. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 98 | VarDecl *ConstantStringClassReference; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 99 | RecordDecl *NSStringRecord; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Fariborz Jahanian | 19d42bf | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 101 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 102 | int BcLabelCount; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 104 | // Needed for super. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 105 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 106 | RecordDecl *SuperStructDecl; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 107 | RecordDecl *ConstantStringDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 109 | TypeDecl *ProtocolTypeDecl; |
| 110 | QualType getProtocolType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 112 | // Needed for header files being rewritten |
| 113 | bool IsHeader; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 115 | std::string InFileName; |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 116 | llvm::raw_ostream* OutFile; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 117 | |
| 118 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 119 | bool objc_impl_method; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 120 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 121 | std::string Preamble; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 122 | |
| 123 | // Block expressions. |
| 124 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 125 | llvm::SmallVector<int, 32> InnerDeclRefsCount; |
| 126 | llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
| 127 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 128 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 130 | // Block related declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 131 | llvm::SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 133 | llvm::SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 134 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 135 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 136 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 137 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 138 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 139 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 140 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 141 | // This maps a property to it's assignment statement. |
| 142 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 143 | // This maps a property to it's synthesied message expression. |
| 144 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 145 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 147 | // This maps an original source AST to it's rewritten form. This allows |
| 148 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 149 | // This is needed to support some of the exotic property rewriting. |
| 150 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 151 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 152 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 153 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 154 | VarDecl *GlobalVarDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 156 | bool DisableReplaceStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 158 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 159 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 160 | virtual void Initialize(ASTContext &context); |
| 161 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 162 | // Top Level Driver code. |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 163 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 164 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 165 | HandleTopLevelSingleDecl(*I); |
| 166 | } |
| 167 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 168 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 169 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 170 | Diagnostic &D, const LangOptions &LOpts, |
| 171 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 172 | |
| 173 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 175 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 177 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 180 | if (ReplacingStmt) |
| 181 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 182 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 183 | if (DisableReplaceStmt) |
| 184 | return; // Used when rewriting the assignment of a property setter. |
| 185 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 186 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 187 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 188 | ReplacedNodes[Old] = New; |
| 189 | return; |
| 190 | } |
| 191 | if (SilenceRewriteMacroWarning) |
| 192 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 193 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 194 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 195 | } |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 196 | |
| 197 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 198 | // Measaure the old text. |
| 199 | int Size = Rewrite.getRangeSize(SrcRange); |
| 200 | if (Size == -1) { |
| 201 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 202 | << Old->getSourceRange(); |
| 203 | return; |
| 204 | } |
| 205 | // Get the new text. |
| 206 | std::string SStr; |
| 207 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 208 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 209 | const std::string &Str = S.str(); |
| 210 | |
| 211 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 212 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 213 | ReplacedNodes[Old] = New; |
| 214 | return; |
| 215 | } |
| 216 | if (SilenceRewriteMacroWarning) |
| 217 | return; |
| 218 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 219 | << Old->getSourceRange(); |
| 220 | } |
| 221 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 222 | void InsertText(SourceLocation Loc, llvm::StringRef Str, |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 223 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 224 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 225 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 226 | SilenceRewriteMacroWarning) |
| 227 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 229 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 232 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 233 | // If removal succeeded or warning disabled return with no warning. |
| 234 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 235 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 237 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 238 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 240 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 241 | llvm::StringRef Str) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 242 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 243 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 244 | SilenceRewriteMacroWarning) |
| 245 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 247 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 248 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 250 | // Syntactic Rewriting. |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 251 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 252 | void RewriteInclude(); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 253 | void RewriteTabs(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 254 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 255 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 256 | ObjCImplementationDecl *IMD, |
| 257 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 259 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 261 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 262 | const FunctionType *&FPRetType); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 263 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 264 | ValueDecl *VD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 266 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 267 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 268 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 269 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 270 | void RewriteFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 271 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 272 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 273 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 274 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 275 | bool needToScanForQualifiers(QualType T); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 276 | bool isSuperReceiver(Expr *recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 277 | QualType getSuperStructType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 278 | QualType getConstantStringStructType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame^] | 279 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 280 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 282 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 283 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 284 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 286 | Stmt *CurrentBody; |
| 287 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 289 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 291 | bool &replaced); |
| 292 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 293 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 295 | SourceRange SrcRange); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 296 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 297 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 298 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 299 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 300 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 301 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 302 | void RewriteTryReturnStmts(Stmt *S); |
| 303 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 304 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 305 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 306 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 307 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 308 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 309 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 310 | SourceLocation OrigEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 312 | Expr **args, unsigned nargs, |
| 313 | SourceLocation StartLoc=SourceLocation(), |
| 314 | SourceLocation EndLoc=SourceLocation()); |
| 315 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 316 | SourceLocation StartLoc=SourceLocation(), |
| 317 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 318 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 319 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 320 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 322 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 323 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 324 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 325 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 326 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 327 | void SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 328 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 329 | void SynthGetSuperClassFunctionDecl(); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 330 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 331 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 333 | // Metadata emission. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 334 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 335 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 337 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 338 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 340 | template<typename MethodIterator> |
| 341 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 342 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 343 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 344 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 345 | const char *ClassName, |
| 346 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 348 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 349 | const char *prefix, |
| 350 | const char *ClassName, |
| 351 | std::string &Result); |
| 352 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | const char *prefix, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 354 | const char *ClassName, |
| 355 | std::string &Result); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 356 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 357 | std::string &Result); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 358 | void SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 360 | std::string &Result); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 361 | void RewriteImplementations(); |
| 362 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 364 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 366 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 368 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 369 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
| 371 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 372 | void RewriteBlockCall(CallExpr *Exp); |
| 373 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 374 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 375 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 376 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 377 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 378 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
| 380 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 381 | const char *funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 383 | const char *funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 384 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 385 | std::string Tag, std::string Desc); |
| 386 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 387 | std::string ImplTag, |
| 388 | int i, const char *funcName, |
| 389 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 390 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 391 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 392 | const char *FunName); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 393 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 395 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 396 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 397 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 398 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 399 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 401 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 402 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 403 | bool isTopLevelBlockPointerType(QualType T) { |
| 404 | return isa<BlockPointerType>(T); |
| 405 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 407 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 408 | bool isObjCType(QualType T) { |
| 409 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 410 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 412 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 414 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 415 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 416 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 418 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 420 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 426 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 427 | const char *&RParen); |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 428 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 430 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 431 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 432 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 434 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 436 | if (From[i] == '"') |
| 437 | To += "\\\""; |
| 438 | else |
| 439 | To += From[i]; |
| 440 | } |
| 441 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 442 | }; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 443 | |
| 444 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 445 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 446 | CastExpr::CastKind Kind, Expr *E) { |
| 447 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Anders Carlsson | 5d270e8 | 2010-04-24 18:38:56 +0000 | [diff] [blame] | 448 | return new (Ctx) CStyleCastExpr(Ty, Kind, E, CXXBaseSpecifierArray(), TInfo, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 449 | SourceLocation(), SourceLocation()); |
| 450 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 454 | NamedDecl *D) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 455 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 457 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 458 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 459 | // All the args are checked/rewritten. Don't call twice! |
| 460 | RewriteBlockPointerDecl(D); |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 467 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 468 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 469 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 472 | static bool IsHeaderFile(const std::string &Filename) { |
| 473 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 475 | if (DotPos == std::string::npos) { |
| 476 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 480 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 481 | // C header: .h |
| 482 | // C++ header: .hh or .H; |
| 483 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 485 | |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 486 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 487 | Diagnostic &D, const LangOptions &LOpts, |
| 488 | bool silenceMacroWarn) |
| 489 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 490 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 491 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 493 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 495 | "rewriter doesn't support user-specified control flow semantics " |
| 496 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 499 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 500 | llvm::raw_ostream* OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | Diagnostic &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 502 | const LangOptions &LOpts, |
| 503 | bool SilenceRewriteMacroWarning) { |
| 504 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 505 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 506 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 507 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 508 | Context = &context; |
| 509 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 510 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 511 | MsgSendFunctionDecl = 0; |
| 512 | MsgSendSuperFunctionDecl = 0; |
| 513 | MsgSendStretFunctionDecl = 0; |
| 514 | MsgSendSuperStretFunctionDecl = 0; |
| 515 | MsgSendFpretFunctionDecl = 0; |
| 516 | GetClassFunctionDecl = 0; |
| 517 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 518 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 519 | SelGetUidFunctionDecl = 0; |
| 520 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 521 | ConstantStringClassReference = 0; |
| 522 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 523 | CurMethodDef = 0; |
| 524 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 525 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 526 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 527 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 528 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 529 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 530 | BcLabelCount = 0; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 531 | SuperContructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 532 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 533 | PropParentMap = 0; |
| 534 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 535 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 536 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 538 | // Get the ID and start/end of the main file. |
| 539 | MainFileID = SM->getMainFileID(); |
| 540 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 541 | MainFileStart = MainBuf->getBufferStart(); |
| 542 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 544 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 546 | // declaring objc_selector outside the parameter list removes a silly |
| 547 | // scope related warning... |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 548 | if (IsHeader) |
Steve Naroff | fcc6fd5 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 549 | Preamble = "#pragma once\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 550 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 551 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 552 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 553 | if (LangOpts.Microsoft) { |
| 554 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 555 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 556 | ": "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 557 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 558 | } |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 559 | Preamble += "};\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 560 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 561 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 562 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 563 | Preamble += "#endif\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 564 | if (LangOpts.Microsoft) { |
| 565 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 566 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 567 | } else |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 568 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 569 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 570 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 571 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 572 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 573 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 574 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 575 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 576 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 577 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 578 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 579 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 580 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 581 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 582 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 583 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 584 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 585 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 586 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 587 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 588 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 589 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | d30f8c5 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 590 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 8dd1525 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 591 | // @synchronized hooks. |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 592 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 593 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 594 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 595 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 596 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 597 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | 4dbab8a | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 598 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 599 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 600 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 601 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 602 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 603 | Preamble += "#endif\n"; |
| 604 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 605 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 606 | Preamble += " int *isa;\n"; |
| 607 | Preamble += " int flags;\n"; |
| 608 | Preamble += " char *str;\n"; |
| 609 | Preamble += " long length;\n"; |
| 610 | Preamble += "};\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 611 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 612 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 613 | Preamble += "#else\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 614 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 615 | Preamble += "#endif\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 616 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 617 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 618 | // Blocks preamble. |
| 619 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 620 | Preamble += "#define BLOCK_IMPL\n"; |
| 621 | Preamble += "struct __block_impl {\n"; |
| 622 | Preamble += " void *isa;\n"; |
| 623 | Preamble += " int Flags;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 624 | Preamble += " int Reserved;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 625 | Preamble += " void *FuncPtr;\n"; |
| 626 | Preamble += "};\n"; |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 627 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | 287a2bf | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 628 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 629 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 630 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 631 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 632 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 633 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 634 | Preamble += "#else\n"; |
Steve Naroff | 7bf01ea | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 635 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 636 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 637 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 638 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 639 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 640 | Preamble += "#endif\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 641 | if (LangOpts.Microsoft) { |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 642 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 643 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 8d269dc | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 644 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 645 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 8d269dc | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 646 | Preamble += "#endif\n"; |
Fariborz Jahanian | f0462ff | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 647 | Preamble += "#define __weak\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 648 | } |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 649 | else { |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 650 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 651 | Preamble += "#define __weak\n"; |
| 652 | } |
Fariborz Jahanian | db217c4 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 653 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 654 | // as this avoids warning in any 64bit/32bit compilation model. |
| 655 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 659 | //===----------------------------------------------------------------------===// |
| 660 | // Top Level Driver Code |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 663 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 664 | if (Diags.hasErrorOccurred()) |
| 665 | return; |
| 666 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 667 | // Two cases: either the decl could be in the main file, or it could be in a |
| 668 | // #included file. If the former, rewrite it now. If the later, check to see |
| 669 | // if we rewrote the #include/#import. |
| 670 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 671 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 673 | // If this is for a builtin, ignore it. |
| 674 | if (Loc.isInvalid()) return; |
| 675 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 676 | // Look for built-in declarations that we need to refer during the rewrite. |
| 677 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 678 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 679 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 680 | // declared in <Foundation/NSString.h> |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 681 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 682 | ConstantStringClassReference = FVD; |
| 683 | return; |
| 684 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 685 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 686 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 687 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 688 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 689 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 690 | RewriteProtocolDecl(PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 692 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 693 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 694 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 695 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 696 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 697 | DIEnd = LSD->decls_end(); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 698 | DI != DIEnd; ++DI) |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 699 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 700 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 701 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | d61ed3b | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 702 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 703 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 706 | //===----------------------------------------------------------------------===// |
| 707 | // Syntactic (non-AST) Rewriting Code |
| 708 | //===----------------------------------------------------------------------===// |
| 709 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 710 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 711 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 712 | llvm::StringRef MainBuf = SM->getBufferData(MainFileID); |
| 713 | const char *MainBufStart = MainBuf.begin(); |
| 714 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 715 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 717 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 718 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 719 | if (*BufPtr == '#') { |
| 720 | if (++BufPtr == MainBufEnd) |
| 721 | return; |
| 722 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 723 | if (++BufPtr == MainBufEnd) |
| 724 | return; |
| 725 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 726 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | SourceLocation ImportLoc = |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 728 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 729 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 730 | BufPtr += ImportLen; |
| 731 | } |
| 732 | } |
| 733 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 736 | void RewriteObjC::RewriteTabs() { |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 737 | llvm::StringRef MainBuf = SM->getBufferData(MainFileID); |
| 738 | const char *MainBufStart = MainBuf.begin(); |
| 739 | const char *MainBufEnd = MainBuf.end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 741 | // Loop over the whole file, looking for tabs. |
| 742 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 743 | if (*BufPtr != '\t') |
| 744 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 746 | // Okay, we found a tab. This tab will turn into at least one character, |
| 747 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 748 | unsigned VCol = 0; |
| 749 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 750 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 751 | ++VCol; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 753 | // Okay, now that we know the virtual column, we know how many spaces to |
| 754 | // insert. We assume 8-character tab-stops. |
| 755 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 757 | // Get the location of the tab. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 758 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 759 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 761 | // Rewrite the single tab character into a sequence of spaces. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 762 | ReplaceText(TabLoc, 1, llvm::StringRef(" ", Spaces)); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 767 | ObjCIvarDecl *OID) { |
| 768 | std::string S; |
| 769 | S = "((struct "; |
| 770 | S += ClassDecl->getIdentifier()->getName(); |
| 771 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 772 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 773 | return S; |
| 774 | } |
| 775 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 776 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 777 | ObjCImplementationDecl *IMD, |
| 778 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 779 | static bool objcGetPropertyDefined = false; |
| 780 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 781 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 782 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 783 | const char *startBuf = SM->getCharacterData(startLoc); |
| 784 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 785 | const char *semiBuf = strchr(startBuf, ';'); |
| 786 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 787 | SourceLocation onePastSemiLoc = |
| 788 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 789 | |
| 790 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 791 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 793 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 795 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 796 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 798 | if (!OID) |
| 799 | return; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 800 | unsigned Attributes = PD->getPropertyAttributes(); |
| 801 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 802 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 803 | ObjCPropertyDecl::OBJC_PR_copy)); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 804 | std::string Getr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 805 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 806 | objcGetPropertyDefined = true; |
| 807 | // FIXME. Is this attribute correct in all cases? |
| 808 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 809 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 810 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 811 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 812 | Getr += "{ "; |
| 813 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 0629704 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 814 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 815 | if (GenGetProperty) { |
| 816 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 817 | Getr += "typedef "; |
| 818 | const FunctionType *FPRetType = 0; |
| 819 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 820 | FPRetType); |
| 821 | Getr += " _TYPE"; |
| 822 | if (FPRetType) { |
| 823 | Getr += ")"; // close the precedence "scope" for "*". |
| 824 | |
| 825 | // Now, emit the argument types (if any). |
| 826 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 827 | Getr += "("; |
| 828 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 829 | if (i) Getr += ", "; |
| 830 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 831 | Getr += ParamStr; |
| 832 | } |
| 833 | if (FT->isVariadic()) { |
| 834 | if (FT->getNumArgs()) Getr += ", "; |
| 835 | Getr += "..."; |
| 836 | } |
| 837 | Getr += ")"; |
| 838 | } else |
| 839 | Getr += "()"; |
| 840 | } |
| 841 | Getr += ";\n"; |
| 842 | Getr += "return (_TYPE)"; |
| 843 | Getr += "objc_getProperty(self, _cmd, "; |
| 844 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Getr); |
| 845 | Getr += ", 1)"; |
| 846 | } |
| 847 | else |
| 848 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 849 | Getr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 850 | InsertText(onePastSemiLoc, Getr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 851 | if (PD->isReadOnly()) |
| 852 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 854 | // Generate the 'setter' function. |
| 855 | std::string Setr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 856 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 857 | ObjCPropertyDecl::OBJC_PR_copy); |
| 858 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 859 | objcSetPropertyDefined = true; |
| 860 | // FIXME. Is this attribute correct in all cases? |
| 861 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 862 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 863 | } |
| 864 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 865 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 866 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 867 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 868 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 869 | if (GenSetProperty) { |
| 870 | Setr += "objc_setProperty (self, _cmd, "; |
| 871 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Setr); |
| 872 | Setr += ", (id)"; |
| 873 | Setr += PD->getNameAsCString(); |
| 874 | Setr += ", "; |
| 875 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 876 | Setr += "0, "; |
| 877 | else |
| 878 | Setr += "1, "; |
| 879 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 880 | Setr += "1)"; |
| 881 | else |
| 882 | Setr += "0)"; |
| 883 | } |
| 884 | else { |
| 885 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
| 886 | Setr += PD->getNameAsCString(); |
| 887 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 888 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 889 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 890 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 891 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 892 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 893 | // Get the start location and compute the semi location. |
| 894 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 895 | const char *startBuf = SM->getCharacterData(startLoc); |
| 896 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 898 | // Translate to typedef's that forward reference structs with the same name |
| 899 | // as the class. As a convenience, we include the original declaration |
| 900 | // as a comment. |
| 901 | std::string typedefString; |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 902 | typedefString += "// @class "; |
| 903 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 904 | I != E; ++I) { |
| 905 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 906 | typedefString += ForwardDecl->getNameAsString(); |
| 907 | if (I+1 != E) |
| 908 | typedefString += ", "; |
| 909 | else |
| 910 | typedefString += ";\n"; |
| 911 | } |
| 912 | |
Chris Lattner | 9ee23b7 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 913 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 914 | I != E; ++I) { |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 915 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 916 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 917 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 918 | typedefString += "\n"; |
| 919 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 920 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 921 | typedefString += "\n"; |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 922 | typedefString += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 923 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 924 | typedefString += ";\n#endif\n"; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 925 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 927 | // Replace the @class with typedefs corresponding to the classes. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 928 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 931 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 932 | // When method is a synthesized one, such as a getter/setter there is |
| 933 | // nothing to rewrite. |
| 934 | if (Method->isSynthesized()) |
| 935 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 936 | SourceLocation LocStart = Method->getLocStart(); |
| 937 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 939 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 940 | SM->getInstantiationLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 941 | InsertText(LocStart, "#if 0\n"); |
| 942 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 943 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 944 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 949 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 951 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 952 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 955 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 956 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 958 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 959 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 961 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 962 | E = CatDecl->prop_end(); I != E; ++I) |
| 963 | RewriteProperty(*I); |
| 964 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | for (ObjCCategoryDecl::instmeth_iterator |
| 966 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 967 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 968 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 970 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 971 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 972 | RewriteMethodDeclaration(*I); |
| 973 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 974 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 975 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 976 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 979 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 980 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 982 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 983 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
| 985 | for (ObjCProtocolDecl::instmeth_iterator |
| 986 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 987 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 988 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 989 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 990 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 991 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 992 | RewriteMethodDeclaration(*I); |
| 993 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 994 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 995 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 996 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 997 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 998 | // Must comment out @optional/@required |
| 999 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1000 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1001 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1002 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1003 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1004 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1006 | } |
| 1007 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1008 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1009 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1015 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1016 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1017 | if (LocStart.isInvalid()) |
| 1018 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1019 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1020 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1023 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1024 | const FunctionType *&FPRetType) { |
| 1025 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1026 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1027 | else if (T->isFunctionPointerType() || |
| 1028 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1029 | // needs special handling, since pointer-to-functions have special |
| 1030 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1031 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1032 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1033 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1034 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1035 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1036 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1037 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1038 | ResultStr += FPRetType->getResultType().getAsString(); |
| 1039 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } else |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1042 | ResultStr += T.getAsString(); |
| 1043 | } |
| 1044 | |
| 1045 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
| 1046 | std::string &ResultStr) { |
| 1047 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1048 | const FunctionType *FPRetType = 0; |
| 1049 | ResultStr += "\nstatic "; |
| 1050 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1051 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1053 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1054 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1056 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1057 | NameStr += "_I_"; |
| 1058 | else |
| 1059 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1061 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1062 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1065 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1066 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1067 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1068 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1070 | { |
| 1071 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1072 | int len = selString.size(); |
| 1073 | for (int i = 0; i < len; i++) |
| 1074 | if (selString[i] == ':') |
| 1075 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1076 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1077 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1078 | // Remember this name for metadata emission |
| 1079 | MethodInternalNames[OMD] = NameStr; |
| 1080 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1082 | // Rewrite arguments |
| 1083 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1085 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1086 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1087 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1088 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1089 | if (!LangOpts.Microsoft) { |
| 1090 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 1091 | ResultStr += "struct "; |
| 1092 | } |
| 1093 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1094 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1095 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1096 | } |
| 1097 | else |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1098 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1100 | ResultStr += " self, "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1101 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1102 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1104 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1105 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1106 | E = OMD->param_end(); PI != E; ++PI) { |
| 1107 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1108 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1109 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1110 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1111 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1112 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1113 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 1114 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1115 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1116 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1117 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 1118 | Context->PrintingPolicy); |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1119 | } else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1120 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1121 | ResultStr += Name; |
| 1122 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1123 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1124 | if (OMD->isVariadic()) |
| 1125 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1126 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1128 | if (FPRetType) { |
| 1129 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1131 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1132 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1133 | ResultStr += "("; |
| 1134 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1135 | if (i) ResultStr += ", "; |
| 1136 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1137 | ResultStr += ParamStr; |
| 1138 | } |
| 1139 | if (FT->isVariadic()) { |
| 1140 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1141 | ResultStr += "..."; |
| 1142 | } |
| 1143 | ResultStr += ")"; |
| 1144 | } else { |
| 1145 | ResultStr += "()"; |
| 1146 | } |
| 1147 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1148 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1149 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1150 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1151 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1153 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1155 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1156 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1157 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1158 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1159 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1160 | ObjCMethodDecl *OMD = *I; |
| 1161 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1162 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1163 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1164 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1165 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1166 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1167 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1168 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1170 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1171 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1172 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1173 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1174 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1175 | ObjCMethodDecl *OMD = *I; |
| 1176 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1177 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1178 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1180 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1181 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1182 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1183 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1184 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1185 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1187 | I != E; ++I) { |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1188 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1191 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1194 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1195 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1196 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1197 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1198 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1199 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1200 | ResultStr += "\n"; |
| 1201 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1202 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1203 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1204 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1205 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1206 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1207 | // Mark this typedef as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1208 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1209 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1210 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
| 1212 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1213 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1214 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1216 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1217 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1218 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1220 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1221 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1222 | RewriteMethodDeclaration(*I); |
| 1223 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1224 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1225 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1226 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1229 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1230 | SourceRange SrcRange) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1231 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1232 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1233 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1234 | ObjCMessageExpr *MsgExpr; |
| 1235 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1236 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1237 | ExprVec.push_back(newStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1239 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1240 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1241 | if (PRE && PropGetters[PRE]) { |
| 1242 | // This allows us to handle chain/nested property getters. |
| 1243 | Receiver = PropGetters[PRE]; |
| 1244 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1245 | if (isa<ObjCSuperExpr>(Receiver)) |
| 1246 | MsgExpr = ObjCMessageExpr::Create(*Context, |
| 1247 | PDecl->getType().getNonReferenceType(), |
| 1248 | /*FIXME?*/SourceLocation(), |
| 1249 | Receiver->getLocStart(), |
| 1250 | /*IsInstanceSuper=*/true, |
| 1251 | cast<Expr>(Receiver)->getType(), |
| 1252 | PDecl->getSetterName(), |
| 1253 | PDecl->getSetterMethodDecl(), |
| 1254 | &ExprVec[0], 1, |
| 1255 | /*FIXME:*/SourceLocation()); |
| 1256 | else |
| 1257 | MsgExpr = ObjCMessageExpr::Create(*Context, |
| 1258 | PDecl->getType().getNonReferenceType(), |
| 1259 | /*FIXME: */SourceLocation(), |
| 1260 | cast<Expr>(Receiver), |
| 1261 | PDecl->getSetterName(), |
| 1262 | PDecl->getSetterMethodDecl(), |
| 1263 | &ExprVec[0], 1, |
| 1264 | /*FIXME:*/SourceLocation()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1265 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1267 | // Now do the actual rewrite. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1268 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | df70577 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1269 | //delete BinOp; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1270 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1271 | // to things that stay around. |
| 1272 | Context->Deallocate(MsgExpr); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1273 | return ReplacingStmt; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1276 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1277 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1278 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1279 | ObjCMessageExpr *MsgExpr; |
| 1280 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1282 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1284 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1285 | if (PRE && PropGetters[PRE]) { |
| 1286 | // This allows us to handle chain/nested property getters. |
| 1287 | Receiver = PropGetters[PRE]; |
| 1288 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (isa<ObjCSuperExpr>(Receiver)) |
| 1291 | MsgExpr = ObjCMessageExpr::Create(*Context, |
| 1292 | PDecl->getType().getNonReferenceType(), |
| 1293 | /*FIXME:*/SourceLocation(), |
| 1294 | Receiver->getLocStart(), |
| 1295 | /*IsInstanceSuper=*/true, |
| 1296 | cast<Expr>(Receiver)->getType(), |
| 1297 | PDecl->getGetterName(), |
| 1298 | PDecl->getGetterMethodDecl(), |
| 1299 | 0, 0, |
| 1300 | /*FIXME:*/SourceLocation()); |
| 1301 | else |
| 1302 | MsgExpr = ObjCMessageExpr::Create(*Context, |
| 1303 | PDecl->getType().getNonReferenceType(), |
| 1304 | /*FIXME:*/SourceLocation(), |
| 1305 | cast<Expr>(Receiver), |
| 1306 | PDecl->getGetterName(), |
| 1307 | PDecl->getGetterMethodDecl(), |
| 1308 | 0, 0, |
| 1309 | /*FIXME:*/SourceLocation()); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1310 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1311 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1312 | |
| 1313 | if (!PropParentMap) |
| 1314 | PropParentMap = new ParentMap(CurrentBody); |
| 1315 | |
| 1316 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1317 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1318 | // We stash away the ReplacingStmt since actually doing the |
| 1319 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1320 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1321 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1322 | // to things that stay around. |
| 1323 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1324 | return PropRefExpr; // return the original... |
| 1325 | } else { |
| 1326 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1328 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1329 | // to things that stay around. |
| 1330 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1331 | return ReplacingStmt; |
| 1332 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1336 | SourceLocation OrigStart, |
| 1337 | bool &replaced) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1338 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1339 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1340 | if (CurMethodDef) { |
Fariborz Jahanian | f9e8c2b | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1341 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1342 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1343 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | f0ed69c | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1344 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1345 | // lookup which class implements the instance variable. |
| 1346 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1348 | clsDeclared); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1349 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1351 | // Synthesize an explicit cast to gain access to the ivar. |
| 1352 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1353 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1354 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1355 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1356 | SourceLocation(), II); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1357 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1358 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1359 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1360 | CastExpr::CK_Unknown, |
| 1361 | IV->getBase()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1362 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1363 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1364 | IV->getBase()->getLocEnd(), |
| 1365 | castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1366 | replaced = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | if (IV->isFreeIvar() && |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1368 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1369 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1370 | IV->getLocation(), |
| 1371 | D->getType()); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1372 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1373 | return ME; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1374 | } |
Fariborz Jahanian | 8131081 | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1375 | // Get the new text |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1376 | // Cannot delete IV->getBase(), since PE points to it. |
| 1377 | // Replace the old base with the cast. This is important when doing |
| 1378 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | IV->setBase(PE); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1380 | return IV; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1381 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1382 | } else { // we are outside a method. |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1383 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1385 | // Explicit ivar refs need to have a cast inserted. |
| 1386 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 12e2e86 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1387 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | 9146e44 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1388 | ObjCInterfaceType *iFaceDecl = |
| 1389 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1390 | // lookup which class implements the instance variable. |
| 1391 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1393 | clsDeclared); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1394 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1396 | // Synthesize an explicit cast to gain access to the ivar. |
| 1397 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1398 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1399 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1400 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1401 | SourceLocation(), II); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1402 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1403 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1404 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1405 | CastExpr::CK_Unknown, |
| 1406 | IV->getBase()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1407 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1408 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 34873d2 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1409 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1410 | replaced = true; |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1411 | // Cannot delete IV->getBase(), since PE points to it. |
| 1412 | // Replace the old base with the cast. This is important when doing |
| 1413 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | IV->setBase(PE); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1415 | return IV; |
| 1416 | } |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1417 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1418 | return IV; |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1421 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1422 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1423 | CI != E; ++CI) { |
| 1424 | if (*CI) { |
| 1425 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1426 | if (newStmt) |
| 1427 | *CI = newStmt; |
| 1428 | } |
| 1429 | } |
| 1430 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1431 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1432 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1433 | replaced); |
| 1434 | return newStmt; |
Fariborz Jahanian | 3143338 | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1435 | } |
| 1436 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1437 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1438 | return newStmt; |
| 1439 | } |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1440 | return S; |
| 1441 | } |
| 1442 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1443 | /// SynthCountByEnumWithState - To print: |
| 1444 | /// ((unsigned int (*) |
| 1445 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1447 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | /// "countByEnumeratingWithState:objects:count:"), |
| 1449 | /// &enumState, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1450 | /// (id *)items, (unsigned int)16) |
| 1451 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1452 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1453 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1454 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1455 | buf += "\n\t\t"; |
| 1456 | buf += "((id)l_collection,\n\t\t"; |
| 1457 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1458 | buf += "\n\t\t"; |
| 1459 | buf += "&enumState, " |
| 1460 | "(id *)items, (unsigned int)16)"; |
| 1461 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1462 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1463 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1464 | /// statement to exit to its outer synthesized loop. |
| 1465 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1466 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1467 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1468 | return S; |
| 1469 | // replace break with goto __break_label |
| 1470 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1472 | SourceLocation startLoc = S->getLocStart(); |
| 1473 | buf = "goto __break_label_"; |
| 1474 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1475 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1476 | |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1481 | /// statement to continue with its inner synthesized loop. |
| 1482 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1483 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1484 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1485 | return S; |
| 1486 | // replace continue with goto __continue_label |
| 1487 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1489 | SourceLocation startLoc = S->getLocStart(); |
| 1490 | buf = "goto __continue_label_"; |
| 1491 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1492 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
| 1496 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1497 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1498 | /// It rewrites: |
| 1499 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1501 | /// Into: |
| 1502 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1504 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1505 | /// id items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1506 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1508 | /// objects:items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1509 | /// if (limit) { |
| 1510 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1511 | /// do { |
| 1512 | /// unsigned long counter = 0; |
| 1513 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1515 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1516 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1517 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1518 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1519 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1521 | /// objects:items count:16]); |
| 1522 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1523 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1524 | /// } |
| 1525 | /// else |
| 1526 | /// elem = nil; |
| 1527 | /// } |
| 1528 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1529 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1530 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1531 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1533 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1535 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1537 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1538 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1539 | const char *elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1540 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1541 | std::string buf; |
| 1542 | buf = "\n{\n\t"; |
| 1543 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1544 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1545 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1546 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1547 | if (ElementType->isObjCQualifiedIdType() || |
| 1548 | ElementType->isObjCQualifiedInterfaceType()) |
| 1549 | // Simply use 'id' for all qualified types. |
| 1550 | elementTypeAsString = "id"; |
| 1551 | else |
| 1552 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1553 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1554 | buf += " "; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1555 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1556 | buf += elementName; |
| 1557 | buf += ";\n\t"; |
| 1558 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1559 | else { |
| 1560 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1561 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1562 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1563 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1564 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1565 | // Simply use 'id' for all qualified types. |
| 1566 | elementTypeAsString = "id"; |
| 1567 | else |
| 1568 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1569 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1571 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1572 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1573 | // id items[16]; |
| 1574 | buf += "id items[16];\n\t"; |
| 1575 | // id l_collection = (id) |
| 1576 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1577 | // Find start location of 'collection' the hard way! |
| 1578 | const char *startCollectionBuf = startBuf; |
| 1579 | startCollectionBuf += 3; // skip 'for' |
| 1580 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1581 | startCollectionBuf++; // skip '(' |
| 1582 | // find 'in' and skip it. |
| 1583 | while (*startCollectionBuf != ' ' || |
| 1584 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1585 | (*(startCollectionBuf+3) != ' ' && |
| 1586 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1587 | startCollectionBuf++; |
| 1588 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
| 1590 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1591 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1592 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1593 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1594 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1595 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1596 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1598 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1599 | // objects:items count:16]; |
| 1600 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1602 | // ((unsigned int (*) |
| 1603 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1605 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | // "countByEnumeratingWithState:objects:count:"), |
| 1607 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1608 | // (id *)items, (unsigned int)16); |
| 1609 | buf += "unsigned long limit =\n\t\t"; |
| 1610 | SynthCountByEnumWithState(buf); |
| 1611 | buf += ";\n\t"; |
| 1612 | /// if (limit) { |
| 1613 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1614 | /// do { |
| 1615 | /// unsigned long counter = 0; |
| 1616 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1618 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1619 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1620 | buf += "if (limit) {\n\t"; |
| 1621 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1622 | buf += "do {\n\t\t"; |
| 1623 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1624 | buf += "do {\n\t\t\t"; |
| 1625 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1626 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1627 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1628 | buf += " = ("; |
| 1629 | buf += elementTypeAsString; |
| 1630 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1631 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1632 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1634 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1635 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1637 | /// objects:items count:16]); |
| 1638 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1639 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1640 | /// } |
| 1641 | /// else |
| 1642 | /// elem = nil; |
| 1643 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1645 | buf = ";\n\t"; |
| 1646 | buf += "__continue_label_"; |
| 1647 | buf += utostr(ObjCBcLabelNo.back()); |
| 1648 | buf += ": ;"; |
| 1649 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1650 | buf += "} while (counter < limit);\n\t"; |
| 1651 | buf += "} while (limit = "; |
| 1652 | SynthCountByEnumWithState(buf); |
| 1653 | buf += ");\n\t"; |
| 1654 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1655 | buf += " = (("; |
| 1656 | buf += elementTypeAsString; |
| 1657 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1658 | buf += "__break_label_"; |
| 1659 | buf += utostr(ObjCBcLabelNo.back()); |
| 1660 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1661 | buf += "}\n\t"; |
| 1662 | buf += "else\n\t\t"; |
| 1663 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1664 | buf += " = (("; |
| 1665 | buf += elementTypeAsString; |
| 1666 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1667 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1669 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1670 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1671 | if (isa<CompoundStmt>(S->getBody())) { |
| 1672 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1673 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1674 | } else { |
| 1675 | /* Need to treat single statements specially. For example: |
| 1676 | * |
| 1677 | * for (A *a in b) if (stuff()) break; |
| 1678 | * for (A *a in b) xxxyy; |
| 1679 | * |
| 1680 | * The following code simply scans ahead to the semi to find the actual end. |
| 1681 | */ |
| 1682 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1683 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1684 | assert(semiBuf && "Can't find ';'"); |
| 1685 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1686 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1687 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1688 | Stmts.pop_back(); |
| 1689 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1690 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1694 | /// This routine rewrites @synchronized(expr) stmt; |
| 1695 | /// into: |
| 1696 | /// objc_sync_enter(expr); |
| 1697 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1698 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1699 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1700 | // Get the start location and compute the semi location. |
| 1701 | SourceLocation startLoc = S->getLocStart(); |
| 1702 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1704 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
| 1706 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1707 | buf = "objc_sync_enter((id)"; |
| 1708 | const char *lparenBuf = startBuf; |
| 1709 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1710 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1712 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1713 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1714 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1715 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1716 | while (*endBuf != ')') endBuf--; |
| 1717 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1718 | buf = ");\n"; |
| 1719 | // declare a new scope with two variables, _stack and _rethrow. |
| 1720 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1721 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1722 | buf += "char *pointers[4];} _stack;\n"; |
| 1723 | buf += "id volatile _rethrow = 0;\n"; |
| 1724 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1725 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1726 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1727 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1728 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1730 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1731 | SourceLocation lastCurlyLoc = startLoc; |
| 1732 | buf = "}\nelse {\n"; |
| 1733 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1734 | buf += "}\n"; |
| 1735 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1736 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1737 | |
| 1738 | std::string syncBuf; |
| 1739 | syncBuf += " objc_sync_exit("; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1740 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1741 | CastExpr::CK_Unknown, |
| 1742 | S->getSynchExpr()); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1743 | std::string syncExprBufS; |
| 1744 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1745 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1746 | PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1747 | syncBuf += syncExprBuf.str(); |
| 1748 | syncBuf += ");"; |
| 1749 | |
| 1750 | buf += syncBuf; |
| 1751 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1752 | buf += "}\n"; |
| 1753 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1755 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1756 | |
| 1757 | bool hasReturns = false; |
| 1758 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1759 | if (hasReturns) |
| 1760 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1761 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
| 1764 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1765 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1766 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1767 | // Perform a bottom up traversal of all children. |
| 1768 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1769 | CI != E; ++CI) |
| 1770 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1771 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1772 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1773 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1775 | TryFinallyContainsReturnDiag); |
| 1776 | } |
| 1777 | return; |
| 1778 | } |
| 1779 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1780 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1781 | { |
| 1782 | // Perform a bottom up traversal of all children. |
| 1783 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1784 | CI != E; ++CI) |
| 1785 | if (*CI) |
| 1786 | HasReturnStmts(*CI, hasReturns); |
| 1787 | |
| 1788 | if (isa<ReturnStmt>(S)) |
| 1789 | hasReturns = true; |
| 1790 | return; |
| 1791 | } |
| 1792 | |
| 1793 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1794 | // Perform a bottom up traversal of all children. |
| 1795 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1796 | CI != E; ++CI) |
| 1797 | if (*CI) { |
| 1798 | RewriteTryReturnStmts(*CI); |
| 1799 | } |
| 1800 | if (isa<ReturnStmt>(S)) { |
| 1801 | SourceLocation startLoc = S->getLocStart(); |
| 1802 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1803 | |
| 1804 | const char *semiBuf = strchr(startBuf, ';'); |
| 1805 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1806 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1807 | |
| 1808 | std::string buf; |
| 1809 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1810 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1811 | ReplaceText(startLoc, 6, buf); |
| 1812 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1813 | } |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1818 | // Perform a bottom up traversal of all children. |
| 1819 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1820 | CI != E; ++CI) |
| 1821 | if (*CI) { |
| 1822 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1823 | } |
| 1824 | if (isa<ReturnStmt>(S)) { |
| 1825 | SourceLocation startLoc = S->getLocStart(); |
| 1826 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1827 | |
| 1828 | const char *semiBuf = strchr(startBuf, ';'); |
| 1829 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1830 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1831 | |
| 1832 | std::string buf; |
| 1833 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1834 | buf += syncExitBuf; |
| 1835 | buf += " return"; |
| 1836 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1837 | ReplaceText(startLoc, 6, buf); |
| 1838 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1839 | } |
| 1840 | return; |
| 1841 | } |
| 1842 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1843 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1844 | // Get the start location and compute the semi location. |
| 1845 | SourceLocation startLoc = S->getLocStart(); |
| 1846 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1848 | assert((*startBuf == '@') && "bogus @try location"); |
| 1849 | |
| 1850 | std::string buf; |
| 1851 | // declare a new scope with two variables, _stack and _rethrow. |
| 1852 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1853 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1854 | buf += "char *pointers[4];} _stack;\n"; |
| 1855 | buf += "id volatile _rethrow = 0;\n"; |
| 1856 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1857 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1858 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1859 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1861 | startLoc = S->getTryBody()->getLocEnd(); |
| 1862 | startBuf = SM->getCharacterData(startLoc); |
| 1863 | |
| 1864 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1866 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1867 | if (S->getNumCatchStmts()) { |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1868 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1869 | buf = " /* @catch begin */ else {\n"; |
| 1870 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1871 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1872 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1873 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1874 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1876 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1877 | } else { /* no catch list */ |
| 1878 | buf = "}\nelse {\n"; |
| 1879 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1880 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1881 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1882 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1883 | bool sawIdTypedCatch = false; |
| 1884 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1885 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1886 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1887 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1888 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1889 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1890 | buf = "if ("; // we are generating code for the first catch clause |
| 1891 | else |
| 1892 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1893 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1894 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1896 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1898 | const char *lParenLoc = strchr(startBuf, '('); |
| 1899 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1900 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1901 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1902 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1903 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1904 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1905 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1906 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1907 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1909 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1910 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1911 | } else if (catchDecl) { |
| 1912 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1913 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1914 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1915 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1916 | sawIdTypedCatch = true; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1917 | } else if (const ObjCObjectPointerType *Ptr = |
| 1918 | t->getAs<ObjCObjectPointerType>()) { |
| 1919 | // Should be a pointer to a class. |
| 1920 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1921 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1922 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1923 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1924 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1925 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1929 | lastCatchBody = Catch->getCatchBody(); |
| 1930 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1931 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1932 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1933 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1934 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1935 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1938 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1939 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1940 | } else { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1941 | assert(false && "@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1942 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1943 | } |
| 1944 | // Complete the catch list... |
| 1945 | if (lastCatchBody) { |
| 1946 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1947 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1948 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1950 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1951 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1952 | buf = "} /* last catch end */\n"; |
| 1953 | buf += "else {\n"; |
| 1954 | buf += " _rethrow = _caught;\n"; |
| 1955 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1956 | buf += "} } /* @catch end */\n"; |
| 1957 | if (!S->getFinallyStmt()) |
| 1958 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1959 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1961 | // Set lastCurlyLoc |
| 1962 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1963 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1964 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1965 | startLoc = finalStmt->getLocStart(); |
| 1966 | startBuf = SM->getCharacterData(startLoc); |
| 1967 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1969 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1971 | Stmt *body = finalStmt->getFinallyBody(); |
| 1972 | SourceLocation startLoc = body->getLocStart(); |
| 1973 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1974 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1975 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1977 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1979 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1980 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1981 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1982 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1984 | // Set lastCurlyLoc |
| 1985 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1987 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1988 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1989 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1990 | buf = "{ /* implicit finally clause */\n"; |
| 1991 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1992 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1993 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1994 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Now check for any return/continue/go statements within the @try. |
| 1997 | // The implicit finally clause won't called if the @try contains any |
| 1998 | // jump statements. |
| 1999 | bool hasReturns = false; |
| 2000 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2001 | if (hasReturns) |
| 2002 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2003 | } |
| 2004 | // Now emit the final closing curly brace... |
| 2005 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2006 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
| 2009 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2010 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2014 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2015 | return 0; |
| 2016 | } |
| 2017 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2019 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2020 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2021 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2022 | // Get the start location and compute the semi location. |
| 2023 | SourceLocation startLoc = S->getLocStart(); |
| 2024 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2026 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2027 | |
| 2028 | std::string buf; |
| 2029 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2030 | if (S->getThrowExpr()) |
| 2031 | buf = "objc_exception_throw("; |
| 2032 | else // add an implicit argument |
| 2033 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2035 | // handle "@ throw" correctly. |
| 2036 | const char *wBuf = strchr(startBuf, 'w'); |
| 2037 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2038 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2040 | const char *semiBuf = strchr(startBuf, ';'); |
| 2041 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2042 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2043 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2046 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2047 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2048 | // Create a new string expression. |
| 2049 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2050 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2051 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2052 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 2053 | StrEncoding.length(), false,StrType, |
| 2054 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2055 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2057 | // Replace this subexpr in the parent. |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2058 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2059 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2062 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2063 | if (!SelGetUidFunctionDecl) |
| 2064 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2065 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2066 | // Create a call to sel_registerName("selName"). |
| 2067 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2068 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2070 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2071 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2072 | false, argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2073 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2074 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2075 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2076 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2077 | return SelExp; |
| 2078 | } |
| 2079 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2080 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2081 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2082 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2083 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2084 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2086 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2087 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2089 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 2090 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2091 | ImplicitCastExpr *ICE = |
| 2092 | new (Context) ImplicitCastExpr(pToFunc, CastExpr::CK_Unknown, |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 2093 | DRE, CXXBaseSpecifierArray(), |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2094 | /*isLvalue=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2096 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2098 | CallExpr *Exp = |
| 2099 | new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 2100 | EndLoc); |
| 2101 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2104 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2105 | const char *&startRef, const char *&endRef) { |
| 2106 | while (startBuf < endBuf) { |
| 2107 | if (*startBuf == '<') |
| 2108 | startRef = startBuf; // mark the start. |
| 2109 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2110 | if (startRef && *startRef == '<') { |
| 2111 | endRef = startBuf; // mark the end. |
| 2112 | return true; |
| 2113 | } |
| 2114 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2115 | } |
| 2116 | startBuf++; |
| 2117 | } |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2121 | static void scanToNextArgument(const char *&argRef) { |
| 2122 | int angle = 0; |
| 2123 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2124 | if (*argRef == '<') |
| 2125 | angle++; |
| 2126 | else if (*argRef == '>') |
| 2127 | angle--; |
| 2128 | argRef++; |
| 2129 | } |
| 2130 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2131 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2132 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2133 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2134 | if (T->isObjCQualifiedIdType()) |
| 2135 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2136 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2137 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2138 | return true; |
| 2139 | } |
| 2140 | if (T->isObjCObjectPointerType()) { |
| 2141 | T = T->getPointeeType(); |
| 2142 | return T->isObjCQualifiedInterfaceType(); |
| 2143 | } |
| 2144 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2147 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2148 | QualType Type = E->getType(); |
| 2149 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2150 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2152 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2153 | Loc = ECE->getLParenLoc(); |
| 2154 | EndLoc = ECE->getRParenLoc(); |
| 2155 | } else { |
| 2156 | Loc = E->getLocStart(); |
| 2157 | EndLoc = E->getLocEnd(); |
| 2158 | } |
| 2159 | // This will defend against trying to rewrite synthesized expressions. |
| 2160 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2161 | return; |
| 2162 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2163 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2164 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2165 | const char *startRef = 0, *endRef = 0; |
| 2166 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2167 | // Get the locations of the startRef, endRef. |
| 2168 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2169 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2170 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2171 | InsertText(LessLoc, "/*"); |
| 2172 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2177 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2178 | SourceLocation Loc; |
| 2179 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2180 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2181 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2182 | Loc = VD->getLocation(); |
| 2183 | Type = VD->getType(); |
| 2184 | } |
| 2185 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2186 | Loc = FD->getLocation(); |
| 2187 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2188 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2189 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2190 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2191 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2192 | if (!proto) |
| 2193 | return; |
| 2194 | Type = proto->getResultType(); |
| 2195 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2196 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2197 | Loc = FD->getLocation(); |
| 2198 | Type = FD->getType(); |
| 2199 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2200 | else |
| 2201 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2203 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2204 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2206 | const char *endBuf = SM->getCharacterData(Loc); |
| 2207 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2208 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2209 | startBuf--; // scan backward (from the decl location) for return type. |
| 2210 | const char *startRef = 0, *endRef = 0; |
| 2211 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2212 | // Get the locations of the startRef, endRef. |
| 2213 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2214 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2215 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2216 | InsertText(LessLoc, "/*"); |
| 2217 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2220 | if (!proto) |
| 2221 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2222 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2223 | const char *startBuf = SM->getCharacterData(Loc); |
| 2224 | const char *startFuncBuf = startBuf; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2225 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2226 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2227 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2229 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2230 | // scan forward (from the decl location) for argument types. |
| 2231 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2232 | const char *startRef = 0, *endRef = 0; |
| 2233 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2234 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | SourceLocation LessLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2236 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2238 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2239 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2240 | InsertText(LessLoc, "/*"); |
| 2241 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2242 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2243 | startBuf = ++endBuf; |
| 2244 | } |
| 2245 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2246 | // If the function name is derived from a macro expansion, then the |
| 2247 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2248 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2249 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2250 | startBuf++; |
| 2251 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2252 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2255 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2256 | QualType QT = ND->getType(); |
| 2257 | const Type* TypePtr = QT->getAs<Type>(); |
| 2258 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2259 | return; |
| 2260 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2261 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2262 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2263 | TypePtr = QT->getAs<Type>(); |
| 2264 | } |
| 2265 | // FIXME. This will not work for multiple declarators; as in: |
| 2266 | // __typeof__(a) b,c,d; |
| 2267 | std::string TypeAsString(QT.getAsString()); |
| 2268 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2269 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2270 | if (ND->getInit()) { |
| 2271 | std::string Name(ND->getNameAsString()); |
| 2272 | TypeAsString += " " + Name + " = "; |
| 2273 | Expr *E = ND->getInit(); |
| 2274 | SourceLocation startLoc; |
| 2275 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2276 | startLoc = ECE->getLParenLoc(); |
| 2277 | else |
| 2278 | startLoc = E->getLocStart(); |
| 2279 | startLoc = SM->getInstantiationLoc(startLoc); |
| 2280 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2281 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2282 | } |
| 2283 | else { |
| 2284 | SourceLocation X = ND->getLocEnd(); |
| 2285 | X = SM->getInstantiationLoc(X); |
| 2286 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2287 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2291 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2292 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2293 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2294 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2295 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2296 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2297 | &ArgTys[0], ArgTys.size(), |
| 2298 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2299 | false, false, 0, 0, |
| 2300 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2301 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2303 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2304 | FunctionDecl::Extern, |
| 2305 | FunctionDecl::None, false); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2308 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2309 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2310 | if (FD->getIdentifier() && |
| 2311 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2312 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2313 | return; |
| 2314 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2315 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2318 | static void RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2319 | std::string TypeString(Type.getAsString()); |
| 2320 | const char *argPtr = TypeString.c_str(); |
| 2321 | if (!strchr(argPtr, '^')) { |
| 2322 | Str += TypeString; |
| 2323 | return; |
| 2324 | } |
| 2325 | while (*argPtr) { |
| 2326 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2327 | argPtr++; |
| 2328 | } |
| 2329 | } |
| 2330 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2331 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2332 | static void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD) { |
| 2333 | QualType Type = VD->getType(); |
| 2334 | std::string TypeString(Type.getAsString()); |
| 2335 | const char *argPtr = TypeString.c_str(); |
| 2336 | int paren = 0; |
| 2337 | while (*argPtr) { |
| 2338 | switch (*argPtr) { |
| 2339 | case '(': |
| 2340 | Str += *argPtr; |
| 2341 | paren++; |
| 2342 | break; |
| 2343 | case ')': |
| 2344 | Str += *argPtr; |
| 2345 | paren--; |
| 2346 | break; |
| 2347 | case '^': |
| 2348 | Str += '*'; |
| 2349 | if (paren == 1) |
| 2350 | Str += VD->getNameAsString(); |
| 2351 | break; |
| 2352 | default: |
| 2353 | Str += *argPtr; |
| 2354 | break; |
| 2355 | } |
| 2356 | argPtr++; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2361 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2362 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2363 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2364 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2365 | if (!proto) |
| 2366 | return; |
| 2367 | QualType Type = proto->getResultType(); |
| 2368 | std::string FdStr = Type.getAsString(); |
| 2369 | FdStr += " "; |
| 2370 | FdStr += FD->getNameAsCString(); |
| 2371 | FdStr += "("; |
| 2372 | unsigned numArgs = proto->getNumArgs(); |
| 2373 | for (unsigned i = 0; i < numArgs; i++) { |
| 2374 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2375 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2376 | if (i+1 < numArgs) |
| 2377 | FdStr += ", "; |
| 2378 | } |
| 2379 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2380 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2381 | CurFunctionDeclToDeclareForBlock = 0; |
| 2382 | } |
| 2383 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2384 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2385 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2386 | if (SuperContructorFunctionDecl) |
| 2387 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2388 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2389 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2390 | QualType argT = Context->getObjCIdType(); |
| 2391 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2392 | ArgTys.push_back(argT); |
| 2393 | ArgTys.push_back(argT); |
| 2394 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2395 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2396 | false, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2397 | false, false, 0, 0, |
| 2398 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2399 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2401 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2402 | FunctionDecl::Extern, |
| 2403 | FunctionDecl::None, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2406 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2407 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2408 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2409 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2410 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2411 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2412 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2413 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2414 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2415 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2416 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2417 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2418 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2419 | false, false, 0, 0, |
| 2420 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2421 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2422 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2423 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2424 | FunctionDecl::Extern, |
| 2425 | FunctionDecl::None, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2428 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2429 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2430 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2431 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2432 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2433 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2434 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2435 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2436 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2437 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2438 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2439 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2440 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2441 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2442 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2443 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2444 | false, false, 0, 0, |
| 2445 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2446 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2448 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2449 | FunctionDecl::Extern, |
| 2450 | FunctionDecl::None, false); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2453 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2454 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2455 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2456 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2457 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2458 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2459 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2460 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2461 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2462 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2463 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2464 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2465 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2466 | false, false, 0, 0, |
| 2467 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2468 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2470 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2471 | FunctionDecl::Extern, |
| 2472 | FunctionDecl::None, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2476 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2477 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2479 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2480 | llvm::SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2481 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2482 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2483 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2484 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2485 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2486 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2487 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2488 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2489 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2490 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2491 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2492 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2493 | false, false, 0, 0, |
| 2494 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2495 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2496 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2497 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2498 | FunctionDecl::Extern, |
| 2499 | FunctionDecl::None, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2502 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2503 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2504 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2505 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2506 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2507 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2508 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2509 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2510 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2511 | ArgTys.push_back(argT); |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2512 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2513 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2514 | true /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2515 | false, false, 0, 0, |
| 2516 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2517 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2519 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2520 | FunctionDecl::Extern, |
| 2521 | FunctionDecl::None, false); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2524 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2525 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2526 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2527 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2528 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2529 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2530 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2531 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2532 | false, false, 0, 0, |
| 2533 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2534 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2536 | getClassIdent, getClassType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2537 | FunctionDecl::Extern, |
| 2538 | FunctionDecl::None, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2541 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2542 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2543 | IdentifierInfo *getSuperClassIdent = |
| 2544 | &Context->Idents.get("class_getSuperclass"); |
| 2545 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2546 | ArgTys.push_back(Context->getObjCClassType()); |
| 2547 | QualType getClassType = Context->getFunctionType(Context->getObjCClassType(), |
| 2548 | &ArgTys[0], ArgTys.size(), |
| 2549 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2550 | false, false, 0, 0, |
| 2551 | FunctionType::ExtInfo()); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2552 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2553 | SourceLocation(), |
| 2554 | getSuperClassIdent, |
| 2555 | getClassType, 0, |
| 2556 | FunctionDecl::Extern, |
| 2557 | FunctionDecl::None, |
| 2558 | false); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2561 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2562 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2563 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2564 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2565 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2566 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2567 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2568 | false /*isVariadic*/, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2569 | false, false, 0, 0, |
| 2570 | FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2571 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2573 | getClassIdent, getClassType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2574 | FunctionDecl::Extern, |
| 2575 | FunctionDecl::None, false); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2578 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2579 | QualType strType = getConstantStringStructType(); |
| 2580 | |
| 2581 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2582 | |
| 2583 | std::string tmpName = InFileName; |
| 2584 | unsigned i; |
| 2585 | for (i=0; i < tmpName.length(); i++) { |
| 2586 | char c = tmpName.at(i); |
| 2587 | // replace any non alphanumeric characters with '_'. |
| 2588 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2589 | tmpName[i] = '_'; |
| 2590 | } |
| 2591 | S += tmpName; |
| 2592 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2593 | S += utostr(NumObjCStringLiterals++); |
| 2594 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2595 | Preamble += "static __NSConstantStringImpl " + S; |
| 2596 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2597 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2598 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2599 | std::string prettyBufS; |
| 2600 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2601 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2602 | PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2603 | Preamble += prettyBuf.str(); |
| 2604 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2605 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
| 2607 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2608 | &Context->Idents.get(S), strType, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2609 | VarDecl::Static, VarDecl::None); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2610 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2611 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | Context->getPointerType(DRE->getType()), |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2613 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2614 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2615 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2616 | CastExpr::CK_Unknown, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2617 | ReplaceStmt(Exp, cast); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2618 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2619 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2622 | bool RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2623 | // check if we are sending a message to 'super' |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2624 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return false; |
| 2625 | return isa<ObjCSuperExpr>(recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2629 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2630 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2631 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2633 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2634 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2636 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2638 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2640 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2641 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2642 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2644 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2645 | FieldTypes[i], 0, |
| 2646 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2647 | /*Mutable=*/false)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2648 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2650 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2651 | } |
| 2652 | return Context->getTagDeclType(SuperStructDecl); |
| 2653 | } |
| 2654 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2655 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2656 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2657 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2659 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2660 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2662 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2664 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2666 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2668 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2670 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2671 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2672 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2674 | ConstantStringDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2675 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2676 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2677 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2678 | /*Mutable=*/true)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2681 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2682 | } |
| 2683 | return Context->getTagDeclType(ConstantStringDecl); |
| 2684 | } |
| 2685 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2686 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2687 | SourceLocation StartLoc, |
| 2688 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2689 | if (!SelGetUidFunctionDecl) |
| 2690 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2691 | if (!MsgSendFunctionDecl) |
| 2692 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2693 | if (!MsgSendSuperFunctionDecl) |
| 2694 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2695 | if (!MsgSendStretFunctionDecl) |
| 2696 | SynthMsgSendStretFunctionDecl(); |
| 2697 | if (!MsgSendSuperStretFunctionDecl) |
| 2698 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2699 | if (!MsgSendFpretFunctionDecl) |
| 2700 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2701 | if (!GetClassFunctionDecl) |
| 2702 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2703 | if (!GetSuperClassFunctionDecl) |
| 2704 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2705 | if (!GetMetaClassFunctionDecl) |
| 2706 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2708 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2709 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2710 | // May need to use objc_msgSend_stret() as well. |
| 2711 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2712 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2713 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2714 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2715 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2716 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2717 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2720 | // Synthesize a call to objc_msgSend(). |
| 2721 | llvm::SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2722 | switch (Exp->getReceiverKind()) { |
| 2723 | case ObjCMessageExpr::SuperClass: { |
| 2724 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2725 | if (MsgSendStretFlavor) |
| 2726 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2727 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2729 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2731 | llvm::SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2733 | // set the receiver to self, the first argument to all methods. |
| 2734 | InitExprs.push_back( |
| 2735 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2736 | CastExpr::CK_Unknown, |
| 2737 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2738 | Context->getObjCIdType(), |
| 2739 | SourceLocation())) |
| 2740 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2742 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2743 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2744 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2745 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2746 | ClassDecl->getIdentifier()->getNameStart(), |
| 2747 | ClassDecl->getIdentifier()->getLength(), |
| 2748 | false, argType, SourceLocation())); |
| 2749 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2750 | &ClsExprs[0], |
| 2751 | ClsExprs.size(), |
| 2752 | StartLoc, |
| 2753 | EndLoc); |
| 2754 | // (Class)objc_getClass("CurrentClass") |
| 2755 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2756 | Context->getObjCClassType(), |
| 2757 | CastExpr::CK_Unknown, Cls); |
| 2758 | ClsExprs.clear(); |
| 2759 | ClsExprs.push_back(ArgExpr); |
| 2760 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2761 | &ClsExprs[0], ClsExprs.size(), |
| 2762 | StartLoc, EndLoc); |
| 2763 | |
| 2764 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2765 | // To turn off a warning, type-cast to 'id' |
| 2766 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2767 | NoTypeInfoCStyleCastExpr(Context, |
| 2768 | Context->getObjCIdType(), |
| 2769 | CastExpr::CK_Unknown, Cls)); |
| 2770 | // struct objc_super |
| 2771 | QualType superType = getSuperStructType(); |
| 2772 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2773 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2774 | if (LangOpts.Microsoft) { |
| 2775 | SynthSuperContructorFunctionDecl(); |
| 2776 | // Simulate a contructor call... |
| 2777 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2778 | superType, SourceLocation()); |
| 2779 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2780 | InitExprs.size(), |
| 2781 | superType, SourceLocation()); |
| 2782 | // The code for super is a little tricky to prevent collision with |
| 2783 | // the structure definition in the header. The rewriter has it's own |
| 2784 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2785 | // we need the cast below. For example: |
| 2786 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2787 | // |
| 2788 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2789 | Context->getPointerType(SuperRep->getType()), |
| 2790 | SourceLocation()); |
| 2791 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2792 | Context->getPointerType(superType), |
| 2793 | CastExpr::CK_Unknown, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2794 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2795 | // (struct objc_super) { <exprs from above> } |
| 2796 | InitListExpr *ILE = |
| 2797 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2798 | &InitExprs[0], InitExprs.size(), |
| 2799 | SourceLocation()); |
| 2800 | TypeSourceInfo *superTInfo |
| 2801 | = Context->getTrivialTypeSourceInfo(superType); |
| 2802 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2803 | superType, ILE, false); |
| 2804 | // struct objc_super * |
| 2805 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2806 | Context->getPointerType(SuperRep->getType()), |
| 2807 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2808 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2809 | MsgExprs.push_back(SuperRep); |
| 2810 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2811 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2812 | |
| 2813 | case ObjCMessageExpr::Class: { |
| 2814 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2815 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2816 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2817 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2818 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2819 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2820 | clsName->getNameStart(), |
| 2821 | clsName->getLength(), |
| 2822 | false, argType, |
| 2823 | SourceLocation())); |
| 2824 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2825 | &ClsExprs[0], |
| 2826 | ClsExprs.size(), |
| 2827 | StartLoc, EndLoc); |
| 2828 | MsgExprs.push_back(Cls); |
| 2829 | break; |
| 2830 | } |
| 2831 | |
| 2832 | case ObjCMessageExpr::SuperInstance:{ |
| 2833 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2834 | if (MsgSendStretFlavor) |
| 2835 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2836 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2837 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 2838 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2839 | |
| 2840 | InitExprs.push_back( |
| 2841 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2842 | CastExpr::CK_Unknown, |
| 2843 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
| 2844 | Context->getObjCIdType(), |
| 2845 | SourceLocation())) |
| 2846 | ); // set the 'receiver'. |
| 2847 | |
| 2848 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2849 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2850 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2851 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2852 | ClassDecl->getIdentifier()->getNameStart(), |
| 2853 | ClassDecl->getIdentifier()->getLength(), |
| 2854 | false, argType, SourceLocation())); |
| 2855 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2856 | &ClsExprs[0], |
| 2857 | ClsExprs.size(), |
| 2858 | StartLoc, EndLoc); |
| 2859 | // (Class)objc_getClass("CurrentClass") |
| 2860 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2861 | Context->getObjCClassType(), |
| 2862 | CastExpr::CK_Unknown, Cls); |
| 2863 | ClsExprs.clear(); |
| 2864 | ClsExprs.push_back(ArgExpr); |
| 2865 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2866 | &ClsExprs[0], ClsExprs.size(), |
| 2867 | StartLoc, EndLoc); |
| 2868 | |
| 2869 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2870 | // To turn off a warning, type-cast to 'id' |
| 2871 | InitExprs.push_back( |
| 2872 | // set 'super class', using class_getSuperclass(). |
| 2873 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2874 | CastExpr::CK_Unknown, Cls)); |
| 2875 | // struct objc_super |
| 2876 | QualType superType = getSuperStructType(); |
| 2877 | Expr *SuperRep; |
| 2878 | |
| 2879 | if (LangOpts.Microsoft) { |
| 2880 | SynthSuperContructorFunctionDecl(); |
| 2881 | // Simulate a contructor call... |
| 2882 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
| 2883 | superType, SourceLocation()); |
| 2884 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2885 | InitExprs.size(), |
| 2886 | superType, SourceLocation()); |
| 2887 | // The code for super is a little tricky to prevent collision with |
| 2888 | // the structure definition in the header. The rewriter has it's own |
| 2889 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2890 | // we need the cast below. For example: |
| 2891 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2892 | // |
| 2893 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2894 | Context->getPointerType(SuperRep->getType()), |
| 2895 | SourceLocation()); |
| 2896 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2897 | Context->getPointerType(superType), |
| 2898 | CastExpr::CK_Unknown, SuperRep); |
| 2899 | } else { |
| 2900 | // (struct objc_super) { <exprs from above> } |
| 2901 | InitListExpr *ILE = |
| 2902 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2903 | &InitExprs[0], InitExprs.size(), |
| 2904 | SourceLocation()); |
| 2905 | TypeSourceInfo *superTInfo |
| 2906 | = Context->getTrivialTypeSourceInfo(superType); |
| 2907 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2908 | superType, ILE, false); |
| 2909 | } |
| 2910 | MsgExprs.push_back(SuperRep); |
| 2911 | break; |
| 2912 | } |
| 2913 | |
| 2914 | case ObjCMessageExpr::Instance: { |
| 2915 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2916 | // Foo<Proto> *. |
| 2917 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2918 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2919 | recExpr = CE->getSubExpr(); |
| 2920 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2921 | CastExpr::CK_Unknown, recExpr); |
| 2922 | MsgExprs.push_back(recExpr); |
| 2923 | break; |
| 2924 | } |
| 2925 | } |
| 2926 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2927 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2928 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2929 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2931 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2932 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2933 | false, argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2934 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2935 | &SelExprs[0], SelExprs.size(), |
| 2936 | StartLoc, |
| 2937 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2938 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2940 | // Now push any user supplied arguments. |
| 2941 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2942 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2943 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2944 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2945 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2946 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2947 | ? Context->getObjCIdType() |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2948 | : ICE->getType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame^] | 2949 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 2950 | if (isTopLevelBlockPointerType(type)) { |
| 2951 | const BlockPointerType *BPT = type->getAs<BlockPointerType>(); |
| 2952 | type = Context->getPointerType(BPT->getPointeeType()); |
| 2953 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2954 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CastExpr::CK_Unknown, |
| 2955 | userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2956 | } |
| 2957 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2958 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2959 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2960 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2961 | userExpr = CE->getSubExpr(); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2962 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2963 | CastExpr::CK_Unknown, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2964 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2966 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2967 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2968 | // out the argument in the original expression (since we aren't deleting |
| 2969 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2970 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2971 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2972 | // Generate the funky cast. |
| 2973 | CastExpr *cast; |
| 2974 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2975 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2977 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2978 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2979 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2980 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2981 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2982 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2983 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2984 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2985 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2986 | E = OMD->param_end(); PI != E; ++PI) { |
| 2987 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2989 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2990 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2991 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2992 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2993 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2994 | } |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2995 | ArgTypes.push_back(t); |
| 2996 | } |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2997 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2998 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | f89eb2b | 2010-02-24 01:25:40 +0000 | [diff] [blame] | 2999 | if (isTopLevelBlockPointerType(returnType)) { |
| 3000 | const BlockPointerType *BPT = returnType->getAs<BlockPointerType>(); |
| 3001 | returnType = Context->getPointerType(BPT->getPointeeType()); |
| 3002 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3003 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3004 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3005 | } |
| 3006 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 3007 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3009 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3011 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3012 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3014 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3015 | // xx.m:13: warning: function called through a non-compatible type |
| 3016 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3017 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3018 | Context->getPointerType(Context->VoidTy), |
| 3019 | CastExpr::CK_Unknown, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3021 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3023 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 327f0f4 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 3024 | // If we don't have a method decl, force a variadic cast. |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3025 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3026 | false, false, 0, 0, |
| 3027 | FunctionType::ExtInfo()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3028 | castType = Context->getPointerType(castType); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3029 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 3030 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3031 | |
| 3032 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3033 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3035 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3036 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | MsgExprs.size(), |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3038 | FT->getResultType(), EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3039 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3040 | if (MsgSendStretFlavor) { |
| 3041 | // We have the method which returns a struct/union. Must also generate |
| 3042 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3043 | // expression which dictate which one to envoke depending on size of |
| 3044 | // method's return type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3046 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3048 | SourceLocation()); |
| 3049 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3050 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3051 | Context->getPointerType(Context->VoidTy), |
| 3052 | CastExpr::CK_Unknown, STDRE); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3053 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 3055 | &ArgTypes[0], ArgTypes.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3056 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3057 | false, false, 0, 0, |
| 3058 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3059 | castType = Context->getPointerType(castType); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3060 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 3061 | cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3063 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3064 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3066 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3067 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3069 | FT->getResultType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3071 | // Build sizeof(returnType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3073 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3074 | Context->getSizeType(), |
| 3075 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3076 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3077 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3078 | // For X86 it is more complicated and some kind of target specific routine |
| 3079 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3081 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3083 | Context->IntTy, |
| 3084 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 3086 | BinaryOperator::LE, |
| 3087 | Context->IntTy, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3088 | SourceLocation()); |
| 3089 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3091 | new (Context) ConditionalOperator(lessThanExpr, |
| 3092 | SourceLocation(), CE, |
| 3093 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3094 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3095 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3097 | return ReplacingStmt; |
| 3098 | } |
| 3099 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3100 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3101 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3102 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3104 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3105 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | |
| 3107 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3108 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3111 | // typedef struct objc_object Protocol; |
| 3112 | QualType RewriteObjC::getProtocolType() { |
| 3113 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3114 | TypeSourceInfo *TInfo |
| 3115 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3116 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3118 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3119 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3120 | } |
| 3121 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3122 | } |
| 3123 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3124 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3125 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3126 | /// The forward references (and metadata) are generated in |
| 3127 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3128 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3129 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3130 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3132 | ID, getProtocolType(), 0, |
| 3133 | VarDecl::Extern, VarDecl::None); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3134 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 3135 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 3136 | Context->getPointerType(DRE->getType()), |
| 3137 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3138 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3139 | CastExpr::CK_Unknown, |
| 3140 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3141 | ReplaceStmt(Exp, castExpr); |
| 3142 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3144 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3149 | const char *endBuf) { |
| 3150 | while (startBuf < endBuf) { |
| 3151 | if (*startBuf == '#') { |
| 3152 | // Skip whitespace. |
| 3153 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3154 | ; |
| 3155 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3156 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3157 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3158 | !strncmp(startBuf, "define", strlen("define")) || |
| 3159 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3160 | !strncmp(startBuf, "else", strlen("else")) || |
| 3161 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3162 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3163 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3164 | !strncmp(startBuf, "include", strlen("include")) || |
| 3165 | !strncmp(startBuf, "import", strlen("import")) || |
| 3166 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3167 | return true; |
| 3168 | } |
| 3169 | startBuf++; |
| 3170 | } |
| 3171 | return false; |
| 3172 | } |
| 3173 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3174 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3175 | /// an objective-c class with ivars. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3176 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3177 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3178 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3180 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3181 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3182 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3183 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3184 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3185 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3186 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3187 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3189 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3190 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3192 | // If no ivars and no root or if its root, directly or indirectly, |
| 3193 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3194 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3195 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3196 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3197 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3198 | return; |
| 3199 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
| 3201 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3202 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3203 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3204 | Result += CDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3205 | if (LangOpts.Microsoft) |
| 3206 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3207 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3208 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3209 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3211 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3212 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3213 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3214 | // NSURL.h, for example): |
| 3215 | // |
| 3216 | // #ifdef XYZ |
| 3217 | // @interface Foo : NSObject |
| 3218 | // #else |
| 3219 | // @interface FooBar : NSObject |
| 3220 | // #endif |
| 3221 | // { |
| 3222 | // int i; |
| 3223 | // } |
| 3224 | // @end |
| 3225 | // |
| 3226 | // This clause is segregated to avoid breaking the common case. |
| 3227 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3229 | CDecl->getClassLoc(); |
| 3230 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3231 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3232 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3233 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3234 | // advance to the end of the referenced protocols. |
| 3235 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3236 | endHeader++; |
| 3237 | } |
| 3238 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3239 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3240 | } else { |
| 3241 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3242 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3243 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3244 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3245 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3246 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3247 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3248 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3249 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3251 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3252 | SourceLocation OnePastCurly = |
| 3253 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3254 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3255 | } |
| 3256 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3258 | // Now comment out any visibility specifiers. |
| 3259 | while (cursor < endBuf) { |
| 3260 | if (*cursor == '@') { |
| 3261 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3262 | // Skip whitespace. |
| 3263 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3264 | /*scan*/; |
| 3265 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3266 | // FIXME: presence of @public, etc. inside comment results in |
| 3267 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3268 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3269 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3270 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3271 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3272 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3273 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3274 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3275 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3276 | // for type checking. |
| 3277 | else if (*cursor == '<') { |
| 3278 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3279 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3280 | cursor = strchr(cursor, '>'); |
| 3281 | cursor++; |
| 3282 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3283 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3284 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3285 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3286 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3287 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3288 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3289 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3290 | // Don't forget to add a ';'!! |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3291 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3292 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3293 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3294 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3295 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3296 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3297 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3298 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3299 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3300 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3301 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3302 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3303 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3306 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3307 | /// class methods. |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3308 | template<typename MethodIterator> |
| 3309 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3310 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3311 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3312 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3313 | const char *ClassName, |
| 3314 | std::string &Result) { |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3315 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3317 | if (!objc_impl_method) { |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3318 | /* struct _objc_method { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3319 | SEL _cmd; |
| 3320 | char *method_types; |
| 3321 | void *_imp; |
| 3322 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3323 | */ |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3324 | Result += "\nstruct _objc_method {\n"; |
| 3325 | Result += "\tSEL _cmd;\n"; |
| 3326 | Result += "\tchar *method_types;\n"; |
| 3327 | Result += "\tvoid *_imp;\n"; |
| 3328 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3330 | objc_impl_method = true; |
Fariborz Jahanian | 74a1cfa | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3331 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3333 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3335 | /* struct { |
| 3336 | struct _objc_method_list *next_method; |
| 3337 | int method_count; |
| 3338 | struct _objc_method method_list[]; |
| 3339 | } |
| 3340 | */ |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3341 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3342 | Result += "\nstatic struct {\n"; |
| 3343 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3344 | Result += "\tint method_count;\n"; |
| 3345 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3346 | Result += utostr(NumMethods); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3347 | Result += "];\n} _OBJC_"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3348 | Result += prefix; |
| 3349 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3350 | Result += "_METHODS_"; |
| 3351 | Result += ClassName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3352 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3353 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3354 | Result += "_meth\")))= "; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3355 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3356 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3357 | Result += "\t,{{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3358 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3359 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3360 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3361 | Result += "\", \""; |
| 3362 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3363 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3364 | Result += MethodInternalNames[*MethodBegin]; |
| 3365 | Result += "}\n"; |
| 3366 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3367 | Result += "\t ,{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3368 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3369 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3370 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3371 | Result += "\", \""; |
| 3372 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3373 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3374 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3375 | Result += "}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3376 | } |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3377 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3380 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3381 | void RewriteObjC:: |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3382 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3383 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3384 | static bool objc_protocol_methods = false; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Output struct protocol_methods holder of method selector and type. |
| 3387 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3388 | /* struct protocol_methods { |
| 3389 | SEL _cmd; |
| 3390 | char *method_types; |
| 3391 | } |
| 3392 | */ |
| 3393 | Result += "\nstruct _protocol_methods {\n"; |
| 3394 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3395 | Result += "\tchar *method_types;\n"; |
| 3396 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3398 | objc_protocol_methods = true; |
| 3399 | } |
| 3400 | // Do not synthesize the protocol more than once. |
| 3401 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3402 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3404 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3405 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3406 | PDecl->instmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3407 | /* struct _objc_protocol_method_list { |
| 3408 | int protocol_method_count; |
| 3409 | struct protocol_methods protocols[]; |
| 3410 | } |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3411 | */ |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3412 | Result += "\nstatic struct {\n"; |
| 3413 | Result += "\tint protocol_method_count;\n"; |
| 3414 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3415 | Result += utostr(NumMethods); |
| 3416 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3417 | Result += PDecl->getNameAsString(); |
| 3418 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3419 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3421 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | for (ObjCProtocolDecl::instmeth_iterator |
| 3423 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3424 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3425 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3426 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3427 | else |
| 3428 | Result += "\t ,{(struct objc_selector *)\""; |
| 3429 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3430 | std::string MethodTypeString; |
| 3431 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3432 | Result += "\", \""; |
| 3433 | Result += MethodTypeString; |
| 3434 | Result += "\"}\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3435 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3436 | Result += "\t }\n};\n"; |
| 3437 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3439 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3440 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3441 | PDecl->classmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3442 | if (NumMethods > 0) { |
| 3443 | /* struct _objc_protocol_method_list { |
| 3444 | int protocol_method_count; |
| 3445 | struct protocol_methods protocols[]; |
| 3446 | } |
| 3447 | */ |
| 3448 | Result += "\nstatic struct {\n"; |
| 3449 | Result += "\tint protocol_method_count;\n"; |
| 3450 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3451 | Result += utostr(NumMethods); |
| 3452 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3453 | Result += PDecl->getNameAsString(); |
| 3454 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3455 | "{\n\t"; |
| 3456 | Result += utostr(NumMethods); |
| 3457 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3459 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3461 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3462 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3463 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3464 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3465 | else |
| 3466 | Result += "\t ,{(struct objc_selector *)\""; |
| 3467 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3468 | std::string MethodTypeString; |
| 3469 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3470 | Result += "\", \""; |
| 3471 | Result += MethodTypeString; |
| 3472 | Result += "\"}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3473 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3474 | Result += "\t }\n};\n"; |
| 3475 | } |
| 3476 | |
| 3477 | // Output: |
| 3478 | /* struct _objc_protocol { |
| 3479 | // Objective-C 1.0 extensions |
| 3480 | struct _objc_protocol_extension *isa; |
| 3481 | char *protocol_name; |
| 3482 | struct _objc_protocol **protocol_list; |
| 3483 | struct _objc_protocol_method_list *instance_methods; |
| 3484 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | }; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3486 | */ |
| 3487 | static bool objc_protocol = false; |
| 3488 | if (!objc_protocol) { |
| 3489 | Result += "\nstruct _objc_protocol {\n"; |
| 3490 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3491 | Result += "\tchar *protocol_name;\n"; |
| 3492 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3493 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3494 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3495 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3497 | objc_protocol = true; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3498 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3500 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3501 | Result += PDecl->getNameAsString(); |
| 3502 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3503 | "{\n\t0, \""; |
| 3504 | Result += PDecl->getNameAsString(); |
| 3505 | Result += "\", 0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3506 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3507 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3508 | Result += PDecl->getNameAsString(); |
| 3509 | Result += ", "; |
| 3510 | } |
| 3511 | else |
| 3512 | Result += "0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3513 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3514 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3515 | Result += PDecl->getNameAsString(); |
| 3516 | Result += "\n"; |
| 3517 | } |
| 3518 | else |
| 3519 | Result += "0\n"; |
| 3520 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3522 | // Mark this protocol as having been generated. |
| 3523 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3524 | assert(false && "protocol already synthesized"); |
| 3525 | |
| 3526 | } |
| 3527 | |
| 3528 | void RewriteObjC:: |
| 3529 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3530 | const char *prefix, const char *ClassName, |
| 3531 | std::string &Result) { |
| 3532 | if (Protocols.empty()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3534 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3535 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3536 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3537 | // Output the top lovel protocol meta-data for the class. |
| 3538 | /* struct _objc_protocol_list { |
| 3539 | struct _objc_protocol_list *next; |
| 3540 | int protocol_count; |
| 3541 | struct _objc_protocol *class_protocols[]; |
| 3542 | } |
| 3543 | */ |
| 3544 | Result += "\nstatic struct {\n"; |
| 3545 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3546 | Result += "\tint protocol_count;\n"; |
| 3547 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3548 | Result += utostr(Protocols.size()); |
| 3549 | Result += "];\n} _OBJC_"; |
| 3550 | Result += prefix; |
| 3551 | Result += "_PROTOCOLS_"; |
| 3552 | Result += ClassName; |
| 3553 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3554 | "{\n\t0, "; |
| 3555 | Result += utostr(Protocols.size()); |
| 3556 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3558 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3559 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3560 | Result += " \n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3561 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3562 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3563 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3564 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3565 | Result += "\n"; |
| 3566 | } |
| 3567 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3570 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3572 | /// implementation. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3573 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3574 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3575 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3576 | // Find category declaration for this implementation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3577 | ObjCCategoryDecl *CDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3579 | CDecl = CDecl->getNextClassCategory()) |
| 3580 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3581 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3583 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3584 | FullCategoryName += '_'; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3585 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3587 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3589 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3590 | |
| 3591 | // If any of our property implementations have associated getters or |
| 3592 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3593 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3594 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3595 | Prop != PropEnd; ++Prop) { |
| 3596 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3597 | continue; |
| 3598 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3599 | continue; |
| 3600 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3601 | if (!PD) |
| 3602 | continue; |
| 3603 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3604 | InstanceMethods.push_back(Getter); |
| 3605 | if (PD->isReadOnly()) |
| 3606 | continue; |
| 3607 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3608 | InstanceMethods.push_back(Setter); |
| 3609 | } |
| 3610 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3611 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3612 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3614 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3615 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3616 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3617 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3619 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 989e039 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3620 | // Null CDecl is case of a category implementation with no category interface |
| 3621 | if (CDecl) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3622 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3623 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3624 | /* struct _objc_category { |
| 3625 | char *category_name; |
| 3626 | char *class_name; |
| 3627 | struct _objc_method_list *instance_methods; |
| 3628 | struct _objc_method_list *class_methods; |
| 3629 | struct _objc_protocol_list *protocols; |
| 3630 | // Objective-C 1.0 extensions |
| 3631 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3633 | // @property decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3635 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3637 | static bool objc_category = false; |
| 3638 | if (!objc_category) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3639 | Result += "\nstruct _objc_category {\n"; |
| 3640 | Result += "\tchar *category_name;\n"; |
| 3641 | Result += "\tchar *class_name;\n"; |
| 3642 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3643 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3644 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3646 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3647 | Result += "};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3648 | objc_category = true; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3649 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3650 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3651 | Result += FullCategoryName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3652 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3653 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3654 | Result += "\"\n\t, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3655 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3656 | Result += "\"\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3658 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3659 | Result += "\t, (struct _objc_method_list *)" |
| 3660 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3661 | Result += FullCategoryName; |
| 3662 | Result += "\n"; |
| 3663 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3664 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3665 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3666 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3667 | Result += "\t, (struct _objc_method_list *)" |
| 3668 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3669 | Result += FullCategoryName; |
| 3670 | Result += "\n"; |
| 3671 | } |
| 3672 | else |
| 3673 | Result += "\t, 0\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3675 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3677 | Result += FullCategoryName; |
| 3678 | Result += "\n"; |
| 3679 | } |
| 3680 | else |
| 3681 | Result += "\t, 0\n"; |
| 3682 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3685 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3686 | /// ivar offset. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 3687 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3689 | std::string &Result) { |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3690 | if (ivar->isBitField()) { |
| 3691 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3692 | // place all bitfields at offset 0. |
| 3693 | Result += "0"; |
| 3694 | } else { |
| 3695 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3696 | Result += IDecl->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3697 | if (LangOpts.Microsoft) |
| 3698 | Result += "_IMPL"; |
| 3699 | Result += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3700 | Result += ivar->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3701 | Result += ")"; |
| 3702 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3705 | //===----------------------------------------------------------------------===// |
| 3706 | // Meta Data Emission |
| 3707 | //===----------------------------------------------------------------------===// |
| 3708 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3709 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3710 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3711 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3712 | |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3713 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3714 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3716 | // produce correct synthesis as yet. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3717 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3720 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3721 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | ? IDecl->ivar_size() |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3723 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3724 | if (NumIvars > 0) { |
| 3725 | static bool objc_ivar = false; |
| 3726 | if (!objc_ivar) { |
| 3727 | /* struct _objc_ivar { |
| 3728 | char *ivar_name; |
| 3729 | char *ivar_type; |
| 3730 | int ivar_offset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3732 | */ |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3733 | Result += "\nstruct _objc_ivar {\n"; |
| 3734 | Result += "\tchar *ivar_name;\n"; |
| 3735 | Result += "\tchar *ivar_type;\n"; |
| 3736 | Result += "\tint ivar_offset;\n"; |
| 3737 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3739 | objc_ivar = true; |
| 3740 | } |
| 3741 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3742 | /* struct { |
| 3743 | int ivar_count; |
| 3744 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | }; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3746 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | Result += "\nstatic struct {\n"; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3748 | Result += "\tint ivar_count;\n"; |
| 3749 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3750 | Result += utostr(NumIvars); |
| 3751 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3752 | Result += IDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3753 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3754 | "{\n\t"; |
| 3755 | Result += utostr(NumIvars); |
| 3756 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3758 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3759 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3760 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3761 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3762 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3763 | IV != IVEnd; ++IV) |
| 3764 | IVars.push_back(*IV); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3765 | IVI = IDecl->ivar_begin(); |
| 3766 | IVE = IDecl->ivar_end(); |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3767 | } else { |
| 3768 | IVI = CDecl->ivar_begin(); |
| 3769 | IVE = CDecl->ivar_end(); |
| 3770 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3771 | Result += "\t,{{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3772 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3773 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3774 | std::string TmpString, StrEncoding; |
| 3775 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3776 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3777 | Result += StrEncoding; |
| 3778 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3779 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3780 | Result += "}\n"; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3781 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3782 | Result += "\t ,{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3783 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3784 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3785 | std::string TmpString, StrEncoding; |
| 3786 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3787 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3788 | Result += StrEncoding; |
| 3789 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3790 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3791 | Result += "}\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3794 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3795 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3797 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3799 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3800 | |
| 3801 | // If any of our property implementations have associated getters or |
| 3802 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3803 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3804 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3805 | Prop != PropEnd; ++Prop) { |
| 3806 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3807 | continue; |
| 3808 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3809 | continue; |
| 3810 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3811 | if (!PD) |
| 3812 | continue; |
| 3813 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3814 | InstanceMethods.push_back(Getter); |
| 3815 | if (PD->isReadOnly()) |
| 3816 | continue; |
| 3817 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3818 | InstanceMethods.push_back(Setter); |
| 3819 | } |
| 3820 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3821 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3823 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3824 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3825 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3827 | // Protocols referenced in class declaration? |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3828 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3829 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3831 | // Declaration of class/meta-class metadata |
| 3832 | /* struct _objc_class { |
| 3833 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3834 | const char *super_class_name; |
| 3835 | char *name; |
| 3836 | long version; |
| 3837 | long info; |
| 3838 | long instance_size; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3839 | struct _objc_ivar_list *ivars; |
| 3840 | struct _objc_method_list *methods; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3841 | struct objc_cache *cache; |
| 3842 | struct objc_protocol_list *protocols; |
| 3843 | const char *ivar_layout; |
| 3844 | struct _objc_class_ext *ext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | }; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3846 | */ |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3847 | static bool objc_class = false; |
| 3848 | if (!objc_class) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3849 | Result += "\nstruct _objc_class {\n"; |
| 3850 | Result += "\tstruct _objc_class *isa;\n"; |
| 3851 | Result += "\tconst char *super_class_name;\n"; |
| 3852 | Result += "\tchar *name;\n"; |
| 3853 | Result += "\tlong version;\n"; |
| 3854 | Result += "\tlong info;\n"; |
| 3855 | Result += "\tlong instance_size;\n"; |
| 3856 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3857 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3858 | Result += "\tstruct objc_cache *cache;\n"; |
| 3859 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3860 | Result += "\tconst char *ivar_layout;\n"; |
| 3861 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3862 | Result += "};\n"; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3863 | objc_class = true; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3864 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3866 | // Meta-class metadata generation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3867 | ObjCInterfaceDecl *RootClass = 0; |
| 3868 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3869 | while (SuperClass) { |
| 3870 | RootClass = SuperClass; |
| 3871 | SuperClass = SuperClass->getSuperClass(); |
| 3872 | } |
| 3873 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3875 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3876 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3877 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3878 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3879 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3880 | Result += "\""; |
| 3881 | |
| 3882 | if (SuperClass) { |
| 3883 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3884 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3885 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3886 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3887 | Result += "\""; |
| 3888 | } |
| 3889 | else { |
| 3890 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3891 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3892 | Result += "\""; |
| 3893 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3894 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3895 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3896 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3897 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3898 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3899 | Result += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | Result += "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3901 | } |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3902 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3903 | Result += ", 0\n"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3904 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3905 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3906 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3907 | Result += ",0,0\n"; |
| 3908 | } |
Fariborz Jahanian | 486f718 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3909 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3910 | Result += "\t,0,0,0,0\n"; |
| 3911 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3912 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3913 | // class metadata generation. |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3914 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3915 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3916 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3917 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3918 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3919 | if (SuperClass) { |
| 3920 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3921 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3922 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3923 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3924 | Result += "\""; |
| 3925 | } |
| 3926 | else { |
| 3927 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3928 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3929 | Result += "\""; |
| 3930 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3931 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3932 | Result += ", 0,1"; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3933 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3934 | Result += ",0"; |
| 3935 | else { |
| 3936 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3937 | Result += ",sizeof(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3938 | Result += CDecl->getNameAsString(); |
Steve Naroff | 14a0746 | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3939 | if (LangOpts.Microsoft) |
| 3940 | Result += "_IMPL"; |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3941 | Result += ")"; |
| 3942 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3943 | if (NumIvars > 0) { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3944 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3945 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3946 | Result += "\n\t"; |
| 3947 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3948 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3949 | Result += ",0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3950 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3951 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3952 | Result += CDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | Result += ", 0\n\t"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3954 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3955 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3956 | Result += ",0,0"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3957 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3958 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3959 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3960 | Result += ", 0,0\n"; |
| 3961 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3962 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3963 | Result += ",0,0,0\n"; |
| 3964 | Result += "};\n"; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3965 | } |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3966 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3967 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3968 | /// and emits meta-data. |
| 3969 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3970 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3971 | int ClsDefCount = ClassImplementation.size(); |
| 3972 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3974 | // Rewrite implemented methods |
| 3975 | for (int i = 0; i < ClsDefCount; i++) |
| 3976 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3978 | for (int i = 0; i < CatDefCount; i++) |
| 3979 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3980 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3982 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3983 | int ClsDefCount = ClassImplementation.size(); |
| 3984 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 3985 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3986 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3987 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3988 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3990 | // For each implemented category, write out all its meta data. |
| 3991 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3992 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3993 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3994 | // Write objc_symtab metadata |
| 3995 | /* |
| 3996 | struct _objc_symtab |
| 3997 | { |
| 3998 | long sel_ref_cnt; |
| 3999 | SEL *refs; |
| 4000 | short cls_def_cnt; |
| 4001 | short cat_def_cnt; |
| 4002 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | }; |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4004 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4006 | Result += "\nstruct _objc_symtab {\n"; |
| 4007 | Result += "\tlong sel_ref_cnt;\n"; |
| 4008 | Result += "\tSEL *refs;\n"; |
| 4009 | Result += "\tshort cls_def_cnt;\n"; |
| 4010 | Result += "\tshort cat_def_cnt;\n"; |
| 4011 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4012 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4014 | Result += "static struct _objc_symtab " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4015 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4017 | + ", " + utostr(CatDefCount) + "\n"; |
| 4018 | for (int i = 0; i < ClsDefCount; i++) { |
| 4019 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4020 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4021 | Result += "\n"; |
| 4022 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4023 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4024 | for (int i = 0; i < CatDefCount; i++) { |
| 4025 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4026 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4027 | Result += "_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4028 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4029 | Result += "\n"; |
| 4030 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4032 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4033 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4034 | // Write objc_module metadata |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4036 | /* |
| 4037 | struct _objc_module { |
| 4038 | long version; |
| 4039 | long size; |
| 4040 | const char *name; |
| 4041 | struct _objc_symtab *symtab; |
| 4042 | } |
| 4043 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4045 | Result += "\nstruct _objc_module {\n"; |
| 4046 | Result += "\tlong version;\n"; |
| 4047 | Result += "\tlong size;\n"; |
| 4048 | Result += "\tconst char *name;\n"; |
| 4049 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4050 | Result += "};\n\n"; |
| 4051 | Result += "static struct _objc_module " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4052 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4054 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4055 | Result += "};\n\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4056 | |
| 4057 | if (LangOpts.Microsoft) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4058 | if (ProtocolExprDecls.size()) { |
| 4059 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4060 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4062 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4063 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4064 | Result += (*I)->getNameAsString(); |
| 4065 | Result += " = &_OBJC_PROTOCOL_"; |
| 4066 | Result += (*I)->getNameAsString(); |
| 4067 | Result += ";\n"; |
| 4068 | } |
| 4069 | Result += "#pragma data_seg(pop)\n\n"; |
| 4070 | } |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4071 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | cab93d5 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4072 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4073 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4074 | Result += "&_OBJC_MODULES;\n"; |
| 4075 | Result += "#pragma data_seg(pop)\n\n"; |
| 4076 | } |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4077 | } |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4078 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4079 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4080 | const std::string &Name, |
| 4081 | ValueDecl *VD) { |
| 4082 | assert(BlockByRefDeclNo.count(VD) && |
| 4083 | "RewriteByRefString: ByRef decl missing"); |
| 4084 | ResultStr += "struct __Block_byref_" + Name + |
| 4085 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4086 | } |
| 4087 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4088 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4089 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4090 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4094 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 4095 | const char *funcName, |
| 4096 | std::string Tag) { |
| 4097 | const FunctionType *AFT = CE->getFunctionType(); |
| 4098 | QualType RT = AFT->getResultType(); |
| 4099 | std::string StructRef = "struct " + Tag; |
| 4100 | std::string S = "static " + RT.getAsString() + " __" + |
| 4101 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4102 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4103 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4105 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4107 | // block (to reference imported block decl refs). |
| 4108 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4109 | } else if (BD->param_empty()) { |
| 4110 | S += "(" + StructRef + " *__cself)"; |
| 4111 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4112 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4113 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4114 | S += '('; |
| 4115 | // first add the implicit argument. |
| 4116 | S += StructRef + " *__cself, "; |
| 4117 | std::string ParamStr; |
| 4118 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4119 | E = BD->param_end(); AI != E; ++AI) { |
| 4120 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4121 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame^] | 4122 | QualType QT = (*AI)->getType(); |
| 4123 | if (isTopLevelBlockPointerType(QT)) { |
| 4124 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4125 | Context->getPointerType(BPT->getPointeeType()). |
| 4126 | getAsStringInternal(ParamStr, Context->PrintingPolicy); |
| 4127 | } |
| 4128 | else |
| 4129 | QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4130 | S += ParamStr; |
| 4131 | } |
| 4132 | if (FT->isVariadic()) { |
| 4133 | if (!BD->param_empty()) S += ", "; |
| 4134 | S += "..."; |
| 4135 | } |
| 4136 | S += ')'; |
| 4137 | } |
| 4138 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4140 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4141 | // First, emit a declaration for all "by ref" decls. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4142 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4143 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4144 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4145 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4146 | std::string TypeString; |
| 4147 | RewriteByRefString(TypeString, Name, (*I)); |
| 4148 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4149 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4150 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4152 | // Next, emit a declaration for all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4153 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4154 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4155 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4156 | // Handle nested closure invocation. For example: |
| 4157 | // |
| 4158 | // void (^myImportedClosure)(void); |
| 4159 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4161 | // void (^anotherClosure)(void); |
| 4162 | // anotherClosure = ^(void) { |
| 4163 | // myImportedClosure(); // import and invoke the closure |
| 4164 | // }; |
| 4165 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4166 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4167 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4168 | S += " = ("; |
| 4169 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4170 | S += ")"; |
| 4171 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4172 | } |
| 4173 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4174 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4175 | QualType QT = (*I)->getType(); |
| 4176 | if (HasLocalVariableExternalStorage(*I)) |
| 4177 | QT = Context->getPointerType(QT); |
| 4178 | QT.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4179 | S += Name + " = __cself->" + |
| 4180 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4181 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4182 | } |
| 4183 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4184 | const char *cstr = RewrittenStr.c_str(); |
| 4185 | while (*cstr++ != '{') ; |
| 4186 | S += cstr; |
| 4187 | S += "\n"; |
| 4188 | return S; |
| 4189 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4190 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4191 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4192 | const char *funcName, |
| 4193 | std::string Tag) { |
| 4194 | std::string StructRef = "struct " + Tag; |
| 4195 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4197 | S += funcName; |
| 4198 | S += "_block_copy_" + utostr(i); |
| 4199 | S += "(" + StructRef; |
| 4200 | S += "*dst, " + StructRef; |
| 4201 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4202 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4203 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4204 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4205 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4206 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4207 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4208 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4209 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4210 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4211 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4212 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4213 | S += "}\n"; |
| 4214 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4215 | S += "\nstatic void __"; |
| 4216 | S += funcName; |
| 4217 | S += "_block_dispose_" + utostr(i); |
| 4218 | S += "(" + StructRef; |
| 4219 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4221 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4222 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4223 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4224 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4225 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4226 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4227 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4228 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4229 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4230 | return S; |
| 4231 | } |
| 4232 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4233 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4234 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4235 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4236 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4238 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4239 | S += " struct " + Desc; |
| 4240 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4242 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4243 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4244 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4246 | if (BlockDeclRefs.size()) { |
| 4247 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4248 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4249 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4250 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4251 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4252 | std::string ArgName = "_" + FieldName; |
| 4253 | // Handle nested closure invocation. For example: |
| 4254 | // |
| 4255 | // void (^myImportedBlock)(void); |
| 4256 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4258 | // void (^anotherBlock)(void); |
| 4259 | // anotherBlock = ^(void) { |
| 4260 | // myImportedBlock(); // import and invoke the closure |
| 4261 | // }; |
| 4262 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4263 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4264 | S += "struct __block_impl *"; |
| 4265 | Constructor += ", void *" + ArgName; |
| 4266 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4267 | QualType QT = (*I)->getType(); |
| 4268 | if (HasLocalVariableExternalStorage(*I)) |
| 4269 | QT = Context->getPointerType(QT); |
| 4270 | QT.getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4271 | QT.getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4272 | Constructor += ", " + ArgName; |
| 4273 | } |
| 4274 | S += FieldName + ";\n"; |
| 4275 | } |
| 4276 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4277 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4278 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4279 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4280 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4281 | std::string ArgName = "_" + FieldName; |
| 4282 | // Handle nested closure invocation. For example: |
| 4283 | // |
| 4284 | // void (^myImportedBlock)(void); |
| 4285 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4287 | // void (^anotherBlock)(void); |
| 4288 | // anotherBlock = ^(void) { |
| 4289 | // myImportedBlock(); // import and invoke the closure |
| 4290 | // }; |
| 4291 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4292 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4293 | S += "struct __block_impl *"; |
| 4294 | Constructor += ", void *" + ArgName; |
| 4295 | } else { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4296 | std::string TypeString; |
| 4297 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4298 | TypeString += " *"; |
| 4299 | FieldName = TypeString + FieldName; |
| 4300 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4301 | Constructor += ", " + ArgName; |
| 4302 | } |
| 4303 | S += FieldName + "; // by ref\n"; |
| 4304 | } |
| 4305 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4306 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4307 | if (GlobalVarDecl) |
| 4308 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4309 | else |
| 4310 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4311 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4313 | Constructor += " Desc = desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4315 | // Initialize all "by copy" arguments. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4316 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4317 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4318 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4319 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4320 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4321 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4322 | else |
| 4323 | Constructor += Name + " = _"; |
| 4324 | Constructor += Name + ";\n"; |
| 4325 | } |
| 4326 | // Initialize all "by ref" arguments. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4327 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4328 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4329 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4330 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4331 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4332 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4333 | else |
| 4334 | Constructor += Name + " = _"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4335 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4336 | } |
| 4337 | } else { |
| 4338 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4339 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4340 | if (GlobalVarDecl) |
| 4341 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4342 | else |
| 4343 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4344 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4345 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4346 | } |
| 4347 | Constructor += " "; |
| 4348 | Constructor += "}\n"; |
| 4349 | S += Constructor; |
| 4350 | S += "};\n"; |
| 4351 | return S; |
| 4352 | } |
| 4353 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4354 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4355 | std::string ImplTag, int i, |
| 4356 | const char *FunName, |
| 4357 | unsigned hasCopy) { |
| 4358 | std::string S = "\nstatic struct " + DescTag; |
| 4359 | |
| 4360 | S += " {\n unsigned long reserved;\n"; |
| 4361 | S += " unsigned long Block_size;\n"; |
| 4362 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4363 | S += " void (*copy)(struct "; |
| 4364 | S += ImplTag; S += "*, struct "; |
| 4365 | S += ImplTag; S += "*);\n"; |
| 4366 | |
| 4367 | S += " void (*dispose)(struct "; |
| 4368 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | S += "} "; |
| 4371 | |
| 4372 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4373 | S += ImplTag + ")"; |
| 4374 | if (hasCopy) { |
| 4375 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 4376 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 4377 | } |
| 4378 | S += "};\n"; |
| 4379 | return S; |
| 4380 | } |
| 4381 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4382 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4383 | const char *FunName) { |
| 4384 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4385 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4386 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4387 | bool RewriteSC = (GlobalVarDecl && |
| 4388 | !Blocks.empty() && |
| 4389 | GlobalVarDecl->getStorageClass() == VarDecl::Static && |
| 4390 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4391 | if (RewriteSC) { |
| 4392 | std::string SC(" void __"); |
| 4393 | SC += GlobalVarDecl->getNameAsString(); |
| 4394 | SC += "() {}"; |
| 4395 | InsertText(FunLocStart, SC); |
| 4396 | } |
| 4397 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4398 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4399 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4400 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4401 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4402 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4403 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4404 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4405 | ValueDecl *VD = Exp->getDecl(); |
| 4406 | BlockDeclRefs.push_back(Exp); |
| 4407 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4408 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4409 | BlockByCopyDecls.push_back(VD); |
| 4410 | } |
| 4411 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4412 | BlockByRefDeclsPtrSet.insert(VD); |
| 4413 | BlockByRefDecls.push_back(VD); |
| 4414 | } |
| 4415 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4416 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4417 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 4418 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4420 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4421 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4422 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4424 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4426 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4427 | |
| 4428 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4429 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4430 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4431 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4432 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4433 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4434 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4436 | BlockDeclRefs.clear(); |
| 4437 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4438 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4439 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4440 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4441 | ImportedBlockDecls.clear(); |
| 4442 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4443 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4444 | // Must insert any 'const/volatile/static here. Since it has been |
| 4445 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4446 | std::string SC; |
| 4447 | if (GlobalVarDecl->getStorageClass() == VarDecl::Static) |
| 4448 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4449 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4450 | SC += "const "; |
| 4451 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4452 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4453 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4454 | SC += "restrict "; |
| 4455 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4458 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4459 | InnerDeclRefsCount.clear(); |
| 4460 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4461 | RewrittenBlockExprs.clear(); |
| 4462 | } |
| 4463 | |
| 4464 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4465 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4466 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4468 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4469 | } |
| 4470 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4471 | static void BuildUniqueMethodName(std::string &Name, |
| 4472 | ObjCMethodDecl *MD) { |
| 4473 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4474 | Name = IFace->getNameAsCString(); |
| 4475 | Name += "__" + MD->getSelector().getAsString(); |
| 4476 | // Convert colons to underscores. |
| 4477 | std::string::size_type loc = 0; |
| 4478 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4479 | Name.replace(loc, 1, "_"); |
| 4480 | } |
| 4481 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4482 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4483 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4484 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4485 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4486 | std::string FuncName; |
| 4487 | BuildUniqueMethodName(FuncName, MD); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4488 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4489 | } |
| 4490 | |
| 4491 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4492 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4493 | CI != E; ++CI) |
| 4494 | if (*CI) { |
| 4495 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4496 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4497 | else |
| 4498 | GetBlockDeclRefExprs(*CI); |
| 4499 | } |
| 4500 | // Handle specific things. |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4501 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4502 | // FIXME: Handle enums. |
| 4503 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4504 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4505 | } |
| 4506 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4507 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4508 | BlockDeclRefExpr *BDRE = |
| 4509 | new (Context)BlockDeclRefExpr(DRE->getDecl(), DRE->getType(), |
| 4510 | DRE->getLocation(), false); |
| 4511 | BlockDeclRefs.push_back(BDRE); |
| 4512 | } |
| 4513 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4514 | return; |
| 4515 | } |
| 4516 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4517 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4518 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4519 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4520 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4521 | CI != E; ++CI) |
| 4522 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4523 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4524 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4525 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4526 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4527 | InnerContexts); |
| 4528 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4529 | else |
| 4530 | GetInnerBlockDeclRefExprs(*CI, |
| 4531 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4532 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4533 | |
| 4534 | } |
| 4535 | // Handle specific things. |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4536 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4537 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4538 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4539 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4540 | } |
| 4541 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4542 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4543 | if (Var->isFunctionOrMethodVarDecl()) |
| 4544 | ImportedLocalExternalDecls.insert(Var); |
| 4545 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4546 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4547 | return; |
| 4548 | } |
| 4549 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame^] | 4550 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4551 | /// whose result type may be a block pointer or whose argument type(s) |
| 4552 | /// might be block pointers to an equivalent funtion type replacing |
| 4553 | /// all block pointers to function pointers. |
| 4554 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4555 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4556 | // FTP will be null for closures that don't take arguments. |
| 4557 | // Generate a funky cast. |
| 4558 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4559 | bool HasBlockType = false; |
| 4560 | QualType Res = FT->getResultType(); |
| 4561 | if (isTopLevelBlockPointerType(Res)) { |
| 4562 | const BlockPointerType *BPT = Res->getAs<BlockPointerType>(); |
| 4563 | Res = Context->getPointerType(BPT->getPointeeType()); |
| 4564 | HasBlockType = true; |
| 4565 | } |
| 4566 | |
| 4567 | if (FTP) { |
| 4568 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4569 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4570 | QualType t = *I; |
| 4571 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 4572 | if (isTopLevelBlockPointerType(t)) { |
| 4573 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
| 4574 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4575 | HasBlockType = true; |
| 4576 | } |
| 4577 | ArgTypes.push_back(t); |
| 4578 | } |
| 4579 | } |
| 4580 | QualType FuncType; |
| 4581 | // FIXME. Does this work if block takes no argument but has a return type |
| 4582 | // which is of block type? |
| 4583 | if (HasBlockType) |
| 4584 | FuncType = Context->getFunctionType(Res, |
| 4585 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4586 | false, false, 0, 0, FunctionType::ExtInfo()); |
| 4587 | else FuncType = QualType(FT, 0); |
| 4588 | return FuncType; |
| 4589 | } |
| 4590 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4591 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4592 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4593 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4594 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4595 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4596 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4597 | } else if (const BlockDeclRefExpr *CDRE = |
| 4598 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4599 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4600 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4601 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4602 | } |
| 4603 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4604 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4605 | } |
| 4606 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4607 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4608 | else if (const ConditionalOperator *CEXPR = |
| 4609 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4610 | Expr *LHSExp = CEXPR->getLHS(); |
| 4611 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4612 | Expr *RHSExp = CEXPR->getRHS(); |
| 4613 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4614 | Expr *CONDExp = CEXPR->getCond(); |
| 4615 | ConditionalOperator *CondExpr = |
| 4616 | new (Context) ConditionalOperator(CONDExp, |
| 4617 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4618 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4619 | Exp->getType()); |
| 4620 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4621 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4622 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4623 | } else { |
| 4624 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4625 | } |
| 4626 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4627 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4628 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4629 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4630 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4632 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4633 | SourceLocation(), |
| 4634 | &Context->Idents.get("__block_impl")); |
| 4635 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4636 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4637 | // Generate a funky cast. |
| 4638 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4640 | // Push the block argument type. |
| 4641 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4642 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4644 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4645 | QualType t = *I; |
| 4646 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4647 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4648 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4649 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4650 | } |
| 4651 | ArgTypes.push_back(t); |
| 4652 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4653 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4654 | // Now do the pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4656 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4657 | false, false, 0, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4658 | FunctionType::ExtInfo()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4660 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4662 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4663 | CastExpr::CK_Unknown, |
| 4664 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4665 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4666 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4667 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4668 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4670 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4671 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4672 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4673 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4674 | FD->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4676 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4677 | CastExpr::CK_Unknown, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4678 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4680 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4681 | // Add the implicit argument. |
| 4682 | BlkExprs.push_back(BlkCast); |
| 4683 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4685 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4686 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4687 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4688 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4689 | BlkExprs.size(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4690 | Exp->getType(), SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4691 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4695 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4696 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4699 | // We need to return the rewritten expression to handle cases where the |
| 4700 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4701 | // For example: |
| 4702 | // |
| 4703 | // int main() { |
| 4704 | // __block Foo *f; |
| 4705 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4707 | // void (^myblock)() = ^() { |
| 4708 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4709 | // i = 77; |
| 4710 | // }; |
| 4711 | //} |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4712 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4713 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4714 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4715 | ValueDecl *VD; |
| 4716 | bool isArrow = true; |
| 4717 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4718 | VD = BDRE->getDecl(); |
| 4719 | else { |
| 4720 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4721 | isArrow = false; |
| 4722 | } |
| 4723 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4724 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4725 | &Context->Idents.get("__forwarding"), |
| 4726 | Context->VoidPtrTy, 0, |
| 4727 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4728 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4729 | FD, SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4730 | FD->getType()); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4731 | |
| 4732 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4733 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4734 | &Context->Idents.get(Name), |
| 4735 | Context->VoidPtrTy, 0, |
| 4736 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4737 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4738 | DeclRefExp->getType()); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4739 | |
| 4740 | |
| 4741 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4742 | // Need parens to enforce precedence. |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4743 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4744 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4745 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4746 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4749 | // Rewrites the imported local variable V with external storage |
| 4750 | // (static, extern, etc.) as *V |
| 4751 | // |
| 4752 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4753 | ValueDecl *VD = DRE->getDecl(); |
| 4754 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4755 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4756 | return DRE; |
| 4757 | Expr *Exp = new (Context) UnaryOperator(DRE, UnaryOperator::Deref, |
| 4758 | DRE->getType(), DRE->getLocation()); |
| 4759 | // Need parens to enforce precedence. |
| 4760 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4761 | Exp); |
| 4762 | ReplaceStmt(DRE, PE); |
| 4763 | return PE; |
| 4764 | } |
| 4765 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4766 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4767 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4768 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4769 | |
| 4770 | // Need to avoid trying to rewrite synthesized casts. |
| 4771 | if (LocStart.isInvalid()) |
| 4772 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4773 | // Need to avoid trying to rewrite casts contained in macros. |
| 4774 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4775 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4777 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4778 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4779 | QualType QT = CE->getType(); |
| 4780 | const Type* TypePtr = QT->getAs<Type>(); |
| 4781 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4782 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4783 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4784 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4785 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4786 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4787 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4788 | return; |
| 4789 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4790 | // advance the location to startArgList. |
| 4791 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4792 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4793 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4794 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4795 | case '^': |
| 4796 | // Replace the '^' with '*'. |
| 4797 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4798 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4799 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | return; |
| 4803 | } |
| 4804 | |
| 4805 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4806 | SourceLocation DeclLoc = FD->getLocation(); |
| 4807 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4809 | // We have 1 or more arguments that have closure pointers. |
| 4810 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4811 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4813 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4815 | parenCount++; |
| 4816 | // advance the location to startArgList. |
| 4817 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4818 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4820 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4822 | while (*argPtr++ && parenCount) { |
| 4823 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4824 | case '^': |
| 4825 | // Replace the '^' with '*'. |
| 4826 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4827 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4828 | break; |
| 4829 | case '(': |
| 4830 | parenCount++; |
| 4831 | break; |
| 4832 | case ')': |
| 4833 | parenCount--; |
| 4834 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | return; |
| 4838 | } |
| 4839 | |
| 4840 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4841 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4842 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4843 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4844 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4845 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4846 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4847 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4848 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4849 | } |
| 4850 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4852 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4853 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4854 | return true; |
| 4855 | } |
| 4856 | return false; |
| 4857 | } |
| 4858 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4859 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4860 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4861 | const char *argPtr = strchr(Name, '('); |
| 4862 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4864 | LParen = argPtr; // output the start. |
| 4865 | argPtr++; // skip past the left paren. |
| 4866 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4868 | while (*argPtr && parenCount) { |
| 4869 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4870 | case '(': parenCount++; break; |
| 4871 | case ')': parenCount--; break; |
| 4872 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4873 | } |
| 4874 | if (parenCount) argPtr++; |
| 4875 | } |
| 4876 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4877 | RParen = argPtr; // output the end |
| 4878 | } |
| 4879 | |
| 4880 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4881 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4882 | RewriteBlockPointerFunctionArgs(FD); |
| 4883 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4885 | // Handle Variables and Typedefs. |
| 4886 | SourceLocation DeclLoc = ND->getLocation(); |
| 4887 | QualType DeclT; |
| 4888 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4889 | DeclT = VD->getType(); |
| 4890 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4891 | DeclT = TDD->getUnderlyingType(); |
| 4892 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4893 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4894 | else |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4895 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4897 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4898 | const char *endBuf = startBuf; |
| 4899 | // scan backward (from the decl location) for the end of the previous decl. |
| 4900 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4901 | startBuf--; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4903 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4904 | // may take block argument types (which will be handled below). |
| 4905 | if (*startBuf == '^') { |
| 4906 | // Replace the '^' with '*', computing a negative offset. |
| 4907 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4908 | ReplaceText(DeclLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4909 | } |
| 4910 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4911 | // Replace the '^' with '*' for arguments. |
| 4912 | DeclLoc = ND->getLocation(); |
| 4913 | startBuf = SM->getCharacterData(DeclLoc); |
| 4914 | const char *argListBegin, *argListEnd; |
| 4915 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4916 | while (argListBegin < argListEnd) { |
| 4917 | if (*argListBegin == '^') { |
| 4918 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4919 | ReplaceText(CaretLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4920 | } |
| 4921 | argListBegin++; |
| 4922 | } |
| 4923 | } |
| 4924 | return; |
| 4925 | } |
| 4926 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4927 | |
| 4928 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4929 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4930 | /// struct Block_byref_id_object *src) { |
| 4931 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4932 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4933 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4934 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4935 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4936 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4937 | /// } |
| 4938 | /// And: |
| 4939 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4940 | /// _Block_object_dispose(_src->object, |
| 4941 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4942 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4943 | /// _Block_object_dispose(_src->object, |
| 4944 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4945 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4946 | /// } |
| 4947 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4948 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4949 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4950 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4951 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4952 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4953 | CopyDestroyCache.insert(flag); |
| 4954 | S = "static void __Block_byref_id_object_copy_"; |
| 4955 | S += utostr(flag); |
| 4956 | S += "(void *dst, void *src) {\n"; |
| 4957 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4958 | // offset into the object pointer is computed as: |
| 4959 | // void * + void* + int + int + void* + void * |
| 4960 | unsigned IntSize = |
| 4961 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4962 | unsigned VoidPtrSize = |
| 4963 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4964 | |
| 4965 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4966 | S += " _Block_object_assign((char*)dst + "; |
| 4967 | S += utostr(offset); |
| 4968 | S += ", *(void * *) ((char*)src + "; |
| 4969 | S += utostr(offset); |
| 4970 | S += "), "; |
| 4971 | S += utostr(flag); |
| 4972 | S += ");\n}\n"; |
| 4973 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4974 | S += "static void __Block_byref_id_object_dispose_"; |
| 4975 | S += utostr(flag); |
| 4976 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4977 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4978 | S += utostr(offset); |
| 4979 | S += "), "; |
| 4980 | S += utostr(flag); |
| 4981 | S += ");\n}\n"; |
| 4982 | return S; |
| 4983 | } |
| 4984 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4985 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4986 | /// the declaration into: |
| 4987 | /// struct __Block_byref_ND { |
| 4988 | /// void *__isa; // NULL for everything except __weak pointers |
| 4989 | /// struct __Block_byref_ND *__forwarding; |
| 4990 | /// int32_t __flags; |
| 4991 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4992 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4993 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4994 | /// typex ND; |
| 4995 | /// }; |
| 4996 | /// |
| 4997 | /// It then replaces declaration of ND variable with: |
| 4998 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4999 | /// __size=sizeof(struct __Block_byref_ND), |
| 5000 | /// ND=initializer-if-any}; |
| 5001 | /// |
| 5002 | /// |
| 5003 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5004 | // Insert declaration for the function in which block literal is |
| 5005 | // used. |
| 5006 | if (CurFunctionDeclToDeclareForBlock) |
| 5007 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5008 | int flag = 0; |
| 5009 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5010 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5011 | if (DeclLoc.isInvalid()) |
| 5012 | // If type location is missing, it is because of missing type (a warning). |
| 5013 | // Use variable's location which is good for this case. |
| 5014 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5015 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5016 | SourceLocation X = ND->getLocEnd(); |
| 5017 | X = SM->getInstantiationLoc(X); |
| 5018 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5019 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5020 | std::string ByrefType; |
| 5021 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5022 | ByrefType += " {\n"; |
| 5023 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5024 | RewriteByRefString(ByrefType, Name, ND); |
| 5025 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5026 | ByrefType += " int __flags;\n"; |
| 5027 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5028 | // Add void *__Block_byref_id_object_copy; |
| 5029 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5030 | QualType Ty = ND->getType(); |
| 5031 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5032 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5033 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5034 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5038 | ByrefType += " " + Name + ";\n"; |
| 5039 | ByrefType += "};\n"; |
| 5040 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5041 | SourceLocation FunLocStart; |
| 5042 | if (CurFunctionDef) |
| 5043 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5044 | else { |
| 5045 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5046 | FunLocStart = CurMethodDef->getLocStart(); |
| 5047 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5048 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5049 | if (Ty.isObjCGCWeak()) { |
| 5050 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5051 | isa = 1; |
| 5052 | } |
| 5053 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5054 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5055 | flag = BLOCK_BYREF_CALLER; |
| 5056 | QualType Ty = ND->getType(); |
| 5057 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5058 | if (Ty->isBlockPointerType()) |
| 5059 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5060 | else |
| 5061 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5062 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5063 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5064 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5065 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5066 | |
| 5067 | // struct __Block_byref_ND ND = |
| 5068 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5069 | // initializer-if-any}; |
| 5070 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5071 | unsigned flags = 0; |
| 5072 | if (HasCopyAndDispose) |
| 5073 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5074 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5075 | ByrefType.clear(); |
| 5076 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5077 | std::string ForwardingCastType("("); |
| 5078 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5079 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5080 | ByrefType += " " + Name + " = {(void*)"; |
| 5081 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5082 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5083 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5084 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5085 | ByrefType += "sizeof("; |
| 5086 | RewriteByRefString(ByrefType, Name, ND); |
| 5087 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5088 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5089 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5090 | ByrefType += utostr(flag); |
| 5091 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5092 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5093 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5094 | ByrefType += "};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5095 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5096 | } |
| 5097 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5098 | SourceLocation startLoc; |
| 5099 | Expr *E = ND->getInit(); |
| 5100 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5101 | startLoc = ECE->getLParenLoc(); |
| 5102 | else |
| 5103 | startLoc = E->getLocStart(); |
Fariborz Jahanian | b8646ed | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 5104 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5105 | endBuf = SM->getCharacterData(startLoc); |
| 5106 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5107 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5108 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5109 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5110 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5111 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5112 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5113 | ByrefType += "sizeof("; |
| 5114 | RewriteByRefString(ByrefType, Name, ND); |
| 5115 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5116 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5117 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5118 | ByrefType += utostr(flag); |
| 5119 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5120 | ByrefType += utostr(flag); |
| 5121 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5122 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5123 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5124 | |
| 5125 | // Complete the newly synthesized compound expression by inserting a right |
| 5126 | // curly brace before the end of the declaration. |
| 5127 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5128 | // also assumes there is only one declarator. For example, the following |
| 5129 | // isn't currently supported by this routine (in general): |
| 5130 | // |
| 5131 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5132 | // |
| 5133 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5134 | const char *semiBuf = strchr(startBuf, ';'); |
| 5135 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5136 | SourceLocation semiLoc = |
| 5137 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 5138 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5139 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5140 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5141 | return; |
| 5142 | } |
| 5143 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5144 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5145 | // Add initializers for any closure decl refs. |
| 5146 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5147 | if (BlockDeclRefs.size()) { |
| 5148 | // Unique all "by copy" declarations. |
| 5149 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5150 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5151 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5152 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5153 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5154 | } |
| 5155 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5156 | // Unique all "by ref" declarations. |
| 5157 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5158 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5159 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5160 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5161 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5162 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5163 | } |
| 5164 | // Find any imported blocks...they will need special attention. |
| 5165 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5166 | if (BlockDeclRefs[i]->isByRef() || |
| 5167 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5168 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5169 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5170 | } |
| 5171 | } |
| 5172 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5173 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 5174 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5175 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5176 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5177 | ID, FType, 0, FunctionDecl::Extern, |
| 5178 | FunctionDecl::None, false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5181 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
| 5182 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5183 | Blocks.push_back(Exp); |
| 5184 | |
| 5185 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5186 | |
| 5187 | // Add inner imported variables now used in current block. |
| 5188 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5189 | if (!InnerBlockDeclRefs.empty()) { |
| 5190 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5191 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5192 | ValueDecl *VD = Exp->getDecl(); |
| 5193 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5194 | // We need to save the copied-in variables in nested |
| 5195 | // blocks because it is needed at the end for some of the API generations. |
| 5196 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5197 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5198 | BlockDeclRefs.push_back(Exp); |
| 5199 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5200 | BlockByCopyDecls.push_back(VD); |
| 5201 | } |
| 5202 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5203 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5204 | BlockDeclRefs.push_back(Exp); |
| 5205 | BlockByRefDeclsPtrSet.insert(VD); |
| 5206 | BlockByRefDecls.push_back(VD); |
| 5207 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5208 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5209 | // Find any imported blocks...they will need special attention. |
| 5210 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5211 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5212 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5213 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5214 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5215 | } |
| 5216 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5217 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5218 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5219 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5220 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5221 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5222 | else if (CurMethodDef) |
| 5223 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5224 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5225 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5226 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5227 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5228 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5229 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5230 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5231 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5232 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame^] | 5233 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5234 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5235 | |
| 5236 | FunctionDecl *FD; |
| 5237 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5239 | // Simulate a contructor call... |
| 5240 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5241 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5242 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5243 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5244 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5245 | // Initialize the block function. |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5246 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5247 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 5248 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5249 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5250 | CastExpr::CK_Unknown, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | InitExprs.push_back(castExpr); |
| 5252 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5253 | // Initialize the block descriptor. |
| 5254 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5256 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5257 | &Context->Idents.get(DescData.c_str()), |
| 5258 | Context->VoidPtrTy, 0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5259 | VarDecl::Static, VarDecl::None); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5260 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 5261 | new (Context) DeclRefExpr(NewVD, |
| 5262 | Context->VoidPtrTy, SourceLocation()), |
| 5263 | UnaryOperator::AddrOf, |
| 5264 | Context->getPointerType(Context->VoidPtrTy), |
| 5265 | SourceLocation()); |
| 5266 | InitExprs.push_back(DescRefExpr); |
| 5267 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5268 | // Add initializers for any closure decl refs. |
| 5269 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5270 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5271 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5272 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5273 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5274 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5275 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5276 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5277 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5278 | if (HasLocalVariableExternalStorage(*I)) { |
| 5279 | QualType QT = (*I)->getType(); |
| 5280 | QT = Context->getPointerType(QT); |
| 5281 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, QT, |
| 5282 | SourceLocation()); |
| 5283 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5284 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5285 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5286 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5287 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5288 | CastExpr::CK_Unknown, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5289 | } else { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5290 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5291 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5292 | if (HasLocalVariableExternalStorage(*I)) { |
| 5293 | QualType QT = (*I)->getType(); |
| 5294 | QT = Context->getPointerType(QT); |
| 5295 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, QT, |
| 5296 | SourceLocation()); |
| 5297 | } |
| 5298 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5299 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5300 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5301 | } |
| 5302 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5303 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5304 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5305 | ValueDecl *ND = (*I); |
| 5306 | std::string Name(ND->getNameAsString()); |
| 5307 | std::string RecName; |
| 5308 | RewriteByRefString(RecName, Name, ND); |
| 5309 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5310 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5311 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5312 | SourceLocation(), II); |
| 5313 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5314 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5315 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5316 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5317 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 5318 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5319 | Context->getPointerType(Exp->getType()), |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5320 | SourceLocation()); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5321 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CastExpr::CK_Unknown, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5322 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5323 | } |
| 5324 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5325 | if (ImportedBlockDecls.size()) { |
| 5326 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5327 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5328 | unsigned IntSize = |
| 5329 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5330 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 5331 | Context->IntTy, SourceLocation()); |
| 5332 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5333 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5334 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5335 | FType, SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5336 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5337 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5338 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5339 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CastExpr::CK_Unknown, |
| 5340 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5341 | BlockDeclRefs.clear(); |
| 5342 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5343 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5344 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5345 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5346 | ImportedBlockDecls.clear(); |
| 5347 | return NewRep; |
| 5348 | } |
| 5349 | |
| 5350 | //===----------------------------------------------------------------------===// |
| 5351 | // Function Body / Expression rewriting |
| 5352 | //===----------------------------------------------------------------------===// |
| 5353 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5354 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5355 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5356 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5357 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5358 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5359 | // we get this right. |
| 5360 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5361 | // Perform a bottom up traversal of all children. |
| 5362 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5363 | CI != E; ++CI) |
| 5364 | if (*CI) |
| 5365 | CollectPropertySetters(*CI); |
| 5366 | |
| 5367 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5368 | if (BinOp->isAssignmentOp()) { |
| 5369 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 5370 | PropSetters[PRE] = BinOp; |
| 5371 | } |
| 5372 | } |
| 5373 | } |
| 5374 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5375 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5376 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5377 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5378 | Stmts.push_back(S); |
| 5379 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5380 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5381 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5382 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5383 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5384 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5385 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5386 | // Perform a bottom up rewrite of all children. |
| 5387 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5388 | CI != E; ++CI) |
| 5389 | if (*CI) { |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5390 | Stmt *newStmt; |
| 5391 | Stmt *S = (*CI); |
| 5392 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5393 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5394 | bool replaced = false; |
| 5395 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 5396 | if (replaced) { |
| 5397 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5398 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5399 | else |
| 5400 | ReplaceStmt(S, newStmt); |
| 5401 | } |
| 5402 | } |
| 5403 | else |
| 5404 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5405 | if (newStmt) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5406 | *CI = newStmt; |
| 5407 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5408 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5409 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5410 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5411 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5412 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5413 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5414 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5415 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5416 | // Rewrite the block body in place. |
| 5417 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5418 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5419 | // Now we snarf the rewritten text and stash it away for later use. |
Ted Kremenek | db2ef37 | 2010-01-07 18:00:35 +0000 | [diff] [blame] | 5420 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5421 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5423 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5424 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5425 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5426 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5427 | return blockTranscribed; |
| 5428 | } |
| 5429 | // Handle specific things. |
| 5430 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5431 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5433 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 5434 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 5435 | if (BinOp) { |
| 5436 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5437 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5438 | DisableReplaceStmt = true; |
| 5439 | // Save the source range. Even if we disable the replacement, the |
| 5440 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5441 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | // self.errorHandler = handler ? handler : |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5443 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5444 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5445 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5446 | DisableReplaceStmt = false; |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5447 | // |
| 5448 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5449 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5450 | // to see the original expression). Consider this example: |
| 5451 | // |
| 5452 | // Foo *obj1, *obj2; |
| 5453 | // |
| 5454 | // obj1.i = [obj2 rrrr]; |
| 5455 | // |
| 5456 | // 'BinOp' for the previous expression looks like: |
| 5457 | // |
| 5458 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5459 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5460 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5461 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5462 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5463 | // |
| 5464 | // 'newStmt' represents the rewritten message expression. For example: |
| 5465 | // |
| 5466 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5467 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5468 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5469 | // (CStyleCastExpr 0x231d220 'void *' |
| 5470 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5471 | // |
| 5472 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 5473 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5474 | // the original RHS (since we haven't altered BinOp). |
| 5475 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5476 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5477 | // node. As a result, we now leak the original AST nodes. |
| 5478 | // |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5479 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5480 | } else { |
| 5481 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5482 | } |
| 5483 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5484 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5485 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5486 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5487 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5488 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5489 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5490 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5491 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5492 | // Before we rewrite it, put the original message expression in a comment. |
| 5493 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5494 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5496 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5497 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5498 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5499 | std::string messString; |
| 5500 | messString += "// "; |
| 5501 | messString.append(startBuf, endBuf-startBuf+1); |
| 5502 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5503 | |
| 5504 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5505 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5506 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5507 | // Tried this, but it didn't work either... |
| 5508 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5509 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5510 | return RewriteMessageExpr(MessExpr); |
| 5511 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5512 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5513 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5514 | return RewriteObjCTryStmt(StmtTry); |
| 5515 | |
| 5516 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5517 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5518 | |
| 5519 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5520 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5521 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5522 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5523 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5524 | |
| 5525 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5526 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5527 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5528 | OrigStmtRange.getEnd()); |
| 5529 | if (BreakStmt *StmtBreakStmt = |
| 5530 | dyn_cast<BreakStmt>(S)) |
| 5531 | return RewriteBreakStmt(StmtBreakStmt); |
| 5532 | if (ContinueStmt *StmtContinueStmt = |
| 5533 | dyn_cast<ContinueStmt>(S)) |
| 5534 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5535 | |
| 5536 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5537 | // and cast exprs. |
| 5538 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5539 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5540 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5541 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5542 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5543 | // the context of an ObjCForCollectionStmt. For example: |
| 5544 | // NSArray *someArray; |
| 5545 | // for (id <FooProtocol> index in someArray) ; |
| 5546 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5547 | // and it depends on the original text locations/positions. |
Benjamin Kramer | acc5fa1 | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5548 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5549 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5550 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5551 | // Blocks rewrite rules. |
| 5552 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5553 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5554 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5555 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5556 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5557 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5559 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5560 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5561 | if (VD->hasAttr<BlocksAttr>()) { |
| 5562 | static unsigned uniqueByrefDeclCount = 0; |
| 5563 | assert(!BlockByRefDeclNo.count(ND) && |
| 5564 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5565 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5566 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5567 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5568 | else |
| 5569 | RewriteTypeOfDecl(VD); |
| 5570 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5571 | } |
| 5572 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5573 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5574 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5576 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5577 | } |
| 5578 | } |
| 5579 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5580 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5581 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5582 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5583 | |
| 5584 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5585 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5586 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5588 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5589 | && "Statement stack mismatch"); |
| 5590 | Stmts.pop_back(); |
| 5591 | } |
| 5592 | // Handle blocks rewriting. |
| 5593 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5594 | if (BDRE->isByRef()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5595 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5596 | } |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5597 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5598 | ValueDecl *VD = DRE->getDecl(); |
| 5599 | if (VD->hasAttr<BlocksAttr>()) |
| 5600 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5601 | if (HasLocalVariableExternalStorage(VD)) |
| 5602 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5605 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5606 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5607 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5608 | ReplaceStmt(S, BlockCall); |
| 5609 | return BlockCall; |
| 5610 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5611 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5612 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5613 | RewriteCastExpr(CE); |
| 5614 | } |
| 5615 | #if 0 |
| 5616 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5617 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5618 | // Get the new text. |
| 5619 | std::string SStr; |
| 5620 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 0905f14 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5621 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5622 | const std::string &Str = Buf.str(); |
| 5623 | |
| 5624 | printf("CAST = %s\n", &Str[0]); |
| 5625 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5626 | delete S; |
| 5627 | return Replacement; |
| 5628 | } |
| 5629 | #endif |
| 5630 | // Return this stmt unmodified. |
| 5631 | return S; |
| 5632 | } |
| 5633 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5634 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5635 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5636 | e = RD->field_end(); i != e; ++i) { |
| 5637 | FieldDecl *FD = *i; |
| 5638 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5639 | RewriteBlockPointerDecl(FD); |
| 5640 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5641 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5642 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5643 | } |
| 5644 | } |
| 5645 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5646 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5647 | /// main file of the input. |
| 5648 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5649 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | b136888 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5650 | if (FD->isOverloadedOperator()) |
| 5651 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5652 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5653 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5654 | // prototype. This enables us to rewrite function declarations and |
| 5655 | // definitions using the same code. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5656 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5657 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5658 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5659 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5660 | CurFunctionDef = FD; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5661 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5662 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5663 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5664 | Body = |
| 5665 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5666 | FD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5667 | CurrentBody = 0; |
| 5668 | if (PropParentMap) { |
| 5669 | delete PropParentMap; |
| 5670 | PropParentMap = 0; |
| 5671 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5672 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5673 | // and any copy/dispose helper functions. |
| 5674 | InsertBlockLiteralsWithinFunction(FD); |
| 5675 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5676 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5678 | return; |
| 5679 | } |
| 5680 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5681 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5682 | CurMethodDef = MD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5683 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5684 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5685 | Body = |
| 5686 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5687 | MD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5688 | CurrentBody = 0; |
| 5689 | if (PropParentMap) { |
| 5690 | delete PropParentMap; |
| 5691 | PropParentMap = 0; |
| 5692 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5693 | InsertBlockLiteralsWithinMethod(MD); |
| 5694 | CurMethodDef = 0; |
| 5695 | } |
| 5696 | } |
| 5697 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5698 | ClassImplementation.push_back(CI); |
| 5699 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5700 | CategoryImplementation.push_back(CI); |
| 5701 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5702 | RewriteForwardClassDecl(CD); |
| 5703 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5704 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5705 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5706 | RewriteBlockPointerDecl(VD); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5707 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5708 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5709 | if (VD->getInit()) { |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5710 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5711 | RewriteCastExpr(CE); |
| 5712 | } |
| 5713 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5714 | } else if (VD->getType()->isRecordType()) { |
| 5715 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5716 | if (RD->isDefinition()) |
| 5717 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5718 | } |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5719 | if (VD->getInit()) { |
| 5720 | GlobalVarDecl = VD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5721 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5722 | CurrentBody = VD->getInit(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5723 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5724 | CurrentBody = 0; |
| 5725 | if (PropParentMap) { |
| 5726 | delete PropParentMap; |
| 5727 | PropParentMap = 0; |
| 5728 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5729 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5730 | VD->getNameAsCString()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5731 | GlobalVarDecl = 0; |
| 5732 | |
| 5733 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5734 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5735 | RewriteCastExpr(CE); |
| 5736 | } |
| 5737 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5738 | return; |
| 5739 | } |
| 5740 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5741 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5742 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5743 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5744 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5745 | return; |
| 5746 | } |
| 5747 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5748 | if (RD->isDefinition()) |
| 5749 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5750 | return; |
| 5751 | } |
| 5752 | // Nothing yet. |
| 5753 | } |
| 5754 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5755 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5756 | if (Diags.hasErrorOccurred()) |
| 5757 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5758 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5759 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5760 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5761 | // Here's a great place to add any extra declarations that may be needed. |
| 5762 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5763 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5764 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5765 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5766 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5767 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5768 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5769 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5770 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5771 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5772 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5774 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5775 | //printf("Changed:\n"); |
| 5776 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5777 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5778 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5779 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5780 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5781 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5782 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5783 | // Rewrite Objective-c meta data* |
| 5784 | std::string ResultStr; |
| 5785 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5786 | // Emit metadata. |
| 5787 | *OutFile << ResultStr; |
| 5788 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5789 | OutFile->flush(); |
| 5790 | } |