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" |
| 26 | #include "llvm/ADT/OwningPtr.h" |
| 27 | #include "llvm/ADT/SmallPtrSet.h" |
| 28 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
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 | } |
| 504 | }; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 505 | |
| 506 | class RewriteObjCFragileABI : public RewriteObjC { |
| 507 | public: |
| 508 | |
| 509 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 510 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 511 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 512 | D, LOpts, |
| 513 | silenceMacroWarn) {} |
| 514 | |
| 515 | ~RewriteObjCFragileABI() {} |
| 516 | virtual void Initialize(ASTContext &context); |
| 517 | |
| 518 | // Rewriting metadata |
| 519 | template<typename MethodIterator> |
| 520 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 521 | MethodIterator MethodEnd, |
| 522 | bool IsInstanceMethod, |
| 523 | StringRef prefix, |
| 524 | StringRef ClassName, |
| 525 | std::string &Result); |
| 526 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 527 | StringRef prefix, |
| 528 | StringRef ClassName, |
| 529 | std::string &Result); |
| 530 | virtual void RewriteObjCProtocolListMetaData( |
| 531 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 532 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 533 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 534 | std::string &Result); |
| 535 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 536 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 537 | std::string &Result); |
| 538 | |
| 539 | // Rewriting ivar |
| 540 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 541 | std::string &Result); |
| 542 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 543 | }; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 547 | NamedDecl *D) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 548 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 549 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 550 | for (FunctionProtoType::param_type_iterator I = fproto->param_type_begin(), |
| 551 | E = fproto->param_type_end(); |
| 552 | I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 553 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 554 | // All the args are checked/rewritten. Don't call twice! |
| 555 | RewriteBlockPointerDecl(D); |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 562 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 563 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 564 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 567 | static bool IsHeaderFile(const std::string &Filename) { |
| 568 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 570 | if (DotPos == std::string::npos) { |
| 571 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 575 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 576 | // C header: .h |
| 577 | // C++ header: .hh or .H; |
| 578 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 581 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 582 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 583 | bool silenceMacroWarn) |
| 584 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 585 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 586 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 587 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 588 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 589 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 590 | DiagnosticsEngine::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 591 | "rewriter doesn't support user-specified control flow semantics " |
| 592 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 595 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 596 | raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 597 | DiagnosticsEngine &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 598 | const LangOptions &LOpts, |
| 599 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 600 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 602 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 603 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 604 | Context = &context; |
| 605 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 606 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 607 | MsgSendFunctionDecl = 0; |
| 608 | MsgSendSuperFunctionDecl = 0; |
| 609 | MsgSendStretFunctionDecl = 0; |
| 610 | MsgSendSuperStretFunctionDecl = 0; |
| 611 | MsgSendFpretFunctionDecl = 0; |
| 612 | GetClassFunctionDecl = 0; |
| 613 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 614 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 615 | SelGetUidFunctionDecl = 0; |
| 616 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 617 | ConstantStringClassReference = 0; |
| 618 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 619 | CurMethodDef = 0; |
| 620 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 621 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 622 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 623 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 624 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 625 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 626 | BcLabelCount = 0; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 627 | SuperConstructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 628 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 629 | PropParentMap = 0; |
| 630 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 631 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 632 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 634 | // Get the ID and start/end of the main file. |
| 635 | MainFileID = SM->getMainFileID(); |
| 636 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 637 | MainFileStart = MainBuf->getBufferStart(); |
| 638 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 640 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
| 644 | // Top Level Driver Code |
| 645 | //===----------------------------------------------------------------------===// |
| 646 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 647 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 648 | if (Diags.hasErrorOccurred()) |
| 649 | return; |
| 650 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 651 | // Two cases: either the decl could be in the main file, or it could be in a |
| 652 | // #included file. If the former, rewrite it now. If the later, check to see |
| 653 | // if we rewrote the #include/#import. |
| 654 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 655 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 657 | // If this is for a builtin, ignore it. |
| 658 | if (Loc.isInvalid()) return; |
| 659 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 660 | // Look for built-in declarations that we need to refer during the rewrite. |
| 661 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 662 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 663 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 664 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 665 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 666 | ConstantStringClassReference = FVD; |
| 667 | return; |
| 668 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 669 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 670 | if (ID->isThisDeclarationADefinition()) |
| 671 | RewriteInterfaceDecl(ID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 672 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 673 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 674 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 675 | if (PD->isThisDeclarationADefinition()) |
| 676 | RewriteProtocolDecl(PD); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 677 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 678 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 679 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 680 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 681 | DI != DIEnd; ) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 682 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 683 | if (!IFace->isThisDeclarationADefinition()) { |
| 684 | SmallVector<Decl *, 8> DG; |
| 685 | SourceLocation StartLoc = IFace->getLocStart(); |
| 686 | do { |
| 687 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 688 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 689 | StartLoc == (*DI)->getLocStart()) |
| 690 | DG.push_back(*DI); |
| 691 | else |
| 692 | break; |
| 693 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 694 | ++DI; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 695 | } while (DI != DIEnd); |
| 696 | RewriteForwardClassDecl(DG); |
| 697 | continue; |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 698 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 699 | } |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 700 | |
| 701 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 702 | if (!Proto->isThisDeclarationADefinition()) { |
| 703 | SmallVector<Decl *, 8> DG; |
| 704 | SourceLocation StartLoc = Proto->getLocStart(); |
| 705 | do { |
| 706 | if (isa<ObjCProtocolDecl>(*DI) && |
| 707 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 708 | StartLoc == (*DI)->getLocStart()) |
| 709 | DG.push_back(*DI); |
| 710 | else |
| 711 | break; |
| 712 | |
| 713 | ++DI; |
| 714 | } while (DI != DIEnd); |
| 715 | RewriteForwardProtocolDecl(DG); |
| 716 | continue; |
| 717 | } |
| 718 | } |
| 719 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 720 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 721 | ++DI; |
| 722 | } |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 724 | // 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] | 725 | if (SM->isWrittenInMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 726 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 729 | //===----------------------------------------------------------------------===// |
| 730 | // Syntactic (non-AST) Rewriting Code |
| 731 | //===----------------------------------------------------------------------===// |
| 732 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 733 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 734 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 735 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 736 | const char *MainBufStart = MainBuf.begin(); |
| 737 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 738 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 740 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 741 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 742 | if (*BufPtr == '#') { |
| 743 | if (++BufPtr == MainBufEnd) |
| 744 | return; |
| 745 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 746 | if (++BufPtr == MainBufEnd) |
| 747 | return; |
| 748 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 749 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 751 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 752 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 753 | BufPtr += ImportLen; |
| 754 | } |
| 755 | } |
| 756 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 759 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 760 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 761 | std::string S; |
| 762 | S = "((struct "; |
| 763 | S += ClassDecl->getIdentifier()->getName(); |
| 764 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 765 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | return S; |
| 767 | } |
| 768 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 769 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 770 | ObjCImplementationDecl *IMD, |
| 771 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 772 | static bool objcGetPropertyDefined = false; |
| 773 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 774 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 775 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 776 | const char *startBuf = SM->getCharacterData(startLoc); |
| 777 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 778 | const char *semiBuf = strchr(startBuf, ';'); |
| 779 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 780 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 781 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 782 | |
| 783 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 784 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 786 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 787 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 788 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 790 | if (!OID) |
| 791 | return; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 792 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 793 | if (!PD->getGetterMethodDecl()->isDefined()) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 794 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 795 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 796 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 797 | std::string Getr; |
| 798 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 799 | objcGetPropertyDefined = true; |
| 800 | // FIXME. Is this attribute correct in all cases? |
| 801 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 802 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 803 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 804 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 805 | PD->getGetterMethodDecl(), Getr); |
| 806 | Getr += "{ "; |
| 807 | // Synthesize an explicit cast to gain access to the ivar. |
| 808 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 809 | if (GenGetProperty) { |
| 810 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 811 | Getr += "typedef "; |
| 812 | const FunctionType *FPRetType = 0; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 813 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr, |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 814 | FPRetType); |
| 815 | Getr += " _TYPE"; |
| 816 | if (FPRetType) { |
| 817 | Getr += ")"; // close the precedence "scope" for "*". |
| 818 | |
| 819 | // Now, emit the argument types (if any). |
| 820 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 821 | Getr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 822 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 823 | if (i) Getr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 824 | std::string ParamStr = |
| 825 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 826 | Getr += ParamStr; |
| 827 | } |
| 828 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 829 | if (FT->getNumParams()) |
| 830 | Getr += ", "; |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 831 | Getr += "..."; |
| 832 | } |
| 833 | Getr += ")"; |
| 834 | } else |
| 835 | Getr += "()"; |
| 836 | } |
| 837 | Getr += ";\n"; |
| 838 | Getr += "return (_TYPE)"; |
| 839 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 840 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 841 | Getr += ", 1)"; |
| 842 | } |
| 843 | else |
| 844 | Getr += "return " + getIvarAccessString(OID); |
| 845 | Getr += "; }"; |
| 846 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 847 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 848 | |
| 849 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 850 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 852 | // Generate the 'setter' function. |
| 853 | std::string Setr; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 854 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 855 | ObjCPropertyDecl::OBJC_PR_copy); |
| 856 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 857 | objcSetPropertyDefined = true; |
| 858 | // FIXME. Is this attribute correct in all cases? |
| 859 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 860 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 861 | } |
| 862 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 863 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 864 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 865 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 866 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 867 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 868 | if (GenSetProperty) { |
| 869 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 870 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 871 | Setr += ", (id)"; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 872 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 873 | Setr += ", "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 874 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 875 | Setr += "0, "; |
| 876 | else |
| 877 | Setr += "1, "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 878 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 879 | Setr += "1)"; |
| 880 | else |
| 881 | Setr += "0)"; |
| 882 | } |
| 883 | else { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 884 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 885 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 886 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 887 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 888 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 889 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 890 | |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 891 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 892 | std::string &typedefString) { |
| 893 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 894 | typedefString += ForwardDecl->getNameAsString(); |
| 895 | typedefString += "\n"; |
| 896 | typedefString += "#define _REWRITER_typedef_"; |
| 897 | typedefString += ForwardDecl->getNameAsString(); |
| 898 | typedefString += "\n"; |
| 899 | typedefString += "typedef struct objc_object "; |
| 900 | typedefString += ForwardDecl->getNameAsString(); |
| 901 | typedefString += ";\n#endif\n"; |
| 902 | } |
| 903 | |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 904 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 905 | const std::string &typedefString) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 906 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 907 | const char *startBuf = SM->getCharacterData(startLoc); |
| 908 | const char *semiPtr = strchr(startBuf, ';'); |
| 909 | // Replace the @class with typedefs corresponding to the classes. |
| 910 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 911 | } |
| 912 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 913 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 914 | std::string typedefString; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 915 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 916 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 917 | if (I == D.begin()) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 918 | // Translate to typedef's that forward reference structs with the same name |
| 919 | // as the class. As a convenience, we include the original declaration |
| 920 | // as a comment. |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 921 | typedefString += "// @class "; |
| 922 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 923 | typedefString += ";\n"; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 924 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 925 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 926 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 927 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 928 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 931 | void RewriteObjC::RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &D) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 932 | std::string typedefString; |
| 933 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 934 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 935 | if (i == 0) { |
| 936 | typedefString += "// @class "; |
| 937 | typedefString += ForwardDecl->getNameAsString(); |
| 938 | typedefString += ";\n"; |
| 939 | } |
| 940 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 941 | } |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 942 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 945 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 946 | // When method is a synthesized one, such as a getter/setter there is |
| 947 | // nothing to rewrite. |
Fariborz Jahanian | e1378a4 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 948 | if (Method->isImplicit()) |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 949 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 950 | SourceLocation LocStart = Method->getLocStart(); |
| 951 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 953 | if (SM->getExpansionLineNumber(LocEnd) > |
| 954 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 955 | InsertText(LocStart, "#if 0\n"); |
| 956 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 957 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 958 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 963 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 965 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 966 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 969 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 970 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 972 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 973 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 975 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 976 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 977 | RewriteProperty(*I); |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 978 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | for (ObjCCategoryDecl::instmeth_iterator |
| 980 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 981 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 982 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 984 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 985 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 986 | RewriteMethodDeclaration(*I); |
| 987 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 988 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 989 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 990 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 993 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 994 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 995 | assert(PDecl->isThisDeclarationADefinition()); |
| 996 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 997 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 998 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
| 1000 | for (ObjCProtocolDecl::instmeth_iterator |
| 1001 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1002 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1003 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1004 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1005 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1006 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1007 | RewriteMethodDeclaration(*I); |
| 1008 | |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1009 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1010 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1011 | RewriteProperty(*I); |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1012 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1013 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1014 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1015 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1016 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1017 | // Must comment out @optional/@required |
| 1018 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1019 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1020 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1021 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1022 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1023 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1025 | } |
| 1026 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1027 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1028 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1034 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1035 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1036 | if (LocStart.isInvalid()) |
| 1037 | llvm_unreachable("Invalid SourceLocation"); |
| 1038 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1039 | ReplaceText(LocStart, 0, "// "); |
| 1040 | } |
| 1041 | |
| 1042 | void |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1043 | RewriteObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1044 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1045 | if (LocStart.isInvalid()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1046 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1047 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1048 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1051 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1052 | const FunctionType *&FPRetType) { |
| 1053 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1054 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1055 | else if (T->isFunctionPointerType() || |
| 1056 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1057 | // needs special handling, since pointer-to-functions have special |
| 1058 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1059 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1060 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1061 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1062 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1063 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1064 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1065 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1066 | ResultStr += |
| 1067 | FPRetType->getReturnType().getAsString(Context->getPrintingPolicy()); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1068 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1071 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1074 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1075 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1076 | std::string &ResultStr) { |
| 1077 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1078 | const FunctionType *FPRetType = 0; |
| 1079 | ResultStr += "\nstatic "; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1080 | RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1081 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1083 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1084 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1086 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1087 | NameStr += "_I_"; |
| 1088 | else |
| 1089 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1091 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1092 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
| 1094 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1095 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1096 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1097 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1098 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1100 | { |
| 1101 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1102 | int len = selString.size(); |
| 1103 | for (int i = 0; i < len; i++) |
| 1104 | if (selString[i] == ':') |
| 1105 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1106 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1107 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1108 | // Remember this name for metadata emission |
| 1109 | MethodInternalNames[OMD] = NameStr; |
| 1110 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1112 | // Rewrite arguments |
| 1113 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1115 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1116 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1117 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1118 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1119 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1120 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1121 | ResultStr += "struct "; |
| 1122 | } |
| 1123 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1124 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1125 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1126 | } |
| 1127 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1128 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1129 | Context->getPrintingPolicy()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1131 | ResultStr += " self, "; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1132 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1133 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1135 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1136 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1137 | E = OMD->param_end(); PI != E; ++PI) { |
| 1138 | ParmVarDecl *PDecl = *PI; |
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 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1185 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1186 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1187 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1188 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1189 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1190 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1191 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1192 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1193 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1194 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1195 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1196 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1197 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1198 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1200 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1201 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1202 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1203 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1204 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1205 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1206 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1207 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1208 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1210 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1211 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1212 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1213 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1214 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1215 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1217 | I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1218 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1221 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1224 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1225 | std::string ResultStr; |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1226 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1227 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1228 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1229 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1230 | ResultStr += "\n"; |
| 1231 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1232 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1233 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1234 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1235 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1236 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1237 | // Mark this typedef as having been generated. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1238 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1239 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1240 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
| 1242 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1243 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1244 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1246 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1247 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1248 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1250 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1251 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1252 | RewriteMethodDeclaration(*I); |
| 1253 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1254 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1255 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1256 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1259 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1260 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1262 | // We just magically know some things about the structure of this |
| 1263 | // expression. |
| 1264 | ObjCMessageExpr *OldMsg = |
| 1265 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1266 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1268 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1269 | // we need to suppress rewriting the sub-statements. |
| 1270 | Expr *Base, *RHS; |
| 1271 | { |
| 1272 | DisableReplaceStmtScope S(*this); |
| 1273 | |
| 1274 | // Rebuild the base expression if we have one. |
| 1275 | Base = 0; |
| 1276 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1277 | Base = OldMsg->getInstanceReceiver(); |
| 1278 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1279 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1280 | } |
| 1281 | |
| 1282 | // Rebuild the RHS. |
| 1283 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1284 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1285 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1286 | } |
| 1287 | |
| 1288 | // TODO: avoid this copy. |
| 1289 | SmallVector<SourceLocation, 1> SelLocs; |
| 1290 | OldMsg->getSelectorLocs(SelLocs); |
| 1291 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1292 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1293 | switch (OldMsg->getReceiverKind()) { |
| 1294 | case ObjCMessageExpr::Class: |
| 1295 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1296 | OldMsg->getValueKind(), |
| 1297 | OldMsg->getLeftLoc(), |
| 1298 | OldMsg->getClassReceiverTypeInfo(), |
| 1299 | OldMsg->getSelector(), |
| 1300 | SelLocs, |
| 1301 | OldMsg->getMethodDecl(), |
| 1302 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1303 | OldMsg->getRightLoc(), |
| 1304 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | |
| 1307 | case ObjCMessageExpr::Instance: |
| 1308 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1309 | OldMsg->getValueKind(), |
| 1310 | OldMsg->getLeftLoc(), |
| 1311 | Base, |
| 1312 | OldMsg->getSelector(), |
| 1313 | SelLocs, |
| 1314 | OldMsg->getMethodDecl(), |
| 1315 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1316 | OldMsg->getRightLoc(), |
| 1317 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | |
| 1320 | case ObjCMessageExpr::SuperClass: |
| 1321 | case ObjCMessageExpr::SuperInstance: |
| 1322 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1323 | OldMsg->getValueKind(), |
| 1324 | OldMsg->getLeftLoc(), |
| 1325 | OldMsg->getSuperLoc(), |
| 1326 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1327 | OldMsg->getSuperType(), |
| 1328 | OldMsg->getSelector(), |
| 1329 | SelLocs, |
| 1330 | OldMsg->getMethodDecl(), |
| 1331 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1332 | OldMsg->getRightLoc(), |
| 1333 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1338 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1339 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1342 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1343 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1344 | |
| 1345 | // We just magically know some things about the structure of this |
| 1346 | // expression. |
| 1347 | ObjCMessageExpr *OldMsg = |
| 1348 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1349 | |
| 1350 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1351 | // we need to suppress rewriting the sub-statements. |
| 1352 | Expr *Base = 0; |
| 1353 | { |
| 1354 | DisableReplaceStmtScope S(*this); |
| 1355 | |
| 1356 | // Rebuild the base expression if we have one. |
| 1357 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1358 | Base = OldMsg->getInstanceReceiver(); |
| 1359 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1360 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1361 | } |
Fariborz Jahanian | bb40ea4 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1362 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1363 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1364 | // Intentionally empty. |
| 1365 | SmallVector<SourceLocation, 1> SelLocs; |
| 1366 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1367 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1368 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1369 | switch (OldMsg->getReceiverKind()) { |
| 1370 | case ObjCMessageExpr::Class: |
| 1371 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1372 | OldMsg->getValueKind(), |
| 1373 | OldMsg->getLeftLoc(), |
| 1374 | OldMsg->getClassReceiverTypeInfo(), |
| 1375 | OldMsg->getSelector(), |
| 1376 | SelLocs, |
| 1377 | OldMsg->getMethodDecl(), |
| 1378 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1379 | OldMsg->getRightLoc(), |
| 1380 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1381 | break; |
| 1382 | |
| 1383 | case ObjCMessageExpr::Instance: |
| 1384 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1385 | OldMsg->getValueKind(), |
| 1386 | OldMsg->getLeftLoc(), |
| 1387 | Base, |
| 1388 | OldMsg->getSelector(), |
| 1389 | SelLocs, |
| 1390 | OldMsg->getMethodDecl(), |
| 1391 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1392 | OldMsg->getRightLoc(), |
| 1393 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | |
| 1396 | case ObjCMessageExpr::SuperClass: |
| 1397 | case ObjCMessageExpr::SuperInstance: |
| 1398 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1399 | OldMsg->getValueKind(), |
| 1400 | OldMsg->getLeftLoc(), |
| 1401 | OldMsg->getSuperLoc(), |
| 1402 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1403 | OldMsg->getSuperType(), |
| 1404 | OldMsg->getSelector(), |
| 1405 | SelLocs, |
| 1406 | OldMsg->getMethodDecl(), |
| 1407 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1408 | OldMsg->getRightLoc(), |
| 1409 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1410 | break; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1411 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1412 | |
| 1413 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1414 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1415 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1418 | /// SynthCountByEnumWithState - To print: |
| 1419 | /// ((unsigned int (*) |
| 1420 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | /// "countByEnumeratingWithState:objects:count:"), |
| 1424 | /// &enumState, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1425 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1426 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1427 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1428 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1429 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1430 | buf += "\n\t\t"; |
| 1431 | buf += "((id)l_collection,\n\t\t"; |
| 1432 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1433 | buf += "\n\t\t"; |
| 1434 | buf += "&enumState, " |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1435 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1436 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1437 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1438 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1439 | /// statement to exit to its outer synthesized loop. |
| 1440 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1441 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1442 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1443 | return S; |
| 1444 | // replace break with goto __break_label |
| 1445 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1447 | SourceLocation startLoc = S->getLocStart(); |
| 1448 | buf = "goto __break_label_"; |
| 1449 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1450 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1456 | /// statement to continue with its inner synthesized loop. |
| 1457 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1458 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1459 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1460 | return S; |
| 1461 | // replace continue with goto __continue_label |
| 1462 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1464 | SourceLocation startLoc = S->getLocStart(); |
| 1465 | buf = "goto __continue_label_"; |
| 1466 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1467 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1469 | return 0; |
| 1470 | } |
| 1471 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1472 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1473 | /// It rewrites: |
| 1474 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1476 | /// Into: |
| 1477 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1479 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1480 | /// id __rw_items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1481 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1483 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1484 | /// if (limit) { |
| 1485 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1486 | /// do { |
| 1487 | /// unsigned long counter = 0; |
| 1488 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1490 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1491 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1492 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1493 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1494 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1496 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1497 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1498 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1499 | /// } |
| 1500 | /// else |
| 1501 | /// elem = nil; |
| 1502 | /// } |
| 1503 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1504 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1505 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1506 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1508 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1510 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1512 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1513 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1514 | StringRef elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1515 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1516 | std::string buf; |
| 1517 | buf = "\n{\n\t"; |
| 1518 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1519 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1520 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1521 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1522 | if (ElementType->isObjCQualifiedIdType() || |
| 1523 | ElementType->isObjCQualifiedInterfaceType()) |
| 1524 | // Simply use 'id' for all qualified types. |
| 1525 | elementTypeAsString = "id"; |
| 1526 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1527 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1528 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1529 | buf += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1530 | elementName = D->getName(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1531 | buf += elementName; |
| 1532 | buf += ";\n\t"; |
| 1533 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1534 | else { |
| 1535 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1536 | elementName = DR->getDecl()->getName(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1537 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1538 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1539 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1540 | // Simply use 'id' for all qualified types. |
| 1541 | elementTypeAsString = "id"; |
| 1542 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1543 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1546 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1547 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1548 | // id __rw_items[16]; |
| 1549 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1550 | // id l_collection = (id) |
| 1551 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1552 | // Find start location of 'collection' the hard way! |
| 1553 | const char *startCollectionBuf = startBuf; |
| 1554 | startCollectionBuf += 3; // skip 'for' |
| 1555 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1556 | startCollectionBuf++; // skip '(' |
| 1557 | // find 'in' and skip it. |
| 1558 | while (*startCollectionBuf != ' ' || |
| 1559 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1560 | (*(startCollectionBuf+3) != ' ' && |
| 1561 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1562 | startCollectionBuf++; |
| 1563 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1566 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1567 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1568 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1569 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1570 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1571 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1573 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1574 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1575 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1577 | // ((unsigned int (*) |
| 1578 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1580 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | // "countByEnumeratingWithState:objects:count:"), |
| 1582 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1583 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1584 | buf += "unsigned long limit =\n\t\t"; |
| 1585 | SynthCountByEnumWithState(buf); |
| 1586 | buf += ";\n\t"; |
| 1587 | /// if (limit) { |
| 1588 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1589 | /// do { |
| 1590 | /// unsigned long counter = 0; |
| 1591 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1593 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1594 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1595 | buf += "if (limit) {\n\t"; |
| 1596 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1597 | buf += "do {\n\t\t"; |
| 1598 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1599 | buf += "do {\n\t\t\t"; |
| 1600 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1601 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1602 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1603 | buf += " = ("; |
| 1604 | buf += elementTypeAsString; |
| 1605 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1606 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1607 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1609 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1610 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1612 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1613 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1614 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1615 | /// } |
| 1616 | /// else |
| 1617 | /// elem = nil; |
| 1618 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1620 | buf = ";\n\t"; |
| 1621 | buf += "__continue_label_"; |
| 1622 | buf += utostr(ObjCBcLabelNo.back()); |
| 1623 | buf += ": ;"; |
| 1624 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1625 | buf += "} while (counter < limit);\n\t"; |
| 1626 | buf += "} while (limit = "; |
| 1627 | SynthCountByEnumWithState(buf); |
| 1628 | buf += ");\n\t"; |
| 1629 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1630 | buf += " = (("; |
| 1631 | buf += elementTypeAsString; |
| 1632 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1633 | buf += "__break_label_"; |
| 1634 | buf += utostr(ObjCBcLabelNo.back()); |
| 1635 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1636 | buf += "}\n\t"; |
| 1637 | buf += "else\n\t\t"; |
| 1638 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1639 | buf += " = (("; |
| 1640 | buf += elementTypeAsString; |
| 1641 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1642 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1644 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1645 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1646 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1647 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1648 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1649 | } else { |
| 1650 | /* Need to treat single statements specially. For example: |
| 1651 | * |
| 1652 | * for (A *a in b) if (stuff()) break; |
| 1653 | * for (A *a in b) xxxyy; |
| 1654 | * |
| 1655 | * The following code simply scans ahead to the semi to find the actual end. |
| 1656 | */ |
| 1657 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1658 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1659 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1660 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1661 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1662 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1663 | Stmts.pop_back(); |
| 1664 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1665 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1669 | /// This routine rewrites @synchronized(expr) stmt; |
| 1670 | /// into: |
| 1671 | /// objc_sync_enter(expr); |
| 1672 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1673 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1674 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1675 | // Get the start location and compute the semi location. |
| 1676 | SourceLocation startLoc = S->getLocStart(); |
| 1677 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1679 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
| 1681 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1682 | buf = "objc_sync_enter((id)"; |
| 1683 | const char *lparenBuf = startBuf; |
| 1684 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1685 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1687 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1688 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1689 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1690 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1691 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1692 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1693 | buf = ");\n"; |
| 1694 | // declare a new scope with two variables, _stack and _rethrow. |
| 1695 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1696 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1697 | buf += "char *pointers[4];} _stack;\n"; |
| 1698 | buf += "id volatile _rethrow = 0;\n"; |
| 1699 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1700 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1701 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1702 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1703 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1705 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1706 | SourceLocation lastCurlyLoc = startLoc; |
| 1707 | buf = "}\nelse {\n"; |
| 1708 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1709 | buf += "}\n"; |
| 1710 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1711 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1712 | |
| 1713 | std::string syncBuf; |
| 1714 | syncBuf += " objc_sync_exit("; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1715 | |
| 1716 | Expr *syncExpr = S->getSynchExpr(); |
| 1717 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1718 | ? CK_BitCast : |
| 1719 | syncExpr->getType()->isBlockPointerType() |
| 1720 | ? CK_BlockPointerToObjCPointerCast |
| 1721 | : CK_CPointerToObjCPointerCast; |
| 1722 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1723 | CK, syncExpr); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1724 | std::string syncExprBufS; |
| 1725 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1726 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1727 | syncBuf += syncExprBuf.str(); |
| 1728 | syncBuf += ");"; |
| 1729 | |
| 1730 | buf += syncBuf; |
| 1731 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1732 | buf += "}\n"; |
| 1733 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1735 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1736 | |
| 1737 | bool hasReturns = false; |
| 1738 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1739 | if (hasReturns) |
| 1740 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1741 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1745 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1746 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1747 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1748 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1749 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1750 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1751 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1752 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1754 | TryFinallyContainsReturnDiag); |
| 1755 | } |
| 1756 | return; |
| 1757 | } |
| 1758 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1759 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1760 | { |
| 1761 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1762 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1763 | if (*CI) |
| 1764 | HasReturnStmts(*CI, hasReturns); |
| 1765 | |
| 1766 | if (isa<ReturnStmt>(S)) |
| 1767 | hasReturns = true; |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1772 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1773 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1774 | if (*CI) { |
| 1775 | RewriteTryReturnStmts(*CI); |
| 1776 | } |
| 1777 | if (isa<ReturnStmt>(S)) { |
| 1778 | SourceLocation startLoc = S->getLocStart(); |
| 1779 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1780 | |
| 1781 | const char *semiBuf = strchr(startBuf, ';'); |
| 1782 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1783 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1784 | |
| 1785 | std::string buf; |
| 1786 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1787 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1788 | ReplaceText(startLoc, 6, buf); |
| 1789 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1790 | } |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1795 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1796 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1797 | if (*CI) { |
| 1798 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1799 | } |
| 1800 | if (isa<ReturnStmt>(S)) { |
| 1801 | SourceLocation startLoc = S->getLocStart(); |
| 1802 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1803 | |
| 1804 | const char *semiBuf = strchr(startBuf, ';'); |
| 1805 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1806 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1807 | |
| 1808 | std::string buf; |
| 1809 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1810 | buf += syncExitBuf; |
| 1811 | buf += " return"; |
| 1812 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1813 | ReplaceText(startLoc, 6, buf); |
| 1814 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1815 | } |
| 1816 | return; |
| 1817 | } |
| 1818 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1819 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1820 | // Get the start location and compute the semi location. |
| 1821 | SourceLocation startLoc = S->getLocStart(); |
| 1822 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1824 | assert((*startBuf == '@') && "bogus @try location"); |
| 1825 | |
| 1826 | std::string buf; |
| 1827 | // declare a new scope with two variables, _stack and _rethrow. |
| 1828 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1829 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1830 | buf += "char *pointers[4];} _stack;\n"; |
| 1831 | buf += "id volatile _rethrow = 0;\n"; |
| 1832 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1833 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1834 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1835 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1837 | startLoc = S->getTryBody()->getLocEnd(); |
| 1838 | startBuf = SM->getCharacterData(startLoc); |
| 1839 | |
| 1840 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1842 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1843 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1844 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1845 | buf = " /* @catch begin */ else {\n"; |
| 1846 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1847 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1848 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1849 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1850 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1852 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1853 | } else { /* no catch list */ |
| 1854 | buf = "}\nelse {\n"; |
| 1855 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1856 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1857 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1858 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1859 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1860 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1861 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1862 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1864 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1865 | buf = "if ("; // we are generating code for the first catch clause |
| 1866 | else |
| 1867 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1868 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1869 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1871 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1873 | const char *lParenLoc = strchr(startBuf, '('); |
| 1874 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1875 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1876 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1877 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1878 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1879 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1880 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1881 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1882 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1884 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1885 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1886 | } else if (catchDecl) { |
| 1887 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1888 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1889 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1890 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1891 | } else if (const ObjCObjectPointerType *Ptr = |
| 1892 | t->getAs<ObjCObjectPointerType>()) { |
| 1893 | // Should be a pointer to a class. |
| 1894 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1895 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1896 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1897 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1898 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1899 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1903 | lastCatchBody = Catch->getCatchBody(); |
| 1904 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1905 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1906 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1907 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1908 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1909 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1912 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1913 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1914 | } else { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1915 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1916 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1917 | } |
| 1918 | // Complete the catch list... |
| 1919 | if (lastCatchBody) { |
| 1920 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1921 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1922 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1924 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1925 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1926 | buf = "} /* last catch end */\n"; |
| 1927 | buf += "else {\n"; |
| 1928 | buf += " _rethrow = _caught;\n"; |
| 1929 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1930 | buf += "} } /* @catch end */\n"; |
| 1931 | if (!S->getFinallyStmt()) |
| 1932 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1933 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1935 | // Set lastCurlyLoc |
| 1936 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1937 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1938 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1939 | startLoc = finalStmt->getLocStart(); |
| 1940 | startBuf = SM->getCharacterData(startLoc); |
| 1941 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1943 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1945 | Stmt *body = finalStmt->getFinallyBody(); |
| 1946 | SourceLocation startLoc = body->getLocStart(); |
| 1947 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1948 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1949 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1951 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1953 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1954 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1955 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1956 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1958 | // Set lastCurlyLoc |
| 1959 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1961 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1962 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1963 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1964 | buf = "{ /* implicit finally clause */\n"; |
| 1965 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1966 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1967 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1968 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Now check for any return/continue/go statements within the @try. |
| 1971 | // The implicit finally clause won't called if the @try contains any |
| 1972 | // jump statements. |
| 1973 | bool hasReturns = false; |
| 1974 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1975 | if (hasReturns) |
| 1976 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1977 | } |
| 1978 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1979 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1980 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1981 | return 0; |
| 1982 | } |
| 1983 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1985 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1986 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1987 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1988 | // Get the start location and compute the semi location. |
| 1989 | SourceLocation startLoc = S->getLocStart(); |
| 1990 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1992 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1993 | |
| 1994 | std::string buf; |
| 1995 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1996 | if (S->getThrowExpr()) |
| 1997 | buf = "objc_exception_throw("; |
| 1998 | else // add an implicit argument |
| 1999 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2001 | // handle "@ throw" correctly. |
| 2002 | const char *wBuf = strchr(startBuf, 'w'); |
| 2003 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2004 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2006 | const char *semiBuf = strchr(startBuf, ';'); |
| 2007 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2008 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2009 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2010 | return 0; |
| 2011 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2012 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2013 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2014 | // Create a new string expression. |
| 2015 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2016 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2017 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2018 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2019 | StringLiteral::Ascii, false, |
| 2020 | StrType, SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2021 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2023 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2024 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2025 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2028 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2029 | if (!SelGetUidFunctionDecl) |
| 2030 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2031 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2032 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2033 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2034 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2036 | Exp->getSelector().getAsString(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2037 | StringLiteral::Ascii, false, |
| 2038 | argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2039 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2040 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2041 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2042 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2043 | return SelExp; |
| 2044 | } |
| 2045 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2046 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2047 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2048 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2049 | // 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] | 2050 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2052 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2053 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2054 | VK_LValue, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2056 | // 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] | 2057 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2058 | ImplicitCastExpr *ICE = |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2059 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2060 | DRE, 0, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2062 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2064 | CallExpr *Exp = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2065 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2066 | FT->getCallResultType(*Context), |
| 2067 | VK_RValue, EndLoc); |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2068 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2071 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2072 | const char *&startRef, const char *&endRef) { |
| 2073 | while (startBuf < endBuf) { |
| 2074 | if (*startBuf == '<') |
| 2075 | startRef = startBuf; // mark the start. |
| 2076 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2077 | if (startRef && *startRef == '<') { |
| 2078 | endRef = startBuf; // mark the end. |
| 2079 | return true; |
| 2080 | } |
| 2081 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2082 | } |
| 2083 | startBuf++; |
| 2084 | } |
| 2085 | return false; |
| 2086 | } |
| 2087 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2088 | static void scanToNextArgument(const char *&argRef) { |
| 2089 | int angle = 0; |
| 2090 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2091 | if (*argRef == '<') |
| 2092 | angle++; |
| 2093 | else if (*argRef == '>') |
| 2094 | angle--; |
| 2095 | argRef++; |
| 2096 | } |
| 2097 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2098 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2099 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2100 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2101 | if (T->isObjCQualifiedIdType()) |
| 2102 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2103 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2104 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2105 | return true; |
| 2106 | } |
| 2107 | if (T->isObjCObjectPointerType()) { |
| 2108 | T = T->getPointeeType(); |
| 2109 | return T->isObjCQualifiedInterfaceType(); |
| 2110 | } |
Fariborz Jahanian | 7bf13c4 | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2111 | if (T->isArrayType()) { |
| 2112 | QualType ElemTy = Context->getBaseElementType(T); |
| 2113 | return needToScanForQualifiers(ElemTy); |
| 2114 | } |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2115 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2118 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2119 | QualType Type = E->getType(); |
| 2120 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2121 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2123 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2124 | Loc = ECE->getLParenLoc(); |
| 2125 | EndLoc = ECE->getRParenLoc(); |
| 2126 | } else { |
| 2127 | Loc = E->getLocStart(); |
| 2128 | EndLoc = E->getLocEnd(); |
| 2129 | } |
| 2130 | // This will defend against trying to rewrite synthesized expressions. |
| 2131 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2132 | return; |
| 2133 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2134 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2135 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2136 | const char *startRef = 0, *endRef = 0; |
| 2137 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2138 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2139 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2140 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2141 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2142 | InsertText(LessLoc, "/*"); |
| 2143 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2148 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2149 | SourceLocation Loc; |
| 2150 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2151 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2152 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2153 | Loc = VD->getLocation(); |
| 2154 | Type = VD->getType(); |
| 2155 | } |
| 2156 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2157 | Loc = FD->getLocation(); |
| 2158 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2159 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2160 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2161 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2162 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2163 | if (!proto) |
| 2164 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2165 | Type = proto->getReturnType(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2166 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2167 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2168 | Loc = FD->getLocation(); |
| 2169 | Type = FD->getType(); |
| 2170 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2171 | else |
| 2172 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2174 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2175 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2177 | const char *endBuf = SM->getCharacterData(Loc); |
| 2178 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2179 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2180 | startBuf--; // scan backward (from the decl location) for return type. |
| 2181 | const char *startRef = 0, *endRef = 0; |
| 2182 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2183 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2184 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2185 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2186 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2187 | InsertText(LessLoc, "/*"); |
| 2188 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2191 | if (!proto) |
| 2192 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2193 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2194 | const char *startBuf = SM->getCharacterData(Loc); |
| 2195 | const char *startFuncBuf = startBuf; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2196 | for (unsigned i = 0; i < proto->getNumParams(); i++) { |
| 2197 | if (needToScanForQualifiers(proto->getParamType(i))) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2198 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2200 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2201 | // scan forward (from the decl location) for argument types. |
| 2202 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2203 | const char *startRef = 0, *endRef = 0; |
| 2204 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2205 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2207 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2209 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2210 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2211 | InsertText(LessLoc, "/*"); |
| 2212 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2213 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2214 | startBuf = ++endBuf; |
| 2215 | } |
| 2216 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2217 | // If the function name is derived from a macro expansion, then the |
| 2218 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2219 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2220 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2221 | startBuf++; |
| 2222 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2223 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2226 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2227 | QualType QT = ND->getType(); |
| 2228 | const Type* TypePtr = QT->getAs<Type>(); |
| 2229 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2230 | return; |
| 2231 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2232 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2233 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2234 | TypePtr = QT->getAs<Type>(); |
| 2235 | } |
| 2236 | // FIXME. This will not work for multiple declarators; as in: |
| 2237 | // __typeof__(a) b,c,d; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2238 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2239 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2240 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2241 | if (ND->getInit()) { |
| 2242 | std::string Name(ND->getNameAsString()); |
| 2243 | TypeAsString += " " + Name + " = "; |
| 2244 | Expr *E = ND->getInit(); |
| 2245 | SourceLocation startLoc; |
| 2246 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2247 | startLoc = ECE->getLParenLoc(); |
| 2248 | else |
| 2249 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2250 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2251 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2252 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2253 | } |
| 2254 | else { |
| 2255 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2256 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2257 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2258 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2262 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2263 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2264 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2265 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2266 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2267 | QualType getFuncType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2268 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2269 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2270 | SourceLocation(), |
| 2271 | SourceLocation(), |
| 2272 | SelGetUidIdent, getFuncType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2273 | SC_Extern); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2276 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2277 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2278 | if (FD->getIdentifier() && |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2279 | FD->getName() == "sel_registerName") { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2280 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2281 | return; |
| 2282 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2283 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2286 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2287 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2288 | const char *argPtr = TypeString.c_str(); |
| 2289 | if (!strchr(argPtr, '^')) { |
| 2290 | Str += TypeString; |
| 2291 | return; |
| 2292 | } |
| 2293 | while (*argPtr) { |
| 2294 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2295 | argPtr++; |
| 2296 | } |
| 2297 | } |
| 2298 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2299 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2300 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2301 | ValueDecl *VD) { |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2302 | QualType Type = VD->getType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2303 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2304 | const char *argPtr = TypeString.c_str(); |
| 2305 | int paren = 0; |
| 2306 | while (*argPtr) { |
| 2307 | switch (*argPtr) { |
| 2308 | case '(': |
| 2309 | Str += *argPtr; |
| 2310 | paren++; |
| 2311 | break; |
| 2312 | case ')': |
| 2313 | Str += *argPtr; |
| 2314 | paren--; |
| 2315 | break; |
| 2316 | case '^': |
| 2317 | Str += '*'; |
| 2318 | if (paren == 1) |
| 2319 | Str += VD->getNameAsString(); |
| 2320 | break; |
| 2321 | default: |
| 2322 | Str += *argPtr; |
| 2323 | break; |
| 2324 | } |
| 2325 | argPtr++; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2330 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2331 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2332 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2333 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2334 | if (!proto) |
| 2335 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2336 | QualType Type = proto->getReturnType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2337 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2338 | FdStr += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2339 | FdStr += FD->getName(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2340 | FdStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2341 | unsigned numArgs = proto->getNumParams(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2342 | for (unsigned i = 0; i < numArgs; i++) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2343 | QualType ArgType = proto->getParamType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2344 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2345 | if (i+1 < numArgs) |
| 2346 | FdStr += ", "; |
| 2347 | } |
| 2348 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2349 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2350 | CurFunctionDeclToDeclareForBlock = 0; |
| 2351 | } |
| 2352 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2353 | // SynthSuperConstructorFunctionDecl - id objc_super(id obj, id super); |
| 2354 | void RewriteObjC::SynthSuperConstructorFunctionDecl() { |
| 2355 | if (SuperConstructorFunctionDecl) |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2356 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2357 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2358 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2359 | QualType argT = Context->getObjCIdType(); |
| 2360 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2361 | ArgTys.push_back(argT); |
| 2362 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2363 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2364 | ArgTys); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2365 | SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2366 | SourceLocation(), |
| 2367 | SourceLocation(), |
| 2368 | msgSendIdent, msgSendType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2369 | 0, SC_Extern); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2372 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2373 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2374 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2375 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2376 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2377 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2378 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2379 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2380 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2381 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2382 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2383 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2384 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2385 | SourceLocation(), |
| 2386 | SourceLocation(), |
| 2387 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2388 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2391 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2392 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2394 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2395 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2396 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2397 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2398 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2399 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2400 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2401 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2402 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2403 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2404 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2405 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2406 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2407 | SourceLocation(), |
| 2408 | SourceLocation(), |
| 2409 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2410 | SC_Extern); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2413 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2414 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2415 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2416 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2417 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2418 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2419 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2420 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2421 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2422 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2423 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2424 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2425 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2426 | SourceLocation(), |
| 2427 | SourceLocation(), |
| 2428 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2429 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2433 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2434 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2435 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2436 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2437 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2438 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2439 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2440 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2441 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2442 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2443 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2444 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2445 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2446 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2447 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2448 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2449 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2451 | SourceLocation(), |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2452 | msgSendIdent, |
| 2453 | msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2454 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2457 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2458 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2459 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2460 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2461 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2462 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2463 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2464 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2465 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2466 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2467 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2468 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2469 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2470 | SourceLocation(), |
| 2471 | SourceLocation(), |
| 2472 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2473 | SC_Extern); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2476 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2477 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2478 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2479 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2480 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2481 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2482 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2483 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2484 | SourceLocation(), |
| 2485 | SourceLocation(), |
| 2486 | getClassIdent, getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2487 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2490 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2491 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2492 | IdentifierInfo *getSuperClassIdent = |
| 2493 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2494 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2495 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2496 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2497 | ArgTys); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2498 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2499 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2500 | SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2501 | getSuperClassIdent, |
| 2502 | getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2503 | SC_Extern); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2506 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2507 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2508 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2509 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2510 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2511 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2512 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2513 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2514 | SourceLocation(), |
| 2515 | SourceLocation(), |
| 2516 | getClassIdent, getClassType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2517 | 0, SC_Extern); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2520 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2521 | QualType strType = getConstantStringStructType(); |
| 2522 | |
| 2523 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2524 | |
| 2525 | std::string tmpName = InFileName; |
| 2526 | unsigned i; |
| 2527 | for (i=0; i < tmpName.length(); i++) { |
| 2528 | char c = tmpName.at(i); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2529 | // replace any non-alphanumeric characters with '_'. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2530 | if (!isAlphanumeric(c)) |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2531 | tmpName[i] = '_'; |
| 2532 | } |
| 2533 | S += tmpName; |
| 2534 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2535 | S += utostr(NumObjCStringLiterals++); |
| 2536 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2537 | Preamble += "static __NSConstantStringImpl " + S; |
| 2538 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2539 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2540 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2541 | std::string prettyBufS; |
| 2542 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2543 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2544 | Preamble += prettyBuf.str(); |
| 2545 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2546 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
| 2548 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2549 | SourceLocation(), &Context->Idents.get(S), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2550 | strType, 0, SC_Static); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2551 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2552 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2553 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2555 | VK_RValue, OK_Ordinary, |
| 2556 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2557 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2558 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2559 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2560 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2561 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2562 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2565 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2566 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2567 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2568 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2569 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2570 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2571 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2573 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2575 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2577 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2578 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2579 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2581 | SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2583 | FieldTypes[i], 0, |
| 2584 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2585 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2586 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2587 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2589 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2590 | } |
| 2591 | return Context->getTagDeclType(SuperStructDecl); |
| 2592 | } |
| 2593 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2594 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2595 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2596 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2597 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2598 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2599 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2601 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2603 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2605 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2607 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2609 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2610 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2611 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2613 | ConstantStringDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2614 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2615 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2616 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2617 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2618 | /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2619 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2622 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2623 | } |
| 2624 | return Context->getTagDeclType(ConstantStringDecl); |
| 2625 | } |
| 2626 | |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2627 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2628 | QualType msgSendType, |
| 2629 | QualType returnType, |
| 2630 | SmallVectorImpl<QualType> &ArgTypes, |
| 2631 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2632 | ObjCMethodDecl *Method) { |
| 2633 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2634 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2635 | false, msgSendType, |
| 2636 | VK_LValue, SourceLocation()); |
| 2637 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2638 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2639 | Context->getPointerType(Context->VoidTy), |
| 2640 | CK_BitCast, STDRE); |
| 2641 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2642 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 2643 | Method ? Method->isVariadic() |
| 2644 | : false); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2645 | castType = Context->getPointerType(castType); |
| 2646 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2647 | cast); |
| 2648 | |
| 2649 | // Don't forget the parens to enforce the proper binding. |
| 2650 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2651 | |
| 2652 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2653 | CallExpr *STCE = new (Context) CallExpr( |
| 2654 | *Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, SourceLocation()); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2655 | return STCE; |
| 2656 | |
| 2657 | } |
| 2658 | |
| 2659 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2660 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2661 | SourceLocation StartLoc, |
| 2662 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2663 | if (!SelGetUidFunctionDecl) |
| 2664 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2665 | if (!MsgSendFunctionDecl) |
| 2666 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2667 | if (!MsgSendSuperFunctionDecl) |
| 2668 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2669 | if (!MsgSendStretFunctionDecl) |
| 2670 | SynthMsgSendStretFunctionDecl(); |
| 2671 | if (!MsgSendSuperStretFunctionDecl) |
| 2672 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2673 | if (!MsgSendFpretFunctionDecl) |
| 2674 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2675 | if (!GetClassFunctionDecl) |
| 2676 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2677 | if (!GetSuperClassFunctionDecl) |
| 2678 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2679 | if (!GetMetaClassFunctionDecl) |
| 2680 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2682 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2683 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2684 | // May need to use objc_msgSend_stret() as well. |
| 2685 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2686 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2687 | QualType resultType = mDecl->getReturnType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2688 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2689 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2690 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2691 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2694 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2695 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2696 | switch (Exp->getReceiverKind()) { |
| 2697 | case ObjCMessageExpr::SuperClass: { |
| 2698 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2699 | if (MsgSendStretFlavor) |
| 2700 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2701 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2703 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2705 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2707 | // set the receiver to self, the first argument to all methods. |
| 2708 | InitExprs.push_back( |
| 2709 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2710 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2711 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2712 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2713 | Context->getObjCIdType(), |
| 2714 | VK_RValue, |
| 2715 | SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2716 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2718 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2719 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2720 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2721 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2722 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2723 | StringLiteral::Ascii, false, |
| 2724 | argType, SourceLocation())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2725 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2726 | &ClsExprs[0], |
| 2727 | ClsExprs.size(), |
| 2728 | StartLoc, |
| 2729 | EndLoc); |
| 2730 | // (Class)objc_getClass("CurrentClass") |
| 2731 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2732 | Context->getObjCClassType(), |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2733 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2734 | ClsExprs.clear(); |
| 2735 | ClsExprs.push_back(ArgExpr); |
| 2736 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2737 | &ClsExprs[0], ClsExprs.size(), |
| 2738 | StartLoc, EndLoc); |
| 2739 | |
| 2740 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2741 | // To turn off a warning, type-cast to 'id' |
| 2742 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2743 | NoTypeInfoCStyleCastExpr(Context, |
| 2744 | Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2745 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2746 | // struct objc_super |
| 2747 | QualType superType = getSuperStructType(); |
| 2748 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2749 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2750 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2751 | SynthSuperConstructorFunctionDecl(); |
| 2752 | // Simulate a constructor call... |
| 2753 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2754 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2755 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2756 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2757 | superType, VK_LValue, |
| 2758 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2759 | // The code for super is a little tricky to prevent collision with |
| 2760 | // the structure definition in the header. The rewriter has it's own |
| 2761 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2762 | // we need the cast below. For example: |
| 2763 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2764 | // |
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()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2769 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2770 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2771 | CK_BitCast, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2772 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2773 | // (struct objc_super) { <exprs from above> } |
| 2774 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2775 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2776 | SourceLocation()); |
| 2777 | TypeSourceInfo *superTInfo |
| 2778 | = Context->getTrivialTypeSourceInfo(superType); |
| 2779 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2780 | superType, VK_LValue, |
| 2781 | ILE, false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2782 | // struct objc_super * |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2783 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2784 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2785 | VK_RValue, OK_Ordinary, |
| 2786 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2787 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2788 | MsgExprs.push_back(SuperRep); |
| 2789 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2790 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2791 | |
| 2792 | case ObjCMessageExpr::Class: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2793 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2794 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2795 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2796 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2797 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2798 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2799 | clsName->getName(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2800 | StringLiteral::Ascii, false, |
Anders Carlsson | 7524540 | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2801 | argType, SourceLocation())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2802 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2803 | &ClsExprs[0], |
| 2804 | ClsExprs.size(), |
| 2805 | StartLoc, EndLoc); |
| 2806 | MsgExprs.push_back(Cls); |
| 2807 | break; |
| 2808 | } |
| 2809 | |
| 2810 | case ObjCMessageExpr::SuperInstance:{ |
| 2811 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2812 | if (MsgSendStretFlavor) |
| 2813 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2814 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2815 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2816 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2817 | |
| 2818 | InitExprs.push_back( |
| 2819 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2820 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2821 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2822 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2823 | Context->getObjCIdType(), |
| 2824 | VK_RValue, SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2825 | ); // set the 'receiver'. |
| 2826 | |
| 2827 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2828 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2829 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2830 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2831 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2832 | StringLiteral::Ascii, false, argType, |
| 2833 | SourceLocation())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2834 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2835 | &ClsExprs[0], |
| 2836 | ClsExprs.size(), |
| 2837 | StartLoc, EndLoc); |
| 2838 | // (Class)objc_getClass("CurrentClass") |
| 2839 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2840 | Context->getObjCClassType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2841 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2842 | ClsExprs.clear(); |
| 2843 | ClsExprs.push_back(ArgExpr); |
| 2844 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2845 | &ClsExprs[0], ClsExprs.size(), |
| 2846 | StartLoc, EndLoc); |
| 2847 | |
| 2848 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2849 | // To turn off a warning, type-cast to 'id' |
| 2850 | InitExprs.push_back( |
| 2851 | // set 'super class', using class_getSuperclass(). |
| 2852 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2853 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2854 | // struct objc_super |
| 2855 | QualType superType = getSuperStructType(); |
| 2856 | Expr *SuperRep; |
| 2857 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2858 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2859 | SynthSuperConstructorFunctionDecl(); |
| 2860 | // Simulate a constructor call... |
| 2861 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2862 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2863 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2864 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2865 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2866 | // The code for super is a little tricky to prevent collision with |
| 2867 | // the structure definition in the header. The rewriter has it's own |
| 2868 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2869 | // we need the cast below. For example: |
| 2870 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2871 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2872 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2873 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2874 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2875 | SourceLocation()); |
| 2876 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2877 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2878 | CK_BitCast, SuperRep); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | } else { |
| 2880 | // (struct objc_super) { <exprs from above> } |
| 2881 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2882 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2883 | SourceLocation()); |
| 2884 | TypeSourceInfo *superTInfo |
| 2885 | = Context->getTrivialTypeSourceInfo(superType); |
| 2886 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2887 | superType, VK_RValue, ILE, |
| 2888 | false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2889 | } |
| 2890 | MsgExprs.push_back(SuperRep); |
| 2891 | break; |
| 2892 | } |
| 2893 | |
| 2894 | case ObjCMessageExpr::Instance: { |
| 2895 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2896 | // Foo<Proto> *. |
| 2897 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2898 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2899 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2900 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2901 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2902 | ? CK_BlockPointerToObjCPointerCast |
| 2903 | : CK_CPointerToObjCPointerCast; |
| 2904 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2905 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2906 | CK, recExpr); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2907 | MsgExprs.push_back(recExpr); |
| 2908 | break; |
| 2909 | } |
| 2910 | } |
| 2911 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2912 | // 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] | 2913 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2914 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2916 | Exp->getSelector().getAsString(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2917 | StringLiteral::Ascii, false, |
| 2918 | argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2919 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2920 | &SelExprs[0], SelExprs.size(), |
| 2921 | StartLoc, |
| 2922 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2923 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2925 | // Now push any user supplied arguments. |
| 2926 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2927 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2928 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2929 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2930 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | 8f49033 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2931 | QualType type = ICE->getType(); |
| 2932 | if (needToScanForQualifiers(type)) |
| 2933 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2934 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2935 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 9e7dbd1 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2936 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2937 | CastKind CK; |
| 2938 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2939 | type->isBooleanType()) { |
| 2940 | CK = CK_IntegralToBoolean; |
| 2941 | } else if (type->isObjCObjectPointerType()) { |
| 2942 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2943 | CK = CK_BlockPointerToObjCPointerCast; |
| 2944 | } else if (SubExpr->getType()->isPointerType()) { |
| 2945 | CK = CK_CPointerToObjCPointerCast; |
| 2946 | } else { |
| 2947 | CK = CK_BitCast; |
| 2948 | } |
| 2949 | } else { |
| 2950 | CK = CK_BitCast; |
| 2951 | } |
| 2952 | |
| 2953 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2954 | } |
| 2955 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2956 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2957 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2958 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2959 | userExpr = CE->getSubExpr(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2960 | CastKind CK; |
| 2961 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2962 | CK = CK_IntegralToPointer; |
| 2963 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2964 | CK = CK_BlockPointerToObjCPointerCast; |
| 2965 | } else if (userExpr->getType()->isPointerType()) { |
| 2966 | CK = CK_CPointerToObjCPointerCast; |
| 2967 | } else { |
| 2968 | CK = CK_BitCast; |
| 2969 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2970 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2971 | CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2974 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2975 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2976 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2977 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2978 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2979 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2980 | // Generate the funky cast. |
| 2981 | CastExpr *cast; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2982 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2983 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2985 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2986 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2987 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2988 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2989 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2990 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2991 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2992 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2993 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2994 | E = OMD->param_end(); PI != E; ++PI) { |
| 2995 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2997 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2998 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2999 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3000 | ArgTypes.push_back(t); |
| 3001 | } |
Fariborz Jahanian | 9f0bc57 | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 3002 | returnType = Exp->getType(); |
| 3003 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3004 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3005 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3006 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3007 | } |
| 3008 | // 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] | 3009 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3011 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3012 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3013 | VK_LValue, SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3014 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3016 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3017 | // xx.m:13: warning: function called through a non-compatible type |
| 3018 | // 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] | 3019 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3020 | Context->getPointerType(Context->VoidTy), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3021 | CK_BitCast, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3023 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3024 | // If we don't have a method decl, force a variadic cast. |
| 3025 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3026 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3027 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3028 | castType = Context->getPointerType(castType); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3029 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3030 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3031 | |
| 3032 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3033 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3035 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3036 | CallExpr *CE = new (Context) |
| 3037 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3038 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3039 | if (MsgSendStretFlavor) { |
| 3040 | // We have the method which returns a struct/union. Must also generate |
| 3041 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3042 | // expression which dictate which one to envoke depending on size of |
| 3043 | // method's return type. |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 3044 | |
| 3045 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3046 | msgSendType, returnType, |
| 3047 | ArgTypes, MsgExprs, |
| 3048 | Exp->getMethodDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3050 | // Build sizeof(returnType) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3051 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3052 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3053 | Context->getTrivialTypeSourceInfo(returnType), |
| 3054 | Context->getSizeType(), SourceLocation(), |
| 3055 | SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3056 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3057 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3058 | // For X86 it is more complicated and some kind of target specific routine |
| 3059 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3061 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3062 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3063 | llvm::APInt(IntSize, 8), |
| 3064 | Context->IntTy, |
| 3065 | SourceLocation()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3066 | BinaryOperator *lessThanExpr = |
| 3067 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3068 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3069 | false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3070 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3072 | new (Context) ConditionalOperator(lessThanExpr, |
| 3073 | SourceLocation(), CE, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3074 | SourceLocation(), STCE, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3075 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3076 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3077 | CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3078 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3079 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3080 | return ReplacingStmt; |
| 3081 | } |
| 3082 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3083 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3084 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3085 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3087 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3088 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3090 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3091 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3094 | // typedef struct objc_object Protocol; |
| 3095 | QualType RewriteObjC::getProtocolType() { |
| 3096 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3097 | TypeSourceInfo *TInfo |
| 3098 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3099 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3100 | SourceLocation(), SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3101 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3102 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3103 | } |
| 3104 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3105 | } |
| 3106 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3107 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3108 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3109 | /// The forward references (and metadata) are generated in |
| 3110 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3111 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3112 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3113 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3114 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3115 | SourceLocation(), ID, getProtocolType(), 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3116 | SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3117 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3118 | VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3119 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3120 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3121 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3122 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3123 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3124 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3125 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3126 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3127 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3128 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3133 | const char *endBuf) { |
| 3134 | while (startBuf < endBuf) { |
| 3135 | if (*startBuf == '#') { |
| 3136 | // Skip whitespace. |
| 3137 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3138 | ; |
| 3139 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3140 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3141 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3142 | !strncmp(startBuf, "define", strlen("define")) || |
| 3143 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3144 | !strncmp(startBuf, "else", strlen("else")) || |
| 3145 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3146 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3147 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3148 | !strncmp(startBuf, "include", strlen("include")) || |
| 3149 | !strncmp(startBuf, "import", strlen("import")) || |
| 3150 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3151 | return true; |
| 3152 | } |
| 3153 | startBuf++; |
| 3154 | } |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3158 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3159 | /// an objective-c class with ivars. |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3160 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3161 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3162 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3163 | assert(CDecl->getName() != "" && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3164 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3165 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3166 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3167 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3168 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3169 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3170 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3171 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3173 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3174 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3176 | // If no ivars and no root or if its root, directly or indirectly, |
| 3177 | // 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] | 3178 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3179 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3180 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3181 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3182 | return; |
| 3183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
| 3185 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3186 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3187 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3188 | Result += CDecl->getNameAsString(); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3189 | if (LangOpts.MicrosoftExt) |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3190 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3191 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3192 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3193 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3195 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3196 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3197 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3198 | // NSURL.h, for example): |
| 3199 | // |
| 3200 | // #ifdef XYZ |
| 3201 | // @interface Foo : NSObject |
| 3202 | // #else |
| 3203 | // @interface FooBar : NSObject |
| 3204 | // #endif |
| 3205 | // { |
| 3206 | // int i; |
| 3207 | // } |
| 3208 | // @end |
| 3209 | // |
| 3210 | // This clause is segregated to avoid breaking the common case. |
| 3211 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3213 | CDecl->getAtStartLoc(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3214 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3215 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3216 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3217 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3218 | // advance to the end of the referenced protocols. |
| 3219 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3220 | endHeader++; |
| 3221 | } |
| 3222 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3223 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3224 | } else { |
| 3225 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3226 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3227 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3228 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3229 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3230 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3231 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3232 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3233 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3235 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3236 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3237 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3238 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3239 | } |
| 3240 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3242 | // Now comment out any visibility specifiers. |
| 3243 | while (cursor < endBuf) { |
| 3244 | if (*cursor == '@') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3245 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3246 | // Skip whitespace. |
| 3247 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3248 | /*scan*/; |
| 3249 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3250 | // FIXME: presence of @public, etc. inside comment results in |
| 3251 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3252 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3253 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3254 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3255 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3256 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3257 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3258 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3259 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3260 | // for type checking. |
| 3261 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3262 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3263 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3264 | cursor = strchr(cursor, '>'); |
| 3265 | cursor++; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3266 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3267 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3268 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3269 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3270 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3271 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3272 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3273 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3274 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3275 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3276 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3277 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3278 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3279 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3280 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3281 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3282 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3283 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3284 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3285 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3286 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3287 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3290 | //===----------------------------------------------------------------------===// |
| 3291 | // Meta Data Emission |
| 3292 | //===----------------------------------------------------------------------===// |
| 3293 | |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3294 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3295 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3296 | /// and emits meta-data. |
| 3297 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3298 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3299 | int ClsDefCount = ClassImplementation.size(); |
| 3300 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3302 | // Rewrite implemented methods |
| 3303 | for (int i = 0; i < ClsDefCount; i++) |
| 3304 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3306 | for (int i = 0; i < CatDefCount; i++) |
| 3307 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3308 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3310 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3311 | const std::string &Name, |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3312 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3313 | assert(BlockByRefDeclNo.count(VD) && |
| 3314 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3315 | if (def) |
| 3316 | ResultStr += "struct "; |
| 3317 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3318 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3319 | } |
| 3320 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3321 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3322 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3323 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3324 | return false; |
| 3325 | } |
| 3326 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3327 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3328 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3329 | std::string Tag) { |
| 3330 | const FunctionType *AFT = CE->getFunctionType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3331 | QualType RT = AFT->getReturnType(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3332 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3333 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3334 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3335 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3336 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3338 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | // 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] | 3340 | // block (to reference imported block decl refs). |
| 3341 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3342 | } else if (BD->param_empty()) { |
| 3343 | S += "(" + StructRef + " *__cself)"; |
| 3344 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3345 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3346 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3347 | S += '('; |
| 3348 | // first add the implicit argument. |
| 3349 | S += StructRef + " *__cself, "; |
| 3350 | std::string ParamStr; |
| 3351 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3352 | E = BD->param_end(); AI != E; ++AI) { |
| 3353 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3354 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3355 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3356 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3357 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3358 | S += ParamStr; |
| 3359 | } |
| 3360 | if (FT->isVariadic()) { |
| 3361 | if (!BD->param_empty()) S += ", "; |
| 3362 | S += "..."; |
| 3363 | } |
| 3364 | S += ')'; |
| 3365 | } |
| 3366 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3368 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3369 | // First, emit a declaration for all "by ref" decls. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3370 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3371 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3372 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3373 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3374 | std::string TypeString; |
| 3375 | RewriteByRefString(TypeString, Name, (*I)); |
| 3376 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3377 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3378 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3380 | // Next, emit a declaration for all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3381 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3382 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3383 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3384 | // Handle nested closure invocation. For example: |
| 3385 | // |
| 3386 | // void (^myImportedClosure)(void); |
| 3387 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3389 | // void (^anotherClosure)(void); |
| 3390 | // anotherClosure = ^(void) { |
| 3391 | // myImportedClosure(); // import and invoke the closure |
| 3392 | // }; |
| 3393 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3394 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3395 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3396 | S += " = ("; |
| 3397 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3398 | S += ")"; |
| 3399 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3400 | } |
| 3401 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3402 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3403 | QualType QT = (*I)->getType(); |
| 3404 | if (HasLocalVariableExternalStorage(*I)) |
| 3405 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3406 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3407 | S += Name + " = __cself->" + |
| 3408 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3409 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3410 | } |
| 3411 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3412 | const char *cstr = RewrittenStr.c_str(); |
| 3413 | while (*cstr++ != '{') ; |
| 3414 | S += cstr; |
| 3415 | S += "\n"; |
| 3416 | return S; |
| 3417 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3418 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3419 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3420 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3421 | std::string Tag) { |
| 3422 | std::string StructRef = "struct " + Tag; |
| 3423 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3425 | S += funcName; |
| 3426 | S += "_block_copy_" + utostr(i); |
| 3427 | S += "(" + StructRef; |
| 3428 | S += "*dst, " + StructRef; |
| 3429 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3431 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3432 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3433 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3434 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3435 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3436 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3437 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3438 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3439 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3440 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3441 | else |
| 3442 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3443 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3444 | S += "}\n"; |
| 3445 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3446 | S += "\nstatic void __"; |
| 3447 | S += funcName; |
| 3448 | S += "_block_dispose_" + utostr(i); |
| 3449 | S += "(" + StructRef; |
| 3450 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3452 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3453 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3454 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3455 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3456 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3457 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3458 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3459 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3460 | else |
| 3461 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3462 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3464 | return S; |
| 3465 | } |
| 3466 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3467 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3468 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3469 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3470 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3472 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3473 | S += " struct " + Desc; |
| 3474 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3476 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3477 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3478 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3480 | if (BlockDeclRefs.size()) { |
| 3481 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3482 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3483 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3484 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3485 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3486 | std::string ArgName = "_" + FieldName; |
| 3487 | // Handle nested closure invocation. For example: |
| 3488 | // |
| 3489 | // void (^myImportedBlock)(void); |
| 3490 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3491 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3492 | // void (^anotherBlock)(void); |
| 3493 | // anotherBlock = ^(void) { |
| 3494 | // myImportedBlock(); // import and invoke the closure |
| 3495 | // }; |
| 3496 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3497 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3498 | S += "struct __block_impl *"; |
| 3499 | Constructor += ", void *" + ArgName; |
| 3500 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3501 | QualType QT = (*I)->getType(); |
| 3502 | if (HasLocalVariableExternalStorage(*I)) |
| 3503 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3504 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3505 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3506 | Constructor += ", " + ArgName; |
| 3507 | } |
| 3508 | S += FieldName + ";\n"; |
| 3509 | } |
| 3510 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3511 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3512 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3513 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3514 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3515 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3516 | { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3517 | std::string TypeString; |
| 3518 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3519 | TypeString += " *"; |
| 3520 | FieldName = TypeString + FieldName; |
| 3521 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3522 | Constructor += ", " + ArgName; |
| 3523 | } |
| 3524 | S += FieldName + "; // by ref\n"; |
| 3525 | } |
| 3526 | // Finish writing the constructor. |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3527 | Constructor += ", int flags=0)"; |
| 3528 | // Initialize all "by copy" arguments. |
| 3529 | bool firsTime = true; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3530 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3531 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3532 | std::string Name = (*I)->getNameAsString(); |
| 3533 | if (firsTime) { |
| 3534 | Constructor += " : "; |
| 3535 | firsTime = false; |
| 3536 | } |
| 3537 | else |
| 3538 | Constructor += ", "; |
| 3539 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3540 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3541 | else |
| 3542 | Constructor += Name + "(_" + Name + ")"; |
| 3543 | } |
| 3544 | // Initialize all "by ref" arguments. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3545 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3546 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3547 | std::string Name = (*I)->getNameAsString(); |
| 3548 | if (firsTime) { |
| 3549 | Constructor += " : "; |
| 3550 | firsTime = false; |
| 3551 | } |
| 3552 | else |
| 3553 | Constructor += ", "; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3554 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | Constructor += " {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3558 | if (GlobalVarDecl) |
| 3559 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3560 | else |
| 3561 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3562 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3564 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3565 | } else { |
| 3566 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3567 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3568 | if (GlobalVarDecl) |
| 3569 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3570 | else |
| 3571 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3572 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3573 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3574 | } |
| 3575 | Constructor += " "; |
| 3576 | Constructor += "}\n"; |
| 3577 | S += Constructor; |
| 3578 | S += "};\n"; |
| 3579 | return S; |
| 3580 | } |
| 3581 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3582 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3583 | std::string ImplTag, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3584 | StringRef FunName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3585 | unsigned hasCopy) { |
| 3586 | std::string S = "\nstatic struct " + DescTag; |
| 3587 | |
| 3588 | S += " {\n unsigned long reserved;\n"; |
| 3589 | S += " unsigned long Block_size;\n"; |
| 3590 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3591 | S += " void (*copy)(struct "; |
| 3592 | S += ImplTag; S += "*, struct "; |
| 3593 | S += ImplTag; S += "*);\n"; |
| 3594 | |
| 3595 | S += " void (*dispose)(struct "; |
| 3596 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3597 | } |
| 3598 | S += "} "; |
| 3599 | |
| 3600 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3601 | S += ImplTag + ")"; |
| 3602 | if (hasCopy) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3603 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3604 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3605 | } |
| 3606 | S += "};\n"; |
| 3607 | return S; |
| 3608 | } |
| 3609 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3610 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3611 | StringRef FunName) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3612 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3613 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3614 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3615 | bool RewriteSC = (GlobalVarDecl && |
| 3616 | !Blocks.empty() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3617 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3618 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3619 | if (RewriteSC) { |
| 3620 | std::string SC(" void __"); |
| 3621 | SC += GlobalVarDecl->getNameAsString(); |
| 3622 | SC += "() {}"; |
| 3623 | InsertText(FunLocStart, SC); |
| 3624 | } |
| 3625 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3626 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3627 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3628 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3629 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3630 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3631 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3632 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3633 | ValueDecl *VD = Exp->getDecl(); |
| 3634 | BlockDeclRefs.push_back(Exp); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3635 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3636 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3637 | BlockByCopyDecls.push_back(VD); |
| 3638 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3639 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3640 | BlockByRefDeclsPtrSet.insert(VD); |
| 3641 | BlockByRefDecls.push_back(VD); |
| 3642 | } |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3643 | // imported objects in the inner blocks not used in the outer |
| 3644 | // blocks must be copied/disposed in the outer block as well. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3645 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3646 | VD->getType()->isObjCObjectPointerType() || |
| 3647 | VD->getType()->isBlockPointerType()) |
| 3648 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3649 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3650 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3651 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3652 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3654 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3655 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3656 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3657 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3658 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3660 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3661 | |
| 3662 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3663 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3664 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3665 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3666 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3667 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3668 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3670 | BlockDeclRefs.clear(); |
| 3671 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3672 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3673 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3674 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | ImportedBlockDecls.clear(); |
| 3676 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3677 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3678 | // Must insert any 'const/volatile/static here. Since it has been |
| 3679 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3680 | std::string SC; |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3681 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3682 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3683 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3684 | SC += "const "; |
| 3685 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3686 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3687 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3688 | SC += "restrict "; |
| 3689 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3692 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3693 | InnerDeclRefsCount.clear(); |
| 3694 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3695 | RewrittenBlockExprs.clear(); |
| 3696 | } |
| 3697 | |
| 3698 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3699 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3700 | StringRef FuncName = FD->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3702 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3703 | } |
| 3704 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3705 | static void BuildUniqueMethodName(std::string &Name, |
| 3706 | ObjCMethodDecl *MD) { |
| 3707 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3708 | Name = IFace->getName(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3709 | Name += "__" + MD->getSelector().getAsString(); |
| 3710 | // Convert colons to underscores. |
| 3711 | std::string::size_type loc = 0; |
| 3712 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3713 | Name.replace(loc, 1, "_"); |
| 3714 | } |
| 3715 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3716 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3717 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3718 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3719 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3720 | std::string FuncName; |
| 3721 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3722 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3726 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3727 | if (*CI) { |
| 3728 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3729 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3730 | else |
| 3731 | GetBlockDeclRefExprs(*CI); |
| 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 | // FIXME: Handle enums. |
| 3737 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3738 | BlockDeclRefs.push_back(DRE); |
| 3739 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3740 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3741 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3742 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3743 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3744 | return; |
| 3745 | } |
| 3746 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3747 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 3748 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3749 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3750 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3751 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3752 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3753 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3754 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3755 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3756 | InnerContexts); |
| 3757 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3758 | else |
| 3759 | GetInnerBlockDeclRefExprs(*CI, |
| 3760 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3761 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3762 | |
| 3763 | } |
| 3764 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3765 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3766 | if (DRE->refersToEnclosingLocal()) { |
| 3767 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3768 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3769 | InnerBlockDeclRefs.push_back(DRE); |
| 3770 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3771 | if (Var->isFunctionOrMethodVarDecl()) |
| 3772 | ImportedLocalExternalDecls.insert(Var); |
| 3773 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3774 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3775 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3776 | return; |
| 3777 | } |
| 3778 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3779 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3780 | /// 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] | 3781 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3782 | /// all block pointers to function pointers. |
| 3783 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3784 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3785 | // FTP will be null for closures that don't take arguments. |
| 3786 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3787 | SmallVector<QualType, 8> ArgTypes; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3788 | QualType Res = FT->getReturnType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3789 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3790 | |
| 3791 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3792 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3793 | E = FTP->param_type_end(); |
| 3794 | I && (I != E); ++I) { |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3795 | QualType t = *I; |
| 3796 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3797 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3798 | HasBlockType = true; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3799 | ArgTypes.push_back(t); |
| 3800 | } |
| 3801 | } |
| 3802 | QualType FuncType; |
| 3803 | // FIXME. Does this work if block takes no argument but has a return type |
| 3804 | // which is of block type? |
| 3805 | if (HasBlockType) |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3806 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3807 | else FuncType = QualType(FT, 0); |
| 3808 | return FuncType; |
| 3809 | } |
| 3810 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3811 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3812 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3813 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3815 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3816 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3817 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3818 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3819 | } |
| 3820 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3821 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3822 | } |
| 3823 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3824 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3825 | else if (const ConditionalOperator *CEXPR = |
| 3826 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3827 | Expr *LHSExp = CEXPR->getLHS(); |
| 3828 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3829 | Expr *RHSExp = CEXPR->getRHS(); |
| 3830 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3831 | Expr *CONDExp = CEXPR->getCond(); |
| 3832 | ConditionalOperator *CondExpr = |
| 3833 | new (Context) ConditionalOperator(CONDExp, |
| 3834 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3835 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3836 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3837 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3838 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3839 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3840 | } else if (const PseudoObjectExpr *POE |
| 3841 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3842 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3843 | } else { |
| 3844 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3845 | } |
| 3846 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3847 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3848 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3849 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3850 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3852 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3853 | SourceLocation(), SourceLocation(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3854 | &Context->Idents.get("__block_impl")); |
| 3855 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3856 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3857 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3858 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3860 | // Push the block argument type. |
| 3861 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3862 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3863 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3864 | E = FTP->param_type_end(); |
| 3865 | I && (I != E); ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3866 | QualType t = *I; |
| 3867 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3868 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3869 | convertToUnqualifiedObjCType(t); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3870 | ArgTypes.push_back(t); |
| 3871 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3872 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3873 | // Now do the pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3874 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3876 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3878 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3879 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3880 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3881 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3882 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3883 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3884 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3886 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3887 | SourceLocation(), |
| 3888 | &Context->Idents.get("FuncPtr"), |
| 3889 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3890 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3891 | ICIS_NoInit); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3892 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3893 | FD->getType(), VK_LValue, |
| 3894 | OK_Ordinary); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3896 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3897 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3898 | CK_BitCast, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3899 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3901 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3902 | // Add the implicit argument. |
| 3903 | BlkExprs.push_back(BlkCast); |
| 3904 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3906 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3907 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3908 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3909 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3910 | Exp->getType(), VK_RValue, |
| 3911 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3912 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3915 | // We need to return the rewritten expression to handle cases where the |
| 3916 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3917 | // For example: |
| 3918 | // |
| 3919 | // int main() { |
| 3920 | // __block Foo *f; |
| 3921 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3922 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3923 | // void (^myblock)() = ^() { |
| 3924 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3925 | // i = 77; |
| 3926 | // }; |
| 3927 | //} |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3928 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3929 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3930 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3931 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3932 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3933 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3934 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3935 | SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3936 | &Context->Idents.get("__forwarding"), |
| 3937 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3938 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3939 | ICIS_NoInit); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3940 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3941 | FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3942 | FD->getType(), VK_LValue, |
| 3943 | OK_Ordinary); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3944 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3945 | StringRef Name = VD->getName(); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3946 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3947 | &Context->Idents.get(Name), |
| 3948 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3949 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3950 | ICIS_NoInit); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3951 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3952 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3953 | |
| 3954 | |
| 3955 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3956 | // Need parens to enforce precedence. |
Fariborz Jahanian | 5bba75f | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3957 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3958 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3959 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3960 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3961 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3964 | // Rewrites the imported local variable V with external storage |
| 3965 | // (static, extern, etc.) as *V |
| 3966 | // |
| 3967 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3968 | ValueDecl *VD = DRE->getDecl(); |
| 3969 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3970 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3971 | return DRE; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3972 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3973 | VK_LValue, OK_Ordinary, |
| 3974 | DRE->getLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3975 | // Need parens to enforce precedence. |
| 3976 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3977 | Exp); |
| 3978 | ReplaceStmt(DRE, PE); |
| 3979 | return PE; |
| 3980 | } |
| 3981 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3982 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3983 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3984 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3985 | |
| 3986 | // Need to avoid trying to rewrite synthesized casts. |
| 3987 | if (LocStart.isInvalid()) |
| 3988 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3989 | // Need to avoid trying to rewrite casts contained in macros. |
| 3990 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3991 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3992 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3993 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3994 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3995 | QualType QT = CE->getType(); |
| 3996 | const Type* TypePtr = QT->getAs<Type>(); |
| 3997 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3998 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3999 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4000 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4001 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4002 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4003 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4004 | return; |
| 4005 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4006 | // advance the location to startArgList. |
| 4007 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4009 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4010 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4011 | case '^': |
| 4012 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4013 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4014 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4015 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4016 | } |
| 4017 | } |
| 4018 | return; |
| 4019 | } |
| 4020 | |
| 4021 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4022 | SourceLocation DeclLoc = FD->getLocation(); |
| 4023 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4024 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4025 | // We have 1 or more arguments that have closure pointers. |
| 4026 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4027 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4029 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | parenCount++; |
| 4032 | // advance the location to startArgList. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4033 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4034 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4036 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4038 | while (*argPtr++ && parenCount) { |
| 4039 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4040 | case '^': |
| 4041 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4042 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4043 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4044 | break; |
| 4045 | case '(': |
| 4046 | parenCount++; |
| 4047 | break; |
| 4048 | case ')': |
| 4049 | parenCount--; |
| 4050 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4051 | } |
| 4052 | } |
| 4053 | return; |
| 4054 | } |
| 4055 | |
| 4056 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4057 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4058 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4059 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4060 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4061 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4062 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4063 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4064 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4065 | } |
| 4066 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4067 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4068 | E = FTP->param_type_end(); |
| 4069 | I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4070 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4071 | return true; |
| 4072 | } |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4076 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4077 | const FunctionProtoType *FTP; |
| 4078 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4079 | if (PT) { |
| 4080 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4081 | } else { |
| 4082 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4083 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4084 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4085 | } |
| 4086 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4087 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4088 | E = FTP->param_type_end(); |
| 4089 | I != E; ++I) { |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4090 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4091 | return true; |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4092 | if ((*I)->isObjCObjectPointerType() && |
| 4093 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4094 | return true; |
| 4095 | } |
| 4096 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4097 | } |
| 4098 | return false; |
| 4099 | } |
| 4100 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4101 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4102 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4103 | const char *argPtr = strchr(Name, '('); |
| 4104 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4106 | LParen = argPtr; // output the start. |
| 4107 | argPtr++; // skip past the left paren. |
| 4108 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4109 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4110 | while (*argPtr && parenCount) { |
| 4111 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4112 | case '(': parenCount++; break; |
| 4113 | case ')': parenCount--; break; |
| 4114 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | } |
| 4116 | if (parenCount) argPtr++; |
| 4117 | } |
| 4118 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4119 | RParen = argPtr; // output the end |
| 4120 | } |
| 4121 | |
| 4122 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4123 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4124 | RewriteBlockPointerFunctionArgs(FD); |
| 4125 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4127 | // Handle Variables and Typedefs. |
| 4128 | SourceLocation DeclLoc = ND->getLocation(); |
| 4129 | QualType DeclT; |
| 4130 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4131 | DeclT = VD->getType(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4132 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4133 | DeclT = TDD->getUnderlyingType(); |
| 4134 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4135 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4136 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4137 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4139 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4140 | const char *endBuf = startBuf; |
| 4141 | // scan backward (from the decl location) for the end of the previous decl. |
| 4142 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4143 | startBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4144 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4145 | std::string buf; |
| 4146 | unsigned OrigLength=0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4147 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4148 | // may take block argument types (which will be handled below). |
| 4149 | if (*startBuf == '^') { |
| 4150 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4151 | buf = '*'; |
| 4152 | startBuf++; |
| 4153 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4154 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4155 | while (*startBuf != ')') { |
| 4156 | buf += *startBuf; |
| 4157 | startBuf++; |
| 4158 | OrigLength++; |
| 4159 | } |
| 4160 | buf += ')'; |
| 4161 | OrigLength++; |
| 4162 | |
| 4163 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4164 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4165 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4166 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4167 | DeclLoc = ND->getLocation(); |
| 4168 | startBuf = SM->getCharacterData(DeclLoc); |
| 4169 | const char *argListBegin, *argListEnd; |
| 4170 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4171 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4172 | if (*argListBegin == '^') |
| 4173 | buf += '*'; |
| 4174 | else if (*argListBegin == '<') { |
| 4175 | buf += "/*"; |
| 4176 | buf += *argListBegin++; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4177 | OrigLength++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4178 | while (*argListBegin != '>') { |
| 4179 | buf += *argListBegin++; |
| 4180 | OrigLength++; |
| 4181 | } |
| 4182 | buf += *argListBegin; |
| 4183 | buf += "*/"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4184 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4185 | else |
| 4186 | buf += *argListBegin; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4187 | argListBegin++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4188 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4189 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4190 | buf += ')'; |
| 4191 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4192 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4193 | ReplaceText(Start, OrigLength, buf); |
| 4194 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4195 | return; |
| 4196 | } |
| 4197 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4198 | |
| 4199 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4200 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4201 | /// struct Block_byref_id_object *src) { |
| 4202 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4203 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4204 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4205 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4206 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4207 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4208 | /// } |
| 4209 | /// And: |
| 4210 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4211 | /// _Block_object_dispose(_src->object, |
| 4212 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4213 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4214 | /// _Block_object_dispose(_src->object, |
| 4215 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4216 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4217 | /// } |
| 4218 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4219 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4220 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4221 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4222 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4223 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4224 | CopyDestroyCache.insert(flag); |
| 4225 | S = "static void __Block_byref_id_object_copy_"; |
| 4226 | S += utostr(flag); |
| 4227 | S += "(void *dst, void *src) {\n"; |
| 4228 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4229 | // offset into the object pointer is computed as: |
| 4230 | // void * + void* + int + int + void* + void * |
| 4231 | unsigned IntSize = |
| 4232 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4233 | unsigned VoidPtrSize = |
| 4234 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4235 | |
Ken Dyck | d9c83e6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4236 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4237 | S += " _Block_object_assign((char*)dst + "; |
| 4238 | S += utostr(offset); |
| 4239 | S += ", *(void * *) ((char*)src + "; |
| 4240 | S += utostr(offset); |
| 4241 | S += "), "; |
| 4242 | S += utostr(flag); |
| 4243 | S += ");\n}\n"; |
| 4244 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4245 | S += "static void __Block_byref_id_object_dispose_"; |
| 4246 | S += utostr(flag); |
| 4247 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4248 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4249 | S += utostr(offset); |
| 4250 | S += "), "; |
| 4251 | S += utostr(flag); |
| 4252 | S += ");\n}\n"; |
| 4253 | return S; |
| 4254 | } |
| 4255 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4256 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4257 | /// the declaration into: |
| 4258 | /// struct __Block_byref_ND { |
| 4259 | /// void *__isa; // NULL for everything except __weak pointers |
| 4260 | /// struct __Block_byref_ND *__forwarding; |
| 4261 | /// int32_t __flags; |
| 4262 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4263 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4264 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4265 | /// typex ND; |
| 4266 | /// }; |
| 4267 | /// |
| 4268 | /// It then replaces declaration of ND variable with: |
| 4269 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4270 | /// __size=sizeof(struct __Block_byref_ND), |
| 4271 | /// ND=initializer-if-any}; |
| 4272 | /// |
| 4273 | /// |
| 4274 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4275 | // Insert declaration for the function in which block literal is |
| 4276 | // used. |
| 4277 | if (CurFunctionDeclToDeclareForBlock) |
| 4278 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4279 | int flag = 0; |
| 4280 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4281 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4282 | if (DeclLoc.isInvalid()) |
| 4283 | // If type location is missing, it is because of missing type (a warning). |
| 4284 | // Use variable's location which is good for this case. |
| 4285 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4286 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4287 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4288 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4289 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4290 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4291 | std::string ByrefType; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4292 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4293 | ByrefType += " {\n"; |
| 4294 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4295 | RewriteByRefString(ByrefType, Name, ND); |
| 4296 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4297 | ByrefType += " int __flags;\n"; |
| 4298 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4299 | // Add void *__Block_byref_id_object_copy; |
| 4300 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4301 | QualType Ty = ND->getType(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4302 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4303 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4304 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4305 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4306 | } |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4307 | |
| 4308 | QualType T = Ty; |
| 4309 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4310 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4311 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4312 | ByrefType += " " + Name + ";\n"; |
| 4313 | ByrefType += "};\n"; |
| 4314 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4315 | SourceLocation FunLocStart; |
| 4316 | if (CurFunctionDef) |
| 4317 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4318 | else { |
| 4319 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4320 | FunLocStart = CurMethodDef->getLocStart(); |
| 4321 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4322 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4323 | if (Ty.isObjCGCWeak()) { |
| 4324 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4325 | isa = 1; |
| 4326 | } |
| 4327 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4328 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4329 | flag = BLOCK_BYREF_CALLER; |
| 4330 | QualType Ty = ND->getType(); |
| 4331 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4332 | if (Ty->isBlockPointerType()) |
| 4333 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4334 | else |
| 4335 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4336 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4337 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4338 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4339 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4340 | |
| 4341 | // struct __Block_byref_ND ND = |
| 4342 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4343 | // initializer-if-any}; |
| 4344 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4345 | unsigned flags = 0; |
| 4346 | if (HasCopyAndDispose) |
| 4347 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4348 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4349 | ByrefType.clear(); |
| 4350 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4351 | std::string ForwardingCastType("("); |
| 4352 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4353 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4354 | ByrefType += " " + Name + " = {(void*)"; |
| 4355 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4356 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4357 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4358 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4359 | ByrefType += "sizeof("; |
| 4360 | RewriteByRefString(ByrefType, Name, ND); |
| 4361 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4362 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4363 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4364 | ByrefType += utostr(flag); |
| 4365 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4366 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4367 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4368 | ByrefType += "};\n"; |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4369 | unsigned nameSize = Name.size(); |
| 4370 | // for block or function pointer declaration. Name is aleady |
| 4371 | // part of the declaration. |
| 4372 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4373 | nameSize = 1; |
| 4374 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4375 | } |
| 4376 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4377 | SourceLocation startLoc; |
| 4378 | Expr *E = ND->getInit(); |
| 4379 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4380 | startLoc = ECE->getLParenLoc(); |
| 4381 | else |
| 4382 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4383 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4384 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4385 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4386 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4387 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4388 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4389 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4390 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4391 | ByrefType += "sizeof("; |
| 4392 | RewriteByRefString(ByrefType, Name, ND); |
| 4393 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4394 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4395 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4396 | ByrefType += utostr(flag); |
| 4397 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4398 | ByrefType += utostr(flag); |
| 4399 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4400 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4401 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4402 | |
| 4403 | // Complete the newly synthesized compound expression by inserting a right |
| 4404 | // curly brace before the end of the declaration. |
| 4405 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4406 | // also assumes there is only one declarator. For example, the following |
| 4407 | // isn't currently supported by this routine (in general): |
| 4408 | // |
| 4409 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4410 | // |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4411 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4412 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4413 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4414 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4415 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4416 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4417 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4418 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4419 | return; |
| 4420 | } |
| 4421 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | // Add initializers for any closure decl refs. |
| 4424 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4425 | if (BlockDeclRefs.size()) { |
| 4426 | // Unique all "by copy" declarations. |
| 4427 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4428 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4429 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4430 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4431 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4432 | } |
| 4433 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4434 | // Unique all "by ref" declarations. |
| 4435 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4436 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4437 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4438 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4439 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4440 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4441 | } |
| 4442 | // Find any imported blocks...they will need special attention. |
| 4443 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4444 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4445 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4446 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4447 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4451 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4452 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4453 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4454 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4455 | SourceLocation(), ID, FType, 0, SC_Extern, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4456 | false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4459 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4460 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4461 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4462 | Blocks.push_back(Exp); |
| 4463 | |
| 4464 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Add inner imported variables now used in current block. |
| 4467 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4468 | if (!InnerBlockDeclRefs.empty()) { |
| 4469 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4470 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4471 | ValueDecl *VD = Exp->getDecl(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4472 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4473 | // We need to save the copied-in variables in nested |
| 4474 | // blocks because it is needed at the end for some of the API generations. |
| 4475 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4476 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4477 | BlockDeclRefs.push_back(Exp); |
| 4478 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4479 | BlockByCopyDecls.push_back(VD); |
| 4480 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4481 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4482 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4483 | BlockDeclRefs.push_back(Exp); |
| 4484 | BlockByRefDeclsPtrSet.insert(VD); |
| 4485 | BlockByRefDecls.push_back(VD); |
| 4486 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4487 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4488 | // Find any imported blocks...they will need special attention. |
| 4489 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4490 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4491 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4492 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4493 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4494 | } |
| 4495 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4496 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4497 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4499 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4500 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4501 | else if (CurMethodDef) |
| 4502 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4503 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4504 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4506 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4508 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4509 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4510 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4511 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4512 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4513 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4514 | |
| 4515 | FunctionDecl *FD; |
| 4516 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 4518 | // Simulate a constructor call... |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4519 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4520 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4521 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4523 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4525 | // Initialize the block function. |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4526 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4527 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4528 | VK_LValue, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4529 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4530 | CK_BitCast, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | InitExprs.push_back(castExpr); |
| 4532 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4533 | // Initialize the block descriptor. |
| 4534 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4536 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4537 | SourceLocation(), SourceLocation(), |
| 4538 | &Context->Idents.get(DescData.c_str()), |
| 4539 | Context->VoidPtrTy, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4540 | SC_Static); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4541 | UnaryOperator *DescRefExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4542 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4543 | Context->VoidPtrTy, |
| 4544 | VK_LValue, |
| 4545 | SourceLocation()), |
| 4546 | UO_AddrOf, |
| 4547 | Context->getPointerType(Context->VoidPtrTy), |
| 4548 | VK_RValue, OK_Ordinary, |
| 4549 | SourceLocation()); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4550 | InitExprs.push_back(DescRefExpr); |
| 4551 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4552 | // Add initializers for any closure decl refs. |
| 4553 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4554 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4555 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4556 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4558 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4559 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4560 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4561 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4562 | SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4563 | if (HasLocalVariableExternalStorage(*I)) { |
| 4564 | QualType QT = (*I)->getType(); |
| 4565 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4566 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4567 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4568 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4569 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4570 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4571 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4572 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4573 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4574 | CK_BitCast, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4575 | } else { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4576 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4577 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4578 | SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4579 | if (HasLocalVariableExternalStorage(*I)) { |
| 4580 | QualType QT = (*I)->getType(); |
| 4581 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4582 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4583 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4586 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4588 | } |
| 4589 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4590 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4591 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4592 | ValueDecl *ND = (*I); |
| 4593 | std::string Name(ND->getNameAsString()); |
| 4594 | std::string RecName; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4595 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4596 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4597 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4598 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4599 | SourceLocation(), SourceLocation(), |
| 4600 | II); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4601 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4602 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4603 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4604 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4605 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4606 | SourceLocation()); |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4607 | bool isNestedCapturedVar = false; |
| 4608 | if (block) |
| 4609 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4610 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4611 | const VarDecl *variable = ci->getVariable(); |
| 4612 | if (variable == ND && ci->isNested()) { |
| 4613 | assert (ci->isByRef() && |
| 4614 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4615 | isNestedCapturedVar = true; |
| 4616 | break; |
| 4617 | } |
| 4618 | } |
| 4619 | // captured nested byref variable has its address passed. Do not take |
| 4620 | // its address again. |
| 4621 | if (!isNestedCapturedVar) |
| 4622 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4623 | Context->getPointerType(Exp->getType()), |
| 4624 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4625 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4629 | if (ImportedBlockDecls.size()) { |
| 4630 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4631 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4632 | unsigned IntSize = |
| 4633 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4634 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4635 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4636 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4637 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4638 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4639 | FType, VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4640 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | Context->getPointerType(NewRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4642 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4643 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4644 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4645 | BlockDeclRefs.clear(); |
| 4646 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4647 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4648 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4649 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4650 | ImportedBlockDecls.clear(); |
| 4651 | return NewRep; |
| 4652 | } |
| 4653 | |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4654 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4655 | if (const ObjCForCollectionStmt * CS = |
| 4656 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4657 | return CS->getElement() == DS; |
| 4658 | return false; |
| 4659 | } |
| 4660 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4661 | //===----------------------------------------------------------------------===// |
| 4662 | // Function Body / Expression rewriting |
| 4663 | //===----------------------------------------------------------------------===// |
| 4664 | |
| 4665 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4667 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4668 | Stmts.push_back(S); |
| 4669 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4670 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4671 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4672 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4673 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4674 | // Pseudo-object operations and ivar references need special |
| 4675 | // treatment because we're going to recursively rewrite them. |
| 4676 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4677 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4678 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4679 | } else { |
| 4680 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4681 | } |
| 4682 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4683 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4684 | } |
| 4685 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4686 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4687 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4688 | // Perform a bottom up rewrite of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4689 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4690 | if (*CI) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4691 | Stmt *childStmt = (*CI); |
| 4692 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4693 | if (newStmt) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4694 | *CI = newStmt; |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4695 | } |
Nick Lewycky | 508ef2c | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4698 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4699 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4700 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4701 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4702 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4703 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4704 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4705 | // Rewrite the block body in place. |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4706 | Stmt *SaveCurrentBody = CurrentBody; |
| 4707 | CurrentBody = BE->getBody(); |
| 4708 | PropParentMap = 0; |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4709 | // block literal on rhs of a property-dot-sytax assignment |
| 4710 | // must be replaced by its synthesize ast so getRewrittenText |
| 4711 | // works as expected. In this case, what actually ends up on RHS |
| 4712 | // is the blockTranscribed which is the helper function for the |
| 4713 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4714 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4715 | DisableReplaceStmt = false; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4716 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4717 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4718 | CurrentBody = SaveCurrentBody; |
| 4719 | PropParentMap = 0; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4720 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4721 | // 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] | 4722 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4723 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4725 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4726 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4727 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4728 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4729 | return blockTranscribed; |
| 4730 | } |
| 4731 | // Handle specific things. |
| 4732 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4733 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4734 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4735 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4736 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4738 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4739 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4741 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4742 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | // Before we rewrite it, put the original message expression in a comment. |
| 4744 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4745 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4746 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4747 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4748 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4750 | std::string messString; |
| 4751 | messString += "// "; |
| 4752 | messString.append(startBuf, endBuf-startBuf+1); |
| 4753 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
| 4755 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4756 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4757 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4758 | // Tried this, but it didn't work either... |
| 4759 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4760 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4761 | return RewriteMessageExpr(MessExpr); |
| 4762 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4764 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4765 | return RewriteObjCTryStmt(StmtTry); |
| 4766 | |
| 4767 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4768 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4769 | |
| 4770 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4771 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4773 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4774 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | |
| 4776 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4777 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4779 | OrigStmtRange.getEnd()); |
| 4780 | if (BreakStmt *StmtBreakStmt = |
| 4781 | dyn_cast<BreakStmt>(S)) |
| 4782 | return RewriteBreakStmt(StmtBreakStmt); |
| 4783 | if (ContinueStmt *StmtContinueStmt = |
| 4784 | dyn_cast<ContinueStmt>(S)) |
| 4785 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | |
| 4787 | // 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] | 4788 | // and cast exprs. |
| 4789 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4790 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4791 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4792 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4793 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4794 | // the context of an ObjCForCollectionStmt. For example: |
| 4795 | // NSArray *someArray; |
| 4796 | // for (id <FooProtocol> index in someArray) ; |
| 4797 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4798 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4799 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4800 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4802 | // Blocks rewrite rules. |
| 4803 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4804 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4805 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4806 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4807 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4808 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4810 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4811 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4812 | if (VD->hasAttr<BlocksAttr>()) { |
| 4813 | static unsigned uniqueByrefDeclCount = 0; |
| 4814 | assert(!BlockByRefDeclNo.count(ND) && |
| 4815 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4816 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4817 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4818 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4819 | else |
| 4820 | RewriteTypeOfDecl(VD); |
| 4821 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4822 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4823 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4824 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4825 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4827 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4828 | } |
| 4829 | } |
| 4830 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4832 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4833 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | |
| 4835 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4836 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4837 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4838 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4839 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4840 | && "Statement stack mismatch"); |
| 4841 | Stmts.pop_back(); |
| 4842 | } |
| 4843 | // Handle blocks rewriting. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4844 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4845 | ValueDecl *VD = DRE->getDecl(); |
| 4846 | if (VD->hasAttr<BlocksAttr>()) |
| 4847 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4848 | if (HasLocalVariableExternalStorage(VD)) |
| 4849 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4852 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4853 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4854 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4855 | ReplaceStmt(S, BlockCall); |
| 4856 | return BlockCall; |
| 4857 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4858 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4859 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4860 | RewriteCastExpr(CE); |
| 4861 | } |
| 4862 | #if 0 |
| 4863 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4864 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4865 | ICE->getSubExpr(), |
| 4866 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4867 | // Get the new text. |
| 4868 | std::string SStr; |
| 4869 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4870 | Replacement->printPretty(Buf); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4871 | const std::string &Str = Buf.str(); |
| 4872 | |
| 4873 | printf("CAST = %s\n", &Str[0]); |
| 4874 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4875 | delete S; |
| 4876 | return Replacement; |
| 4877 | } |
| 4878 | #endif |
| 4879 | // Return this stmt unmodified. |
| 4880 | return S; |
| 4881 | } |
| 4882 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4883 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4884 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4885 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4886 | FieldDecl *FD = *i; |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4887 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4888 | RewriteBlockPointerDecl(FD); |
| 4889 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4890 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4891 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4892 | } |
| 4893 | } |
| 4894 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4895 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4896 | /// main file of the input. |
| 4897 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4898 | switch (D->getKind()) { |
| 4899 | case Decl::Function: { |
| 4900 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4901 | if (FD->isOverloadedOperator()) |
| 4902 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4904 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4905 | // prototype. This enables us to rewrite function declarations and |
| 4906 | // definitions using the same code. |
| 4907 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4908 | |
Argyrios Kyrtzidis | 75627ad | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4909 | if (!FD->isThisDeclarationADefinition()) |
| 4910 | break; |
| 4911 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4912 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4913 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4914 | CurFunctionDef = FD; |
| 4915 | CurFunctionDeclToDeclareForBlock = FD; |
| 4916 | CurrentBody = Body; |
| 4917 | Body = |
| 4918 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4919 | FD->setBody(Body); |
| 4920 | CurrentBody = 0; |
| 4921 | if (PropParentMap) { |
| 4922 | delete PropParentMap; |
| 4923 | PropParentMap = 0; |
| 4924 | } |
| 4925 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4926 | // and any copy/dispose helper functions. |
| 4927 | InsertBlockLiteralsWithinFunction(FD); |
| 4928 | CurFunctionDef = 0; |
| 4929 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4930 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4931 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4933 | case Decl::ObjCMethod: { |
| 4934 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4935 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4936 | CurMethodDef = MD; |
| 4937 | CurrentBody = Body; |
| 4938 | Body = |
| 4939 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4940 | MD->setBody(Body); |
| 4941 | CurrentBody = 0; |
| 4942 | if (PropParentMap) { |
| 4943 | delete PropParentMap; |
| 4944 | PropParentMap = 0; |
| 4945 | } |
| 4946 | InsertBlockLiteralsWithinMethod(MD); |
| 4947 | CurMethodDef = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4948 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4949 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4950 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4951 | case Decl::ObjCImplementation: { |
| 4952 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4953 | ClassImplementation.push_back(CI); |
| 4954 | break; |
| 4955 | } |
| 4956 | case Decl::ObjCCategoryImpl: { |
| 4957 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4958 | CategoryImplementation.push_back(CI); |
| 4959 | break; |
| 4960 | } |
| 4961 | case Decl::Var: { |
| 4962 | VarDecl *VD = cast<VarDecl>(D); |
| 4963 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4964 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4965 | RewriteBlockPointerDecl(VD); |
| 4966 | else if (VD->getType()->isFunctionPointerType()) { |
| 4967 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4968 | if (VD->getInit()) { |
| 4969 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4970 | RewriteCastExpr(CE); |
| 4971 | } |
| 4972 | } |
| 4973 | } else if (VD->getType()->isRecordType()) { |
| 4974 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4975 | if (RD->isCompleteDefinition()) |
| 4976 | RewriteRecordBody(RD); |
| 4977 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4978 | if (VD->getInit()) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4979 | GlobalVarDecl = VD; |
| 4980 | CurrentBody = VD->getInit(); |
| 4981 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4982 | CurrentBody = 0; |
| 4983 | if (PropParentMap) { |
| 4984 | delete PropParentMap; |
| 4985 | PropParentMap = 0; |
| 4986 | } |
| 4987 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4988 | GlobalVarDecl = 0; |
| 4989 | |
| 4990 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4991 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4992 | RewriteCastExpr(CE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4993 | } |
| 4994 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4995 | break; |
| 4996 | } |
| 4997 | case Decl::TypeAlias: |
| 4998 | case Decl::Typedef: { |
| 4999 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5000 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5001 | RewriteBlockPointerDecl(TD); |
| 5002 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5003 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5004 | } |
| 5005 | break; |
| 5006 | } |
| 5007 | case Decl::CXXRecord: |
| 5008 | case Decl::Record: { |
| 5009 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5010 | if (RD->isCompleteDefinition()) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5011 | RewriteRecordBody(RD); |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5012 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5013 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5014 | default: |
| 5015 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5016 | } |
| 5017 | // Nothing yet. |
| 5018 | } |
| 5019 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5020 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5021 | if (Diags.hasErrorOccurred()) |
| 5022 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5024 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5025 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5026 | // Here's a great place to add any extra declarations that may be needed. |
| 5027 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5028 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5029 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5030 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5031 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5032 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5033 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5034 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5035 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5036 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5037 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5038 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5039 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5040 | //printf("Changed:\n"); |
| 5041 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5042 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5043 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5044 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5045 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5046 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5047 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5048 | // Rewrite Objective-c meta data* |
| 5049 | std::string ResultStr; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5050 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5051 | // Emit metadata. |
| 5052 | *OutFile << ResultStr; |
| 5053 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5054 | OutFile->flush(); |
| 5055 | } |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5056 | |
| 5057 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5058 | InitializeCommon(context); |
| 5059 | |
| 5060 | // declaring objc_selector outside the parameter list removes a silly |
| 5061 | // scope related warning... |
| 5062 | if (IsHeader) |
| 5063 | Preamble = "#pragma once\n"; |
| 5064 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5065 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5066 | Preamble += "struct objc_object *superClass; "; |
| 5067 | if (LangOpts.MicrosoftExt) { |
| 5068 | // Add a constructor for creating temporary objects. |
| 5069 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5070 | ": "; |
| 5071 | Preamble += "object(o), superClass(s) {} "; |
| 5072 | } |
| 5073 | Preamble += "};\n"; |
| 5074 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5075 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5076 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5077 | Preamble += "#endif\n"; |
| 5078 | if (LangOpts.MicrosoftExt) { |
| 5079 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5080 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5081 | } else |
| 5082 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5083 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5084 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5085 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5086 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5087 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5088 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5089 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5090 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5091 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5092 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5093 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5094 | Preamble += "(const char *);\n"; |
| 5095 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5096 | Preamble += "(struct objc_class *);\n"; |
| 5097 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5098 | Preamble += "(const char *);\n"; |
| 5099 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5100 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5101 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5102 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5103 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5104 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5105 | // @synchronized hooks. |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5106 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5107 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5108 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5109 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5110 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5111 | Preamble += "unsigned long state;\n\t"; |
| 5112 | Preamble += "void **itemsPtr;\n\t"; |
| 5113 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5114 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5115 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5116 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5117 | Preamble += "#endif\n"; |
| 5118 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5119 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5120 | Preamble += " int *isa;\n"; |
| 5121 | Preamble += " int flags;\n"; |
| 5122 | Preamble += " char *str;\n"; |
| 5123 | Preamble += " long length;\n"; |
| 5124 | Preamble += "};\n"; |
| 5125 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5126 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5127 | Preamble += "#else\n"; |
| 5128 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5129 | Preamble += "#endif\n"; |
| 5130 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5131 | Preamble += "#endif\n"; |
| 5132 | // Blocks preamble. |
| 5133 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5134 | Preamble += "#define BLOCK_IMPL\n"; |
| 5135 | Preamble += "struct __block_impl {\n"; |
| 5136 | Preamble += " void *isa;\n"; |
| 5137 | Preamble += " int Flags;\n"; |
| 5138 | Preamble += " int Reserved;\n"; |
| 5139 | Preamble += " void *FuncPtr;\n"; |
| 5140 | Preamble += "};\n"; |
| 5141 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5142 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5143 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5144 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5145 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5146 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5147 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5148 | Preamble += "#else\n"; |
| 5149 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5150 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5151 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5152 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5153 | Preamble += "#endif\n"; |
| 5154 | Preamble += "#endif\n"; |
| 5155 | if (LangOpts.MicrosoftExt) { |
| 5156 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5157 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5158 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5159 | Preamble += "#define __attribute__(X)\n"; |
| 5160 | Preamble += "#endif\n"; |
| 5161 | Preamble += "#define __weak\n"; |
| 5162 | } |
| 5163 | else { |
| 5164 | Preamble += "#define __block\n"; |
| 5165 | Preamble += "#define __weak\n"; |
| 5166 | } |
| 5167 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5168 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5169 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5170 | } |
| 5171 | |
| 5172 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5173 | /// ivar offset. |
| 5174 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5175 | std::string &Result) { |
| 5176 | if (ivar->isBitField()) { |
| 5177 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5178 | // place all bitfields at offset 0. |
| 5179 | Result += "0"; |
| 5180 | } else { |
| 5181 | Result += "__OFFSETOFIVAR__(struct "; |
| 5182 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5183 | if (LangOpts.MicrosoftExt) |
| 5184 | Result += "_IMPL"; |
| 5185 | Result += ", "; |
| 5186 | Result += ivar->getNameAsString(); |
| 5187 | Result += ")"; |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5192 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5193 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5194 | StringRef ClassName, std::string &Result) { |
| 5195 | static bool objc_protocol_methods = false; |
| 5196 | |
| 5197 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5198 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5199 | /* struct protocol_methods { |
| 5200 | SEL _cmd; |
| 5201 | char *method_types; |
| 5202 | } |
| 5203 | */ |
| 5204 | Result += "\nstruct _protocol_methods {\n"; |
| 5205 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5206 | Result += "\tchar *method_types;\n"; |
| 5207 | Result += "};\n"; |
| 5208 | |
| 5209 | objc_protocol_methods = true; |
| 5210 | } |
| 5211 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5212 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5213 | return; |
| 5214 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5215 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5216 | PDecl = Def; |
| 5217 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5218 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5219 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5220 | PDecl->instmeth_end()); |
| 5221 | /* struct _objc_protocol_method_list { |
| 5222 | int protocol_method_count; |
| 5223 | struct protocol_methods protocols[]; |
| 5224 | } |
| 5225 | */ |
| 5226 | Result += "\nstatic struct {\n"; |
| 5227 | Result += "\tint protocol_method_count;\n"; |
| 5228 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5229 | Result += utostr(NumMethods); |
| 5230 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5231 | Result += PDecl->getNameAsString(); |
| 5232 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5233 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5234 | |
| 5235 | // Output instance methods declared in this protocol. |
| 5236 | for (ObjCProtocolDecl::instmeth_iterator |
| 5237 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5238 | I != E; ++I) { |
| 5239 | if (I == PDecl->instmeth_begin()) |
| 5240 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5241 | else |
| 5242 | Result += "\t ,{(struct objc_selector *)\""; |
| 5243 | Result += (*I)->getSelector().getAsString(); |
| 5244 | std::string MethodTypeString; |
| 5245 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5246 | Result += "\", \""; |
| 5247 | Result += MethodTypeString; |
| 5248 | Result += "\"}\n"; |
| 5249 | } |
| 5250 | Result += "\t }\n};\n"; |
| 5251 | } |
| 5252 | |
| 5253 | // Output class methods declared in this protocol. |
| 5254 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5255 | PDecl->classmeth_end()); |
| 5256 | if (NumMethods > 0) { |
| 5257 | /* struct _objc_protocol_method_list { |
| 5258 | int protocol_method_count; |
| 5259 | struct protocol_methods protocols[]; |
| 5260 | } |
| 5261 | */ |
| 5262 | Result += "\nstatic struct {\n"; |
| 5263 | Result += "\tint protocol_method_count;\n"; |
| 5264 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5265 | Result += utostr(NumMethods); |
| 5266 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5267 | Result += PDecl->getNameAsString(); |
| 5268 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5269 | "{\n\t"; |
| 5270 | Result += utostr(NumMethods); |
| 5271 | Result += "\n"; |
| 5272 | |
| 5273 | // Output instance methods declared in this protocol. |
| 5274 | for (ObjCProtocolDecl::classmeth_iterator |
| 5275 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5276 | I != E; ++I) { |
| 5277 | if (I == PDecl->classmeth_begin()) |
| 5278 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5279 | else |
| 5280 | Result += "\t ,{(struct objc_selector *)\""; |
| 5281 | Result += (*I)->getSelector().getAsString(); |
| 5282 | std::string MethodTypeString; |
| 5283 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5284 | Result += "\", \""; |
| 5285 | Result += MethodTypeString; |
| 5286 | Result += "\"}\n"; |
| 5287 | } |
| 5288 | Result += "\t }\n};\n"; |
| 5289 | } |
| 5290 | |
| 5291 | // Output: |
| 5292 | /* struct _objc_protocol { |
| 5293 | // Objective-C 1.0 extensions |
| 5294 | struct _objc_protocol_extension *isa; |
| 5295 | char *protocol_name; |
| 5296 | struct _objc_protocol **protocol_list; |
| 5297 | struct _objc_protocol_method_list *instance_methods; |
| 5298 | struct _objc_protocol_method_list *class_methods; |
| 5299 | }; |
| 5300 | */ |
| 5301 | static bool objc_protocol = false; |
| 5302 | if (!objc_protocol) { |
| 5303 | Result += "\nstruct _objc_protocol {\n"; |
| 5304 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5305 | Result += "\tchar *protocol_name;\n"; |
| 5306 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5307 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5308 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5309 | Result += "};\n"; |
| 5310 | |
| 5311 | objc_protocol = true; |
| 5312 | } |
| 5313 | |
| 5314 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5315 | Result += PDecl->getNameAsString(); |
| 5316 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5317 | "{\n\t0, \""; |
| 5318 | Result += PDecl->getNameAsString(); |
| 5319 | Result += "\", 0, "; |
| 5320 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5321 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5322 | Result += PDecl->getNameAsString(); |
| 5323 | Result += ", "; |
| 5324 | } |
| 5325 | else |
| 5326 | Result += "0, "; |
| 5327 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5328 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5329 | Result += PDecl->getNameAsString(); |
| 5330 | Result += "\n"; |
| 5331 | } |
| 5332 | else |
| 5333 | Result += "0\n"; |
| 5334 | Result += "};\n"; |
| 5335 | |
| 5336 | // Mark this protocol as having been generated. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5337 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5338 | llvm_unreachable("protocol already synthesized"); |
| 5339 | |
| 5340 | } |
| 5341 | |
| 5342 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5343 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5344 | StringRef prefix, StringRef ClassName, |
| 5345 | std::string &Result) { |
| 5346 | if (Protocols.empty()) return; |
| 5347 | |
| 5348 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5349 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5350 | |
| 5351 | // Output the top lovel protocol meta-data for the class. |
| 5352 | /* struct _objc_protocol_list { |
| 5353 | struct _objc_protocol_list *next; |
| 5354 | int protocol_count; |
| 5355 | struct _objc_protocol *class_protocols[]; |
| 5356 | } |
| 5357 | */ |
| 5358 | Result += "\nstatic struct {\n"; |
| 5359 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5360 | Result += "\tint protocol_count;\n"; |
| 5361 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5362 | Result += utostr(Protocols.size()); |
| 5363 | Result += "];\n} _OBJC_"; |
| 5364 | Result += prefix; |
| 5365 | Result += "_PROTOCOLS_"; |
| 5366 | Result += ClassName; |
| 5367 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5368 | "{\n\t0, "; |
| 5369 | Result += utostr(Protocols.size()); |
| 5370 | Result += "\n"; |
| 5371 | |
| 5372 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5373 | Result += Protocols[0]->getNameAsString(); |
| 5374 | Result += " \n"; |
| 5375 | |
| 5376 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5377 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5378 | Result += Protocols[i]->getNameAsString(); |
| 5379 | Result += "\n"; |
| 5380 | } |
| 5381 | Result += "\t }\n};\n"; |
| 5382 | } |
| 5383 | |
| 5384 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5385 | std::string &Result) { |
| 5386 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5387 | |
| 5388 | // Explicitly declared @interface's are already synthesized. |
| 5389 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5390 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5391 | // produce correct synthesis as yet. |
| 5392 | RewriteObjCInternalStruct(CDecl, Result); |
| 5393 | } |
| 5394 | |
| 5395 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5396 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5397 | ? IDecl->ivar_size() |
| 5398 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5399 | if (NumIvars > 0) { |
| 5400 | static bool objc_ivar = false; |
| 5401 | if (!objc_ivar) { |
| 5402 | /* struct _objc_ivar { |
| 5403 | char *ivar_name; |
| 5404 | char *ivar_type; |
| 5405 | int ivar_offset; |
| 5406 | }; |
| 5407 | */ |
| 5408 | Result += "\nstruct _objc_ivar {\n"; |
| 5409 | Result += "\tchar *ivar_name;\n"; |
| 5410 | Result += "\tchar *ivar_type;\n"; |
| 5411 | Result += "\tint ivar_offset;\n"; |
| 5412 | Result += "};\n"; |
| 5413 | |
| 5414 | objc_ivar = true; |
| 5415 | } |
| 5416 | |
| 5417 | /* struct { |
| 5418 | int ivar_count; |
| 5419 | struct _objc_ivar ivar_list[nIvars]; |
| 5420 | }; |
| 5421 | */ |
| 5422 | Result += "\nstatic struct {\n"; |
| 5423 | Result += "\tint ivar_count;\n"; |
| 5424 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5425 | Result += utostr(NumIvars); |
| 5426 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5427 | Result += IDecl->getNameAsString(); |
| 5428 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5429 | "{\n\t"; |
| 5430 | Result += utostr(NumIvars); |
| 5431 | Result += "\n"; |
| 5432 | |
| 5433 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5434 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5435 | if (!IDecl->ivar_empty()) { |
| 5436 | for (ObjCInterfaceDecl::ivar_iterator |
| 5437 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5438 | IV != IVEnd; ++IV) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5439 | IVars.push_back(*IV); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5440 | IVI = IDecl->ivar_begin(); |
| 5441 | IVE = IDecl->ivar_end(); |
| 5442 | } else { |
| 5443 | IVI = CDecl->ivar_begin(); |
| 5444 | IVE = CDecl->ivar_end(); |
| 5445 | } |
| 5446 | Result += "\t,{{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5447 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5448 | Result += "\", \""; |
| 5449 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5450 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5451 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5452 | Result += StrEncoding; |
| 5453 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5454 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5455 | Result += "}\n"; |
| 5456 | for (++IVI; IVI != IVE; ++IVI) { |
| 5457 | Result += "\t ,{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5458 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5459 | Result += "\", \""; |
| 5460 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5461 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5462 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5463 | Result += StrEncoding; |
| 5464 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5465 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5466 | Result += "}\n"; |
| 5467 | } |
| 5468 | |
| 5469 | Result += "\t }\n};\n"; |
| 5470 | } |
| 5471 | |
| 5472 | // Build _objc_method_list for class's instance methods if needed |
| 5473 | SmallVector<ObjCMethodDecl *, 32> |
| 5474 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5475 | |
| 5476 | // If any of our property implementations have associated getters or |
| 5477 | // setters, produce metadata for them as well. |
| 5478 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5479 | PropEnd = IDecl->propimpl_end(); |
| 5480 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5481 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5482 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5483 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5484 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5485 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5486 | if (!PD) |
| 5487 | continue; |
| 5488 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5489 | if (!Getter->isDefined()) |
| 5490 | InstanceMethods.push_back(Getter); |
| 5491 | if (PD->isReadOnly()) |
| 5492 | continue; |
| 5493 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5494 | if (!Setter->isDefined()) |
| 5495 | InstanceMethods.push_back(Setter); |
| 5496 | } |
| 5497 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5498 | true, "", IDecl->getName(), Result); |
| 5499 | |
| 5500 | // Build _objc_method_list for class's class methods if needed |
| 5501 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5502 | false, "", IDecl->getName(), Result); |
| 5503 | |
| 5504 | // Protocols referenced in class declaration? |
| 5505 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5506 | "CLASS", CDecl->getName(), Result); |
| 5507 | |
| 5508 | // Declaration of class/meta-class metadata |
| 5509 | /* struct _objc_class { |
| 5510 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5511 | const char *super_class_name; |
| 5512 | char *name; |
| 5513 | long version; |
| 5514 | long info; |
| 5515 | long instance_size; |
| 5516 | struct _objc_ivar_list *ivars; |
| 5517 | struct _objc_method_list *methods; |
| 5518 | struct objc_cache *cache; |
| 5519 | struct objc_protocol_list *protocols; |
| 5520 | const char *ivar_layout; |
| 5521 | struct _objc_class_ext *ext; |
| 5522 | }; |
| 5523 | */ |
| 5524 | static bool objc_class = false; |
| 5525 | if (!objc_class) { |
| 5526 | Result += "\nstruct _objc_class {\n"; |
| 5527 | Result += "\tstruct _objc_class *isa;\n"; |
| 5528 | Result += "\tconst char *super_class_name;\n"; |
| 5529 | Result += "\tchar *name;\n"; |
| 5530 | Result += "\tlong version;\n"; |
| 5531 | Result += "\tlong info;\n"; |
| 5532 | Result += "\tlong instance_size;\n"; |
| 5533 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5534 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5535 | Result += "\tstruct objc_cache *cache;\n"; |
| 5536 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5537 | Result += "\tconst char *ivar_layout;\n"; |
| 5538 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5539 | Result += "};\n"; |
| 5540 | objc_class = true; |
| 5541 | } |
| 5542 | |
| 5543 | // Meta-class metadata generation. |
| 5544 | ObjCInterfaceDecl *RootClass = 0; |
| 5545 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5546 | while (SuperClass) { |
| 5547 | RootClass = SuperClass; |
| 5548 | SuperClass = SuperClass->getSuperClass(); |
| 5549 | } |
| 5550 | SuperClass = CDecl->getSuperClass(); |
| 5551 | |
| 5552 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5553 | Result += CDecl->getNameAsString(); |
| 5554 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5555 | "{\n\t(struct _objc_class *)\""; |
| 5556 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5557 | Result += "\""; |
| 5558 | |
| 5559 | if (SuperClass) { |
| 5560 | Result += ", \""; |
| 5561 | Result += SuperClass->getNameAsString(); |
| 5562 | Result += "\", \""; |
| 5563 | Result += CDecl->getNameAsString(); |
| 5564 | Result += "\""; |
| 5565 | } |
| 5566 | else { |
| 5567 | Result += ", 0, \""; |
| 5568 | Result += CDecl->getNameAsString(); |
| 5569 | Result += "\""; |
| 5570 | } |
| 5571 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5572 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5573 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5574 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5575 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5576 | Result += IDecl->getNameAsString(); |
| 5577 | Result += "\n"; |
| 5578 | } |
| 5579 | else |
| 5580 | Result += ", 0\n"; |
| 5581 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5582 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5583 | Result += CDecl->getNameAsString(); |
| 5584 | Result += ",0,0\n"; |
| 5585 | } |
| 5586 | else |
| 5587 | Result += "\t,0,0,0,0\n"; |
| 5588 | Result += "};\n"; |
| 5589 | |
| 5590 | // class metadata generation. |
| 5591 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5592 | Result += CDecl->getNameAsString(); |
| 5593 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5594 | "{\n\t&_OBJC_METACLASS_"; |
| 5595 | Result += CDecl->getNameAsString(); |
| 5596 | if (SuperClass) { |
| 5597 | Result += ", \""; |
| 5598 | Result += SuperClass->getNameAsString(); |
| 5599 | Result += "\", \""; |
| 5600 | Result += CDecl->getNameAsString(); |
| 5601 | Result += "\""; |
| 5602 | } |
| 5603 | else { |
| 5604 | Result += ", 0, \""; |
| 5605 | Result += CDecl->getNameAsString(); |
| 5606 | Result += "\""; |
| 5607 | } |
| 5608 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5609 | Result += ", 0,1"; |
| 5610 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5611 | Result += ",0"; |
| 5612 | else { |
| 5613 | // class has size. Must synthesize its size. |
| 5614 | Result += ",sizeof(struct "; |
| 5615 | Result += CDecl->getNameAsString(); |
| 5616 | if (LangOpts.MicrosoftExt) |
| 5617 | Result += "_IMPL"; |
| 5618 | Result += ")"; |
| 5619 | } |
| 5620 | if (NumIvars > 0) { |
| 5621 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5622 | Result += CDecl->getNameAsString(); |
| 5623 | Result += "\n\t"; |
| 5624 | } |
| 5625 | else |
| 5626 | Result += ",0"; |
| 5627 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5628 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5629 | Result += CDecl->getNameAsString(); |
| 5630 | Result += ", 0\n\t"; |
| 5631 | } |
| 5632 | else |
| 5633 | Result += ",0,0"; |
| 5634 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5635 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5636 | Result += CDecl->getNameAsString(); |
| 5637 | Result += ", 0,0\n"; |
| 5638 | } |
| 5639 | else |
| 5640 | Result += ",0,0,0\n"; |
| 5641 | Result += "};\n"; |
| 5642 | } |
| 5643 | |
| 5644 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5645 | int ClsDefCount = ClassImplementation.size(); |
| 5646 | int CatDefCount = CategoryImplementation.size(); |
| 5647 | |
| 5648 | // For each implemented class, write out all its meta data. |
| 5649 | for (int i = 0; i < ClsDefCount; i++) |
| 5650 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5651 | |
| 5652 | // For each implemented category, write out all its meta data. |
| 5653 | for (int i = 0; i < CatDefCount; i++) |
| 5654 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5655 | |
| 5656 | // Write objc_symtab metadata |
| 5657 | /* |
| 5658 | struct _objc_symtab |
| 5659 | { |
| 5660 | long sel_ref_cnt; |
| 5661 | SEL *refs; |
| 5662 | short cls_def_cnt; |
| 5663 | short cat_def_cnt; |
| 5664 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5665 | }; |
| 5666 | */ |
| 5667 | |
| 5668 | Result += "\nstruct _objc_symtab {\n"; |
| 5669 | Result += "\tlong sel_ref_cnt;\n"; |
| 5670 | Result += "\tSEL *refs;\n"; |
| 5671 | Result += "\tshort cls_def_cnt;\n"; |
| 5672 | Result += "\tshort cat_def_cnt;\n"; |
| 5673 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5674 | Result += "};\n\n"; |
| 5675 | |
| 5676 | Result += "static struct _objc_symtab " |
| 5677 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5678 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5679 | + ", " + utostr(CatDefCount) + "\n"; |
| 5680 | for (int i = 0; i < ClsDefCount; i++) { |
| 5681 | Result += "\t,&_OBJC_CLASS_"; |
| 5682 | Result += ClassImplementation[i]->getNameAsString(); |
| 5683 | Result += "\n"; |
| 5684 | } |
| 5685 | |
| 5686 | for (int i = 0; i < CatDefCount; i++) { |
| 5687 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5688 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5689 | Result += "_"; |
| 5690 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5691 | Result += "\n"; |
| 5692 | } |
| 5693 | |
| 5694 | Result += "};\n\n"; |
| 5695 | |
| 5696 | // Write objc_module metadata |
| 5697 | |
| 5698 | /* |
| 5699 | struct _objc_module { |
| 5700 | long version; |
| 5701 | long size; |
| 5702 | const char *name; |
| 5703 | struct _objc_symtab *symtab; |
| 5704 | } |
| 5705 | */ |
| 5706 | |
| 5707 | Result += "\nstruct _objc_module {\n"; |
| 5708 | Result += "\tlong version;\n"; |
| 5709 | Result += "\tlong size;\n"; |
| 5710 | Result += "\tconst char *name;\n"; |
| 5711 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5712 | Result += "};\n\n"; |
| 5713 | Result += "static struct _objc_module " |
| 5714 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5715 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5716 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5717 | Result += "};\n\n"; |
| 5718 | |
| 5719 | if (LangOpts.MicrosoftExt) { |
| 5720 | if (ProtocolExprDecls.size()) { |
| 5721 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5722 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5723 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5724 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5725 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5726 | Result += (*I)->getNameAsString(); |
| 5727 | Result += " = &_OBJC_PROTOCOL_"; |
| 5728 | Result += (*I)->getNameAsString(); |
| 5729 | Result += ";\n"; |
| 5730 | } |
| 5731 | Result += "#pragma data_seg(pop)\n\n"; |
| 5732 | } |
| 5733 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5734 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5735 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5736 | Result += "&_OBJC_MODULES;\n"; |
| 5737 | Result += "#pragma data_seg(pop)\n\n"; |
| 5738 | } |
| 5739 | } |
| 5740 | |
| 5741 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5742 | /// implementation. |
| 5743 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5744 | std::string &Result) { |
| 5745 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5746 | // Find category declaration for this implementation. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5747 | ObjCCategoryDecl *CDecl |
| 5748 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5749 | |
| 5750 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5751 | FullCategoryName += '_'; |
| 5752 | FullCategoryName += IDecl->getNameAsString(); |
| 5753 | |
| 5754 | // Build _objc_method_list for class's instance methods if needed |
| 5755 | SmallVector<ObjCMethodDecl *, 32> |
| 5756 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5757 | |
| 5758 | // If any of our property implementations have associated getters or |
| 5759 | // setters, produce metadata for them as well. |
| 5760 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5761 | PropEnd = IDecl->propimpl_end(); |
| 5762 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5763 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5764 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5765 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5766 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5767 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5768 | if (!PD) |
| 5769 | continue; |
| 5770 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5771 | InstanceMethods.push_back(Getter); |
| 5772 | if (PD->isReadOnly()) |
| 5773 | continue; |
| 5774 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5775 | InstanceMethods.push_back(Setter); |
| 5776 | } |
| 5777 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5778 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5779 | Result); |
| 5780 | |
| 5781 | // Build _objc_method_list for class's class methods if needed |
| 5782 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5783 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5784 | Result); |
| 5785 | |
| 5786 | // Protocols referenced in class declaration? |
| 5787 | // Null CDecl is case of a category implementation with no category interface |
| 5788 | if (CDecl) |
| 5789 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5790 | FullCategoryName, Result); |
| 5791 | /* struct _objc_category { |
| 5792 | char *category_name; |
| 5793 | char *class_name; |
| 5794 | struct _objc_method_list *instance_methods; |
| 5795 | struct _objc_method_list *class_methods; |
| 5796 | struct _objc_protocol_list *protocols; |
| 5797 | // Objective-C 1.0 extensions |
| 5798 | uint32_t size; // sizeof (struct _objc_category) |
| 5799 | struct _objc_property_list *instance_properties; // category's own |
| 5800 | // @property decl. |
| 5801 | }; |
| 5802 | */ |
| 5803 | |
| 5804 | static bool objc_category = false; |
| 5805 | if (!objc_category) { |
| 5806 | Result += "\nstruct _objc_category {\n"; |
| 5807 | Result += "\tchar *category_name;\n"; |
| 5808 | Result += "\tchar *class_name;\n"; |
| 5809 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5810 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5811 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5812 | Result += "\tunsigned int size;\n"; |
| 5813 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5814 | Result += "};\n"; |
| 5815 | objc_category = true; |
| 5816 | } |
| 5817 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5818 | Result += FullCategoryName; |
| 5819 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5820 | Result += IDecl->getNameAsString(); |
| 5821 | Result += "\"\n\t, \""; |
| 5822 | Result += ClassDecl->getNameAsString(); |
| 5823 | Result += "\"\n"; |
| 5824 | |
| 5825 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5826 | Result += "\t, (struct _objc_method_list *)" |
| 5827 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5828 | Result += FullCategoryName; |
| 5829 | Result += "\n"; |
| 5830 | } |
| 5831 | else |
| 5832 | Result += "\t, 0\n"; |
| 5833 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5834 | Result += "\t, (struct _objc_method_list *)" |
| 5835 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5836 | Result += FullCategoryName; |
| 5837 | Result += "\n"; |
| 5838 | } |
| 5839 | else |
| 5840 | Result += "\t, 0\n"; |
| 5841 | |
| 5842 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5843 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5844 | Result += FullCategoryName; |
| 5845 | Result += "\n"; |
| 5846 | } |
| 5847 | else |
| 5848 | Result += "\t, 0\n"; |
| 5849 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5850 | } |
| 5851 | |
| 5852 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5853 | /// class methods. |
| 5854 | template<typename MethodIterator> |
| 5855 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5856 | MethodIterator MethodEnd, |
| 5857 | bool IsInstanceMethod, |
| 5858 | StringRef prefix, |
| 5859 | StringRef ClassName, |
| 5860 | std::string &Result) { |
| 5861 | if (MethodBegin == MethodEnd) return; |
| 5862 | |
| 5863 | if (!objc_impl_method) { |
| 5864 | /* struct _objc_method { |
| 5865 | SEL _cmd; |
| 5866 | char *method_types; |
| 5867 | void *_imp; |
| 5868 | } |
| 5869 | */ |
| 5870 | Result += "\nstruct _objc_method {\n"; |
| 5871 | Result += "\tSEL _cmd;\n"; |
| 5872 | Result += "\tchar *method_types;\n"; |
| 5873 | Result += "\tvoid *_imp;\n"; |
| 5874 | Result += "};\n"; |
| 5875 | |
| 5876 | objc_impl_method = true; |
| 5877 | } |
| 5878 | |
| 5879 | // Build _objc_method_list for class's methods if needed |
| 5880 | |
| 5881 | /* struct { |
| 5882 | struct _objc_method_list *next_method; |
| 5883 | int method_count; |
| 5884 | struct _objc_method method_list[]; |
| 5885 | } |
| 5886 | */ |
| 5887 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5888 | Result += "\nstatic struct {\n"; |
| 5889 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5890 | Result += "\tint method_count;\n"; |
| 5891 | Result += "\tstruct _objc_method method_list["; |
| 5892 | Result += utostr(NumMethods); |
| 5893 | Result += "];\n} _OBJC_"; |
| 5894 | Result += prefix; |
| 5895 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5896 | Result += "_METHODS_"; |
| 5897 | Result += ClassName; |
| 5898 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5899 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5900 | Result += "_meth\")))= "; |
| 5901 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5902 | |
| 5903 | Result += "\t,{{(SEL)\""; |
| 5904 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5905 | std::string MethodTypeString; |
| 5906 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5907 | Result += "\", \""; |
| 5908 | Result += MethodTypeString; |
| 5909 | Result += "\", (void *)"; |
| 5910 | Result += MethodInternalNames[*MethodBegin]; |
| 5911 | Result += "}\n"; |
| 5912 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5913 | Result += "\t ,{(SEL)\""; |
| 5914 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5915 | std::string MethodTypeString; |
| 5916 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5917 | Result += "\", \""; |
| 5918 | Result += MethodTypeString; |
| 5919 | Result += "\", (void *)"; |
| 5920 | Result += MethodInternalNames[*MethodBegin]; |
| 5921 | Result += "}\n"; |
| 5922 | } |
| 5923 | Result += "\t }\n};\n"; |
| 5924 | } |
| 5925 | |
| 5926 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5927 | SourceRange OldRange = IV->getSourceRange(); |
| 5928 | Expr *BaseExpr = IV->getBase(); |
| 5929 | |
| 5930 | // Rewrite the base, but without actually doing replaces. |
| 5931 | { |
| 5932 | DisableReplaceStmtScope S(*this); |
| 5933 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5934 | IV->setBase(BaseExpr); |
| 5935 | } |
| 5936 | |
| 5937 | ObjCIvarDecl *D = IV->getDecl(); |
| 5938 | |
| 5939 | Expr *Replacement = IV; |
| 5940 | if (CurMethodDef) { |
| 5941 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5942 | const ObjCInterfaceType *iFaceDecl = |
| 5943 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5944 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5945 | // lookup which class implements the instance variable. |
| 5946 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5947 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5948 | clsDeclared); |
| 5949 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5950 | |
| 5951 | // Synthesize an explicit cast to gain access to the ivar. |
| 5952 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5953 | RecName += "_IMPL"; |
| 5954 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5955 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5956 | SourceLocation(), SourceLocation(), |
| 5957 | II); |
| 5958 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5959 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5960 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5961 | CK_BitCast, |
| 5962 | IV->getBase()); |
| 5963 | // Don't forget the parens to enforce the proper binding. |
| 5964 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5965 | OldRange.getEnd(), |
| 5966 | castExpr); |
| 5967 | if (IV->isFreeIvar() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5968 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5969 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5970 | IV->getLocation(), |
| 5971 | D->getType(), |
| 5972 | VK_LValue, OK_Ordinary); |
| 5973 | Replacement = ME; |
| 5974 | } else { |
| 5975 | IV->setBase(PE); |
| 5976 | } |
| 5977 | } |
| 5978 | } else { // we are outside a method. |
| 5979 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5980 | |
| 5981 | // Explicit ivar refs need to have a cast inserted. |
| 5982 | // FIXME: consider sharing some of this code with the code above. |
| 5983 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5984 | const ObjCInterfaceType *iFaceDecl = |
| 5985 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5986 | // lookup which class implements the instance variable. |
| 5987 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5988 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5989 | clsDeclared); |
| 5990 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5991 | |
| 5992 | // Synthesize an explicit cast to gain access to the ivar. |
| 5993 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5994 | RecName += "_IMPL"; |
| 5995 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5996 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5997 | SourceLocation(), SourceLocation(), |
| 5998 | II); |
| 5999 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 6000 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 6001 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 6002 | CK_BitCast, |
| 6003 | IV->getBase()); |
| 6004 | // Don't forget the parens to enforce the proper binding. |
| 6005 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 6006 | IV->getBase()->getLocEnd(), castExpr); |
| 6007 | // Cannot delete IV->getBase(), since PE points to it. |
| 6008 | // Replace the old base with the cast. This is important when doing |
| 6009 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 6010 | IV->setBase(PE); |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 6015 | return Replacement; |
| 6016 | } |