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 | |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 15 | #include "clang/AST/AST.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Rewrite/Core/Rewriter.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 30 | #include <memory> |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 31 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 33 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 34 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 35 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 36 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 37 | protected: |
| 38 | |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 39 | enum { |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 40 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 41 | block, ... */ |
| 42 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 43 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 44 | __block variable */ |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 45 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 46 | helpers */ |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 47 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 48 | support routines */ |
| 49 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 50 | }; |
| 51 | |
| 52 | enum { |
| 53 | BLOCK_NEEDS_FREE = (1 << 24), |
| 54 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 55 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 56 | BLOCK_IS_GC = (1 << 27), |
| 57 | BLOCK_IS_GLOBAL = (1 << 28), |
| 58 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 59 | }; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 60 | static const int OBJC_ABI_VERSION = 7; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 62 | Rewriter Rewrite; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 63 | DiagnosticsEngine &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 64 | const LangOptions &LangOpts; |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 65 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 66 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 67 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 68 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 69 | const char *MainFileStart, *MainFileEnd; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 70 | Stmt *CurrentBody; |
| 71 | ParentMap *PropParentMap; // created lazily. |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 72 | std::string InFileName; |
| 73 | raw_ostream* OutFile; |
| 74 | std::string Preamble; |
| 75 | |
| 76 | TypeDecl *ProtocolTypeDecl; |
| 77 | VarDecl *GlobalVarDecl; |
| 78 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 79 | // ObjC string constant support. |
| 80 | unsigned NumObjCStringLiterals; |
| 81 | VarDecl *ConstantStringClassReference; |
| 82 | RecordDecl *NSStringRecord; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 83 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 84 | // ObjC foreach break/continue generation support. |
| 85 | int BcLabelCount; |
| 86 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 87 | unsigned TryFinallyContainsReturnDiag; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 88 | // Needed for super. |
| 89 | ObjCMethodDecl *CurMethodDef; |
| 90 | RecordDecl *SuperStructDecl; |
| 91 | RecordDecl *ConstantStringDecl; |
| 92 | |
| 93 | FunctionDecl *MsgSendFunctionDecl; |
| 94 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 95 | FunctionDecl *MsgSendStretFunctionDecl; |
| 96 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 97 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 98 | FunctionDecl *GetClassFunctionDecl; |
| 99 | FunctionDecl *GetMetaClassFunctionDecl; |
| 100 | FunctionDecl *GetSuperClassFunctionDecl; |
| 101 | FunctionDecl *SelGetUidFunctionDecl; |
| 102 | FunctionDecl *CFStringFunctionDecl; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 103 | FunctionDecl *SuperConstructorFunctionDecl; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 104 | FunctionDecl *CurFunctionDef; |
| 105 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 107 | /* Misc. containers needed for meta-data rewrite. */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 109 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 111 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 113 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 114 | SmallVector<Stmt *, 32> Stmts; |
| 115 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 116 | // Remember all the @protocol(<expr>) expressions. |
| 117 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 118 | |
| 119 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 120 | |
| 121 | // Block expressions. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | SmallVector<BlockExpr *, 32> Blocks; |
| 123 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 124 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 125 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 126 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 128 | // Block related declarations. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 130 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 131 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 133 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 134 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 135 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 136 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 137 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 138 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 139 | // This maps an original source AST to it's rewritten form. This allows |
| 140 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 141 | // This is needed to support some of the exotic property rewriting. |
| 142 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 143 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 144 | // Needed for header files being rewritten |
| 145 | bool IsHeader; |
| 146 | bool SilenceRewriteMacroWarning; |
| 147 | bool objc_impl_method; |
| 148 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 149 | bool DisableReplaceStmt; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 150 | class DisableReplaceStmtScope { |
| 151 | RewriteObjC &R; |
| 152 | bool SavedValue; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 153 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 154 | public: |
| 155 | DisableReplaceStmtScope(RewriteObjC &R) |
| 156 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 157 | R.DisableReplaceStmt = true; |
| 158 | } |
| 159 | ~DisableReplaceStmtScope() { |
| 160 | R.DisableReplaceStmt = SavedValue; |
| 161 | } |
| 162 | }; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 163 | void InitializeCommon(ASTContext &context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 165 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 167 | // Top Level Driver code. |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 168 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 169 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 170 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 171 | if (!Class->isThisDeclarationADefinition()) { |
| 172 | RewriteForwardClassDecl(D); |
| 173 | break; |
| 174 | } |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 175 | } |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 177 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 178 | if (!Proto->isThisDeclarationADefinition()) { |
| 179 | RewriteForwardProtocolDecl(D); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 184 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 185 | } |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 186 | return true; |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 187 | } |
| 188 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 189 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 190 | RewriteObjC(std::string inFile, raw_ostream *OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 191 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 192 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 193 | |
| 194 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 196 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 198 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 199 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 201 | if (ReplacingStmt) |
| 202 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 203 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 204 | if (DisableReplaceStmt) |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 205 | return; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 206 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 207 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 208 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 209 | ReplacedNodes[Old] = New; |
| 210 | return; |
| 211 | } |
| 212 | if (SilenceRewriteMacroWarning) |
| 213 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 214 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 215 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 216 | } |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 217 | |
| 218 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 219 | if (DisableReplaceStmt) |
| 220 | return; |
| 221 | |
Nick Lewycky | 508ef2c | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 222 | // Measure the old text. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 223 | int Size = Rewrite.getRangeSize(SrcRange); |
| 224 | if (Size == -1) { |
| 225 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 226 | << Old->getSourceRange(); |
| 227 | return; |
| 228 | } |
| 229 | // Get the new text. |
| 230 | std::string SStr; |
| 231 | llvm::raw_string_ostream S(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 232 | New->printPretty(S, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 233 | const std::string &Str = S.str(); |
| 234 | |
| 235 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 236 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 237 | ReplacedNodes[Old] = New; |
| 238 | return; |
| 239 | } |
| 240 | if (SilenceRewriteMacroWarning) |
| 241 | return; |
| 242 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 243 | << Old->getSourceRange(); |
| 244 | } |
| 245 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 246 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 247 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 248 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 249 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 250 | SilenceRewriteMacroWarning) |
| 251 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 253 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 254 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 256 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 257 | StringRef Str) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 258 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 259 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 260 | SilenceRewriteMacroWarning) |
| 261 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 263 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 266 | // Syntactic Rewriting. |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 267 | void RewriteRecordBody(RecordDecl *RD); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 268 | void RewriteInclude(); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 269 | void RewriteForwardClassDecl(DeclGroupRef D); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 270 | void RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &DG); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 271 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 272 | const std::string &typedefString); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 273 | void RewriteImplementations(); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 274 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 275 | ObjCImplementationDecl *IMD, |
| 276 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 277 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 278 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 279 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 280 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 281 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 282 | const FunctionType *&FPRetType); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 283 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 284 | ValueDecl *VD, bool def=false); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 285 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 286 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 287 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 288 | void RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 289 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 290 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 291 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 292 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 293 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 294 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 295 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 296 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 297 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 299 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 300 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 301 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 302 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 303 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 304 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 305 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 306 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 307 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 308 | void RewriteTryReturnStmts(Stmt *S); |
| 309 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 310 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 311 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 312 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 313 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 314 | SourceLocation OrigEnd); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 315 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 316 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 317 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 318 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 319 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 321 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 323 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 324 | void RewriteByRefVar(VarDecl *VD); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 325 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 326 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 327 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 328 | |
| 329 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 330 | std::string &Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 331 | |
| 332 | virtual void Initialize(ASTContext &context) = 0; |
| 333 | |
| 334 | // Metadata Rewriting. |
| 335 | virtual void RewriteMetaDataIntoBuffer(std::string &Result) = 0; |
| 336 | virtual void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
| 337 | StringRef prefix, |
| 338 | StringRef ClassName, |
| 339 | std::string &Result) = 0; |
| 340 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 341 | std::string &Result) = 0; |
| 342 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 343 | StringRef prefix, |
| 344 | StringRef ClassName, |
| 345 | std::string &Result) = 0; |
| 346 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 347 | std::string &Result) = 0; |
| 348 | |
| 349 | // Rewriting ivar access |
| 350 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) = 0; |
| 351 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 352 | std::string &Result) = 0; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 353 | |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 354 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 355 | // rewriting routines on the new ASTs. |
| 356 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 357 | Expr **args, unsigned nargs, |
| 358 | SourceLocation StartLoc=SourceLocation(), |
| 359 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 360 | CallExpr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 361 | QualType msgSendType, |
| 362 | QualType returnType, |
| 363 | SmallVectorImpl<QualType> &ArgTypes, |
| 364 | SmallVectorImpl<Expr*> &MsgExprs, |
| 365 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 366 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 367 | SourceLocation StartLoc=SourceLocation(), |
| 368 | SourceLocation EndLoc=SourceLocation()); |
| 369 | |
| 370 | void SynthCountByEnumWithState(std::string &buf); |
| 371 | void SynthMsgSendFunctionDecl(); |
| 372 | void SynthMsgSendSuperFunctionDecl(); |
| 373 | void SynthMsgSendStretFunctionDecl(); |
| 374 | void SynthMsgSendFpretFunctionDecl(); |
| 375 | void SynthMsgSendSuperStretFunctionDecl(); |
| 376 | void SynthGetClassFunctionDecl(); |
| 377 | void SynthGetMetaClassFunctionDecl(); |
| 378 | void SynthGetSuperClassFunctionDecl(); |
| 379 | void SynthSelGetUidFunctionDecl(); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 380 | void SynthSuperConstructorFunctionDecl(); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 381 | |
| 382 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 384 | StringRef funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 386 | StringRef funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 387 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 388 | std::string Tag, std::string Desc); |
| 389 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 390 | std::string ImplTag, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 391 | int i, StringRef funcName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 392 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 393 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 394 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 395 | StringRef FunName); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 396 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 397 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 398 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 400 | // Misc. helper routines. |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 401 | QualType getProtocolType(); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 402 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 403 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 404 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 405 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 406 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 407 | |
| 408 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 409 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 410 | void GetBlockDeclRefExprs(Stmt *S); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 411 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 412 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 413 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 415 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 416 | // 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] | 417 | bool isTopLevelBlockPointerType(QualType T) { |
| 418 | return isa<BlockPointerType>(T); |
| 419 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 421 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 422 | /// to a function pointer type and upon success, returns true; false |
| 423 | /// otherwise. |
| 424 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 425 | if (isTopLevelBlockPointerType(T)) { |
| 426 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 427 | T = Context->getPointerType(BPT->getPointeeType()); |
| 428 | return true; |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 433 | bool needToScanForQualifiers(QualType T); |
| 434 | QualType getSuperStructType(); |
| 435 | QualType getConstantStringStructType(); |
| 436 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 437 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 438 | |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 439 | void convertToUnqualifiedObjCType(QualType &T) { |
| 440 | if (T->isObjCQualifiedIdType()) |
| 441 | T = Context->getObjCIdType(); |
| 442 | else if (T->isObjCQualifiedClassType()) |
| 443 | T = Context->getObjCClassType(); |
| 444 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 9f0bc57 | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 445 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 446 | if (const ObjCObjectPointerType * OBJPT = |
| 447 | T->getAsObjCInterfacePointerType()) { |
| 448 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 449 | T = QualType(IFaceT, 0); |
| 450 | T = Context->getPointerType(T); |
| 451 | } |
| 452 | } |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 455 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 456 | bool isObjCType(QualType T) { |
| 457 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 458 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 460 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 462 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 463 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 464 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 466 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 468 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 474 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 475 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 476 | const char *&RParen); |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 477 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 478 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 480 | if (From[i] == '"') |
| 481 | To += "\\\""; |
| 482 | else |
| 483 | To += From[i]; |
| 484 | } |
| 485 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 486 | |
| 487 | QualType getSimpleFunctionType(QualType result, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 488 | ArrayRef<QualType> args, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 489 | bool variadic = false) { |
Fariborz Jahanian | e1378a4 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 490 | if (result == Context->getObjCInstanceType()) |
| 491 | result = Context->getObjCIdType(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 492 | FunctionProtoType::ExtProtoInfo fpi; |
| 493 | fpi.Variadic = variadic; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 494 | return Context->getFunctionType(result, args, fpi); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 495 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 496 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 497 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 498 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 499 | CastKind Kind, Expr *E) { |
| 500 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 501 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 502 | SourceLocation(), SourceLocation()); |
| 503 | } |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 504 | |
| 505 | StringLiteral *getStringLiteral(StringRef Str) { |
| 506 | QualType StrType = Context->getConstantArrayType( |
| 507 | Context->CharTy, llvm::APInt(32, Str.size() + 1), ArrayType::Normal, |
| 508 | 0); |
| 509 | return StringLiteral::Create(*Context, Str, StringLiteral::Ascii, |
| 510 | /*Pascal=*/false, StrType, SourceLocation()); |
| 511 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 512 | }; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 513 | |
| 514 | class RewriteObjCFragileABI : public RewriteObjC { |
| 515 | public: |
| 516 | |
| 517 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 518 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 519 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 520 | D, LOpts, |
| 521 | silenceMacroWarn) {} |
| 522 | |
| 523 | ~RewriteObjCFragileABI() {} |
| 524 | virtual void Initialize(ASTContext &context); |
| 525 | |
| 526 | // Rewriting metadata |
| 527 | template<typename MethodIterator> |
| 528 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 529 | MethodIterator MethodEnd, |
| 530 | bool IsInstanceMethod, |
| 531 | StringRef prefix, |
| 532 | StringRef ClassName, |
| 533 | std::string &Result); |
| 534 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 535 | StringRef prefix, |
| 536 | StringRef ClassName, |
| 537 | std::string &Result); |
| 538 | virtual void RewriteObjCProtocolListMetaData( |
| 539 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 540 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 541 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 542 | std::string &Result); |
| 543 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 544 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 545 | std::string &Result); |
| 546 | |
| 547 | // Rewriting ivar |
| 548 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 549 | std::string &Result); |
| 550 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 551 | }; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 555 | NamedDecl *D) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 556 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 557 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 558 | for (FunctionProtoType::param_type_iterator I = fproto->param_type_begin(), |
| 559 | E = fproto->param_type_end(); |
| 560 | I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 561 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 562 | // All the args are checked/rewritten. Don't call twice! |
| 563 | RewriteBlockPointerDecl(D); |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 570 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 571 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 572 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 575 | static bool IsHeaderFile(const std::string &Filename) { |
| 576 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 578 | if (DotPos == std::string::npos) { |
| 579 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 583 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 584 | // C header: .h |
| 585 | // C++ header: .hh or .H; |
| 586 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 589 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 590 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 591 | bool silenceMacroWarn) |
| 592 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 593 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 594 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 595 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 596 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 597 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 598 | DiagnosticsEngine::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 599 | "rewriter doesn't support user-specified control flow semantics " |
| 600 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 603 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 604 | raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 605 | DiagnosticsEngine &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 606 | const LangOptions &LOpts, |
| 607 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 608 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 611 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 612 | Context = &context; |
| 613 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 614 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 615 | MsgSendFunctionDecl = 0; |
| 616 | MsgSendSuperFunctionDecl = 0; |
| 617 | MsgSendStretFunctionDecl = 0; |
| 618 | MsgSendSuperStretFunctionDecl = 0; |
| 619 | MsgSendFpretFunctionDecl = 0; |
| 620 | GetClassFunctionDecl = 0; |
| 621 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 622 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 623 | SelGetUidFunctionDecl = 0; |
| 624 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 625 | ConstantStringClassReference = 0; |
| 626 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 627 | CurMethodDef = 0; |
| 628 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 629 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 630 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 631 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 632 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 633 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 634 | BcLabelCount = 0; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 635 | SuperConstructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 636 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 637 | PropParentMap = 0; |
| 638 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 639 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 640 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 642 | // Get the ID and start/end of the main file. |
| 643 | MainFileID = SM->getMainFileID(); |
| 644 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 645 | MainFileStart = MainBuf->getBufferStart(); |
| 646 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 648 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 651 | //===----------------------------------------------------------------------===// |
| 652 | // Top Level Driver Code |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 655 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 656 | if (Diags.hasErrorOccurred()) |
| 657 | return; |
| 658 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 659 | // Two cases: either the decl could be in the main file, or it could be in a |
| 660 | // #included file. If the former, rewrite it now. If the later, check to see |
| 661 | // if we rewrote the #include/#import. |
| 662 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 663 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 665 | // If this is for a builtin, ignore it. |
| 666 | if (Loc.isInvalid()) return; |
| 667 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 668 | // Look for built-in declarations that we need to refer during the rewrite. |
| 669 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 670 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 671 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 672 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 673 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 674 | ConstantStringClassReference = FVD; |
| 675 | return; |
| 676 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 677 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 678 | if (ID->isThisDeclarationADefinition()) |
| 679 | RewriteInterfaceDecl(ID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 680 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 681 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 682 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 683 | if (PD->isThisDeclarationADefinition()) |
| 684 | RewriteProtocolDecl(PD); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 685 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 686 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 687 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 688 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 689 | DI != DIEnd; ) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 690 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 691 | if (!IFace->isThisDeclarationADefinition()) { |
| 692 | SmallVector<Decl *, 8> DG; |
| 693 | SourceLocation StartLoc = IFace->getLocStart(); |
| 694 | do { |
| 695 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 696 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 697 | StartLoc == (*DI)->getLocStart()) |
| 698 | DG.push_back(*DI); |
| 699 | else |
| 700 | break; |
| 701 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 702 | ++DI; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 703 | } while (DI != DIEnd); |
| 704 | RewriteForwardClassDecl(DG); |
| 705 | continue; |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 706 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 707 | } |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 708 | |
| 709 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 710 | if (!Proto->isThisDeclarationADefinition()) { |
| 711 | SmallVector<Decl *, 8> DG; |
| 712 | SourceLocation StartLoc = Proto->getLocStart(); |
| 713 | do { |
| 714 | if (isa<ObjCProtocolDecl>(*DI) && |
| 715 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 716 | StartLoc == (*DI)->getLocStart()) |
| 717 | DG.push_back(*DI); |
| 718 | else |
| 719 | break; |
| 720 | |
| 721 | ++DI; |
| 722 | } while (DI != DIEnd); |
| 723 | RewriteForwardProtocolDecl(DG); |
| 724 | continue; |
| 725 | } |
| 726 | } |
| 727 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 728 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 729 | ++DI; |
| 730 | } |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 732 | // If we have a decl in the main file, see if we should rewrite it. |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 733 | if (SM->isWrittenInMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 734 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 737 | //===----------------------------------------------------------------------===// |
| 738 | // Syntactic (non-AST) Rewriting Code |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 741 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 742 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 743 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 744 | const char *MainBufStart = MainBuf.begin(); |
| 745 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 746 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 748 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 749 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 750 | if (*BufPtr == '#') { |
| 751 | if (++BufPtr == MainBufEnd) |
| 752 | return; |
| 753 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 754 | if (++BufPtr == MainBufEnd) |
| 755 | return; |
| 756 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 757 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 759 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 760 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 761 | BufPtr += ImportLen; |
| 762 | } |
| 763 | } |
| 764 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 767 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 768 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 769 | std::string S; |
| 770 | S = "((struct "; |
| 771 | S += ClassDecl->getIdentifier()->getName(); |
| 772 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 773 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 774 | return S; |
| 775 | } |
| 776 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 777 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 778 | ObjCImplementationDecl *IMD, |
| 779 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 780 | static bool objcGetPropertyDefined = false; |
| 781 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 782 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 783 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 784 | const char *startBuf = SM->getCharacterData(startLoc); |
| 785 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 786 | const char *semiBuf = strchr(startBuf, ';'); |
| 787 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 788 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 789 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 790 | |
| 791 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 792 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 795 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
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; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 800 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 801 | if (!PD->getGetterMethodDecl()->isDefined()) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 802 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 803 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 804 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 805 | std::string Getr; |
| 806 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 807 | objcGetPropertyDefined = true; |
| 808 | // FIXME. Is this attribute correct in all cases? |
| 809 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 810 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 811 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 812 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 813 | PD->getGetterMethodDecl(), Getr); |
| 814 | Getr += "{ "; |
| 815 | // Synthesize an explicit cast to gain access to the ivar. |
| 816 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 817 | if (GenGetProperty) { |
| 818 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 819 | Getr += "typedef "; |
| 820 | const FunctionType *FPRetType = 0; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 821 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr, |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 822 | FPRetType); |
| 823 | Getr += " _TYPE"; |
| 824 | if (FPRetType) { |
| 825 | Getr += ")"; // close the precedence "scope" for "*". |
| 826 | |
| 827 | // Now, emit the argument types (if any). |
| 828 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 829 | Getr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 830 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 831 | if (i) Getr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 832 | std::string ParamStr = |
| 833 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 834 | Getr += ParamStr; |
| 835 | } |
| 836 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 837 | if (FT->getNumParams()) |
| 838 | Getr += ", "; |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 839 | Getr += "..."; |
| 840 | } |
| 841 | Getr += ")"; |
| 842 | } else |
| 843 | Getr += "()"; |
| 844 | } |
| 845 | Getr += ";\n"; |
| 846 | Getr += "return (_TYPE)"; |
| 847 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 848 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 849 | Getr += ", 1)"; |
| 850 | } |
| 851 | else |
| 852 | Getr += "return " + getIvarAccessString(OID); |
| 853 | Getr += "; }"; |
| 854 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 855 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 856 | |
| 857 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 858 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 860 | // Generate the 'setter' function. |
| 861 | std::string Setr; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 862 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 863 | ObjCPropertyDecl::OBJC_PR_copy); |
| 864 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 865 | objcSetPropertyDefined = true; |
| 866 | // FIXME. Is this attribute correct in all cases? |
| 867 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 868 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 869 | } |
| 870 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 871 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 872 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 873 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 874 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 875 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 876 | if (GenSetProperty) { |
| 877 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 878 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 879 | Setr += ", (id)"; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 880 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 881 | Setr += ", "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 882 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 883 | Setr += "0, "; |
| 884 | else |
| 885 | Setr += "1, "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 886 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 887 | Setr += "1)"; |
| 888 | else |
| 889 | Setr += "0)"; |
| 890 | } |
| 891 | else { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 892 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 893 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 894 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 895 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 896 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 897 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 899 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 900 | std::string &typedefString) { |
| 901 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 902 | typedefString += ForwardDecl->getNameAsString(); |
| 903 | typedefString += "\n"; |
| 904 | typedefString += "#define _REWRITER_typedef_"; |
| 905 | typedefString += ForwardDecl->getNameAsString(); |
| 906 | typedefString += "\n"; |
| 907 | typedefString += "typedef struct objc_object "; |
| 908 | typedefString += ForwardDecl->getNameAsString(); |
| 909 | typedefString += ";\n#endif\n"; |
| 910 | } |
| 911 | |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 912 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 913 | const std::string &typedefString) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 914 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 915 | const char *startBuf = SM->getCharacterData(startLoc); |
| 916 | const char *semiPtr = strchr(startBuf, ';'); |
| 917 | // Replace the @class with typedefs corresponding to the classes. |
| 918 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 919 | } |
| 920 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 921 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 922 | std::string typedefString; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 923 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 924 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 925 | if (I == D.begin()) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 926 | // Translate to typedef's that forward reference structs with the same name |
| 927 | // as the class. As a convenience, we include the original declaration |
| 928 | // as a comment. |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 929 | typedefString += "// @class "; |
| 930 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 931 | typedefString += ";\n"; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 932 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 933 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 934 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 935 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 936 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 937 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 939 | void RewriteObjC::RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &D) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 940 | std::string typedefString; |
| 941 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 942 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 943 | if (i == 0) { |
| 944 | typedefString += "// @class "; |
| 945 | typedefString += ForwardDecl->getNameAsString(); |
| 946 | typedefString += ";\n"; |
| 947 | } |
| 948 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 949 | } |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 950 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 953 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 954 | // When method is a synthesized one, such as a getter/setter there is |
| 955 | // nothing to rewrite. |
Fariborz Jahanian | e1378a4 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 956 | if (Method->isImplicit()) |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 957 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 958 | SourceLocation LocStart = Method->getLocStart(); |
| 959 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 961 | if (SM->getExpansionLineNumber(LocEnd) > |
| 962 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 963 | InsertText(LocStart, "#if 0\n"); |
| 964 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 965 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 966 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 971 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 973 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 974 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 977 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 978 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 980 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 981 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 983 | for (auto *I : CatDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 984 | RewriteProperty(I); |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 985 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 986 | for (auto *I : CatDecl->instance_methods()) |
| 987 | RewriteMethodDeclaration(I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 989 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 990 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 991 | RewriteMethodDeclaration(*I); |
| 992 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 993 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 994 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 995 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 998 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 999 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 1000 | assert(PDecl->isThisDeclarationADefinition()); |
| 1001 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1002 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1003 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 1005 | for (auto *I : PDecl->instance_methods()) |
| 1006 | RewriteMethodDeclaration(I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1007 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1008 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1009 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1010 | RewriteMethodDeclaration(*I); |
| 1011 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 1012 | for (auto *I : PDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1013 | RewriteProperty(I); |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1014 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1015 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1016 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1017 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1018 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1019 | // Must comment out @optional/@required |
| 1020 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1021 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1022 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1023 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1024 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1025 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1027 | } |
| 1028 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1029 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1030 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1036 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1037 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1038 | if (LocStart.isInvalid()) |
| 1039 | llvm_unreachable("Invalid SourceLocation"); |
| 1040 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1041 | ReplaceText(LocStart, 0, "// "); |
| 1042 | } |
| 1043 | |
| 1044 | void |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1045 | RewriteObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1046 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1047 | if (LocStart.isInvalid()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1048 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1049 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1050 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1053 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1054 | const FunctionType *&FPRetType) { |
| 1055 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1056 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1057 | else if (T->isFunctionPointerType() || |
| 1058 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1059 | // needs special handling, since pointer-to-functions have special |
| 1060 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1061 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1062 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1063 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1064 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1065 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1066 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1067 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1068 | ResultStr += |
| 1069 | FPRetType->getReturnType().getAsString(Context->getPrintingPolicy()); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1070 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1073 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1076 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1077 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1078 | std::string &ResultStr) { |
| 1079 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1080 | const FunctionType *FPRetType = 0; |
| 1081 | ResultStr += "\nstatic "; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1082 | RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 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 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1086 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1088 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1089 | NameStr += "_I_"; |
| 1090 | else |
| 1091 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1093 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1094 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1097 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1098 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1099 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1102 | { |
| 1103 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1104 | int len = selString.size(); |
| 1105 | for (int i = 0; i < len; i++) |
| 1106 | if (selString[i] == ':') |
| 1107 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1108 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1109 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1110 | // Remember this name for metadata emission |
| 1111 | MethodInternalNames[OMD] = NameStr; |
| 1112 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1114 | // Rewrite arguments |
| 1115 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1117 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1118 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1119 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1120 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1121 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1122 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1123 | ResultStr += "struct "; |
| 1124 | } |
| 1125 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1126 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1127 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1128 | } |
| 1129 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1130 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1131 | Context->getPrintingPolicy()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1133 | ResultStr += " self, "; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1134 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1135 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1137 | // Method arguments. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 1138 | for (const auto *PDecl : OMD->params()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1139 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1140 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1141 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1142 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1143 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1144 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1145 | QualType QT = PDecl->getType(); |
| 1146 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1147 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1148 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1149 | ResultStr += Name; |
| 1150 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1151 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1152 | if (OMD->isVariadic()) |
| 1153 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1154 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1156 | if (FPRetType) { |
| 1157 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1159 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1160 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1161 | ResultStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1162 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1163 | if (i) ResultStr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1164 | std::string ParamStr = |
| 1165 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1166 | ResultStr += ParamStr; |
| 1167 | } |
| 1168 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1169 | if (FT->getNumParams()) |
| 1170 | ResultStr += ", "; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1171 | ResultStr += "..."; |
| 1172 | } |
| 1173 | ResultStr += ")"; |
| 1174 | } else { |
| 1175 | ResultStr += "()"; |
| 1176 | } |
| 1177 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1178 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1179 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1180 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1181 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1183 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 1185 | for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1186 | std::string ResultStr; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1187 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1188 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1189 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1190 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1191 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1192 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1193 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1194 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1196 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1197 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1198 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1199 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1200 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1201 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1202 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1203 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1204 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1206 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1207 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1208 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1209 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1210 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1211 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1213 | I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1214 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1217 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1220 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1221 | std::string ResultStr; |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1222 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1223 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1224 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1225 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1226 | ResultStr += "\n"; |
| 1227 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1228 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1229 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1230 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1231 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1232 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1233 | // Mark this typedef as having been generated. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1234 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1235 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1236 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 1238 | for (auto *I : ClassDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1239 | RewriteProperty(I); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 1240 | for (auto *I : ClassDecl->instance_methods()) |
| 1241 | RewriteMethodDeclaration(I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1243 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1244 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1245 | RewriteMethodDeclaration(*I); |
| 1246 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1247 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1248 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1249 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1252 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1253 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1255 | // We just magically know some things about the structure of this |
| 1256 | // expression. |
| 1257 | ObjCMessageExpr *OldMsg = |
| 1258 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1259 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1261 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1262 | // we need to suppress rewriting the sub-statements. |
| 1263 | Expr *Base, *RHS; |
| 1264 | { |
| 1265 | DisableReplaceStmtScope S(*this); |
| 1266 | |
| 1267 | // Rebuild the base expression if we have one. |
| 1268 | Base = 0; |
| 1269 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1270 | Base = OldMsg->getInstanceReceiver(); |
| 1271 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1272 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1273 | } |
| 1274 | |
| 1275 | // Rebuild the RHS. |
| 1276 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1277 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1278 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1279 | } |
| 1280 | |
| 1281 | // TODO: avoid this copy. |
| 1282 | SmallVector<SourceLocation, 1> SelLocs; |
| 1283 | OldMsg->getSelectorLocs(SelLocs); |
| 1284 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1285 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1286 | switch (OldMsg->getReceiverKind()) { |
| 1287 | case ObjCMessageExpr::Class: |
| 1288 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1289 | OldMsg->getValueKind(), |
| 1290 | OldMsg->getLeftLoc(), |
| 1291 | OldMsg->getClassReceiverTypeInfo(), |
| 1292 | OldMsg->getSelector(), |
| 1293 | SelLocs, |
| 1294 | OldMsg->getMethodDecl(), |
| 1295 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1296 | OldMsg->getRightLoc(), |
| 1297 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | |
| 1300 | case ObjCMessageExpr::Instance: |
| 1301 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1302 | OldMsg->getValueKind(), |
| 1303 | OldMsg->getLeftLoc(), |
| 1304 | Base, |
| 1305 | OldMsg->getSelector(), |
| 1306 | SelLocs, |
| 1307 | OldMsg->getMethodDecl(), |
| 1308 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1309 | OldMsg->getRightLoc(), |
| 1310 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | |
| 1313 | case ObjCMessageExpr::SuperClass: |
| 1314 | case ObjCMessageExpr::SuperInstance: |
| 1315 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1316 | OldMsg->getValueKind(), |
| 1317 | OldMsg->getLeftLoc(), |
| 1318 | OldMsg->getSuperLoc(), |
| 1319 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1320 | OldMsg->getSuperType(), |
| 1321 | OldMsg->getSelector(), |
| 1322 | SelLocs, |
| 1323 | OldMsg->getMethodDecl(), |
| 1324 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1325 | OldMsg->getRightLoc(), |
| 1326 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1327 | break; |
| 1328 | } |
| 1329 | |
| 1330 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1331 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1332 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1335 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1336 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1337 | |
| 1338 | // We just magically know some things about the structure of this |
| 1339 | // expression. |
| 1340 | ObjCMessageExpr *OldMsg = |
| 1341 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1342 | |
| 1343 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1344 | // we need to suppress rewriting the sub-statements. |
| 1345 | Expr *Base = 0; |
| 1346 | { |
| 1347 | DisableReplaceStmtScope S(*this); |
| 1348 | |
| 1349 | // Rebuild the base expression if we have one. |
| 1350 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1351 | Base = OldMsg->getInstanceReceiver(); |
| 1352 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1353 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1354 | } |
Fariborz Jahanian | bb40ea4 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1355 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1356 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1357 | // Intentionally empty. |
| 1358 | SmallVector<SourceLocation, 1> SelLocs; |
| 1359 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1360 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1361 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1362 | switch (OldMsg->getReceiverKind()) { |
| 1363 | case ObjCMessageExpr::Class: |
| 1364 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1365 | OldMsg->getValueKind(), |
| 1366 | OldMsg->getLeftLoc(), |
| 1367 | OldMsg->getClassReceiverTypeInfo(), |
| 1368 | OldMsg->getSelector(), |
| 1369 | SelLocs, |
| 1370 | OldMsg->getMethodDecl(), |
| 1371 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1372 | OldMsg->getRightLoc(), |
| 1373 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1374 | break; |
| 1375 | |
| 1376 | case ObjCMessageExpr::Instance: |
| 1377 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1378 | OldMsg->getValueKind(), |
| 1379 | OldMsg->getLeftLoc(), |
| 1380 | Base, |
| 1381 | OldMsg->getSelector(), |
| 1382 | SelLocs, |
| 1383 | OldMsg->getMethodDecl(), |
| 1384 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1385 | OldMsg->getRightLoc(), |
| 1386 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1387 | break; |
| 1388 | |
| 1389 | case ObjCMessageExpr::SuperClass: |
| 1390 | case ObjCMessageExpr::SuperInstance: |
| 1391 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1392 | OldMsg->getValueKind(), |
| 1393 | OldMsg->getLeftLoc(), |
| 1394 | OldMsg->getSuperLoc(), |
| 1395 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1396 | OldMsg->getSuperType(), |
| 1397 | OldMsg->getSelector(), |
| 1398 | SelLocs, |
| 1399 | OldMsg->getMethodDecl(), |
| 1400 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1401 | OldMsg->getRightLoc(), |
| 1402 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1403 | break; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1404 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1405 | |
| 1406 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1407 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1408 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1411 | /// SynthCountByEnumWithState - To print: |
| 1412 | /// ((unsigned int (*) |
| 1413 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1415 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | /// "countByEnumeratingWithState:objects:count:"), |
| 1417 | /// &enumState, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1418 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1419 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1420 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1421 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1422 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1423 | buf += "\n\t\t"; |
| 1424 | buf += "((id)l_collection,\n\t\t"; |
| 1425 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1426 | buf += "\n\t\t"; |
| 1427 | buf += "&enumState, " |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1428 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1429 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1430 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1431 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1432 | /// statement to exit to its outer synthesized loop. |
| 1433 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1434 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1435 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1436 | return S; |
| 1437 | // replace break with goto __break_label |
| 1438 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1440 | SourceLocation startLoc = S->getLocStart(); |
| 1441 | buf = "goto __break_label_"; |
| 1442 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1443 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1449 | /// statement to continue with its inner synthesized loop. |
| 1450 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1451 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1452 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1453 | return S; |
| 1454 | // replace continue with goto __continue_label |
| 1455 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1457 | SourceLocation startLoc = S->getLocStart(); |
| 1458 | buf = "goto __continue_label_"; |
| 1459 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1460 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1465 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1466 | /// It rewrites: |
| 1467 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1469 | /// Into: |
| 1470 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1472 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1473 | /// id __rw_items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1474 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1476 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1477 | /// if (limit) { |
| 1478 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1479 | /// do { |
| 1480 | /// unsigned long counter = 0; |
| 1481 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1483 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1484 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1485 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1486 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1487 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1489 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1490 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1491 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1492 | /// } |
| 1493 | /// else |
| 1494 | /// elem = nil; |
| 1495 | /// } |
| 1496 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1497 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1498 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1499 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1501 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1503 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1505 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1506 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1507 | StringRef elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1508 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1509 | std::string buf; |
| 1510 | buf = "\n{\n\t"; |
| 1511 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1512 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1513 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1514 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1515 | if (ElementType->isObjCQualifiedIdType() || |
| 1516 | ElementType->isObjCQualifiedInterfaceType()) |
| 1517 | // Simply use 'id' for all qualified types. |
| 1518 | elementTypeAsString = "id"; |
| 1519 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1520 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1521 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1522 | buf += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1523 | elementName = D->getName(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1524 | buf += elementName; |
| 1525 | buf += ";\n\t"; |
| 1526 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1527 | else { |
| 1528 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1529 | elementName = DR->getDecl()->getName(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1530 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1531 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1532 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1533 | // Simply use 'id' for all qualified types. |
| 1534 | elementTypeAsString = "id"; |
| 1535 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1536 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1537 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1539 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1540 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1541 | // id __rw_items[16]; |
| 1542 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1543 | // id l_collection = (id) |
| 1544 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1545 | // Find start location of 'collection' the hard way! |
| 1546 | const char *startCollectionBuf = startBuf; |
| 1547 | startCollectionBuf += 3; // skip 'for' |
| 1548 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1549 | startCollectionBuf++; // skip '(' |
| 1550 | // find 'in' and skip it. |
| 1551 | while (*startCollectionBuf != ' ' || |
| 1552 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1553 | (*(startCollectionBuf+3) != ' ' && |
| 1554 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1555 | startCollectionBuf++; |
| 1556 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | |
| 1558 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1559 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1560 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1561 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1562 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1563 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1564 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1566 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1567 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1568 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1570 | // ((unsigned int (*) |
| 1571 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1573 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | // "countByEnumeratingWithState:objects:count:"), |
| 1575 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1576 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1577 | buf += "unsigned long limit =\n\t\t"; |
| 1578 | SynthCountByEnumWithState(buf); |
| 1579 | buf += ";\n\t"; |
| 1580 | /// if (limit) { |
| 1581 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1582 | /// do { |
| 1583 | /// unsigned long counter = 0; |
| 1584 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1586 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1587 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1588 | buf += "if (limit) {\n\t"; |
| 1589 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1590 | buf += "do {\n\t\t"; |
| 1591 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1592 | buf += "do {\n\t\t\t"; |
| 1593 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1594 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1595 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1596 | buf += " = ("; |
| 1597 | buf += elementTypeAsString; |
| 1598 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1599 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1600 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1602 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1603 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1605 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1606 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1607 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1608 | /// } |
| 1609 | /// else |
| 1610 | /// elem = nil; |
| 1611 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1613 | buf = ";\n\t"; |
| 1614 | buf += "__continue_label_"; |
| 1615 | buf += utostr(ObjCBcLabelNo.back()); |
| 1616 | buf += ": ;"; |
| 1617 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1618 | buf += "} while (counter < limit);\n\t"; |
| 1619 | buf += "} while (limit = "; |
| 1620 | SynthCountByEnumWithState(buf); |
| 1621 | buf += ");\n\t"; |
| 1622 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1623 | buf += " = (("; |
| 1624 | buf += elementTypeAsString; |
| 1625 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1626 | buf += "__break_label_"; |
| 1627 | buf += utostr(ObjCBcLabelNo.back()); |
| 1628 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1629 | buf += "}\n\t"; |
| 1630 | buf += "else\n\t\t"; |
| 1631 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1632 | buf += " = (("; |
| 1633 | buf += elementTypeAsString; |
| 1634 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1635 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1637 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1638 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1639 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1640 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1641 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1642 | } else { |
| 1643 | /* Need to treat single statements specially. For example: |
| 1644 | * |
| 1645 | * for (A *a in b) if (stuff()) break; |
| 1646 | * for (A *a in b) xxxyy; |
| 1647 | * |
| 1648 | * The following code simply scans ahead to the semi to find the actual end. |
| 1649 | */ |
| 1650 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1651 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1652 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1653 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1654 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1655 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1656 | Stmts.pop_back(); |
| 1657 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1658 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1662 | /// This routine rewrites @synchronized(expr) stmt; |
| 1663 | /// into: |
| 1664 | /// objc_sync_enter(expr); |
| 1665 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1666 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1667 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1668 | // Get the start location and compute the semi location. |
| 1669 | SourceLocation startLoc = S->getLocStart(); |
| 1670 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1672 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | |
| 1674 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1675 | buf = "objc_sync_enter((id)"; |
| 1676 | const char *lparenBuf = startBuf; |
| 1677 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1678 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1680 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1681 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1682 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1683 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1684 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1685 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1686 | buf = ");\n"; |
| 1687 | // declare a new scope with two variables, _stack and _rethrow. |
| 1688 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1689 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1690 | buf += "char *pointers[4];} _stack;\n"; |
| 1691 | buf += "id volatile _rethrow = 0;\n"; |
| 1692 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1693 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1694 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1695 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1696 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1698 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1699 | SourceLocation lastCurlyLoc = startLoc; |
| 1700 | buf = "}\nelse {\n"; |
| 1701 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1702 | buf += "}\n"; |
| 1703 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1704 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1705 | |
| 1706 | std::string syncBuf; |
| 1707 | syncBuf += " objc_sync_exit("; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1708 | |
| 1709 | Expr *syncExpr = S->getSynchExpr(); |
| 1710 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1711 | ? CK_BitCast : |
| 1712 | syncExpr->getType()->isBlockPointerType() |
| 1713 | ? CK_BlockPointerToObjCPointerCast |
| 1714 | : CK_CPointerToObjCPointerCast; |
| 1715 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1716 | CK, syncExpr); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1717 | std::string syncExprBufS; |
| 1718 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1719 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1720 | syncBuf += syncExprBuf.str(); |
| 1721 | syncBuf += ");"; |
| 1722 | |
| 1723 | buf += syncBuf; |
| 1724 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1725 | buf += "}\n"; |
| 1726 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1728 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1729 | |
| 1730 | bool hasReturns = false; |
| 1731 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1732 | if (hasReturns) |
| 1733 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1734 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1735 | return 0; |
| 1736 | } |
| 1737 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1738 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1739 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1740 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1741 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1742 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1743 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1744 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1745 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1747 | TryFinallyContainsReturnDiag); |
| 1748 | } |
| 1749 | return; |
| 1750 | } |
| 1751 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1752 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1753 | { |
| 1754 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1755 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1756 | if (*CI) |
| 1757 | HasReturnStmts(*CI, hasReturns); |
| 1758 | |
| 1759 | if (isa<ReturnStmt>(S)) |
| 1760 | hasReturns = true; |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1765 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1766 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1767 | if (*CI) { |
| 1768 | RewriteTryReturnStmts(*CI); |
| 1769 | } |
| 1770 | if (isa<ReturnStmt>(S)) { |
| 1771 | SourceLocation startLoc = S->getLocStart(); |
| 1772 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1773 | |
| 1774 | const char *semiBuf = strchr(startBuf, ';'); |
| 1775 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1776 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1777 | |
| 1778 | std::string buf; |
| 1779 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1780 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1781 | ReplaceText(startLoc, 6, buf); |
| 1782 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1783 | } |
| 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1788 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1789 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1790 | if (*CI) { |
| 1791 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1792 | } |
| 1793 | if (isa<ReturnStmt>(S)) { |
| 1794 | SourceLocation startLoc = S->getLocStart(); |
| 1795 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1796 | |
| 1797 | const char *semiBuf = strchr(startBuf, ';'); |
| 1798 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1799 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1800 | |
| 1801 | std::string buf; |
| 1802 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1803 | buf += syncExitBuf; |
| 1804 | buf += " return"; |
| 1805 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1806 | ReplaceText(startLoc, 6, buf); |
| 1807 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1808 | } |
| 1809 | return; |
| 1810 | } |
| 1811 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1812 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1813 | // Get the start location and compute the semi location. |
| 1814 | SourceLocation startLoc = S->getLocStart(); |
| 1815 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | assert((*startBuf == '@') && "bogus @try location"); |
| 1818 | |
| 1819 | std::string buf; |
| 1820 | // declare a new scope with two variables, _stack and _rethrow. |
| 1821 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1822 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1823 | buf += "char *pointers[4];} _stack;\n"; |
| 1824 | buf += "id volatile _rethrow = 0;\n"; |
| 1825 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1826 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1827 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1828 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1830 | startLoc = S->getTryBody()->getLocEnd(); |
| 1831 | startBuf = SM->getCharacterData(startLoc); |
| 1832 | |
| 1833 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1835 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1836 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1837 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1838 | buf = " /* @catch begin */ else {\n"; |
| 1839 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1840 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1841 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1842 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1843 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1845 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1846 | } else { /* no catch list */ |
| 1847 | buf = "}\nelse {\n"; |
| 1848 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1849 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1850 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1851 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1852 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1853 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1854 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1855 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1857 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1858 | buf = "if ("; // we are generating code for the first catch clause |
| 1859 | else |
| 1860 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1861 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1862 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1864 | assert((*startBuf == '@') && "bogus @catch location"); |
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 | const char *lParenLoc = strchr(startBuf, '('); |
| 1867 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1868 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1869 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1870 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1871 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1872 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1873 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1874 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1875 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1877 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1878 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1879 | } else if (catchDecl) { |
| 1880 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1881 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1882 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1883 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1884 | } else if (const ObjCObjectPointerType *Ptr = |
| 1885 | t->getAs<ObjCObjectPointerType>()) { |
| 1886 | // Should be a pointer to a class. |
| 1887 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1888 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1889 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1890 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1891 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1892 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1896 | lastCatchBody = Catch->getCatchBody(); |
| 1897 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1898 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1899 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1900 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1901 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1902 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1905 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1906 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1907 | } else { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1908 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1909 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1910 | } |
| 1911 | // Complete the catch list... |
| 1912 | if (lastCatchBody) { |
| 1913 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1914 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1915 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1917 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1918 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1919 | buf = "} /* last catch end */\n"; |
| 1920 | buf += "else {\n"; |
| 1921 | buf += " _rethrow = _caught;\n"; |
| 1922 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1923 | buf += "} } /* @catch end */\n"; |
| 1924 | if (!S->getFinallyStmt()) |
| 1925 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1926 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1928 | // Set lastCurlyLoc |
| 1929 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1930 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1931 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1932 | startLoc = finalStmt->getLocStart(); |
| 1933 | startBuf = SM->getCharacterData(startLoc); |
| 1934 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1936 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1938 | Stmt *body = finalStmt->getFinallyBody(); |
| 1939 | SourceLocation startLoc = body->getLocStart(); |
| 1940 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1941 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1942 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1944 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1946 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1947 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1948 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1949 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1951 | // Set lastCurlyLoc |
| 1952 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1954 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1955 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1956 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1957 | buf = "{ /* implicit finally clause */\n"; |
| 1958 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1959 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1960 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1961 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Now check for any return/continue/go statements within the @try. |
| 1964 | // The implicit finally clause won't called if the @try contains any |
| 1965 | // jump statements. |
| 1966 | bool hasReturns = false; |
| 1967 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1968 | if (hasReturns) |
| 1969 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1970 | } |
| 1971 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1972 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1973 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
| 1976 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1978 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1979 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1980 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1981 | // Get the start location and compute the semi location. |
| 1982 | SourceLocation startLoc = S->getLocStart(); |
| 1983 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1985 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1986 | |
| 1987 | std::string buf; |
| 1988 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1989 | if (S->getThrowExpr()) |
| 1990 | buf = "objc_exception_throw("; |
| 1991 | else // add an implicit argument |
| 1992 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1994 | // handle "@ throw" correctly. |
| 1995 | const char *wBuf = strchr(startBuf, 'w'); |
| 1996 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1997 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1999 | const char *semiBuf = strchr(startBuf, ';'); |
| 2000 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2001 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2002 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2003 | return 0; |
| 2004 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2005 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2006 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2007 | // Create a new string expression. |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2008 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2009 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2010 | Expr *Replacement = getStringLiteral(StrEncoding); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2011 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2013 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2014 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2015 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2018 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2019 | if (!SelGetUidFunctionDecl) |
| 2020 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2021 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2022 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2023 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2024 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2025 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2026 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2027 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2028 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2029 | return SelExp; |
| 2030 | } |
| 2031 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2032 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2033 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2034 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2035 | // 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] | 2036 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2038 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2039 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2040 | VK_LValue, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2042 | // 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] | 2043 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2044 | ImplicitCastExpr *ICE = |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2045 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2046 | DRE, 0, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2048 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2050 | CallExpr *Exp = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2051 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2052 | FT->getCallResultType(*Context), |
| 2053 | VK_RValue, EndLoc); |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2054 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2057 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2058 | const char *&startRef, const char *&endRef) { |
| 2059 | while (startBuf < endBuf) { |
| 2060 | if (*startBuf == '<') |
| 2061 | startRef = startBuf; // mark the start. |
| 2062 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2063 | if (startRef && *startRef == '<') { |
| 2064 | endRef = startBuf; // mark the end. |
| 2065 | return true; |
| 2066 | } |
| 2067 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2068 | } |
| 2069 | startBuf++; |
| 2070 | } |
| 2071 | return false; |
| 2072 | } |
| 2073 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2074 | static void scanToNextArgument(const char *&argRef) { |
| 2075 | int angle = 0; |
| 2076 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2077 | if (*argRef == '<') |
| 2078 | angle++; |
| 2079 | else if (*argRef == '>') |
| 2080 | angle--; |
| 2081 | argRef++; |
| 2082 | } |
| 2083 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2084 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2085 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2086 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2087 | if (T->isObjCQualifiedIdType()) |
| 2088 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2089 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2090 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2091 | return true; |
| 2092 | } |
| 2093 | if (T->isObjCObjectPointerType()) { |
| 2094 | T = T->getPointeeType(); |
| 2095 | return T->isObjCQualifiedInterfaceType(); |
| 2096 | } |
Fariborz Jahanian | 7bf13c4 | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2097 | if (T->isArrayType()) { |
| 2098 | QualType ElemTy = Context->getBaseElementType(T); |
| 2099 | return needToScanForQualifiers(ElemTy); |
| 2100 | } |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2101 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2104 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2105 | QualType Type = E->getType(); |
| 2106 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2107 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2109 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2110 | Loc = ECE->getLParenLoc(); |
| 2111 | EndLoc = ECE->getRParenLoc(); |
| 2112 | } else { |
| 2113 | Loc = E->getLocStart(); |
| 2114 | EndLoc = E->getLocEnd(); |
| 2115 | } |
| 2116 | // This will defend against trying to rewrite synthesized expressions. |
| 2117 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2118 | return; |
| 2119 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2120 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2121 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2122 | const char *startRef = 0, *endRef = 0; |
| 2123 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2124 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2125 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2126 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2127 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2128 | InsertText(LessLoc, "/*"); |
| 2129 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | } |
| 2133 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2134 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2135 | SourceLocation Loc; |
| 2136 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2137 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2138 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2139 | Loc = VD->getLocation(); |
| 2140 | Type = VD->getType(); |
| 2141 | } |
| 2142 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2143 | Loc = FD->getLocation(); |
| 2144 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2145 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2146 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2147 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2148 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2149 | if (!proto) |
| 2150 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2151 | Type = proto->getReturnType(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2152 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2153 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2154 | Loc = FD->getLocation(); |
| 2155 | Type = FD->getType(); |
| 2156 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2157 | else |
| 2158 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2160 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2161 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2163 | const char *endBuf = SM->getCharacterData(Loc); |
| 2164 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2165 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2166 | startBuf--; // scan backward (from the decl location) for return type. |
| 2167 | const char *startRef = 0, *endRef = 0; |
| 2168 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2169 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2170 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2171 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2172 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2173 | InsertText(LessLoc, "/*"); |
| 2174 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2175 | } |
| 2176 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2177 | if (!proto) |
| 2178 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2179 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2180 | const char *startBuf = SM->getCharacterData(Loc); |
| 2181 | const char *startFuncBuf = startBuf; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2182 | for (unsigned i = 0; i < proto->getNumParams(); i++) { |
| 2183 | if (needToScanForQualifiers(proto->getParamType(i))) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2184 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2186 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2187 | // scan forward (from the decl location) for argument types. |
| 2188 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2189 | const char *startRef = 0, *endRef = 0; |
| 2190 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2191 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2193 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2195 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2196 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2197 | InsertText(LessLoc, "/*"); |
| 2198 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2199 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2200 | startBuf = ++endBuf; |
| 2201 | } |
| 2202 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2203 | // If the function name is derived from a macro expansion, then the |
| 2204 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2205 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2206 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2207 | startBuf++; |
| 2208 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2209 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2212 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2213 | QualType QT = ND->getType(); |
| 2214 | const Type* TypePtr = QT->getAs<Type>(); |
| 2215 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2216 | return; |
| 2217 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2218 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2219 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2220 | TypePtr = QT->getAs<Type>(); |
| 2221 | } |
| 2222 | // FIXME. This will not work for multiple declarators; as in: |
| 2223 | // __typeof__(a) b,c,d; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2224 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2225 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2226 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2227 | if (ND->getInit()) { |
| 2228 | std::string Name(ND->getNameAsString()); |
| 2229 | TypeAsString += " " + Name + " = "; |
| 2230 | Expr *E = ND->getInit(); |
| 2231 | SourceLocation startLoc; |
| 2232 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2233 | startLoc = ECE->getLParenLoc(); |
| 2234 | else |
| 2235 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2236 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2237 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2238 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2239 | } |
| 2240 | else { |
| 2241 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2242 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2243 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2244 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2248 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2249 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2250 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2251 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2252 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2253 | QualType getFuncType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2254 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2255 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2256 | SourceLocation(), |
| 2257 | SourceLocation(), |
| 2258 | SelGetUidIdent, getFuncType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2259 | SC_Extern); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2262 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2263 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2264 | if (FD->getIdentifier() && |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2265 | FD->getName() == "sel_registerName") { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2266 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2267 | return; |
| 2268 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2269 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2272 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2273 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2274 | const char *argPtr = TypeString.c_str(); |
| 2275 | if (!strchr(argPtr, '^')) { |
| 2276 | Str += TypeString; |
| 2277 | return; |
| 2278 | } |
| 2279 | while (*argPtr) { |
| 2280 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2281 | argPtr++; |
| 2282 | } |
| 2283 | } |
| 2284 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2285 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2286 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2287 | ValueDecl *VD) { |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2288 | QualType Type = VD->getType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2289 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2290 | const char *argPtr = TypeString.c_str(); |
| 2291 | int paren = 0; |
| 2292 | while (*argPtr) { |
| 2293 | switch (*argPtr) { |
| 2294 | case '(': |
| 2295 | Str += *argPtr; |
| 2296 | paren++; |
| 2297 | break; |
| 2298 | case ')': |
| 2299 | Str += *argPtr; |
| 2300 | paren--; |
| 2301 | break; |
| 2302 | case '^': |
| 2303 | Str += '*'; |
| 2304 | if (paren == 1) |
| 2305 | Str += VD->getNameAsString(); |
| 2306 | break; |
| 2307 | default: |
| 2308 | Str += *argPtr; |
| 2309 | break; |
| 2310 | } |
| 2311 | argPtr++; |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2316 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2317 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2318 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2319 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2320 | if (!proto) |
| 2321 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2322 | QualType Type = proto->getReturnType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2323 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2324 | FdStr += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2325 | FdStr += FD->getName(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2326 | FdStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2327 | unsigned numArgs = proto->getNumParams(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2328 | for (unsigned i = 0; i < numArgs; i++) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2329 | QualType ArgType = proto->getParamType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2330 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2331 | if (i+1 < numArgs) |
| 2332 | FdStr += ", "; |
| 2333 | } |
| 2334 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2335 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2336 | CurFunctionDeclToDeclareForBlock = 0; |
| 2337 | } |
| 2338 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2339 | // SynthSuperConstructorFunctionDecl - id objc_super(id obj, id super); |
| 2340 | void RewriteObjC::SynthSuperConstructorFunctionDecl() { |
| 2341 | if (SuperConstructorFunctionDecl) |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2342 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2343 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2344 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2345 | QualType argT = Context->getObjCIdType(); |
| 2346 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2347 | ArgTys.push_back(argT); |
| 2348 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2349 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2350 | ArgTys); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2351 | SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2352 | SourceLocation(), |
| 2353 | SourceLocation(), |
| 2354 | msgSendIdent, msgSendType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2355 | 0, SC_Extern); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2358 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2359 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2360 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2361 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2362 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2363 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2364 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2365 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2366 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2367 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2368 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2369 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2370 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2371 | SourceLocation(), |
| 2372 | SourceLocation(), |
| 2373 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2374 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2377 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2378 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2379 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2380 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2381 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2382 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2383 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2384 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2385 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2386 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2387 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2388 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2389 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2390 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2391 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2392 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2393 | SourceLocation(), |
| 2394 | SourceLocation(), |
| 2395 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2396 | SC_Extern); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2399 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2400 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2401 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2402 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2403 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2404 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2405 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2406 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2407 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2408 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2409 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2410 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2411 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2412 | SourceLocation(), |
| 2413 | SourceLocation(), |
| 2414 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2415 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2419 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2420 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2422 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2423 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2424 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2425 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2426 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2427 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2428 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2429 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2430 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2431 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2432 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2433 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2434 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2435 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2437 | SourceLocation(), |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2438 | msgSendIdent, |
| 2439 | msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2440 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2443 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2444 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2445 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2446 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2447 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2448 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2449 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2450 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2451 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2452 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2453 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2454 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2455 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2456 | SourceLocation(), |
| 2457 | SourceLocation(), |
| 2458 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2459 | SC_Extern); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2462 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2463 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2464 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2465 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2466 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2467 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2468 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2469 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2470 | SourceLocation(), |
| 2471 | SourceLocation(), |
| 2472 | getClassIdent, getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2473 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2476 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2477 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2478 | IdentifierInfo *getSuperClassIdent = |
| 2479 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2480 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2481 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2482 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2483 | ArgTys); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2484 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2485 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2486 | SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2487 | getSuperClassIdent, |
| 2488 | getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2489 | SC_Extern); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2492 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2493 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2494 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2495 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2496 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2497 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2498 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2499 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2500 | SourceLocation(), |
| 2501 | SourceLocation(), |
| 2502 | getClassIdent, getClassType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2503 | 0, SC_Extern); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2506 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2507 | QualType strType = getConstantStringStructType(); |
| 2508 | |
| 2509 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2510 | |
| 2511 | std::string tmpName = InFileName; |
| 2512 | unsigned i; |
| 2513 | for (i=0; i < tmpName.length(); i++) { |
| 2514 | char c = tmpName.at(i); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2515 | // replace any non-alphanumeric characters with '_'. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2516 | if (!isAlphanumeric(c)) |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2517 | tmpName[i] = '_'; |
| 2518 | } |
| 2519 | S += tmpName; |
| 2520 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2521 | S += utostr(NumObjCStringLiterals++); |
| 2522 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2523 | Preamble += "static __NSConstantStringImpl " + S; |
| 2524 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2525 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2526 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2527 | std::string prettyBufS; |
| 2528 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2529 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2530 | Preamble += prettyBuf.str(); |
| 2531 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2532 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | |
| 2534 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2535 | SourceLocation(), &Context->Idents.get(S), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2536 | strType, 0, SC_Static); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2537 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2538 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2539 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2541 | VK_RValue, OK_Ordinary, |
| 2542 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2543 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2544 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2545 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2546 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2547 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2548 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2551 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2552 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2553 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2554 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2555 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2556 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2557 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2559 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2561 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2563 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2564 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2565 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2567 | SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2569 | FieldTypes[i], 0, |
| 2570 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2571 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2572 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2575 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2576 | } |
| 2577 | return Context->getTagDeclType(SuperStructDecl); |
| 2578 | } |
| 2579 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2580 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2581 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2582 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2583 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2584 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2585 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2587 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2589 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2591 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2593 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2595 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2596 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2597 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2599 | ConstantStringDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2600 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2601 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2602 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2603 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2604 | /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2605 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2608 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2609 | } |
| 2610 | return Context->getTagDeclType(ConstantStringDecl); |
| 2611 | } |
| 2612 | |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2613 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2614 | QualType msgSendType, |
| 2615 | QualType returnType, |
| 2616 | SmallVectorImpl<QualType> &ArgTypes, |
| 2617 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2618 | ObjCMethodDecl *Method) { |
| 2619 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2620 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2621 | false, msgSendType, |
| 2622 | VK_LValue, SourceLocation()); |
| 2623 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2624 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2625 | Context->getPointerType(Context->VoidTy), |
| 2626 | CK_BitCast, STDRE); |
| 2627 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2628 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 2629 | Method ? Method->isVariadic() |
| 2630 | : false); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2631 | castType = Context->getPointerType(castType); |
| 2632 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2633 | cast); |
| 2634 | |
| 2635 | // Don't forget the parens to enforce the proper binding. |
| 2636 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2637 | |
| 2638 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2639 | CallExpr *STCE = new (Context) CallExpr( |
| 2640 | *Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, SourceLocation()); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2641 | return STCE; |
| 2642 | |
| 2643 | } |
| 2644 | |
| 2645 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2646 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2647 | SourceLocation StartLoc, |
| 2648 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2649 | if (!SelGetUidFunctionDecl) |
| 2650 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2651 | if (!MsgSendFunctionDecl) |
| 2652 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2653 | if (!MsgSendSuperFunctionDecl) |
| 2654 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2655 | if (!MsgSendStretFunctionDecl) |
| 2656 | SynthMsgSendStretFunctionDecl(); |
| 2657 | if (!MsgSendSuperStretFunctionDecl) |
| 2658 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2659 | if (!MsgSendFpretFunctionDecl) |
| 2660 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2661 | if (!GetClassFunctionDecl) |
| 2662 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2663 | if (!GetSuperClassFunctionDecl) |
| 2664 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2665 | if (!GetMetaClassFunctionDecl) |
| 2666 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2668 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2669 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2670 | // May need to use objc_msgSend_stret() as well. |
| 2671 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2672 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2673 | QualType resultType = mDecl->getReturnType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2674 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2675 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2676 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2677 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2680 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2681 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2682 | switch (Exp->getReceiverKind()) { |
| 2683 | case ObjCMessageExpr::SuperClass: { |
| 2684 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2685 | if (MsgSendStretFlavor) |
| 2686 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2687 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2689 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2691 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2693 | // set the receiver to self, the first argument to all methods. |
| 2694 | InitExprs.push_back( |
| 2695 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2696 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2697 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2698 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2699 | Context->getObjCIdType(), |
| 2700 | VK_RValue, |
| 2701 | SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2702 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2704 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2705 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2706 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2707 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2708 | &ClsExprs[0], |
| 2709 | ClsExprs.size(), |
| 2710 | StartLoc, |
| 2711 | EndLoc); |
| 2712 | // (Class)objc_getClass("CurrentClass") |
| 2713 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2714 | Context->getObjCClassType(), |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2715 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2716 | ClsExprs.clear(); |
| 2717 | ClsExprs.push_back(ArgExpr); |
| 2718 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2719 | &ClsExprs[0], ClsExprs.size(), |
| 2720 | StartLoc, EndLoc); |
| 2721 | |
| 2722 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2723 | // To turn off a warning, type-cast to 'id' |
| 2724 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2725 | NoTypeInfoCStyleCastExpr(Context, |
| 2726 | Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2727 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2728 | // struct objc_super |
| 2729 | QualType superType = getSuperStructType(); |
| 2730 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2731 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2732 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2733 | SynthSuperConstructorFunctionDecl(); |
| 2734 | // Simulate a constructor call... |
| 2735 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2736 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2737 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2738 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2739 | superType, VK_LValue, |
| 2740 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2741 | // The code for super is a little tricky to prevent collision with |
| 2742 | // the structure definition in the header. The rewriter has it's own |
| 2743 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2744 | // we need the cast below. For example: |
| 2745 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2746 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2747 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2748 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2749 | VK_RValue, OK_Ordinary, |
| 2750 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2751 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2752 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2753 | CK_BitCast, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2754 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2755 | // (struct objc_super) { <exprs from above> } |
| 2756 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2757 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2758 | SourceLocation()); |
| 2759 | TypeSourceInfo *superTInfo |
| 2760 | = Context->getTrivialTypeSourceInfo(superType); |
| 2761 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2762 | superType, VK_LValue, |
| 2763 | ILE, false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2764 | // struct objc_super * |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2765 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2766 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2767 | VK_RValue, OK_Ordinary, |
| 2768 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2769 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2770 | MsgExprs.push_back(SuperRep); |
| 2771 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2772 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2773 | |
| 2774 | case ObjCMessageExpr::Class: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2775 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2776 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2777 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2778 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2779 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2780 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2781 | &ClsExprs[0], |
| 2782 | ClsExprs.size(), |
| 2783 | StartLoc, EndLoc); |
| 2784 | MsgExprs.push_back(Cls); |
| 2785 | break; |
| 2786 | } |
| 2787 | |
| 2788 | case ObjCMessageExpr::SuperInstance:{ |
| 2789 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2790 | if (MsgSendStretFlavor) |
| 2791 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2792 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2793 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2794 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2795 | |
| 2796 | InitExprs.push_back( |
| 2797 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2798 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2799 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2800 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2801 | Context->getObjCIdType(), |
| 2802 | VK_RValue, SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2803 | ); // set the 'receiver'. |
| 2804 | |
| 2805 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2806 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2807 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2808 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2809 | &ClsExprs[0], |
| 2810 | ClsExprs.size(), |
| 2811 | StartLoc, EndLoc); |
| 2812 | // (Class)objc_getClass("CurrentClass") |
| 2813 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2814 | Context->getObjCClassType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2815 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2816 | ClsExprs.clear(); |
| 2817 | ClsExprs.push_back(ArgExpr); |
| 2818 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2819 | &ClsExprs[0], ClsExprs.size(), |
| 2820 | StartLoc, EndLoc); |
| 2821 | |
| 2822 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2823 | // To turn off a warning, type-cast to 'id' |
| 2824 | InitExprs.push_back( |
| 2825 | // set 'super class', using class_getSuperclass(). |
| 2826 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2827 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2828 | // struct objc_super |
| 2829 | QualType superType = getSuperStructType(); |
| 2830 | Expr *SuperRep; |
| 2831 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2832 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2833 | SynthSuperConstructorFunctionDecl(); |
| 2834 | // Simulate a constructor call... |
| 2835 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2836 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2837 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2838 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2839 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2840 | // The code for super is a little tricky to prevent collision with |
| 2841 | // the structure definition in the header. The rewriter has it's own |
| 2842 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2843 | // we need the cast below. For example: |
| 2844 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2845 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2846 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2847 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2848 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2849 | SourceLocation()); |
| 2850 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2851 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2852 | CK_BitCast, SuperRep); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2853 | } else { |
| 2854 | // (struct objc_super) { <exprs from above> } |
| 2855 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2856 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2857 | SourceLocation()); |
| 2858 | TypeSourceInfo *superTInfo |
| 2859 | = Context->getTrivialTypeSourceInfo(superType); |
| 2860 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2861 | superType, VK_RValue, ILE, |
| 2862 | false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2863 | } |
| 2864 | MsgExprs.push_back(SuperRep); |
| 2865 | break; |
| 2866 | } |
| 2867 | |
| 2868 | case ObjCMessageExpr::Instance: { |
| 2869 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2870 | // Foo<Proto> *. |
| 2871 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2872 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2873 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2874 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2875 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2876 | ? CK_BlockPointerToObjCPointerCast |
| 2877 | : CK_CPointerToObjCPointerCast; |
| 2878 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2880 | CK, recExpr); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2881 | MsgExprs.push_back(recExpr); |
| 2882 | break; |
| 2883 | } |
| 2884 | } |
| 2885 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2886 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2887 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2888 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2889 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2890 | &SelExprs[0], SelExprs.size(), |
| 2891 | StartLoc, |
| 2892 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2893 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2895 | // Now push any user supplied arguments. |
| 2896 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2897 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2898 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2899 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2900 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | 8f49033 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2901 | QualType type = ICE->getType(); |
| 2902 | if (needToScanForQualifiers(type)) |
| 2903 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2904 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2905 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 9e7dbd1 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2906 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2907 | CastKind CK; |
| 2908 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2909 | type->isBooleanType()) { |
| 2910 | CK = CK_IntegralToBoolean; |
| 2911 | } else if (type->isObjCObjectPointerType()) { |
| 2912 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2913 | CK = CK_BlockPointerToObjCPointerCast; |
| 2914 | } else if (SubExpr->getType()->isPointerType()) { |
| 2915 | CK = CK_CPointerToObjCPointerCast; |
| 2916 | } else { |
| 2917 | CK = CK_BitCast; |
| 2918 | } |
| 2919 | } else { |
| 2920 | CK = CK_BitCast; |
| 2921 | } |
| 2922 | |
| 2923 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2924 | } |
| 2925 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2926 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2927 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2928 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2929 | userExpr = CE->getSubExpr(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2930 | CastKind CK; |
| 2931 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2932 | CK = CK_IntegralToPointer; |
| 2933 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2934 | CK = CK_BlockPointerToObjCPointerCast; |
| 2935 | } else if (userExpr->getType()->isPointerType()) { |
| 2936 | CK = CK_CPointerToObjCPointerCast; |
| 2937 | } else { |
| 2938 | CK = CK_BitCast; |
| 2939 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2940 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2941 | CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2944 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2945 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2946 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2947 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2948 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2949 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2950 | // Generate the funky cast. |
| 2951 | CastExpr *cast; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2952 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2953 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2955 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2956 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2957 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2958 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2959 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2960 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2961 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2962 | // Push any user argument types. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2963 | for (const auto *PI : OMD->params()) { |
| 2964 | QualType t = PI->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | ? Context->getObjCIdType() |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2966 | : PI->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2967 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2968 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2969 | ArgTypes.push_back(t); |
| 2970 | } |
Fariborz Jahanian | 9f0bc57 | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 2971 | returnType = Exp->getType(); |
| 2972 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2973 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2974 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2975 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2976 | } |
| 2977 | // 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] | 2978 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2980 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2981 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2982 | VK_LValue, SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2983 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2985 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2986 | // xx.m:13: warning: function called through a non-compatible type |
| 2987 | // 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] | 2988 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2989 | Context->getPointerType(Context->VoidTy), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2990 | CK_BitCast, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2992 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2993 | // If we don't have a method decl, force a variadic cast. |
| 2994 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2995 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2996 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2997 | castType = Context->getPointerType(castType); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2998 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2999 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3002 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3004 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3005 | CallExpr *CE = new (Context) |
| 3006 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3007 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3008 | if (MsgSendStretFlavor) { |
| 3009 | // We have the method which returns a struct/union. Must also generate |
| 3010 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3011 | // expression which dictate which one to envoke depending on size of |
| 3012 | // method's return type. |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 3013 | |
| 3014 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3015 | msgSendType, returnType, |
| 3016 | ArgTypes, MsgExprs, |
| 3017 | Exp->getMethodDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3019 | // Build sizeof(returnType) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3020 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3021 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3022 | Context->getTrivialTypeSourceInfo(returnType), |
| 3023 | Context->getSizeType(), SourceLocation(), |
| 3024 | SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3025 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3026 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3027 | // For X86 it is more complicated and some kind of target specific routine |
| 3028 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3030 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3031 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3032 | llvm::APInt(IntSize, 8), |
| 3033 | Context->IntTy, |
| 3034 | SourceLocation()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3035 | BinaryOperator *lessThanExpr = |
| 3036 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3037 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3038 | false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3039 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3041 | new (Context) ConditionalOperator(lessThanExpr, |
| 3042 | SourceLocation(), CE, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3043 | SourceLocation(), STCE, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3044 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3045 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3046 | CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3047 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3048 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3049 | return ReplacingStmt; |
| 3050 | } |
| 3051 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3052 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3053 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3054 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3056 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3057 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3059 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3060 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3063 | // typedef struct objc_object Protocol; |
| 3064 | QualType RewriteObjC::getProtocolType() { |
| 3065 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3066 | TypeSourceInfo *TInfo |
| 3067 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3068 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3069 | SourceLocation(), SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3070 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3071 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3072 | } |
| 3073 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3074 | } |
| 3075 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3076 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3077 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3078 | /// The forward references (and metadata) are generated in |
| 3079 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3080 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3081 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3082 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3084 | SourceLocation(), ID, getProtocolType(), 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3085 | SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3086 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3087 | VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3088 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3089 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3090 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3091 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3092 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3093 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3094 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3095 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3096 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3097 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3102 | const char *endBuf) { |
| 3103 | while (startBuf < endBuf) { |
| 3104 | if (*startBuf == '#') { |
| 3105 | // Skip whitespace. |
| 3106 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3107 | ; |
| 3108 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3109 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3110 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3111 | !strncmp(startBuf, "define", strlen("define")) || |
| 3112 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3113 | !strncmp(startBuf, "else", strlen("else")) || |
| 3114 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3115 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3116 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3117 | !strncmp(startBuf, "include", strlen("include")) || |
| 3118 | !strncmp(startBuf, "import", strlen("import")) || |
| 3119 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3120 | return true; |
| 3121 | } |
| 3122 | startBuf++; |
| 3123 | } |
| 3124 | return false; |
| 3125 | } |
| 3126 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3127 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3128 | /// an objective-c class with ivars. |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3129 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3130 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3131 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3132 | assert(CDecl->getName() != "" && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3133 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3134 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3135 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3136 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3137 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3138 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3139 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3140 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3142 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3143 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3145 | // If no ivars and no root or if its root, directly or indirectly, |
| 3146 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3147 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3148 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3149 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3150 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3151 | return; |
| 3152 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | |
| 3154 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3155 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3156 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3157 | Result += CDecl->getNameAsString(); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3158 | if (LangOpts.MicrosoftExt) |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3159 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3160 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3161 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3162 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3164 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3165 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3166 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3167 | // NSURL.h, for example): |
| 3168 | // |
| 3169 | // #ifdef XYZ |
| 3170 | // @interface Foo : NSObject |
| 3171 | // #else |
| 3172 | // @interface FooBar : NSObject |
| 3173 | // #endif |
| 3174 | // { |
| 3175 | // int i; |
| 3176 | // } |
| 3177 | // @end |
| 3178 | // |
| 3179 | // This clause is segregated to avoid breaking the common case. |
| 3180 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3182 | CDecl->getAtStartLoc(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3183 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3184 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3186 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3187 | // advance to the end of the referenced protocols. |
| 3188 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3189 | endHeader++; |
| 3190 | } |
| 3191 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3192 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3193 | } else { |
| 3194 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3195 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3196 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3197 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3198 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3199 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3200 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3201 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3202 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3204 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3205 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3206 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3207 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3208 | } |
| 3209 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3211 | // Now comment out any visibility specifiers. |
| 3212 | while (cursor < endBuf) { |
| 3213 | if (*cursor == '@') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3214 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3215 | // Skip whitespace. |
| 3216 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3217 | /*scan*/; |
| 3218 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3219 | // FIXME: presence of @public, etc. inside comment results in |
| 3220 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3221 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3222 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3223 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3224 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3225 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3226 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3227 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3228 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3229 | // for type checking. |
| 3230 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3231 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3232 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3233 | cursor = strchr(cursor, '>'); |
| 3234 | cursor++; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3235 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3236 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3237 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3238 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3239 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3240 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3241 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3242 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3243 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3244 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3245 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3246 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3247 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3248 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3249 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3250 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3251 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3252 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3253 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3254 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3255 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3256 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3259 | //===----------------------------------------------------------------------===// |
| 3260 | // Meta Data Emission |
| 3261 | //===----------------------------------------------------------------------===// |
| 3262 | |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3263 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3264 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3265 | /// and emits meta-data. |
| 3266 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3267 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3268 | int ClsDefCount = ClassImplementation.size(); |
| 3269 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3271 | // Rewrite implemented methods |
| 3272 | for (int i = 0; i < ClsDefCount; i++) |
| 3273 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3275 | for (int i = 0; i < CatDefCount; i++) |
| 3276 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3279 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3280 | const std::string &Name, |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3281 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3282 | assert(BlockByRefDeclNo.count(VD) && |
| 3283 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3284 | if (def) |
| 3285 | ResultStr += "struct "; |
| 3286 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3287 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3288 | } |
| 3289 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3290 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3291 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3292 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3293 | return false; |
| 3294 | } |
| 3295 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3296 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3297 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3298 | std::string Tag) { |
| 3299 | const FunctionType *AFT = CE->getFunctionType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3300 | QualType RT = AFT->getReturnType(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3301 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3302 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3303 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3304 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3305 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3307 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | // 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] | 3309 | // block (to reference imported block decl refs). |
| 3310 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3311 | } else if (BD->param_empty()) { |
| 3312 | S += "(" + StructRef + " *__cself)"; |
| 3313 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3314 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3315 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3316 | S += '('; |
| 3317 | // first add the implicit argument. |
| 3318 | S += StructRef + " *__cself, "; |
| 3319 | std::string ParamStr; |
| 3320 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3321 | E = BD->param_end(); AI != E; ++AI) { |
| 3322 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3323 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3324 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3325 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3326 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3327 | S += ParamStr; |
| 3328 | } |
| 3329 | if (FT->isVariadic()) { |
| 3330 | if (!BD->param_empty()) S += ", "; |
| 3331 | S += "..."; |
| 3332 | } |
| 3333 | S += ')'; |
| 3334 | } |
| 3335 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3337 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3338 | // First, emit a declaration for all "by ref" decls. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3339 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3340 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3341 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3342 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3343 | std::string TypeString; |
| 3344 | RewriteByRefString(TypeString, Name, (*I)); |
| 3345 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3346 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3347 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3349 | // Next, emit a declaration for all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3350 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3351 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3352 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3353 | // Handle nested closure invocation. For example: |
| 3354 | // |
| 3355 | // void (^myImportedClosure)(void); |
| 3356 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3358 | // void (^anotherClosure)(void); |
| 3359 | // anotherClosure = ^(void) { |
| 3360 | // myImportedClosure(); // import and invoke the closure |
| 3361 | // }; |
| 3362 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3363 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3364 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3365 | S += " = ("; |
| 3366 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3367 | S += ")"; |
| 3368 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3369 | } |
| 3370 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3371 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3372 | QualType QT = (*I)->getType(); |
| 3373 | if (HasLocalVariableExternalStorage(*I)) |
| 3374 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3375 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3376 | S += Name + " = __cself->" + |
| 3377 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3378 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3379 | } |
| 3380 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3381 | const char *cstr = RewrittenStr.c_str(); |
| 3382 | while (*cstr++ != '{') ; |
| 3383 | S += cstr; |
| 3384 | S += "\n"; |
| 3385 | return S; |
| 3386 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3387 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3388 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3389 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3390 | std::string Tag) { |
| 3391 | std::string StructRef = "struct " + Tag; |
| 3392 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3394 | S += funcName; |
| 3395 | S += "_block_copy_" + utostr(i); |
| 3396 | S += "(" + StructRef; |
| 3397 | S += "*dst, " + StructRef; |
| 3398 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3400 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3401 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3402 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3403 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3404 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3405 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3406 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3407 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3408 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3409 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3410 | else |
| 3411 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3412 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3413 | S += "}\n"; |
| 3414 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3415 | S += "\nstatic void __"; |
| 3416 | S += funcName; |
| 3417 | S += "_block_dispose_" + utostr(i); |
| 3418 | S += "(" + StructRef; |
| 3419 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3421 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3422 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3423 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3424 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3425 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3426 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3427 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3428 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3429 | else |
| 3430 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3431 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3433 | return S; |
| 3434 | } |
| 3435 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3436 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3437 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3438 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3439 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3440 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3441 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3442 | S += " struct " + Desc; |
| 3443 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3445 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3446 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3447 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3449 | if (BlockDeclRefs.size()) { |
| 3450 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3451 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3452 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3453 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3454 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3455 | std::string ArgName = "_" + FieldName; |
| 3456 | // Handle nested closure invocation. For example: |
| 3457 | // |
| 3458 | // void (^myImportedBlock)(void); |
| 3459 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3461 | // void (^anotherBlock)(void); |
| 3462 | // anotherBlock = ^(void) { |
| 3463 | // myImportedBlock(); // import and invoke the closure |
| 3464 | // }; |
| 3465 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3466 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3467 | S += "struct __block_impl *"; |
| 3468 | Constructor += ", void *" + ArgName; |
| 3469 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3470 | QualType QT = (*I)->getType(); |
| 3471 | if (HasLocalVariableExternalStorage(*I)) |
| 3472 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3473 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3474 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3475 | Constructor += ", " + ArgName; |
| 3476 | } |
| 3477 | S += FieldName + ";\n"; |
| 3478 | } |
| 3479 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3480 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3481 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3482 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3483 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3484 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3485 | { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3486 | std::string TypeString; |
| 3487 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3488 | TypeString += " *"; |
| 3489 | FieldName = TypeString + FieldName; |
| 3490 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3491 | Constructor += ", " + ArgName; |
| 3492 | } |
| 3493 | S += FieldName + "; // by ref\n"; |
| 3494 | } |
| 3495 | // Finish writing the constructor. |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3496 | Constructor += ", int flags=0)"; |
| 3497 | // Initialize all "by copy" arguments. |
| 3498 | bool firsTime = true; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3499 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3500 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3501 | std::string Name = (*I)->getNameAsString(); |
| 3502 | if (firsTime) { |
| 3503 | Constructor += " : "; |
| 3504 | firsTime = false; |
| 3505 | } |
| 3506 | else |
| 3507 | Constructor += ", "; |
| 3508 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3509 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3510 | else |
| 3511 | Constructor += Name + "(_" + Name + ")"; |
| 3512 | } |
| 3513 | // Initialize all "by ref" arguments. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3514 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3515 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3516 | std::string Name = (*I)->getNameAsString(); |
| 3517 | if (firsTime) { |
| 3518 | Constructor += " : "; |
| 3519 | firsTime = false; |
| 3520 | } |
| 3521 | else |
| 3522 | Constructor += ", "; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3523 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | Constructor += " {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3527 | if (GlobalVarDecl) |
| 3528 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3529 | else |
| 3530 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3531 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3533 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3534 | } else { |
| 3535 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3536 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3537 | if (GlobalVarDecl) |
| 3538 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3539 | else |
| 3540 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3541 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3542 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3543 | } |
| 3544 | Constructor += " "; |
| 3545 | Constructor += "}\n"; |
| 3546 | S += Constructor; |
| 3547 | S += "};\n"; |
| 3548 | return S; |
| 3549 | } |
| 3550 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3551 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3552 | std::string ImplTag, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3553 | StringRef FunName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3554 | unsigned hasCopy) { |
| 3555 | std::string S = "\nstatic struct " + DescTag; |
| 3556 | |
| 3557 | S += " {\n unsigned long reserved;\n"; |
| 3558 | S += " unsigned long Block_size;\n"; |
| 3559 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3560 | S += " void (*copy)(struct "; |
| 3561 | S += ImplTag; S += "*, struct "; |
| 3562 | S += ImplTag; S += "*);\n"; |
| 3563 | |
| 3564 | S += " void (*dispose)(struct "; |
| 3565 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3566 | } |
| 3567 | S += "} "; |
| 3568 | |
| 3569 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3570 | S += ImplTag + ")"; |
| 3571 | if (hasCopy) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3572 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3573 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3574 | } |
| 3575 | S += "};\n"; |
| 3576 | return S; |
| 3577 | } |
| 3578 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3579 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3580 | StringRef FunName) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3581 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3582 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3583 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3584 | bool RewriteSC = (GlobalVarDecl && |
| 3585 | !Blocks.empty() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3586 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3587 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3588 | if (RewriteSC) { |
| 3589 | std::string SC(" void __"); |
| 3590 | SC += GlobalVarDecl->getNameAsString(); |
| 3591 | SC += "() {}"; |
| 3592 | InsertText(FunLocStart, SC); |
| 3593 | } |
| 3594 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3595 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3596 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3597 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3598 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3599 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3600 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3601 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3602 | ValueDecl *VD = Exp->getDecl(); |
| 3603 | BlockDeclRefs.push_back(Exp); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3604 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3605 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3606 | BlockByCopyDecls.push_back(VD); |
| 3607 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3608 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3609 | BlockByRefDeclsPtrSet.insert(VD); |
| 3610 | BlockByRefDecls.push_back(VD); |
| 3611 | } |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3612 | // imported objects in the inner blocks not used in the outer |
| 3613 | // blocks must be copied/disposed in the outer block as well. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3614 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3615 | VD->getType()->isObjCObjectPointerType() || |
| 3616 | VD->getType()->isBlockPointerType()) |
| 3617 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3618 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3619 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3620 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3621 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3623 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3624 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3625 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3626 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3627 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3629 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3630 | |
| 3631 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3632 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3633 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3634 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3635 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3636 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3637 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3639 | BlockDeclRefs.clear(); |
| 3640 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3641 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3642 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3643 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3644 | ImportedBlockDecls.clear(); |
| 3645 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3646 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3647 | // Must insert any 'const/volatile/static here. Since it has been |
| 3648 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3649 | std::string SC; |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3650 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3651 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3652 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3653 | SC += "const "; |
| 3654 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3655 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3656 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3657 | SC += "restrict "; |
| 3658 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3661 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3662 | InnerDeclRefsCount.clear(); |
| 3663 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3664 | RewrittenBlockExprs.clear(); |
| 3665 | } |
| 3666 | |
| 3667 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3668 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3669 | StringRef FuncName = FD->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3671 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3672 | } |
| 3673 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3674 | static void BuildUniqueMethodName(std::string &Name, |
| 3675 | ObjCMethodDecl *MD) { |
| 3676 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3677 | Name = IFace->getName(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3678 | Name += "__" + MD->getSelector().getAsString(); |
| 3679 | // Convert colons to underscores. |
| 3680 | std::string::size_type loc = 0; |
| 3681 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3682 | Name.replace(loc, 1, "_"); |
| 3683 | } |
| 3684 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3685 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3686 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3687 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3688 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3689 | std::string FuncName; |
| 3690 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3691 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3695 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3696 | if (*CI) { |
| 3697 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3698 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3699 | else |
| 3700 | GetBlockDeclRefExprs(*CI); |
| 3701 | } |
| 3702 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3703 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3704 | if (DRE->refersToEnclosingLocal()) { |
| 3705 | // FIXME: Handle enums. |
| 3706 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3707 | BlockDeclRefs.push_back(DRE); |
| 3708 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3709 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3710 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3711 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3712 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3713 | return; |
| 3714 | } |
| 3715 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3716 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 3717 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3718 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3719 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3720 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3721 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3722 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3723 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3724 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3725 | InnerContexts); |
| 3726 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3727 | else |
| 3728 | GetInnerBlockDeclRefExprs(*CI, |
| 3729 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3730 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3731 | |
| 3732 | } |
| 3733 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3734 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3735 | if (DRE->refersToEnclosingLocal()) { |
| 3736 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3737 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3738 | InnerBlockDeclRefs.push_back(DRE); |
| 3739 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3740 | if (Var->isFunctionOrMethodVarDecl()) |
| 3741 | ImportedLocalExternalDecls.insert(Var); |
| 3742 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3743 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3744 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3745 | return; |
| 3746 | } |
| 3747 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3748 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3749 | /// whose result type may be a block pointer or whose argument type(s) |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3750 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3751 | /// all block pointers to function pointers. |
| 3752 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3753 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3754 | // FTP will be null for closures that don't take arguments. |
| 3755 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3756 | SmallVector<QualType, 8> ArgTypes; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3757 | QualType Res = FT->getReturnType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3758 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3759 | |
| 3760 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3761 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3762 | E = FTP->param_type_end(); |
| 3763 | I && (I != E); ++I) { |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3764 | QualType t = *I; |
| 3765 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3766 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3767 | HasBlockType = true; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3768 | ArgTypes.push_back(t); |
| 3769 | } |
| 3770 | } |
| 3771 | QualType FuncType; |
| 3772 | // FIXME. Does this work if block takes no argument but has a return type |
| 3773 | // which is of block type? |
| 3774 | if (HasBlockType) |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3775 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3776 | else FuncType = QualType(FT, 0); |
| 3777 | return FuncType; |
| 3778 | } |
| 3779 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3780 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3781 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3782 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3784 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3785 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3786 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3787 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3788 | } |
| 3789 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3790 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3791 | } |
| 3792 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3793 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3794 | else if (const ConditionalOperator *CEXPR = |
| 3795 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3796 | Expr *LHSExp = CEXPR->getLHS(); |
| 3797 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3798 | Expr *RHSExp = CEXPR->getRHS(); |
| 3799 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3800 | Expr *CONDExp = CEXPR->getCond(); |
| 3801 | ConditionalOperator *CondExpr = |
| 3802 | new (Context) ConditionalOperator(CONDExp, |
| 3803 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3804 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3805 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3806 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3807 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3808 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3809 | } else if (const PseudoObjectExpr *POE |
| 3810 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3811 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3812 | } else { |
| 3813 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3814 | } |
| 3815 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3816 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3817 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3818 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3819 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3821 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3822 | SourceLocation(), SourceLocation(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3823 | &Context->Idents.get("__block_impl")); |
| 3824 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3825 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3826 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3827 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3829 | // Push the block argument type. |
| 3830 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3831 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3832 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3833 | E = FTP->param_type_end(); |
| 3834 | I && (I != E); ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3835 | QualType t = *I; |
| 3836 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3837 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3838 | convertToUnqualifiedObjCType(t); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3839 | ArgTypes.push_back(t); |
| 3840 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3841 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3842 | // Now do the pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3843 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3845 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3847 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3848 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3849 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3850 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3851 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3852 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3853 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3855 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3856 | SourceLocation(), |
| 3857 | &Context->Idents.get("FuncPtr"), |
| 3858 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3859 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3860 | ICIS_NoInit); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3861 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3862 | FD->getType(), VK_LValue, |
| 3863 | OK_Ordinary); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3865 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3866 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3867 | CK_BitCast, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3868 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3870 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3871 | // Add the implicit argument. |
| 3872 | BlkExprs.push_back(BlkCast); |
| 3873 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3875 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3876 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3877 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3878 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3879 | Exp->getType(), VK_RValue, |
| 3880 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3881 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3884 | // We need to return the rewritten expression to handle cases where the |
| 3885 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3886 | // For example: |
| 3887 | // |
| 3888 | // int main() { |
| 3889 | // __block Foo *f; |
| 3890 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3892 | // void (^myblock)() = ^() { |
| 3893 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3894 | // i = 77; |
| 3895 | // }; |
| 3896 | //} |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3897 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3898 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3899 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3900 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3901 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3902 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3903 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3904 | SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3905 | &Context->Idents.get("__forwarding"), |
| 3906 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3907 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3908 | ICIS_NoInit); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3909 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3910 | FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3911 | FD->getType(), VK_LValue, |
| 3912 | OK_Ordinary); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3913 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3914 | StringRef Name = VD->getName(); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3915 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3916 | &Context->Idents.get(Name), |
| 3917 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3918 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3919 | ICIS_NoInit); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3920 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3921 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3922 | |
| 3923 | |
| 3924 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3925 | // Need parens to enforce precedence. |
Fariborz Jahanian | 5bba75f | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3926 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3927 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3928 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3929 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3930 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3933 | // Rewrites the imported local variable V with external storage |
| 3934 | // (static, extern, etc.) as *V |
| 3935 | // |
| 3936 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3937 | ValueDecl *VD = DRE->getDecl(); |
| 3938 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3939 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3940 | return DRE; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3941 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3942 | VK_LValue, OK_Ordinary, |
| 3943 | DRE->getLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3944 | // Need parens to enforce precedence. |
| 3945 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3946 | Exp); |
| 3947 | ReplaceStmt(DRE, PE); |
| 3948 | return PE; |
| 3949 | } |
| 3950 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3951 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3952 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3953 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3954 | |
| 3955 | // Need to avoid trying to rewrite synthesized casts. |
| 3956 | if (LocStart.isInvalid()) |
| 3957 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3958 | // Need to avoid trying to rewrite casts contained in macros. |
| 3959 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3960 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3962 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3963 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3964 | QualType QT = CE->getType(); |
| 3965 | const Type* TypePtr = QT->getAs<Type>(); |
| 3966 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3967 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3968 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 3969 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 3970 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3971 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3972 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3973 | return; |
| 3974 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3975 | // advance the location to startArgList. |
| 3976 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3978 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3979 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3980 | case '^': |
| 3981 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3982 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3983 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3984 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | return; |
| 3988 | } |
| 3989 | |
| 3990 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3991 | SourceLocation DeclLoc = FD->getLocation(); |
| 3992 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3994 | // We have 1 or more arguments that have closure pointers. |
| 3995 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3996 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3998 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4000 | parenCount++; |
| 4001 | // advance the location to startArgList. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4002 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4003 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4005 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4007 | while (*argPtr++ && parenCount) { |
| 4008 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4009 | case '^': |
| 4010 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4011 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4012 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4013 | break; |
| 4014 | case '(': |
| 4015 | parenCount++; |
| 4016 | break; |
| 4017 | case ')': |
| 4018 | parenCount--; |
| 4019 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | return; |
| 4023 | } |
| 4024 | |
| 4025 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4026 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4027 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4028 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4029 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4030 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4031 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4032 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4033 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4034 | } |
| 4035 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4036 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4037 | E = FTP->param_type_end(); |
| 4038 | I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4039 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | return true; |
| 4041 | } |
| 4042 | return false; |
| 4043 | } |
| 4044 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4045 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4046 | const FunctionProtoType *FTP; |
| 4047 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4048 | if (PT) { |
| 4049 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4050 | } else { |
| 4051 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4052 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4053 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4054 | } |
| 4055 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4056 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4057 | E = FTP->param_type_end(); |
| 4058 | I != E; ++I) { |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4059 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4060 | return true; |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4061 | if ((*I)->isObjCObjectPointerType() && |
| 4062 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4063 | return true; |
| 4064 | } |
| 4065 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4066 | } |
| 4067 | return false; |
| 4068 | } |
| 4069 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4070 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4071 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4072 | const char *argPtr = strchr(Name, '('); |
| 4073 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4074 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4075 | LParen = argPtr; // output the start. |
| 4076 | argPtr++; // skip past the left paren. |
| 4077 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4079 | while (*argPtr && parenCount) { |
| 4080 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4081 | case '(': parenCount++; break; |
| 4082 | case ')': parenCount--; break; |
| 4083 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4084 | } |
| 4085 | if (parenCount) argPtr++; |
| 4086 | } |
| 4087 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4088 | RParen = argPtr; // output the end |
| 4089 | } |
| 4090 | |
| 4091 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4092 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4093 | RewriteBlockPointerFunctionArgs(FD); |
| 4094 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4096 | // Handle Variables and Typedefs. |
| 4097 | SourceLocation DeclLoc = ND->getLocation(); |
| 4098 | QualType DeclT; |
| 4099 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4100 | DeclT = VD->getType(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4101 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4102 | DeclT = TDD->getUnderlyingType(); |
| 4103 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4104 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4106 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4108 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4109 | const char *endBuf = startBuf; |
| 4110 | // scan backward (from the decl location) for the end of the previous decl. |
| 4111 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4112 | startBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4113 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4114 | std::string buf; |
| 4115 | unsigned OrigLength=0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4116 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4117 | // may take block argument types (which will be handled below). |
| 4118 | if (*startBuf == '^') { |
| 4119 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4120 | buf = '*'; |
| 4121 | startBuf++; |
| 4122 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4123 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4124 | while (*startBuf != ')') { |
| 4125 | buf += *startBuf; |
| 4126 | startBuf++; |
| 4127 | OrigLength++; |
| 4128 | } |
| 4129 | buf += ')'; |
| 4130 | OrigLength++; |
| 4131 | |
| 4132 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4133 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4134 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4135 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4136 | DeclLoc = ND->getLocation(); |
| 4137 | startBuf = SM->getCharacterData(DeclLoc); |
| 4138 | const char *argListBegin, *argListEnd; |
| 4139 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4140 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4141 | if (*argListBegin == '^') |
| 4142 | buf += '*'; |
| 4143 | else if (*argListBegin == '<') { |
| 4144 | buf += "/*"; |
| 4145 | buf += *argListBegin++; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4146 | OrigLength++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4147 | while (*argListBegin != '>') { |
| 4148 | buf += *argListBegin++; |
| 4149 | OrigLength++; |
| 4150 | } |
| 4151 | buf += *argListBegin; |
| 4152 | buf += "*/"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4153 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4154 | else |
| 4155 | buf += *argListBegin; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4156 | argListBegin++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4157 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4158 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4159 | buf += ')'; |
| 4160 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4161 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4162 | ReplaceText(Start, OrigLength, buf); |
| 4163 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4164 | return; |
| 4165 | } |
| 4166 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4167 | |
| 4168 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4169 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4170 | /// struct Block_byref_id_object *src) { |
| 4171 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4172 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4173 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4174 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4175 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4176 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4177 | /// } |
| 4178 | /// And: |
| 4179 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4180 | /// _Block_object_dispose(_src->object, |
| 4181 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4182 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4183 | /// _Block_object_dispose(_src->object, |
| 4184 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4185 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4186 | /// } |
| 4187 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4188 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4189 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4190 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4191 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4192 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4193 | CopyDestroyCache.insert(flag); |
| 4194 | S = "static void __Block_byref_id_object_copy_"; |
| 4195 | S += utostr(flag); |
| 4196 | S += "(void *dst, void *src) {\n"; |
| 4197 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4198 | // offset into the object pointer is computed as: |
| 4199 | // void * + void* + int + int + void* + void * |
| 4200 | unsigned IntSize = |
| 4201 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4202 | unsigned VoidPtrSize = |
| 4203 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4204 | |
Ken Dyck | d9c83e6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4205 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4206 | S += " _Block_object_assign((char*)dst + "; |
| 4207 | S += utostr(offset); |
| 4208 | S += ", *(void * *) ((char*)src + "; |
| 4209 | S += utostr(offset); |
| 4210 | S += "), "; |
| 4211 | S += utostr(flag); |
| 4212 | S += ");\n}\n"; |
| 4213 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4214 | S += "static void __Block_byref_id_object_dispose_"; |
| 4215 | S += utostr(flag); |
| 4216 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4217 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4218 | S += utostr(offset); |
| 4219 | S += "), "; |
| 4220 | S += utostr(flag); |
| 4221 | S += ");\n}\n"; |
| 4222 | return S; |
| 4223 | } |
| 4224 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4225 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4226 | /// the declaration into: |
| 4227 | /// struct __Block_byref_ND { |
| 4228 | /// void *__isa; // NULL for everything except __weak pointers |
| 4229 | /// struct __Block_byref_ND *__forwarding; |
| 4230 | /// int32_t __flags; |
| 4231 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4232 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4233 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4234 | /// typex ND; |
| 4235 | /// }; |
| 4236 | /// |
| 4237 | /// It then replaces declaration of ND variable with: |
| 4238 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4239 | /// __size=sizeof(struct __Block_byref_ND), |
| 4240 | /// ND=initializer-if-any}; |
| 4241 | /// |
| 4242 | /// |
| 4243 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4244 | // Insert declaration for the function in which block literal is |
| 4245 | // used. |
| 4246 | if (CurFunctionDeclToDeclareForBlock) |
| 4247 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4248 | int flag = 0; |
| 4249 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4250 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4251 | if (DeclLoc.isInvalid()) |
| 4252 | // If type location is missing, it is because of missing type (a warning). |
| 4253 | // Use variable's location which is good for this case. |
| 4254 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4255 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4256 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4257 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4258 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4259 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4260 | std::string ByrefType; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4261 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4262 | ByrefType += " {\n"; |
| 4263 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4264 | RewriteByRefString(ByrefType, Name, ND); |
| 4265 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4266 | ByrefType += " int __flags;\n"; |
| 4267 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4268 | // Add void *__Block_byref_id_object_copy; |
| 4269 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4270 | QualType Ty = ND->getType(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4271 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4272 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4273 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4274 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4275 | } |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4276 | |
| 4277 | QualType T = Ty; |
| 4278 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4279 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4280 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4281 | ByrefType += " " + Name + ";\n"; |
| 4282 | ByrefType += "};\n"; |
| 4283 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4284 | SourceLocation FunLocStart; |
| 4285 | if (CurFunctionDef) |
| 4286 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4287 | else { |
| 4288 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4289 | FunLocStart = CurMethodDef->getLocStart(); |
| 4290 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4291 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4292 | if (Ty.isObjCGCWeak()) { |
| 4293 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4294 | isa = 1; |
| 4295 | } |
| 4296 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4297 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4298 | flag = BLOCK_BYREF_CALLER; |
| 4299 | QualType Ty = ND->getType(); |
| 4300 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4301 | if (Ty->isBlockPointerType()) |
| 4302 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4303 | else |
| 4304 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4305 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4306 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4307 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4308 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4309 | |
| 4310 | // struct __Block_byref_ND ND = |
| 4311 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4312 | // initializer-if-any}; |
| 4313 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4314 | unsigned flags = 0; |
| 4315 | if (HasCopyAndDispose) |
| 4316 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4317 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4318 | ByrefType.clear(); |
| 4319 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4320 | std::string ForwardingCastType("("); |
| 4321 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4322 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4323 | ByrefType += " " + Name + " = {(void*)"; |
| 4324 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4325 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4326 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4327 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4328 | ByrefType += "sizeof("; |
| 4329 | RewriteByRefString(ByrefType, Name, ND); |
| 4330 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4331 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4332 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4333 | ByrefType += utostr(flag); |
| 4334 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4335 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4336 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4337 | ByrefType += "};\n"; |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4338 | unsigned nameSize = Name.size(); |
| 4339 | // for block or function pointer declaration. Name is aleady |
| 4340 | // part of the declaration. |
| 4341 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4342 | nameSize = 1; |
| 4343 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4344 | } |
| 4345 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4346 | SourceLocation startLoc; |
| 4347 | Expr *E = ND->getInit(); |
| 4348 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4349 | startLoc = ECE->getLParenLoc(); |
| 4350 | else |
| 4351 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4352 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4353 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4354 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4355 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4356 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4357 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4358 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4359 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4360 | ByrefType += "sizeof("; |
| 4361 | RewriteByRefString(ByrefType, Name, ND); |
| 4362 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4363 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4364 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4365 | ByrefType += utostr(flag); |
| 4366 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4367 | ByrefType += utostr(flag); |
| 4368 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4369 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4370 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4371 | |
| 4372 | // Complete the newly synthesized compound expression by inserting a right |
| 4373 | // curly brace before the end of the declaration. |
| 4374 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4375 | // also assumes there is only one declarator. For example, the following |
| 4376 | // isn't currently supported by this routine (in general): |
| 4377 | // |
| 4378 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4379 | // |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4380 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4381 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4382 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4383 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4384 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4385 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4386 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4387 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4388 | return; |
| 4389 | } |
| 4390 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4392 | // Add initializers for any closure decl refs. |
| 4393 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4394 | if (BlockDeclRefs.size()) { |
| 4395 | // Unique all "by copy" declarations. |
| 4396 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4397 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4398 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4399 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4400 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4401 | } |
| 4402 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4403 | // Unique all "by ref" declarations. |
| 4404 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4405 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4406 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4407 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4408 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4409 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4410 | } |
| 4411 | // Find any imported blocks...they will need special attention. |
| 4412 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4413 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4414 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4415 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4416 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4420 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4421 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4422 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4423 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4424 | SourceLocation(), ID, FType, 0, SC_Extern, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4425 | false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4428 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4429 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4430 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4431 | Blocks.push_back(Exp); |
| 4432 | |
| 4433 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4434 | |
| 4435 | // Add inner imported variables now used in current block. |
| 4436 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4437 | if (!InnerBlockDeclRefs.empty()) { |
| 4438 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4439 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4440 | ValueDecl *VD = Exp->getDecl(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4441 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4442 | // We need to save the copied-in variables in nested |
| 4443 | // blocks because it is needed at the end for some of the API generations. |
| 4444 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4445 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4446 | BlockDeclRefs.push_back(Exp); |
| 4447 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4448 | BlockByCopyDecls.push_back(VD); |
| 4449 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4450 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4451 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4452 | BlockDeclRefs.push_back(Exp); |
| 4453 | BlockByRefDeclsPtrSet.insert(VD); |
| 4454 | BlockByRefDecls.push_back(VD); |
| 4455 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4456 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4457 | // Find any imported blocks...they will need special attention. |
| 4458 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4459 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4460 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4461 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4462 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4463 | } |
| 4464 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4465 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4466 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4468 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4469 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4470 | else if (CurMethodDef) |
| 4471 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4472 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4473 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4475 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4476 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4477 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4478 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4479 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4480 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4481 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4482 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4483 | |
| 4484 | FunctionDecl *FD; |
| 4485 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 4487 | // Simulate a constructor call... |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4488 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4489 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4490 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4492 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4493 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4494 | // Initialize the block function. |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4495 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4496 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4497 | VK_LValue, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4498 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4499 | CK_BitCast, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | InitExprs.push_back(castExpr); |
| 4501 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4502 | // Initialize the block descriptor. |
| 4503 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4505 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4506 | SourceLocation(), SourceLocation(), |
| 4507 | &Context->Idents.get(DescData.c_str()), |
| 4508 | Context->VoidPtrTy, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4509 | SC_Static); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4510 | UnaryOperator *DescRefExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4511 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4512 | Context->VoidPtrTy, |
| 4513 | VK_LValue, |
| 4514 | SourceLocation()), |
| 4515 | UO_AddrOf, |
| 4516 | Context->getPointerType(Context->VoidPtrTy), |
| 4517 | VK_RValue, OK_Ordinary, |
| 4518 | SourceLocation()); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4519 | InitExprs.push_back(DescRefExpr); |
| 4520 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4521 | // Add initializers for any closure decl refs. |
| 4522 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4523 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4524 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4525 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4526 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4527 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4528 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4529 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4530 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4531 | SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4532 | if (HasLocalVariableExternalStorage(*I)) { |
| 4533 | QualType QT = (*I)->getType(); |
| 4534 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4535 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4536 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4537 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4538 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4539 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4540 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4541 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4542 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4543 | CK_BitCast, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4544 | } else { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4545 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4546 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4547 | SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4548 | if (HasLocalVariableExternalStorage(*I)) { |
| 4549 | QualType QT = (*I)->getType(); |
| 4550 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4551 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4552 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4555 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | } |
| 4558 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4559 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4560 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4561 | ValueDecl *ND = (*I); |
| 4562 | std::string Name(ND->getNameAsString()); |
| 4563 | std::string RecName; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4564 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4565 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4566 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4567 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4568 | SourceLocation(), SourceLocation(), |
| 4569 | II); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4570 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4571 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4572 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4573 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4574 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4575 | SourceLocation()); |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4576 | bool isNestedCapturedVar = false; |
| 4577 | if (block) |
| 4578 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4579 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4580 | const VarDecl *variable = ci->getVariable(); |
| 4581 | if (variable == ND && ci->isNested()) { |
| 4582 | assert (ci->isByRef() && |
| 4583 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4584 | isNestedCapturedVar = true; |
| 4585 | break; |
| 4586 | } |
| 4587 | } |
| 4588 | // captured nested byref variable has its address passed. Do not take |
| 4589 | // its address again. |
| 4590 | if (!isNestedCapturedVar) |
| 4591 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4592 | Context->getPointerType(Exp->getType()), |
| 4593 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4594 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4596 | } |
| 4597 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4598 | if (ImportedBlockDecls.size()) { |
| 4599 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4600 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4601 | unsigned IntSize = |
| 4602 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4603 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4604 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4605 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4606 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4607 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4608 | FType, VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4609 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4610 | Context->getPointerType(NewRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4611 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4612 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4613 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4614 | BlockDeclRefs.clear(); |
| 4615 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4616 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4617 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4618 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4619 | ImportedBlockDecls.clear(); |
| 4620 | return NewRep; |
| 4621 | } |
| 4622 | |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4623 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4624 | if (const ObjCForCollectionStmt * CS = |
| 4625 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4626 | return CS->getElement() == DS; |
| 4627 | return false; |
| 4628 | } |
| 4629 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4630 | //===----------------------------------------------------------------------===// |
| 4631 | // Function Body / Expression rewriting |
| 4632 | //===----------------------------------------------------------------------===// |
| 4633 | |
| 4634 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4636 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4637 | Stmts.push_back(S); |
| 4638 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4639 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4640 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4642 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4643 | // Pseudo-object operations and ivar references need special |
| 4644 | // treatment because we're going to recursively rewrite them. |
| 4645 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4646 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4647 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4648 | } else { |
| 4649 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4650 | } |
| 4651 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4652 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4653 | } |
| 4654 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4655 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4657 | // Perform a bottom up rewrite of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4658 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4659 | if (*CI) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4660 | Stmt *childStmt = (*CI); |
| 4661 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4662 | if (newStmt) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4663 | *CI = newStmt; |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4664 | } |
Nick Lewycky | 508ef2c | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4665 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4667 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4668 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4669 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4670 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4671 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4672 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4673 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4674 | // Rewrite the block body in place. |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4675 | Stmt *SaveCurrentBody = CurrentBody; |
| 4676 | CurrentBody = BE->getBody(); |
| 4677 | PropParentMap = 0; |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4678 | // block literal on rhs of a property-dot-sytax assignment |
| 4679 | // must be replaced by its synthesize ast so getRewrittenText |
| 4680 | // works as expected. In this case, what actually ends up on RHS |
| 4681 | // is the blockTranscribed which is the helper function for the |
| 4682 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4683 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4684 | DisableReplaceStmt = false; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4685 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4686 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4687 | CurrentBody = SaveCurrentBody; |
| 4688 | PropParentMap = 0; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4689 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4690 | // Now we snarf the rewritten text and stash it away for later use. |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4691 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4692 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4694 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4695 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4696 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4697 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4698 | return blockTranscribed; |
| 4699 | } |
| 4700 | // Handle specific things. |
| 4701 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4702 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4704 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4705 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4707 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4708 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4710 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4711 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4712 | // Before we rewrite it, put the original message expression in a comment. |
| 4713 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4714 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4716 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4717 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4718 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4719 | std::string messString; |
| 4720 | messString += "// "; |
| 4721 | messString.append(startBuf, endBuf-startBuf+1); |
| 4722 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | |
| 4724 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4726 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4727 | // Tried this, but it didn't work either... |
| 4728 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4729 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4730 | return RewriteMessageExpr(MessExpr); |
| 4731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4733 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4734 | return RewriteObjCTryStmt(StmtTry); |
| 4735 | |
| 4736 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4737 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4738 | |
| 4739 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4740 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4742 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4743 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | |
| 4745 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4746 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4748 | OrigStmtRange.getEnd()); |
| 4749 | if (BreakStmt *StmtBreakStmt = |
| 4750 | dyn_cast<BreakStmt>(S)) |
| 4751 | return RewriteBreakStmt(StmtBreakStmt); |
| 4752 | if (ContinueStmt *StmtContinueStmt = |
| 4753 | dyn_cast<ContinueStmt>(S)) |
| 4754 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
| 4756 | // 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] | 4757 | // and cast exprs. |
| 4758 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4759 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4760 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4762 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4763 | // the context of an ObjCForCollectionStmt. For example: |
| 4764 | // NSArray *someArray; |
| 4765 | // for (id <FooProtocol> index in someArray) ; |
| 4766 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4767 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4768 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4769 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4771 | // Blocks rewrite rules. |
| 4772 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4773 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4774 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4775 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4776 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4777 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4779 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4780 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4781 | if (VD->hasAttr<BlocksAttr>()) { |
| 4782 | static unsigned uniqueByrefDeclCount = 0; |
| 4783 | assert(!BlockByRefDeclNo.count(ND) && |
| 4784 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4785 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4786 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4787 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4788 | else |
| 4789 | RewriteTypeOfDecl(VD); |
| 4790 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4791 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4792 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4793 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4794 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4796 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4797 | } |
| 4798 | } |
| 4799 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4800 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4801 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4802 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
| 4804 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4805 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4806 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4808 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4809 | && "Statement stack mismatch"); |
| 4810 | Stmts.pop_back(); |
| 4811 | } |
| 4812 | // Handle blocks rewriting. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4813 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4814 | ValueDecl *VD = DRE->getDecl(); |
| 4815 | if (VD->hasAttr<BlocksAttr>()) |
| 4816 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4817 | if (HasLocalVariableExternalStorage(VD)) |
| 4818 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4821 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4822 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4823 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4824 | ReplaceStmt(S, BlockCall); |
| 4825 | return BlockCall; |
| 4826 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4827 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4828 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4829 | RewriteCastExpr(CE); |
| 4830 | } |
| 4831 | #if 0 |
| 4832 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4833 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4834 | ICE->getSubExpr(), |
| 4835 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4836 | // Get the new text. |
| 4837 | std::string SStr; |
| 4838 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4839 | Replacement->printPretty(Buf); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4840 | const std::string &Str = Buf.str(); |
| 4841 | |
| 4842 | printf("CAST = %s\n", &Str[0]); |
| 4843 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4844 | delete S; |
| 4845 | return Replacement; |
| 4846 | } |
| 4847 | #endif |
| 4848 | // Return this stmt unmodified. |
| 4849 | return S; |
| 4850 | } |
| 4851 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4852 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4853 | for (auto *FD : RD->fields()) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4854 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4855 | RewriteBlockPointerDecl(FD); |
| 4856 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4857 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4858 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4859 | } |
| 4860 | } |
| 4861 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4862 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4863 | /// main file of the input. |
| 4864 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4865 | switch (D->getKind()) { |
| 4866 | case Decl::Function: { |
| 4867 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4868 | if (FD->isOverloadedOperator()) |
| 4869 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4871 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4872 | // prototype. This enables us to rewrite function declarations and |
| 4873 | // definitions using the same code. |
| 4874 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4875 | |
Argyrios Kyrtzidis | 75627ad | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4876 | if (!FD->isThisDeclarationADefinition()) |
| 4877 | break; |
| 4878 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4879 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4880 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4881 | CurFunctionDef = FD; |
| 4882 | CurFunctionDeclToDeclareForBlock = FD; |
| 4883 | CurrentBody = Body; |
| 4884 | Body = |
| 4885 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4886 | FD->setBody(Body); |
| 4887 | CurrentBody = 0; |
| 4888 | if (PropParentMap) { |
| 4889 | delete PropParentMap; |
| 4890 | PropParentMap = 0; |
| 4891 | } |
| 4892 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4893 | // and any copy/dispose helper functions. |
| 4894 | InsertBlockLiteralsWithinFunction(FD); |
| 4895 | CurFunctionDef = 0; |
| 4896 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4897 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4898 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4900 | case Decl::ObjCMethod: { |
| 4901 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4902 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4903 | CurMethodDef = MD; |
| 4904 | CurrentBody = Body; |
| 4905 | Body = |
| 4906 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4907 | MD->setBody(Body); |
| 4908 | CurrentBody = 0; |
| 4909 | if (PropParentMap) { |
| 4910 | delete PropParentMap; |
| 4911 | PropParentMap = 0; |
| 4912 | } |
| 4913 | InsertBlockLiteralsWithinMethod(MD); |
| 4914 | CurMethodDef = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4915 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4916 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4917 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4918 | case Decl::ObjCImplementation: { |
| 4919 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4920 | ClassImplementation.push_back(CI); |
| 4921 | break; |
| 4922 | } |
| 4923 | case Decl::ObjCCategoryImpl: { |
| 4924 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4925 | CategoryImplementation.push_back(CI); |
| 4926 | break; |
| 4927 | } |
| 4928 | case Decl::Var: { |
| 4929 | VarDecl *VD = cast<VarDecl>(D); |
| 4930 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4931 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4932 | RewriteBlockPointerDecl(VD); |
| 4933 | else if (VD->getType()->isFunctionPointerType()) { |
| 4934 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4935 | if (VD->getInit()) { |
| 4936 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4937 | RewriteCastExpr(CE); |
| 4938 | } |
| 4939 | } |
| 4940 | } else if (VD->getType()->isRecordType()) { |
| 4941 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4942 | if (RD->isCompleteDefinition()) |
| 4943 | RewriteRecordBody(RD); |
| 4944 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4945 | if (VD->getInit()) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4946 | GlobalVarDecl = VD; |
| 4947 | CurrentBody = VD->getInit(); |
| 4948 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4949 | CurrentBody = 0; |
| 4950 | if (PropParentMap) { |
| 4951 | delete PropParentMap; |
| 4952 | PropParentMap = 0; |
| 4953 | } |
| 4954 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4955 | GlobalVarDecl = 0; |
| 4956 | |
| 4957 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4958 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4959 | RewriteCastExpr(CE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4960 | } |
| 4961 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4962 | break; |
| 4963 | } |
| 4964 | case Decl::TypeAlias: |
| 4965 | case Decl::Typedef: { |
| 4966 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4967 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 4968 | RewriteBlockPointerDecl(TD); |
| 4969 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4970 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4971 | } |
| 4972 | break; |
| 4973 | } |
| 4974 | case Decl::CXXRecord: |
| 4975 | case Decl::Record: { |
| 4976 | RecordDecl *RD = cast<RecordDecl>(D); |
| 4977 | if (RD->isCompleteDefinition()) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4978 | RewriteRecordBody(RD); |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4979 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4980 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4981 | default: |
| 4982 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4983 | } |
| 4984 | // Nothing yet. |
| 4985 | } |
| 4986 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4987 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4988 | if (Diags.hasErrorOccurred()) |
| 4989 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4990 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4991 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4992 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4993 | // Here's a great place to add any extra declarations that may be needed. |
| 4994 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4996 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 4997 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 4998 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4999 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5000 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5001 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5002 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5003 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5004 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5005 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5006 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5007 | //printf("Changed:\n"); |
| 5008 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5009 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5010 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5011 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5012 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5013 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5014 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5015 | // Rewrite Objective-c meta data* |
| 5016 | std::string ResultStr; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5017 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5018 | // Emit metadata. |
| 5019 | *OutFile << ResultStr; |
| 5020 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5021 | OutFile->flush(); |
| 5022 | } |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5023 | |
| 5024 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5025 | InitializeCommon(context); |
| 5026 | |
| 5027 | // declaring objc_selector outside the parameter list removes a silly |
| 5028 | // scope related warning... |
| 5029 | if (IsHeader) |
| 5030 | Preamble = "#pragma once\n"; |
| 5031 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5032 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5033 | Preamble += "struct objc_object *superClass; "; |
| 5034 | if (LangOpts.MicrosoftExt) { |
| 5035 | // Add a constructor for creating temporary objects. |
| 5036 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5037 | ": "; |
| 5038 | Preamble += "object(o), superClass(s) {} "; |
| 5039 | } |
| 5040 | Preamble += "};\n"; |
| 5041 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5042 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5043 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5044 | Preamble += "#endif\n"; |
| 5045 | if (LangOpts.MicrosoftExt) { |
| 5046 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5047 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5048 | } else |
| 5049 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5050 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5051 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5052 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5053 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5054 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5055 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5056 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5057 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5058 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5059 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5060 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5061 | Preamble += "(const char *);\n"; |
| 5062 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5063 | Preamble += "(struct objc_class *);\n"; |
| 5064 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5065 | Preamble += "(const char *);\n"; |
| 5066 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5067 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5068 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5069 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5070 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5071 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5072 | // @synchronized hooks. |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5073 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5074 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5075 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5076 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5077 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5078 | Preamble += "unsigned long state;\n\t"; |
| 5079 | Preamble += "void **itemsPtr;\n\t"; |
| 5080 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5081 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5082 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5083 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5084 | Preamble += "#endif\n"; |
| 5085 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5086 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5087 | Preamble += " int *isa;\n"; |
| 5088 | Preamble += " int flags;\n"; |
| 5089 | Preamble += " char *str;\n"; |
| 5090 | Preamble += " long length;\n"; |
| 5091 | Preamble += "};\n"; |
| 5092 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5093 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5094 | Preamble += "#else\n"; |
| 5095 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5096 | Preamble += "#endif\n"; |
| 5097 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5098 | Preamble += "#endif\n"; |
| 5099 | // Blocks preamble. |
| 5100 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5101 | Preamble += "#define BLOCK_IMPL\n"; |
| 5102 | Preamble += "struct __block_impl {\n"; |
| 5103 | Preamble += " void *isa;\n"; |
| 5104 | Preamble += " int Flags;\n"; |
| 5105 | Preamble += " int Reserved;\n"; |
| 5106 | Preamble += " void *FuncPtr;\n"; |
| 5107 | Preamble += "};\n"; |
| 5108 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5109 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5110 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5111 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5112 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5113 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5114 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5115 | Preamble += "#else\n"; |
| 5116 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5117 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5118 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5119 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5120 | Preamble += "#endif\n"; |
| 5121 | Preamble += "#endif\n"; |
| 5122 | if (LangOpts.MicrosoftExt) { |
| 5123 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5124 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5125 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5126 | Preamble += "#define __attribute__(X)\n"; |
| 5127 | Preamble += "#endif\n"; |
| 5128 | Preamble += "#define __weak\n"; |
| 5129 | } |
| 5130 | else { |
| 5131 | Preamble += "#define __block\n"; |
| 5132 | Preamble += "#define __weak\n"; |
| 5133 | } |
| 5134 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5135 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5136 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5137 | } |
| 5138 | |
| 5139 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5140 | /// ivar offset. |
| 5141 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5142 | std::string &Result) { |
| 5143 | if (ivar->isBitField()) { |
| 5144 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5145 | // place all bitfields at offset 0. |
| 5146 | Result += "0"; |
| 5147 | } else { |
| 5148 | Result += "__OFFSETOFIVAR__(struct "; |
| 5149 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5150 | if (LangOpts.MicrosoftExt) |
| 5151 | Result += "_IMPL"; |
| 5152 | Result += ", "; |
| 5153 | Result += ivar->getNameAsString(); |
| 5154 | Result += ")"; |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5159 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5160 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5161 | StringRef ClassName, std::string &Result) { |
| 5162 | static bool objc_protocol_methods = false; |
| 5163 | |
| 5164 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5165 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5166 | /* struct protocol_methods { |
| 5167 | SEL _cmd; |
| 5168 | char *method_types; |
| 5169 | } |
| 5170 | */ |
| 5171 | Result += "\nstruct _protocol_methods {\n"; |
| 5172 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5173 | Result += "\tchar *method_types;\n"; |
| 5174 | Result += "};\n"; |
| 5175 | |
| 5176 | objc_protocol_methods = true; |
| 5177 | } |
| 5178 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5179 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5180 | return; |
| 5181 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5182 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5183 | PDecl = Def; |
| 5184 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5185 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5186 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5187 | PDecl->instmeth_end()); |
| 5188 | /* struct _objc_protocol_method_list { |
| 5189 | int protocol_method_count; |
| 5190 | struct protocol_methods protocols[]; |
| 5191 | } |
| 5192 | */ |
| 5193 | Result += "\nstatic struct {\n"; |
| 5194 | Result += "\tint protocol_method_count;\n"; |
| 5195 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5196 | Result += utostr(NumMethods); |
| 5197 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5198 | Result += PDecl->getNameAsString(); |
| 5199 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5200 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5201 | |
| 5202 | // Output instance methods declared in this protocol. |
| 5203 | for (ObjCProtocolDecl::instmeth_iterator |
| 5204 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5205 | I != E; ++I) { |
| 5206 | if (I == PDecl->instmeth_begin()) |
| 5207 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5208 | else |
| 5209 | Result += "\t ,{(struct objc_selector *)\""; |
| 5210 | Result += (*I)->getSelector().getAsString(); |
| 5211 | std::string MethodTypeString; |
| 5212 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5213 | Result += "\", \""; |
| 5214 | Result += MethodTypeString; |
| 5215 | Result += "\"}\n"; |
| 5216 | } |
| 5217 | Result += "\t }\n};\n"; |
| 5218 | } |
| 5219 | |
| 5220 | // Output class methods declared in this protocol. |
| 5221 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5222 | PDecl->classmeth_end()); |
| 5223 | if (NumMethods > 0) { |
| 5224 | /* struct _objc_protocol_method_list { |
| 5225 | int protocol_method_count; |
| 5226 | struct protocol_methods protocols[]; |
| 5227 | } |
| 5228 | */ |
| 5229 | Result += "\nstatic struct {\n"; |
| 5230 | Result += "\tint protocol_method_count;\n"; |
| 5231 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5232 | Result += utostr(NumMethods); |
| 5233 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5234 | Result += PDecl->getNameAsString(); |
| 5235 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5236 | "{\n\t"; |
| 5237 | Result += utostr(NumMethods); |
| 5238 | Result += "\n"; |
| 5239 | |
| 5240 | // Output instance methods declared in this protocol. |
| 5241 | for (ObjCProtocolDecl::classmeth_iterator |
| 5242 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5243 | I != E; ++I) { |
| 5244 | if (I == PDecl->classmeth_begin()) |
| 5245 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5246 | else |
| 5247 | Result += "\t ,{(struct objc_selector *)\""; |
| 5248 | Result += (*I)->getSelector().getAsString(); |
| 5249 | std::string MethodTypeString; |
| 5250 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5251 | Result += "\", \""; |
| 5252 | Result += MethodTypeString; |
| 5253 | Result += "\"}\n"; |
| 5254 | } |
| 5255 | Result += "\t }\n};\n"; |
| 5256 | } |
| 5257 | |
| 5258 | // Output: |
| 5259 | /* struct _objc_protocol { |
| 5260 | // Objective-C 1.0 extensions |
| 5261 | struct _objc_protocol_extension *isa; |
| 5262 | char *protocol_name; |
| 5263 | struct _objc_protocol **protocol_list; |
| 5264 | struct _objc_protocol_method_list *instance_methods; |
| 5265 | struct _objc_protocol_method_list *class_methods; |
| 5266 | }; |
| 5267 | */ |
| 5268 | static bool objc_protocol = false; |
| 5269 | if (!objc_protocol) { |
| 5270 | Result += "\nstruct _objc_protocol {\n"; |
| 5271 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5272 | Result += "\tchar *protocol_name;\n"; |
| 5273 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5274 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5275 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5276 | Result += "};\n"; |
| 5277 | |
| 5278 | objc_protocol = true; |
| 5279 | } |
| 5280 | |
| 5281 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5282 | Result += PDecl->getNameAsString(); |
| 5283 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5284 | "{\n\t0, \""; |
| 5285 | Result += PDecl->getNameAsString(); |
| 5286 | Result += "\", 0, "; |
| 5287 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5288 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5289 | Result += PDecl->getNameAsString(); |
| 5290 | Result += ", "; |
| 5291 | } |
| 5292 | else |
| 5293 | Result += "0, "; |
| 5294 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5295 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5296 | Result += PDecl->getNameAsString(); |
| 5297 | Result += "\n"; |
| 5298 | } |
| 5299 | else |
| 5300 | Result += "0\n"; |
| 5301 | Result += "};\n"; |
| 5302 | |
| 5303 | // Mark this protocol as having been generated. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5304 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5305 | llvm_unreachable("protocol already synthesized"); |
| 5306 | |
| 5307 | } |
| 5308 | |
| 5309 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5310 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5311 | StringRef prefix, StringRef ClassName, |
| 5312 | std::string &Result) { |
| 5313 | if (Protocols.empty()) return; |
| 5314 | |
| 5315 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5316 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5317 | |
| 5318 | // Output the top lovel protocol meta-data for the class. |
| 5319 | /* struct _objc_protocol_list { |
| 5320 | struct _objc_protocol_list *next; |
| 5321 | int protocol_count; |
| 5322 | struct _objc_protocol *class_protocols[]; |
| 5323 | } |
| 5324 | */ |
| 5325 | Result += "\nstatic struct {\n"; |
| 5326 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5327 | Result += "\tint protocol_count;\n"; |
| 5328 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5329 | Result += utostr(Protocols.size()); |
| 5330 | Result += "];\n} _OBJC_"; |
| 5331 | Result += prefix; |
| 5332 | Result += "_PROTOCOLS_"; |
| 5333 | Result += ClassName; |
| 5334 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5335 | "{\n\t0, "; |
| 5336 | Result += utostr(Protocols.size()); |
| 5337 | Result += "\n"; |
| 5338 | |
| 5339 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5340 | Result += Protocols[0]->getNameAsString(); |
| 5341 | Result += " \n"; |
| 5342 | |
| 5343 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5344 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5345 | Result += Protocols[i]->getNameAsString(); |
| 5346 | Result += "\n"; |
| 5347 | } |
| 5348 | Result += "\t }\n};\n"; |
| 5349 | } |
| 5350 | |
| 5351 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5352 | std::string &Result) { |
| 5353 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5354 | |
| 5355 | // Explicitly declared @interface's are already synthesized. |
| 5356 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5357 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5358 | // produce correct synthesis as yet. |
| 5359 | RewriteObjCInternalStruct(CDecl, Result); |
| 5360 | } |
| 5361 | |
| 5362 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5363 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5364 | ? IDecl->ivar_size() |
| 5365 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5366 | if (NumIvars > 0) { |
| 5367 | static bool objc_ivar = false; |
| 5368 | if (!objc_ivar) { |
| 5369 | /* struct _objc_ivar { |
| 5370 | char *ivar_name; |
| 5371 | char *ivar_type; |
| 5372 | int ivar_offset; |
| 5373 | }; |
| 5374 | */ |
| 5375 | Result += "\nstruct _objc_ivar {\n"; |
| 5376 | Result += "\tchar *ivar_name;\n"; |
| 5377 | Result += "\tchar *ivar_type;\n"; |
| 5378 | Result += "\tint ivar_offset;\n"; |
| 5379 | Result += "};\n"; |
| 5380 | |
| 5381 | objc_ivar = true; |
| 5382 | } |
| 5383 | |
| 5384 | /* struct { |
| 5385 | int ivar_count; |
| 5386 | struct _objc_ivar ivar_list[nIvars]; |
| 5387 | }; |
| 5388 | */ |
| 5389 | Result += "\nstatic struct {\n"; |
| 5390 | Result += "\tint ivar_count;\n"; |
| 5391 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5392 | Result += utostr(NumIvars); |
| 5393 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5394 | Result += IDecl->getNameAsString(); |
| 5395 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5396 | "{\n\t"; |
| 5397 | Result += utostr(NumIvars); |
| 5398 | Result += "\n"; |
| 5399 | |
| 5400 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5401 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5402 | if (!IDecl->ivar_empty()) { |
| 5403 | for (ObjCInterfaceDecl::ivar_iterator |
| 5404 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5405 | IV != IVEnd; ++IV) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5406 | IVars.push_back(*IV); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5407 | IVI = IDecl->ivar_begin(); |
| 5408 | IVE = IDecl->ivar_end(); |
| 5409 | } else { |
| 5410 | IVI = CDecl->ivar_begin(); |
| 5411 | IVE = CDecl->ivar_end(); |
| 5412 | } |
| 5413 | Result += "\t,{{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5414 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5415 | Result += "\", \""; |
| 5416 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5417 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5418 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5419 | Result += StrEncoding; |
| 5420 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5421 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5422 | Result += "}\n"; |
| 5423 | for (++IVI; IVI != IVE; ++IVI) { |
| 5424 | Result += "\t ,{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5425 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5426 | Result += "\", \""; |
| 5427 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5428 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5429 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5430 | Result += StrEncoding; |
| 5431 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5432 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5433 | Result += "}\n"; |
| 5434 | } |
| 5435 | |
| 5436 | Result += "\t }\n};\n"; |
| 5437 | } |
| 5438 | |
| 5439 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 5440 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5441 | |
| 5442 | // If any of our property implementations have associated getters or |
| 5443 | // setters, produce metadata for them as well. |
| 5444 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5445 | PropEnd = IDecl->propimpl_end(); |
| 5446 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5447 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5448 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5449 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5450 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5451 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5452 | if (!PD) |
| 5453 | continue; |
| 5454 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5455 | if (!Getter->isDefined()) |
| 5456 | InstanceMethods.push_back(Getter); |
| 5457 | if (PD->isReadOnly()) |
| 5458 | continue; |
| 5459 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5460 | if (!Setter->isDefined()) |
| 5461 | InstanceMethods.push_back(Setter); |
| 5462 | } |
| 5463 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5464 | true, "", IDecl->getName(), Result); |
| 5465 | |
| 5466 | // Build _objc_method_list for class's class methods if needed |
| 5467 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5468 | false, "", IDecl->getName(), Result); |
| 5469 | |
| 5470 | // Protocols referenced in class declaration? |
| 5471 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5472 | "CLASS", CDecl->getName(), Result); |
| 5473 | |
| 5474 | // Declaration of class/meta-class metadata |
| 5475 | /* struct _objc_class { |
| 5476 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5477 | const char *super_class_name; |
| 5478 | char *name; |
| 5479 | long version; |
| 5480 | long info; |
| 5481 | long instance_size; |
| 5482 | struct _objc_ivar_list *ivars; |
| 5483 | struct _objc_method_list *methods; |
| 5484 | struct objc_cache *cache; |
| 5485 | struct objc_protocol_list *protocols; |
| 5486 | const char *ivar_layout; |
| 5487 | struct _objc_class_ext *ext; |
| 5488 | }; |
| 5489 | */ |
| 5490 | static bool objc_class = false; |
| 5491 | if (!objc_class) { |
| 5492 | Result += "\nstruct _objc_class {\n"; |
| 5493 | Result += "\tstruct _objc_class *isa;\n"; |
| 5494 | Result += "\tconst char *super_class_name;\n"; |
| 5495 | Result += "\tchar *name;\n"; |
| 5496 | Result += "\tlong version;\n"; |
| 5497 | Result += "\tlong info;\n"; |
| 5498 | Result += "\tlong instance_size;\n"; |
| 5499 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5500 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5501 | Result += "\tstruct objc_cache *cache;\n"; |
| 5502 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5503 | Result += "\tconst char *ivar_layout;\n"; |
| 5504 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5505 | Result += "};\n"; |
| 5506 | objc_class = true; |
| 5507 | } |
| 5508 | |
| 5509 | // Meta-class metadata generation. |
| 5510 | ObjCInterfaceDecl *RootClass = 0; |
| 5511 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5512 | while (SuperClass) { |
| 5513 | RootClass = SuperClass; |
| 5514 | SuperClass = SuperClass->getSuperClass(); |
| 5515 | } |
| 5516 | SuperClass = CDecl->getSuperClass(); |
| 5517 | |
| 5518 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5519 | Result += CDecl->getNameAsString(); |
| 5520 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5521 | "{\n\t(struct _objc_class *)\""; |
| 5522 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5523 | Result += "\""; |
| 5524 | |
| 5525 | if (SuperClass) { |
| 5526 | Result += ", \""; |
| 5527 | Result += SuperClass->getNameAsString(); |
| 5528 | Result += "\", \""; |
| 5529 | Result += CDecl->getNameAsString(); |
| 5530 | Result += "\""; |
| 5531 | } |
| 5532 | else { |
| 5533 | Result += ", 0, \""; |
| 5534 | Result += CDecl->getNameAsString(); |
| 5535 | Result += "\""; |
| 5536 | } |
| 5537 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5538 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5539 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5540 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5541 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5542 | Result += IDecl->getNameAsString(); |
| 5543 | Result += "\n"; |
| 5544 | } |
| 5545 | else |
| 5546 | Result += ", 0\n"; |
| 5547 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5548 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5549 | Result += CDecl->getNameAsString(); |
| 5550 | Result += ",0,0\n"; |
| 5551 | } |
| 5552 | else |
| 5553 | Result += "\t,0,0,0,0\n"; |
| 5554 | Result += "};\n"; |
| 5555 | |
| 5556 | // class metadata generation. |
| 5557 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5558 | Result += CDecl->getNameAsString(); |
| 5559 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5560 | "{\n\t&_OBJC_METACLASS_"; |
| 5561 | Result += CDecl->getNameAsString(); |
| 5562 | if (SuperClass) { |
| 5563 | Result += ", \""; |
| 5564 | Result += SuperClass->getNameAsString(); |
| 5565 | Result += "\", \""; |
| 5566 | Result += CDecl->getNameAsString(); |
| 5567 | Result += "\""; |
| 5568 | } |
| 5569 | else { |
| 5570 | Result += ", 0, \""; |
| 5571 | Result += CDecl->getNameAsString(); |
| 5572 | Result += "\""; |
| 5573 | } |
| 5574 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5575 | Result += ", 0,1"; |
| 5576 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5577 | Result += ",0"; |
| 5578 | else { |
| 5579 | // class has size. Must synthesize its size. |
| 5580 | Result += ",sizeof(struct "; |
| 5581 | Result += CDecl->getNameAsString(); |
| 5582 | if (LangOpts.MicrosoftExt) |
| 5583 | Result += "_IMPL"; |
| 5584 | Result += ")"; |
| 5585 | } |
| 5586 | if (NumIvars > 0) { |
| 5587 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5588 | Result += CDecl->getNameAsString(); |
| 5589 | Result += "\n\t"; |
| 5590 | } |
| 5591 | else |
| 5592 | Result += ",0"; |
| 5593 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5594 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5595 | Result += CDecl->getNameAsString(); |
| 5596 | Result += ", 0\n\t"; |
| 5597 | } |
| 5598 | else |
| 5599 | Result += ",0,0"; |
| 5600 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5601 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5602 | Result += CDecl->getNameAsString(); |
| 5603 | Result += ", 0,0\n"; |
| 5604 | } |
| 5605 | else |
| 5606 | Result += ",0,0,0\n"; |
| 5607 | Result += "};\n"; |
| 5608 | } |
| 5609 | |
| 5610 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5611 | int ClsDefCount = ClassImplementation.size(); |
| 5612 | int CatDefCount = CategoryImplementation.size(); |
| 5613 | |
| 5614 | // For each implemented class, write out all its meta data. |
| 5615 | for (int i = 0; i < ClsDefCount; i++) |
| 5616 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5617 | |
| 5618 | // For each implemented category, write out all its meta data. |
| 5619 | for (int i = 0; i < CatDefCount; i++) |
| 5620 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5621 | |
| 5622 | // Write objc_symtab metadata |
| 5623 | /* |
| 5624 | struct _objc_symtab |
| 5625 | { |
| 5626 | long sel_ref_cnt; |
| 5627 | SEL *refs; |
| 5628 | short cls_def_cnt; |
| 5629 | short cat_def_cnt; |
| 5630 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5631 | }; |
| 5632 | */ |
| 5633 | |
| 5634 | Result += "\nstruct _objc_symtab {\n"; |
| 5635 | Result += "\tlong sel_ref_cnt;\n"; |
| 5636 | Result += "\tSEL *refs;\n"; |
| 5637 | Result += "\tshort cls_def_cnt;\n"; |
| 5638 | Result += "\tshort cat_def_cnt;\n"; |
| 5639 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5640 | Result += "};\n\n"; |
| 5641 | |
| 5642 | Result += "static struct _objc_symtab " |
| 5643 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5644 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5645 | + ", " + utostr(CatDefCount) + "\n"; |
| 5646 | for (int i = 0; i < ClsDefCount; i++) { |
| 5647 | Result += "\t,&_OBJC_CLASS_"; |
| 5648 | Result += ClassImplementation[i]->getNameAsString(); |
| 5649 | Result += "\n"; |
| 5650 | } |
| 5651 | |
| 5652 | for (int i = 0; i < CatDefCount; i++) { |
| 5653 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5654 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5655 | Result += "_"; |
| 5656 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5657 | Result += "\n"; |
| 5658 | } |
| 5659 | |
| 5660 | Result += "};\n\n"; |
| 5661 | |
| 5662 | // Write objc_module metadata |
| 5663 | |
| 5664 | /* |
| 5665 | struct _objc_module { |
| 5666 | long version; |
| 5667 | long size; |
| 5668 | const char *name; |
| 5669 | struct _objc_symtab *symtab; |
| 5670 | } |
| 5671 | */ |
| 5672 | |
| 5673 | Result += "\nstruct _objc_module {\n"; |
| 5674 | Result += "\tlong version;\n"; |
| 5675 | Result += "\tlong size;\n"; |
| 5676 | Result += "\tconst char *name;\n"; |
| 5677 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5678 | Result += "};\n\n"; |
| 5679 | Result += "static struct _objc_module " |
| 5680 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5681 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5682 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5683 | Result += "};\n\n"; |
| 5684 | |
| 5685 | if (LangOpts.MicrosoftExt) { |
| 5686 | if (ProtocolExprDecls.size()) { |
| 5687 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5688 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5689 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5690 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5691 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5692 | Result += (*I)->getNameAsString(); |
| 5693 | Result += " = &_OBJC_PROTOCOL_"; |
| 5694 | Result += (*I)->getNameAsString(); |
| 5695 | Result += ";\n"; |
| 5696 | } |
| 5697 | Result += "#pragma data_seg(pop)\n\n"; |
| 5698 | } |
| 5699 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5700 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5701 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5702 | Result += "&_OBJC_MODULES;\n"; |
| 5703 | Result += "#pragma data_seg(pop)\n\n"; |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5708 | /// implementation. |
| 5709 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5710 | std::string &Result) { |
| 5711 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5712 | // Find category declaration for this implementation. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5713 | ObjCCategoryDecl *CDecl |
| 5714 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5715 | |
| 5716 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5717 | FullCategoryName += '_'; |
| 5718 | FullCategoryName += IDecl->getNameAsString(); |
| 5719 | |
| 5720 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame^] | 5721 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5722 | |
| 5723 | // If any of our property implementations have associated getters or |
| 5724 | // setters, produce metadata for them as well. |
| 5725 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5726 | PropEnd = IDecl->propimpl_end(); |
| 5727 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5728 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5729 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5730 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5731 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5732 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5733 | if (!PD) |
| 5734 | continue; |
| 5735 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5736 | InstanceMethods.push_back(Getter); |
| 5737 | if (PD->isReadOnly()) |
| 5738 | continue; |
| 5739 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5740 | InstanceMethods.push_back(Setter); |
| 5741 | } |
| 5742 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5743 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5744 | Result); |
| 5745 | |
| 5746 | // Build _objc_method_list for class's class methods if needed |
| 5747 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5748 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5749 | Result); |
| 5750 | |
| 5751 | // Protocols referenced in class declaration? |
| 5752 | // Null CDecl is case of a category implementation with no category interface |
| 5753 | if (CDecl) |
| 5754 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5755 | FullCategoryName, Result); |
| 5756 | /* struct _objc_category { |
| 5757 | char *category_name; |
| 5758 | char *class_name; |
| 5759 | struct _objc_method_list *instance_methods; |
| 5760 | struct _objc_method_list *class_methods; |
| 5761 | struct _objc_protocol_list *protocols; |
| 5762 | // Objective-C 1.0 extensions |
| 5763 | uint32_t size; // sizeof (struct _objc_category) |
| 5764 | struct _objc_property_list *instance_properties; // category's own |
| 5765 | // @property decl. |
| 5766 | }; |
| 5767 | */ |
| 5768 | |
| 5769 | static bool objc_category = false; |
| 5770 | if (!objc_category) { |
| 5771 | Result += "\nstruct _objc_category {\n"; |
| 5772 | Result += "\tchar *category_name;\n"; |
| 5773 | Result += "\tchar *class_name;\n"; |
| 5774 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5775 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5776 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5777 | Result += "\tunsigned int size;\n"; |
| 5778 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5779 | Result += "};\n"; |
| 5780 | objc_category = true; |
| 5781 | } |
| 5782 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5783 | Result += FullCategoryName; |
| 5784 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5785 | Result += IDecl->getNameAsString(); |
| 5786 | Result += "\"\n\t, \""; |
| 5787 | Result += ClassDecl->getNameAsString(); |
| 5788 | Result += "\"\n"; |
| 5789 | |
| 5790 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5791 | Result += "\t, (struct _objc_method_list *)" |
| 5792 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5793 | Result += FullCategoryName; |
| 5794 | Result += "\n"; |
| 5795 | } |
| 5796 | else |
| 5797 | Result += "\t, 0\n"; |
| 5798 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5799 | Result += "\t, (struct _objc_method_list *)" |
| 5800 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5801 | Result += FullCategoryName; |
| 5802 | Result += "\n"; |
| 5803 | } |
| 5804 | else |
| 5805 | Result += "\t, 0\n"; |
| 5806 | |
| 5807 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5808 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5809 | Result += FullCategoryName; |
| 5810 | Result += "\n"; |
| 5811 | } |
| 5812 | else |
| 5813 | Result += "\t, 0\n"; |
| 5814 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5815 | } |
| 5816 | |
| 5817 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5818 | /// class methods. |
| 5819 | template<typename MethodIterator> |
| 5820 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5821 | MethodIterator MethodEnd, |
| 5822 | bool IsInstanceMethod, |
| 5823 | StringRef prefix, |
| 5824 | StringRef ClassName, |
| 5825 | std::string &Result) { |
| 5826 | if (MethodBegin == MethodEnd) return; |
| 5827 | |
| 5828 | if (!objc_impl_method) { |
| 5829 | /* struct _objc_method { |
| 5830 | SEL _cmd; |
| 5831 | char *method_types; |
| 5832 | void *_imp; |
| 5833 | } |
| 5834 | */ |
| 5835 | Result += "\nstruct _objc_method {\n"; |
| 5836 | Result += "\tSEL _cmd;\n"; |
| 5837 | Result += "\tchar *method_types;\n"; |
| 5838 | Result += "\tvoid *_imp;\n"; |
| 5839 | Result += "};\n"; |
| 5840 | |
| 5841 | objc_impl_method = true; |
| 5842 | } |
| 5843 | |
| 5844 | // Build _objc_method_list for class's methods if needed |
| 5845 | |
| 5846 | /* struct { |
| 5847 | struct _objc_method_list *next_method; |
| 5848 | int method_count; |
| 5849 | struct _objc_method method_list[]; |
| 5850 | } |
| 5851 | */ |
| 5852 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5853 | Result += "\nstatic struct {\n"; |
| 5854 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5855 | Result += "\tint method_count;\n"; |
| 5856 | Result += "\tstruct _objc_method method_list["; |
| 5857 | Result += utostr(NumMethods); |
| 5858 | Result += "];\n} _OBJC_"; |
| 5859 | Result += prefix; |
| 5860 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5861 | Result += "_METHODS_"; |
| 5862 | Result += ClassName; |
| 5863 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5864 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5865 | Result += "_meth\")))= "; |
| 5866 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5867 | |
| 5868 | Result += "\t,{{(SEL)\""; |
| 5869 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5870 | std::string MethodTypeString; |
| 5871 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5872 | Result += "\", \""; |
| 5873 | Result += MethodTypeString; |
| 5874 | Result += "\", (void *)"; |
| 5875 | Result += MethodInternalNames[*MethodBegin]; |
| 5876 | Result += "}\n"; |
| 5877 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5878 | Result += "\t ,{(SEL)\""; |
| 5879 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5880 | std::string MethodTypeString; |
| 5881 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5882 | Result += "\", \""; |
| 5883 | Result += MethodTypeString; |
| 5884 | Result += "\", (void *)"; |
| 5885 | Result += MethodInternalNames[*MethodBegin]; |
| 5886 | Result += "}\n"; |
| 5887 | } |
| 5888 | Result += "\t }\n};\n"; |
| 5889 | } |
| 5890 | |
| 5891 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5892 | SourceRange OldRange = IV->getSourceRange(); |
| 5893 | Expr *BaseExpr = IV->getBase(); |
| 5894 | |
| 5895 | // Rewrite the base, but without actually doing replaces. |
| 5896 | { |
| 5897 | DisableReplaceStmtScope S(*this); |
| 5898 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5899 | IV->setBase(BaseExpr); |
| 5900 | } |
| 5901 | |
| 5902 | ObjCIvarDecl *D = IV->getDecl(); |
| 5903 | |
| 5904 | Expr *Replacement = IV; |
| 5905 | if (CurMethodDef) { |
| 5906 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5907 | const ObjCInterfaceType *iFaceDecl = |
| 5908 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5909 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5910 | // lookup which class implements the instance variable. |
| 5911 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5912 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5913 | clsDeclared); |
| 5914 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5915 | |
| 5916 | // Synthesize an explicit cast to gain access to the ivar. |
| 5917 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5918 | RecName += "_IMPL"; |
| 5919 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5920 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5921 | SourceLocation(), SourceLocation(), |
| 5922 | II); |
| 5923 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5924 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5925 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5926 | CK_BitCast, |
| 5927 | IV->getBase()); |
| 5928 | // Don't forget the parens to enforce the proper binding. |
| 5929 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5930 | OldRange.getEnd(), |
| 5931 | castExpr); |
| 5932 | if (IV->isFreeIvar() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5933 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5934 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5935 | IV->getLocation(), |
| 5936 | D->getType(), |
| 5937 | VK_LValue, OK_Ordinary); |
| 5938 | Replacement = ME; |
| 5939 | } else { |
| 5940 | IV->setBase(PE); |
| 5941 | } |
| 5942 | } |
| 5943 | } else { // we are outside a method. |
| 5944 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5945 | |
| 5946 | // Explicit ivar refs need to have a cast inserted. |
| 5947 | // FIXME: consider sharing some of this code with the code above. |
| 5948 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5949 | const ObjCInterfaceType *iFaceDecl = |
| 5950 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5951 | // lookup which class implements the instance variable. |
| 5952 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5953 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5954 | clsDeclared); |
| 5955 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5956 | |
| 5957 | // Synthesize an explicit cast to gain access to the ivar. |
| 5958 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5959 | RecName += "_IMPL"; |
| 5960 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5961 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5962 | SourceLocation(), SourceLocation(), |
| 5963 | II); |
| 5964 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5965 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5966 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5967 | CK_BitCast, |
| 5968 | IV->getBase()); |
| 5969 | // Don't forget the parens to enforce the proper binding. |
| 5970 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 5971 | IV->getBase()->getLocEnd(), castExpr); |
| 5972 | // Cannot delete IV->getBase(), since PE points to it. |
| 5973 | // Replace the old base with the cast. This is important when doing |
| 5974 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 5975 | IV->setBase(PE); |
| 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 5980 | return Replacement; |
| 5981 | } |