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 | } |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 504 | |
| 505 | StringLiteral *getStringLiteral(StringRef Str) { |
| 506 | QualType StrType = Context->getConstantArrayType( |
| 507 | Context->CharTy, llvm::APInt(32, Str.size() + 1), ArrayType::Normal, |
| 508 | 0); |
| 509 | return StringLiteral::Create(*Context, Str, StringLiteral::Ascii, |
| 510 | /*Pascal=*/false, StrType, SourceLocation()); |
| 511 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 512 | }; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 513 | |
| 514 | class RewriteObjCFragileABI : public RewriteObjC { |
| 515 | public: |
| 516 | |
| 517 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 518 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 519 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 520 | D, LOpts, |
| 521 | silenceMacroWarn) {} |
| 522 | |
| 523 | ~RewriteObjCFragileABI() {} |
| 524 | virtual void Initialize(ASTContext &context); |
| 525 | |
| 526 | // Rewriting metadata |
| 527 | template<typename MethodIterator> |
| 528 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 529 | MethodIterator MethodEnd, |
| 530 | bool IsInstanceMethod, |
| 531 | StringRef prefix, |
| 532 | StringRef ClassName, |
| 533 | std::string &Result); |
| 534 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 535 | StringRef prefix, |
| 536 | StringRef ClassName, |
| 537 | std::string &Result); |
| 538 | virtual void RewriteObjCProtocolListMetaData( |
| 539 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 540 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 541 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 542 | std::string &Result); |
| 543 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 544 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 545 | std::string &Result); |
| 546 | |
| 547 | // Rewriting ivar |
| 548 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 549 | std::string &Result); |
| 550 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 551 | }; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 555 | NamedDecl *D) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 556 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 557 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 558 | for (FunctionProtoType::param_type_iterator I = fproto->param_type_begin(), |
| 559 | E = fproto->param_type_end(); |
| 560 | I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 561 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 562 | // All the args are checked/rewritten. Don't call twice! |
| 563 | RewriteBlockPointerDecl(D); |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 570 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 571 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 572 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 575 | static bool IsHeaderFile(const std::string &Filename) { |
| 576 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 578 | if (DotPos == std::string::npos) { |
| 579 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 583 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 584 | // C header: .h |
| 585 | // C++ header: .hh or .H; |
| 586 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 589 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 590 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 591 | bool silenceMacroWarn) |
| 592 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 593 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 594 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 595 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 596 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 597 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 598 | DiagnosticsEngine::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 599 | "rewriter doesn't support user-specified control flow semantics " |
| 600 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 603 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 604 | raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 605 | DiagnosticsEngine &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 606 | const LangOptions &LOpts, |
| 607 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 608 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 611 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 612 | Context = &context; |
| 613 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 614 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 615 | MsgSendFunctionDecl = 0; |
| 616 | MsgSendSuperFunctionDecl = 0; |
| 617 | MsgSendStretFunctionDecl = 0; |
| 618 | MsgSendSuperStretFunctionDecl = 0; |
| 619 | MsgSendFpretFunctionDecl = 0; |
| 620 | GetClassFunctionDecl = 0; |
| 621 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 622 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 623 | SelGetUidFunctionDecl = 0; |
| 624 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 625 | ConstantStringClassReference = 0; |
| 626 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 627 | CurMethodDef = 0; |
| 628 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 629 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 630 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 631 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 632 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 633 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 634 | BcLabelCount = 0; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 635 | SuperConstructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 636 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 637 | PropParentMap = 0; |
| 638 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 639 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 640 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 642 | // Get the ID and start/end of the main file. |
| 643 | MainFileID = SM->getMainFileID(); |
| 644 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 645 | MainFileStart = MainBuf->getBufferStart(); |
| 646 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 648 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 651 | //===----------------------------------------------------------------------===// |
| 652 | // Top Level Driver Code |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 655 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 656 | if (Diags.hasErrorOccurred()) |
| 657 | return; |
| 658 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 659 | // Two cases: either the decl could be in the main file, or it could be in a |
| 660 | // #included file. If the former, rewrite it now. If the later, check to see |
| 661 | // if we rewrote the #include/#import. |
| 662 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 663 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 665 | // If this is for a builtin, ignore it. |
| 666 | if (Loc.isInvalid()) return; |
| 667 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 668 | // Look for built-in declarations that we need to refer during the rewrite. |
| 669 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 670 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 671 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 672 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 673 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 674 | ConstantStringClassReference = FVD; |
| 675 | return; |
| 676 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 677 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 678 | if (ID->isThisDeclarationADefinition()) |
| 679 | RewriteInterfaceDecl(ID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 680 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 681 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 682 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 683 | if (PD->isThisDeclarationADefinition()) |
| 684 | RewriteProtocolDecl(PD); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 685 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 686 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 687 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 688 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 689 | DI != DIEnd; ) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 690 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 691 | if (!IFace->isThisDeclarationADefinition()) { |
| 692 | SmallVector<Decl *, 8> DG; |
| 693 | SourceLocation StartLoc = IFace->getLocStart(); |
| 694 | do { |
| 695 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 696 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 697 | StartLoc == (*DI)->getLocStart()) |
| 698 | DG.push_back(*DI); |
| 699 | else |
| 700 | break; |
| 701 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 702 | ++DI; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 703 | } while (DI != DIEnd); |
| 704 | RewriteForwardClassDecl(DG); |
| 705 | continue; |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 706 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 707 | } |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 708 | |
| 709 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 710 | if (!Proto->isThisDeclarationADefinition()) { |
| 711 | SmallVector<Decl *, 8> DG; |
| 712 | SourceLocation StartLoc = Proto->getLocStart(); |
| 713 | do { |
| 714 | if (isa<ObjCProtocolDecl>(*DI) && |
| 715 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 716 | StartLoc == (*DI)->getLocStart()) |
| 717 | DG.push_back(*DI); |
| 718 | else |
| 719 | break; |
| 720 | |
| 721 | ++DI; |
| 722 | } while (DI != DIEnd); |
| 723 | RewriteForwardProtocolDecl(DG); |
| 724 | continue; |
| 725 | } |
| 726 | } |
| 727 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 728 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 729 | ++DI; |
| 730 | } |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 732 | // If we have a decl in the main file, see if we should rewrite it. |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 733 | if (SM->isWrittenInMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 734 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 737 | //===----------------------------------------------------------------------===// |
| 738 | // Syntactic (non-AST) Rewriting Code |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 741 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 742 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 743 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 744 | const char *MainBufStart = MainBuf.begin(); |
| 745 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 746 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 748 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 749 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 750 | if (*BufPtr == '#') { |
| 751 | if (++BufPtr == MainBufEnd) |
| 752 | return; |
| 753 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 754 | if (++BufPtr == MainBufEnd) |
| 755 | return; |
| 756 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 757 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 759 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 760 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 761 | BufPtr += ImportLen; |
| 762 | } |
| 763 | } |
| 764 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 767 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 768 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 769 | std::string S; |
| 770 | S = "((struct "; |
| 771 | S += ClassDecl->getIdentifier()->getName(); |
| 772 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 773 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 774 | return S; |
| 775 | } |
| 776 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 777 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 778 | ObjCImplementationDecl *IMD, |
| 779 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 780 | static bool objcGetPropertyDefined = false; |
| 781 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 782 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 783 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 784 | const char *startBuf = SM->getCharacterData(startLoc); |
| 785 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 786 | const char *semiBuf = strchr(startBuf, ';'); |
| 787 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 788 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 789 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 790 | |
| 791 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 792 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 795 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 796 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 798 | if (!OID) |
| 799 | return; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 800 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 801 | if (!PD->getGetterMethodDecl()->isDefined()) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 802 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 803 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 804 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 805 | std::string Getr; |
| 806 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 807 | objcGetPropertyDefined = true; |
| 808 | // FIXME. Is this attribute correct in all cases? |
| 809 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 810 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 811 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 812 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 813 | PD->getGetterMethodDecl(), Getr); |
| 814 | Getr += "{ "; |
| 815 | // Synthesize an explicit cast to gain access to the ivar. |
| 816 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 817 | if (GenGetProperty) { |
| 818 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 819 | Getr += "typedef "; |
| 820 | const FunctionType *FPRetType = 0; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 821 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr, |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 822 | FPRetType); |
| 823 | Getr += " _TYPE"; |
| 824 | if (FPRetType) { |
| 825 | Getr += ")"; // close the precedence "scope" for "*". |
| 826 | |
| 827 | // Now, emit the argument types (if any). |
| 828 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 829 | Getr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 830 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 831 | if (i) Getr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 832 | std::string ParamStr = |
| 833 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 834 | Getr += ParamStr; |
| 835 | } |
| 836 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 837 | if (FT->getNumParams()) |
| 838 | Getr += ", "; |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 839 | Getr += "..."; |
| 840 | } |
| 841 | Getr += ")"; |
| 842 | } else |
| 843 | Getr += "()"; |
| 844 | } |
| 845 | Getr += ";\n"; |
| 846 | Getr += "return (_TYPE)"; |
| 847 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 848 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 849 | Getr += ", 1)"; |
| 850 | } |
| 851 | else |
| 852 | Getr += "return " + getIvarAccessString(OID); |
| 853 | Getr += "; }"; |
| 854 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 855 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 856 | |
| 857 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 858 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 860 | // Generate the 'setter' function. |
| 861 | std::string Setr; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 862 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 863 | ObjCPropertyDecl::OBJC_PR_copy); |
| 864 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 865 | objcSetPropertyDefined = true; |
| 866 | // FIXME. Is this attribute correct in all cases? |
| 867 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 868 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 869 | } |
| 870 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 871 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 872 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 873 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 874 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 875 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 876 | if (GenSetProperty) { |
| 877 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 878 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 879 | Setr += ", (id)"; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 880 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 881 | Setr += ", "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 882 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 883 | Setr += "0, "; |
| 884 | else |
| 885 | Setr += "1, "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 886 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 887 | Setr += "1)"; |
| 888 | else |
| 889 | Setr += "0)"; |
| 890 | } |
| 891 | else { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 892 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 893 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 894 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 895 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 896 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 897 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 899 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 900 | std::string &typedefString) { |
| 901 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 902 | typedefString += ForwardDecl->getNameAsString(); |
| 903 | typedefString += "\n"; |
| 904 | typedefString += "#define _REWRITER_typedef_"; |
| 905 | typedefString += ForwardDecl->getNameAsString(); |
| 906 | typedefString += "\n"; |
| 907 | typedefString += "typedef struct objc_object "; |
| 908 | typedefString += ForwardDecl->getNameAsString(); |
| 909 | typedefString += ";\n#endif\n"; |
| 910 | } |
| 911 | |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 912 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 913 | const std::string &typedefString) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 914 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 915 | const char *startBuf = SM->getCharacterData(startLoc); |
| 916 | const char *semiPtr = strchr(startBuf, ';'); |
| 917 | // Replace the @class with typedefs corresponding to the classes. |
| 918 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 919 | } |
| 920 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 921 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 922 | std::string typedefString; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 923 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 924 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 925 | if (I == D.begin()) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 926 | // Translate to typedef's that forward reference structs with the same name |
| 927 | // as the class. As a convenience, we include the original declaration |
| 928 | // as a comment. |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 929 | typedefString += "// @class "; |
| 930 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 931 | typedefString += ";\n"; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 932 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 933 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 934 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 935 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 936 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 937 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 939 | void RewriteObjC::RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &D) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 940 | std::string typedefString; |
| 941 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 942 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 943 | if (i == 0) { |
| 944 | typedefString += "// @class "; |
| 945 | typedefString += ForwardDecl->getNameAsString(); |
| 946 | typedefString += ";\n"; |
| 947 | } |
| 948 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 949 | } |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 950 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 953 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 954 | // When method is a synthesized one, such as a getter/setter there is |
| 955 | // nothing to rewrite. |
Fariborz Jahanian | e1378a4 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 956 | if (Method->isImplicit()) |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 957 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 958 | SourceLocation LocStart = Method->getLocStart(); |
| 959 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 961 | if (SM->getExpansionLineNumber(LocEnd) > |
| 962 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 963 | InsertText(LocStart, "#if 0\n"); |
| 964 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 965 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 966 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 971 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 973 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 974 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 977 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 978 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 980 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 981 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 983 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 984 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 985 | RewriteProperty(*I); |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 986 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | for (ObjCCategoryDecl::instmeth_iterator |
| 988 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 989 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 990 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 992 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 993 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 994 | RewriteMethodDeclaration(*I); |
| 995 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 996 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 997 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 998 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1001 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1002 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 1003 | assert(PDecl->isThisDeclarationADefinition()); |
| 1004 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1005 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1006 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
| 1008 | for (ObjCProtocolDecl::instmeth_iterator |
| 1009 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1010 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1011 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1012 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1013 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1014 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1015 | RewriteMethodDeclaration(*I); |
| 1016 | |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1017 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1018 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1019 | RewriteProperty(*I); |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1020 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1021 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1022 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1023 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1024 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1025 | // Must comment out @optional/@required |
| 1026 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1027 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1028 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1029 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1030 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1031 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1033 | } |
| 1034 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1035 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1036 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1042 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1043 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1044 | if (LocStart.isInvalid()) |
| 1045 | llvm_unreachable("Invalid SourceLocation"); |
| 1046 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1047 | ReplaceText(LocStart, 0, "// "); |
| 1048 | } |
| 1049 | |
| 1050 | void |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1051 | RewriteObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1052 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1053 | if (LocStart.isInvalid()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1054 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1055 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1056 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1059 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1060 | const FunctionType *&FPRetType) { |
| 1061 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1062 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1063 | else if (T->isFunctionPointerType() || |
| 1064 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1065 | // needs special handling, since pointer-to-functions have special |
| 1066 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1067 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1068 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1069 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1070 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1071 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1072 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1073 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1074 | ResultStr += |
| 1075 | FPRetType->getReturnType().getAsString(Context->getPrintingPolicy()); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1076 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1079 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1082 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1083 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1084 | std::string &ResultStr) { |
| 1085 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1086 | const FunctionType *FPRetType = 0; |
| 1087 | ResultStr += "\nstatic "; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1088 | RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1089 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1091 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1092 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1094 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1095 | NameStr += "_I_"; |
| 1096 | else |
| 1097 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1099 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1100 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1103 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1104 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1105 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1106 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1108 | { |
| 1109 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1110 | int len = selString.size(); |
| 1111 | for (int i = 0; i < len; i++) |
| 1112 | if (selString[i] == ':') |
| 1113 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1114 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1115 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1116 | // Remember this name for metadata emission |
| 1117 | MethodInternalNames[OMD] = NameStr; |
| 1118 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1120 | // Rewrite arguments |
| 1121 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1123 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1124 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1125 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1126 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1127 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1128 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1129 | ResultStr += "struct "; |
| 1130 | } |
| 1131 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1132 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1133 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1134 | } |
| 1135 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1136 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1137 | Context->getPrintingPolicy()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1139 | ResultStr += " self, "; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1140 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1141 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1143 | // Method arguments. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 1144 | for (const auto *PDecl : OMD->params()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1145 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1146 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1147 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1148 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1149 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1150 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1151 | QualType QT = PDecl->getType(); |
| 1152 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1153 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1154 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1155 | ResultStr += Name; |
| 1156 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1157 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1158 | if (OMD->isVariadic()) |
| 1159 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1160 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1162 | if (FPRetType) { |
| 1163 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1165 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1166 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1167 | ResultStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1168 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1169 | if (i) ResultStr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1170 | std::string ParamStr = |
| 1171 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1172 | ResultStr += ParamStr; |
| 1173 | } |
| 1174 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1175 | if (FT->getNumParams()) |
| 1176 | ResultStr += ", "; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1177 | ResultStr += "..."; |
| 1178 | } |
| 1179 | ResultStr += ")"; |
| 1180 | } else { |
| 1181 | ResultStr += "()"; |
| 1182 | } |
| 1183 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1184 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1185 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1186 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1187 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1189 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1191 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1192 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1193 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1194 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1195 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1196 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1197 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1198 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1199 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1200 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1201 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1202 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1203 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1204 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1206 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1207 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1208 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1209 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1210 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1211 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1212 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1213 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1214 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1216 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1217 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1218 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1219 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1220 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1221 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1223 | I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1224 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1227 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1230 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1231 | std::string ResultStr; |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1232 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1233 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1234 | ResultStr = "#ifndef _REWRITER_typedef_"; |
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"; |
| 1237 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1238 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1239 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1240 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1241 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1242 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1243 | // Mark this typedef as having been generated. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1244 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1245 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1246 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
| 1248 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1249 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1250 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1252 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1253 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1254 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1256 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1257 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1258 | RewriteMethodDeclaration(*I); |
| 1259 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1260 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1261 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1262 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1265 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1266 | SourceRange OldRange = PseudoOp->getSourceRange(); |
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 | // We just magically know some things about the structure of this |
| 1269 | // expression. |
| 1270 | ObjCMessageExpr *OldMsg = |
| 1271 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1272 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1274 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1275 | // we need to suppress rewriting the sub-statements. |
| 1276 | Expr *Base, *RHS; |
| 1277 | { |
| 1278 | DisableReplaceStmtScope S(*this); |
| 1279 | |
| 1280 | // Rebuild the base expression if we have one. |
| 1281 | Base = 0; |
| 1282 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1283 | Base = OldMsg->getInstanceReceiver(); |
| 1284 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1285 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1286 | } |
| 1287 | |
| 1288 | // Rebuild the RHS. |
| 1289 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1290 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1291 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1292 | } |
| 1293 | |
| 1294 | // TODO: avoid this copy. |
| 1295 | SmallVector<SourceLocation, 1> SelLocs; |
| 1296 | OldMsg->getSelectorLocs(SelLocs); |
| 1297 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1298 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1299 | switch (OldMsg->getReceiverKind()) { |
| 1300 | case ObjCMessageExpr::Class: |
| 1301 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1302 | OldMsg->getValueKind(), |
| 1303 | OldMsg->getLeftLoc(), |
| 1304 | OldMsg->getClassReceiverTypeInfo(), |
| 1305 | OldMsg->getSelector(), |
| 1306 | SelLocs, |
| 1307 | OldMsg->getMethodDecl(), |
| 1308 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1309 | OldMsg->getRightLoc(), |
| 1310 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | |
| 1313 | case ObjCMessageExpr::Instance: |
| 1314 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1315 | OldMsg->getValueKind(), |
| 1316 | OldMsg->getLeftLoc(), |
| 1317 | Base, |
| 1318 | OldMsg->getSelector(), |
| 1319 | SelLocs, |
| 1320 | OldMsg->getMethodDecl(), |
| 1321 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1322 | OldMsg->getRightLoc(), |
| 1323 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | |
| 1326 | case ObjCMessageExpr::SuperClass: |
| 1327 | case ObjCMessageExpr::SuperInstance: |
| 1328 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1329 | OldMsg->getValueKind(), |
| 1330 | OldMsg->getLeftLoc(), |
| 1331 | OldMsg->getSuperLoc(), |
| 1332 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1333 | OldMsg->getSuperType(), |
| 1334 | OldMsg->getSelector(), |
| 1335 | SelLocs, |
| 1336 | OldMsg->getMethodDecl(), |
| 1337 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1338 | OldMsg->getRightLoc(), |
| 1339 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1344 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1345 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1348 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1349 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1350 | |
| 1351 | // We just magically know some things about the structure of this |
| 1352 | // expression. |
| 1353 | ObjCMessageExpr *OldMsg = |
| 1354 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1355 | |
| 1356 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1357 | // we need to suppress rewriting the sub-statements. |
| 1358 | Expr *Base = 0; |
| 1359 | { |
| 1360 | DisableReplaceStmtScope S(*this); |
| 1361 | |
| 1362 | // Rebuild the base expression if we have one. |
| 1363 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1364 | Base = OldMsg->getInstanceReceiver(); |
| 1365 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1366 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1367 | } |
Fariborz Jahanian | bb40ea4 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1368 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1369 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1370 | // Intentionally empty. |
| 1371 | SmallVector<SourceLocation, 1> SelLocs; |
| 1372 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1373 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1374 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1375 | switch (OldMsg->getReceiverKind()) { |
| 1376 | case ObjCMessageExpr::Class: |
| 1377 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1378 | OldMsg->getValueKind(), |
| 1379 | OldMsg->getLeftLoc(), |
| 1380 | OldMsg->getClassReceiverTypeInfo(), |
| 1381 | OldMsg->getSelector(), |
| 1382 | SelLocs, |
| 1383 | OldMsg->getMethodDecl(), |
| 1384 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1385 | OldMsg->getRightLoc(), |
| 1386 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1387 | break; |
| 1388 | |
| 1389 | case ObjCMessageExpr::Instance: |
| 1390 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1391 | OldMsg->getValueKind(), |
| 1392 | OldMsg->getLeftLoc(), |
| 1393 | Base, |
| 1394 | OldMsg->getSelector(), |
| 1395 | SelLocs, |
| 1396 | OldMsg->getMethodDecl(), |
| 1397 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1398 | OldMsg->getRightLoc(), |
| 1399 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | |
| 1402 | case ObjCMessageExpr::SuperClass: |
| 1403 | case ObjCMessageExpr::SuperInstance: |
| 1404 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1405 | OldMsg->getValueKind(), |
| 1406 | OldMsg->getLeftLoc(), |
| 1407 | OldMsg->getSuperLoc(), |
| 1408 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1409 | OldMsg->getSuperType(), |
| 1410 | OldMsg->getSelector(), |
| 1411 | SelLocs, |
| 1412 | OldMsg->getMethodDecl(), |
| 1413 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1414 | OldMsg->getRightLoc(), |
| 1415 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1416 | break; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1417 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1418 | |
| 1419 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1420 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1421 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1424 | /// SynthCountByEnumWithState - To print: |
| 1425 | /// ((unsigned int (*) |
| 1426 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1428 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | /// "countByEnumeratingWithState:objects:count:"), |
| 1430 | /// &enumState, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1431 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1432 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1433 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1434 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1435 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1436 | buf += "\n\t\t"; |
| 1437 | buf += "((id)l_collection,\n\t\t"; |
| 1438 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1439 | buf += "\n\t\t"; |
| 1440 | buf += "&enumState, " |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1441 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1442 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1443 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1444 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1445 | /// statement to exit to its outer synthesized loop. |
| 1446 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1447 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1448 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1449 | return S; |
| 1450 | // replace break with goto __break_label |
| 1451 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1453 | SourceLocation startLoc = S->getLocStart(); |
| 1454 | buf = "goto __break_label_"; |
| 1455 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1456 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1457 | |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1462 | /// statement to continue with its inner synthesized loop. |
| 1463 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1464 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1465 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1466 | return S; |
| 1467 | // replace continue with goto __continue_label |
| 1468 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1470 | SourceLocation startLoc = S->getLocStart(); |
| 1471 | buf = "goto __continue_label_"; |
| 1472 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1473 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
| 1477 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1478 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1479 | /// It rewrites: |
| 1480 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1482 | /// Into: |
| 1483 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1485 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1486 | /// id __rw_items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1487 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1489 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1490 | /// if (limit) { |
| 1491 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1492 | /// do { |
| 1493 | /// unsigned long counter = 0; |
| 1494 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1496 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1497 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1498 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1499 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1500 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1502 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1503 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1504 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1505 | /// } |
| 1506 | /// else |
| 1507 | /// elem = nil; |
| 1508 | /// } |
| 1509 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1510 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1511 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1512 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1514 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1516 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1518 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1519 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1520 | StringRef elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1521 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1522 | std::string buf; |
| 1523 | buf = "\n{\n\t"; |
| 1524 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1525 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1526 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1527 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1528 | if (ElementType->isObjCQualifiedIdType() || |
| 1529 | ElementType->isObjCQualifiedInterfaceType()) |
| 1530 | // Simply use 'id' for all qualified types. |
| 1531 | elementTypeAsString = "id"; |
| 1532 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1533 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1534 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1535 | buf += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1536 | elementName = D->getName(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1537 | buf += elementName; |
| 1538 | buf += ";\n\t"; |
| 1539 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1540 | else { |
| 1541 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1542 | elementName = DR->getDecl()->getName(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1543 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1544 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1545 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1546 | // Simply use 'id' for all qualified types. |
| 1547 | elementTypeAsString = "id"; |
| 1548 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1549 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1552 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1553 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1554 | // id __rw_items[16]; |
| 1555 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1556 | // id l_collection = (id) |
| 1557 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1558 | // Find start location of 'collection' the hard way! |
| 1559 | const char *startCollectionBuf = startBuf; |
| 1560 | startCollectionBuf += 3; // skip 'for' |
| 1561 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1562 | startCollectionBuf++; // skip '(' |
| 1563 | // find 'in' and skip it. |
| 1564 | while (*startCollectionBuf != ' ' || |
| 1565 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1566 | (*(startCollectionBuf+3) != ' ' && |
| 1567 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1568 | startCollectionBuf++; |
| 1569 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1572 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1573 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1574 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1575 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1576 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1577 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1579 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1580 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1581 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1583 | // ((unsigned int (*) |
| 1584 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1586 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | // "countByEnumeratingWithState:objects:count:"), |
| 1588 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1589 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1590 | buf += "unsigned long limit =\n\t\t"; |
| 1591 | SynthCountByEnumWithState(buf); |
| 1592 | buf += ";\n\t"; |
| 1593 | /// if (limit) { |
| 1594 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1595 | /// do { |
| 1596 | /// unsigned long counter = 0; |
| 1597 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1599 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1600 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1601 | buf += "if (limit) {\n\t"; |
| 1602 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1603 | buf += "do {\n\t\t"; |
| 1604 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1605 | buf += "do {\n\t\t\t"; |
| 1606 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1607 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1608 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1609 | buf += " = ("; |
| 1610 | buf += elementTypeAsString; |
| 1611 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1612 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1613 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1615 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1616 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1618 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1619 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1620 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1621 | /// } |
| 1622 | /// else |
| 1623 | /// elem = nil; |
| 1624 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1626 | buf = ";\n\t"; |
| 1627 | buf += "__continue_label_"; |
| 1628 | buf += utostr(ObjCBcLabelNo.back()); |
| 1629 | buf += ": ;"; |
| 1630 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1631 | buf += "} while (counter < limit);\n\t"; |
| 1632 | buf += "} while (limit = "; |
| 1633 | SynthCountByEnumWithState(buf); |
| 1634 | buf += ");\n\t"; |
| 1635 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1636 | buf += " = (("; |
| 1637 | buf += elementTypeAsString; |
| 1638 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1639 | buf += "__break_label_"; |
| 1640 | buf += utostr(ObjCBcLabelNo.back()); |
| 1641 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1642 | buf += "}\n\t"; |
| 1643 | buf += "else\n\t\t"; |
| 1644 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1645 | buf += " = (("; |
| 1646 | buf += elementTypeAsString; |
| 1647 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1648 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1650 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1651 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1652 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1653 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1654 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1655 | } else { |
| 1656 | /* Need to treat single statements specially. For example: |
| 1657 | * |
| 1658 | * for (A *a in b) if (stuff()) break; |
| 1659 | * for (A *a in b) xxxyy; |
| 1660 | * |
| 1661 | * The following code simply scans ahead to the semi to find the actual end. |
| 1662 | */ |
| 1663 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1664 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1665 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1666 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1667 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1668 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1669 | Stmts.pop_back(); |
| 1670 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1671 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1675 | /// This routine rewrites @synchronized(expr) stmt; |
| 1676 | /// into: |
| 1677 | /// objc_sync_enter(expr); |
| 1678 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1679 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1680 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1681 | // Get the start location and compute the semi location. |
| 1682 | SourceLocation startLoc = S->getLocStart(); |
| 1683 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1685 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
| 1687 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1688 | buf = "objc_sync_enter((id)"; |
| 1689 | const char *lparenBuf = startBuf; |
| 1690 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1691 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1693 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1694 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1695 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1696 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1697 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1698 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1699 | buf = ");\n"; |
| 1700 | // declare a new scope with two variables, _stack and _rethrow. |
| 1701 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1702 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1703 | buf += "char *pointers[4];} _stack;\n"; |
| 1704 | buf += "id volatile _rethrow = 0;\n"; |
| 1705 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1706 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1707 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1708 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1709 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1711 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1712 | SourceLocation lastCurlyLoc = startLoc; |
| 1713 | buf = "}\nelse {\n"; |
| 1714 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1715 | buf += "}\n"; |
| 1716 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1717 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1718 | |
| 1719 | std::string syncBuf; |
| 1720 | syncBuf += " objc_sync_exit("; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1721 | |
| 1722 | Expr *syncExpr = S->getSynchExpr(); |
| 1723 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1724 | ? CK_BitCast : |
| 1725 | syncExpr->getType()->isBlockPointerType() |
| 1726 | ? CK_BlockPointerToObjCPointerCast |
| 1727 | : CK_CPointerToObjCPointerCast; |
| 1728 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1729 | CK, syncExpr); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1730 | std::string syncExprBufS; |
| 1731 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1732 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1733 | syncBuf += syncExprBuf.str(); |
| 1734 | syncBuf += ");"; |
| 1735 | |
| 1736 | buf += syncBuf; |
| 1737 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1738 | buf += "}\n"; |
| 1739 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1741 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1742 | |
| 1743 | bool hasReturns = false; |
| 1744 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1745 | if (hasReturns) |
| 1746 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1747 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1751 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1752 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1753 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1754 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1755 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1756 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1757 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1758 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1760 | TryFinallyContainsReturnDiag); |
| 1761 | } |
| 1762 | return; |
| 1763 | } |
| 1764 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1765 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1766 | { |
| 1767 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1768 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1769 | if (*CI) |
| 1770 | HasReturnStmts(*CI, hasReturns); |
| 1771 | |
| 1772 | if (isa<ReturnStmt>(S)) |
| 1773 | hasReturns = true; |
| 1774 | return; |
| 1775 | } |
| 1776 | |
| 1777 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1778 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1779 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1780 | if (*CI) { |
| 1781 | RewriteTryReturnStmts(*CI); |
| 1782 | } |
| 1783 | if (isa<ReturnStmt>(S)) { |
| 1784 | SourceLocation startLoc = S->getLocStart(); |
| 1785 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1786 | |
| 1787 | const char *semiBuf = strchr(startBuf, ';'); |
| 1788 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1789 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1790 | |
| 1791 | std::string buf; |
| 1792 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1793 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1794 | ReplaceText(startLoc, 6, buf); |
| 1795 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1796 | } |
| 1797 | return; |
| 1798 | } |
| 1799 | |
| 1800 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1801 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1802 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1803 | if (*CI) { |
| 1804 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1805 | } |
| 1806 | if (isa<ReturnStmt>(S)) { |
| 1807 | SourceLocation startLoc = S->getLocStart(); |
| 1808 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1809 | |
| 1810 | const char *semiBuf = strchr(startBuf, ';'); |
| 1811 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1812 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1813 | |
| 1814 | std::string buf; |
| 1815 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1816 | buf += syncExitBuf; |
| 1817 | buf += " return"; |
| 1818 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1819 | ReplaceText(startLoc, 6, buf); |
| 1820 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1821 | } |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1825 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1826 | // Get the start location and compute the semi location. |
| 1827 | SourceLocation startLoc = S->getLocStart(); |
| 1828 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1830 | assert((*startBuf == '@') && "bogus @try location"); |
| 1831 | |
| 1832 | std::string buf; |
| 1833 | // declare a new scope with two variables, _stack and _rethrow. |
| 1834 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1835 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1836 | buf += "char *pointers[4];} _stack;\n"; |
| 1837 | buf += "id volatile _rethrow = 0;\n"; |
| 1838 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1839 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1840 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1841 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1843 | startLoc = S->getTryBody()->getLocEnd(); |
| 1844 | startBuf = SM->getCharacterData(startLoc); |
| 1845 | |
| 1846 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1848 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1849 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1850 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1851 | buf = " /* @catch begin */ else {\n"; |
| 1852 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1853 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1854 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1855 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1856 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1858 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1859 | } else { /* no catch list */ |
| 1860 | buf = "}\nelse {\n"; |
| 1861 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1862 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1863 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1864 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1865 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1866 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1867 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1868 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1870 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1871 | buf = "if ("; // we are generating code for the first catch clause |
| 1872 | else |
| 1873 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1874 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1875 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1877 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1879 | const char *lParenLoc = strchr(startBuf, '('); |
| 1880 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1881 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1882 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1883 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1884 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1885 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1886 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1887 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1888 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1890 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1891 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1892 | } else if (catchDecl) { |
| 1893 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1894 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1895 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1896 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1897 | } else if (const ObjCObjectPointerType *Ptr = |
| 1898 | t->getAs<ObjCObjectPointerType>()) { |
| 1899 | // Should be a pointer to a class. |
| 1900 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1901 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1902 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1903 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1904 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1905 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1909 | lastCatchBody = Catch->getCatchBody(); |
| 1910 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1911 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1912 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1913 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1914 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1915 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1918 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1919 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1920 | } else { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1921 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1922 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1923 | } |
| 1924 | // Complete the catch list... |
| 1925 | if (lastCatchBody) { |
| 1926 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1927 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1928 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1930 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1931 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1932 | buf = "} /* last catch end */\n"; |
| 1933 | buf += "else {\n"; |
| 1934 | buf += " _rethrow = _caught;\n"; |
| 1935 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1936 | buf += "} } /* @catch end */\n"; |
| 1937 | if (!S->getFinallyStmt()) |
| 1938 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1939 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1941 | // Set lastCurlyLoc |
| 1942 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1943 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1944 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1945 | startLoc = finalStmt->getLocStart(); |
| 1946 | startBuf = SM->getCharacterData(startLoc); |
| 1947 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1949 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1951 | Stmt *body = finalStmt->getFinallyBody(); |
| 1952 | SourceLocation startLoc = body->getLocStart(); |
| 1953 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1954 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1955 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1957 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1959 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1960 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1961 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1962 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1964 | // Set lastCurlyLoc |
| 1965 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1967 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1968 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1969 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1970 | buf = "{ /* implicit finally clause */\n"; |
| 1971 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1972 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1973 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1974 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Now check for any return/continue/go statements within the @try. |
| 1977 | // The implicit finally clause won't called if the @try contains any |
| 1978 | // jump statements. |
| 1979 | bool hasReturns = false; |
| 1980 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1981 | if (hasReturns) |
| 1982 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1983 | } |
| 1984 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1985 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1986 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1991 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1992 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1993 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1994 | // Get the start location and compute the semi location. |
| 1995 | SourceLocation startLoc = S->getLocStart(); |
| 1996 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1998 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1999 | |
| 2000 | std::string buf; |
| 2001 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2002 | if (S->getThrowExpr()) |
| 2003 | buf = "objc_exception_throw("; |
| 2004 | else // add an implicit argument |
| 2005 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2007 | // handle "@ throw" correctly. |
| 2008 | const char *wBuf = strchr(startBuf, 'w'); |
| 2009 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2010 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2012 | const char *semiBuf = strchr(startBuf, ';'); |
| 2013 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2014 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2015 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2016 | return 0; |
| 2017 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2018 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2019 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2020 | // Create a new string expression. |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2021 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2022 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2023 | Expr *Replacement = getStringLiteral(StrEncoding); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2024 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2026 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2027 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2028 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2031 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2032 | if (!SelGetUidFunctionDecl) |
| 2033 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2034 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2035 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2036 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2037 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2038 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2039 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2040 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2041 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2042 | return SelExp; |
| 2043 | } |
| 2044 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2045 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2046 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2047 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2048 | // 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] | 2049 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2051 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2052 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2053 | VK_LValue, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2055 | // 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] | 2056 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2057 | ImplicitCastExpr *ICE = |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2058 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2059 | DRE, 0, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2061 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2063 | CallExpr *Exp = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2064 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2065 | FT->getCallResultType(*Context), |
| 2066 | VK_RValue, EndLoc); |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2067 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2070 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2071 | const char *&startRef, const char *&endRef) { |
| 2072 | while (startBuf < endBuf) { |
| 2073 | if (*startBuf == '<') |
| 2074 | startRef = startBuf; // mark the start. |
| 2075 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2076 | if (startRef && *startRef == '<') { |
| 2077 | endRef = startBuf; // mark the end. |
| 2078 | return true; |
| 2079 | } |
| 2080 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2081 | } |
| 2082 | startBuf++; |
| 2083 | } |
| 2084 | return false; |
| 2085 | } |
| 2086 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2087 | static void scanToNextArgument(const char *&argRef) { |
| 2088 | int angle = 0; |
| 2089 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2090 | if (*argRef == '<') |
| 2091 | angle++; |
| 2092 | else if (*argRef == '>') |
| 2093 | angle--; |
| 2094 | argRef++; |
| 2095 | } |
| 2096 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2097 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2098 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2099 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2100 | if (T->isObjCQualifiedIdType()) |
| 2101 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2102 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2103 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2104 | return true; |
| 2105 | } |
| 2106 | if (T->isObjCObjectPointerType()) { |
| 2107 | T = T->getPointeeType(); |
| 2108 | return T->isObjCQualifiedInterfaceType(); |
| 2109 | } |
Fariborz Jahanian | 7bf13c4 | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2110 | if (T->isArrayType()) { |
| 2111 | QualType ElemTy = Context->getBaseElementType(T); |
| 2112 | return needToScanForQualifiers(ElemTy); |
| 2113 | } |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2114 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2117 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2118 | QualType Type = E->getType(); |
| 2119 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2120 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2122 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2123 | Loc = ECE->getLParenLoc(); |
| 2124 | EndLoc = ECE->getRParenLoc(); |
| 2125 | } else { |
| 2126 | Loc = E->getLocStart(); |
| 2127 | EndLoc = E->getLocEnd(); |
| 2128 | } |
| 2129 | // This will defend against trying to rewrite synthesized expressions. |
| 2130 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2131 | return; |
| 2132 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2133 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2134 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2135 | const char *startRef = 0, *endRef = 0; |
| 2136 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2137 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2138 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2139 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2140 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2141 | InsertText(LessLoc, "/*"); |
| 2142 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2147 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2148 | SourceLocation Loc; |
| 2149 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2150 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2151 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2152 | Loc = VD->getLocation(); |
| 2153 | Type = VD->getType(); |
| 2154 | } |
| 2155 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2156 | Loc = FD->getLocation(); |
| 2157 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2158 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2159 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2160 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2161 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2162 | if (!proto) |
| 2163 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2164 | Type = proto->getReturnType(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2165 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2166 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2167 | Loc = FD->getLocation(); |
| 2168 | Type = FD->getType(); |
| 2169 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2170 | else |
| 2171 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2173 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2174 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2176 | const char *endBuf = SM->getCharacterData(Loc); |
| 2177 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2178 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2179 | startBuf--; // scan backward (from the decl location) for return type. |
| 2180 | const char *startRef = 0, *endRef = 0; |
| 2181 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2182 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2183 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2184 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2185 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2186 | InsertText(LessLoc, "/*"); |
| 2187 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2188 | } |
| 2189 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2190 | if (!proto) |
| 2191 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2192 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2193 | const char *startBuf = SM->getCharacterData(Loc); |
| 2194 | const char *startFuncBuf = startBuf; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2195 | for (unsigned i = 0; i < proto->getNumParams(); i++) { |
| 2196 | if (needToScanForQualifiers(proto->getParamType(i))) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2197 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2199 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2200 | // scan forward (from the decl location) for argument types. |
| 2201 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2202 | const char *startRef = 0, *endRef = 0; |
| 2203 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2204 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2206 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2208 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2209 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2210 | InsertText(LessLoc, "/*"); |
| 2211 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2212 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2213 | startBuf = ++endBuf; |
| 2214 | } |
| 2215 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2216 | // If the function name is derived from a macro expansion, then the |
| 2217 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2218 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2219 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2220 | startBuf++; |
| 2221 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2222 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2225 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2226 | QualType QT = ND->getType(); |
| 2227 | const Type* TypePtr = QT->getAs<Type>(); |
| 2228 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2229 | return; |
| 2230 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2231 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2232 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2233 | TypePtr = QT->getAs<Type>(); |
| 2234 | } |
| 2235 | // FIXME. This will not work for multiple declarators; as in: |
| 2236 | // __typeof__(a) b,c,d; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2237 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2238 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2239 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2240 | if (ND->getInit()) { |
| 2241 | std::string Name(ND->getNameAsString()); |
| 2242 | TypeAsString += " " + Name + " = "; |
| 2243 | Expr *E = ND->getInit(); |
| 2244 | SourceLocation startLoc; |
| 2245 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2246 | startLoc = ECE->getLParenLoc(); |
| 2247 | else |
| 2248 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2249 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2250 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2251 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2252 | } |
| 2253 | else { |
| 2254 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2255 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2256 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2257 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2261 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2262 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2263 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2264 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2265 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2266 | QualType getFuncType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2267 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2268 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2269 | SourceLocation(), |
| 2270 | SourceLocation(), |
| 2271 | SelGetUidIdent, getFuncType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2272 | SC_Extern); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2275 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2276 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2277 | if (FD->getIdentifier() && |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2278 | FD->getName() == "sel_registerName") { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2279 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2280 | return; |
| 2281 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2282 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2285 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2286 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2287 | const char *argPtr = TypeString.c_str(); |
| 2288 | if (!strchr(argPtr, '^')) { |
| 2289 | Str += TypeString; |
| 2290 | return; |
| 2291 | } |
| 2292 | while (*argPtr) { |
| 2293 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2294 | argPtr++; |
| 2295 | } |
| 2296 | } |
| 2297 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2298 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2299 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2300 | ValueDecl *VD) { |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2301 | QualType Type = VD->getType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2302 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2303 | const char *argPtr = TypeString.c_str(); |
| 2304 | int paren = 0; |
| 2305 | while (*argPtr) { |
| 2306 | switch (*argPtr) { |
| 2307 | case '(': |
| 2308 | Str += *argPtr; |
| 2309 | paren++; |
| 2310 | break; |
| 2311 | case ')': |
| 2312 | Str += *argPtr; |
| 2313 | paren--; |
| 2314 | break; |
| 2315 | case '^': |
| 2316 | Str += '*'; |
| 2317 | if (paren == 1) |
| 2318 | Str += VD->getNameAsString(); |
| 2319 | break; |
| 2320 | default: |
| 2321 | Str += *argPtr; |
| 2322 | break; |
| 2323 | } |
| 2324 | argPtr++; |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2329 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2330 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2331 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2332 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2333 | if (!proto) |
| 2334 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2335 | QualType Type = proto->getReturnType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2336 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2337 | FdStr += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2338 | FdStr += FD->getName(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2339 | FdStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2340 | unsigned numArgs = proto->getNumParams(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2341 | for (unsigned i = 0; i < numArgs; i++) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2342 | QualType ArgType = proto->getParamType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2343 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2344 | if (i+1 < numArgs) |
| 2345 | FdStr += ", "; |
| 2346 | } |
| 2347 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2348 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2349 | CurFunctionDeclToDeclareForBlock = 0; |
| 2350 | } |
| 2351 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2352 | // SynthSuperConstructorFunctionDecl - id objc_super(id obj, id super); |
| 2353 | void RewriteObjC::SynthSuperConstructorFunctionDecl() { |
| 2354 | if (SuperConstructorFunctionDecl) |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2355 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2356 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2357 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2358 | QualType argT = Context->getObjCIdType(); |
| 2359 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2360 | ArgTys.push_back(argT); |
| 2361 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2362 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2363 | ArgTys); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2364 | SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2365 | SourceLocation(), |
| 2366 | SourceLocation(), |
| 2367 | msgSendIdent, msgSendType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2368 | 0, SC_Extern); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2371 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2372 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2373 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2374 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2375 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2376 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2377 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2378 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2379 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2380 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2381 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2382 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2383 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2384 | SourceLocation(), |
| 2385 | SourceLocation(), |
| 2386 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2387 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2390 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2391 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2392 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2393 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2394 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2395 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2396 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2397 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2398 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2399 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2400 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2401 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2402 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2403 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2404 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2405 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2406 | SourceLocation(), |
| 2407 | SourceLocation(), |
| 2408 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2409 | SC_Extern); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2412 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2413 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2414 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2415 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2416 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2417 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2418 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2419 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2420 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2421 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2422 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2423 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2424 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2425 | SourceLocation(), |
| 2426 | SourceLocation(), |
| 2427 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2428 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2432 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2433 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2435 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2436 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2437 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2438 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2439 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2440 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2441 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2442 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2443 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2444 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2445 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2446 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2447 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2448 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2450 | SourceLocation(), |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2451 | msgSendIdent, |
| 2452 | msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2453 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2456 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2457 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2458 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2459 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2460 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2461 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2462 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2463 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2464 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2465 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2466 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2467 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2468 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2469 | SourceLocation(), |
| 2470 | SourceLocation(), |
| 2471 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2472 | SC_Extern); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2475 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2476 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2477 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2478 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2479 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2480 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2481 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2482 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2483 | SourceLocation(), |
| 2484 | SourceLocation(), |
| 2485 | getClassIdent, getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2486 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2489 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2490 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2491 | IdentifierInfo *getSuperClassIdent = |
| 2492 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2493 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2494 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2495 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2496 | ArgTys); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2497 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2498 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2499 | SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2500 | getSuperClassIdent, |
| 2501 | getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2502 | SC_Extern); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2505 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2506 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2507 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2508 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2509 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2510 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2511 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2512 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2513 | SourceLocation(), |
| 2514 | SourceLocation(), |
| 2515 | getClassIdent, getClassType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2516 | 0, SC_Extern); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2519 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2520 | QualType strType = getConstantStringStructType(); |
| 2521 | |
| 2522 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2523 | |
| 2524 | std::string tmpName = InFileName; |
| 2525 | unsigned i; |
| 2526 | for (i=0; i < tmpName.length(); i++) { |
| 2527 | char c = tmpName.at(i); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2528 | // replace any non-alphanumeric characters with '_'. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2529 | if (!isAlphanumeric(c)) |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2530 | tmpName[i] = '_'; |
| 2531 | } |
| 2532 | S += tmpName; |
| 2533 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2534 | S += utostr(NumObjCStringLiterals++); |
| 2535 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2536 | Preamble += "static __NSConstantStringImpl " + S; |
| 2537 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2538 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2539 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2540 | std::string prettyBufS; |
| 2541 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2542 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2543 | Preamble += prettyBuf.str(); |
| 2544 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2545 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
| 2547 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2548 | SourceLocation(), &Context->Idents.get(S), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2549 | strType, 0, SC_Static); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2550 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2551 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2552 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2554 | VK_RValue, OK_Ordinary, |
| 2555 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2556 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2557 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2558 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2559 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2560 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2561 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2564 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2565 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2566 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2567 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2568 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2569 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2570 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2572 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2574 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2576 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2577 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2578 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2580 | SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2582 | FieldTypes[i], 0, |
| 2583 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2584 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2585 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2586 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2588 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2589 | } |
| 2590 | return Context->getTagDeclType(SuperStructDecl); |
| 2591 | } |
| 2592 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2593 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2594 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2595 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2596 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2597 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2598 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2600 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2602 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2604 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2606 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2608 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2609 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2610 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2612 | ConstantStringDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2613 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2614 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2615 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2616 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2617 | /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2618 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2621 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2622 | } |
| 2623 | return Context->getTagDeclType(ConstantStringDecl); |
| 2624 | } |
| 2625 | |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2626 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2627 | QualType msgSendType, |
| 2628 | QualType returnType, |
| 2629 | SmallVectorImpl<QualType> &ArgTypes, |
| 2630 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2631 | ObjCMethodDecl *Method) { |
| 2632 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2633 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2634 | false, msgSendType, |
| 2635 | VK_LValue, SourceLocation()); |
| 2636 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2637 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2638 | Context->getPointerType(Context->VoidTy), |
| 2639 | CK_BitCast, STDRE); |
| 2640 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2641 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 2642 | Method ? Method->isVariadic() |
| 2643 | : false); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2644 | castType = Context->getPointerType(castType); |
| 2645 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2646 | cast); |
| 2647 | |
| 2648 | // Don't forget the parens to enforce the proper binding. |
| 2649 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2650 | |
| 2651 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2652 | CallExpr *STCE = new (Context) CallExpr( |
| 2653 | *Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, SourceLocation()); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2654 | return STCE; |
| 2655 | |
| 2656 | } |
| 2657 | |
| 2658 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2659 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2660 | SourceLocation StartLoc, |
| 2661 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2662 | if (!SelGetUidFunctionDecl) |
| 2663 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2664 | if (!MsgSendFunctionDecl) |
| 2665 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2666 | if (!MsgSendSuperFunctionDecl) |
| 2667 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2668 | if (!MsgSendStretFunctionDecl) |
| 2669 | SynthMsgSendStretFunctionDecl(); |
| 2670 | if (!MsgSendSuperStretFunctionDecl) |
| 2671 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2672 | if (!MsgSendFpretFunctionDecl) |
| 2673 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2674 | if (!GetClassFunctionDecl) |
| 2675 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2676 | if (!GetSuperClassFunctionDecl) |
| 2677 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2678 | if (!GetMetaClassFunctionDecl) |
| 2679 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2681 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2682 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2683 | // May need to use objc_msgSend_stret() as well. |
| 2684 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2685 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2686 | QualType resultType = mDecl->getReturnType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2687 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2688 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2689 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2690 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2691 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2693 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2694 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2695 | switch (Exp->getReceiverKind()) { |
| 2696 | case ObjCMessageExpr::SuperClass: { |
| 2697 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2698 | if (MsgSendStretFlavor) |
| 2699 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2700 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2702 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2704 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2706 | // set the receiver to self, the first argument to all methods. |
| 2707 | InitExprs.push_back( |
| 2708 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2709 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2710 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2711 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2712 | Context->getObjCIdType(), |
| 2713 | VK_RValue, |
| 2714 | SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2715 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2717 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2718 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2719 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2720 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2721 | &ClsExprs[0], |
| 2722 | ClsExprs.size(), |
| 2723 | StartLoc, |
| 2724 | EndLoc); |
| 2725 | // (Class)objc_getClass("CurrentClass") |
| 2726 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2727 | Context->getObjCClassType(), |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2728 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2729 | ClsExprs.clear(); |
| 2730 | ClsExprs.push_back(ArgExpr); |
| 2731 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2732 | &ClsExprs[0], ClsExprs.size(), |
| 2733 | StartLoc, EndLoc); |
| 2734 | |
| 2735 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2736 | // To turn off a warning, type-cast to 'id' |
| 2737 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2738 | NoTypeInfoCStyleCastExpr(Context, |
| 2739 | Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2740 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2741 | // struct objc_super |
| 2742 | QualType superType = getSuperStructType(); |
| 2743 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2744 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2745 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2746 | SynthSuperConstructorFunctionDecl(); |
| 2747 | // Simulate a constructor call... |
| 2748 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2749 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2750 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2751 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2752 | superType, VK_LValue, |
| 2753 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2754 | // The code for super is a little tricky to prevent collision with |
| 2755 | // the structure definition in the header. The rewriter has it's own |
| 2756 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2757 | // we need the cast below. For example: |
| 2758 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2759 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2760 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2761 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2762 | VK_RValue, OK_Ordinary, |
| 2763 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2764 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2765 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2766 | CK_BitCast, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2767 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2768 | // (struct objc_super) { <exprs from above> } |
| 2769 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2770 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2771 | SourceLocation()); |
| 2772 | TypeSourceInfo *superTInfo |
| 2773 | = Context->getTrivialTypeSourceInfo(superType); |
| 2774 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2775 | superType, VK_LValue, |
| 2776 | ILE, false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2777 | // struct objc_super * |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2778 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2779 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2780 | VK_RValue, OK_Ordinary, |
| 2781 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2782 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2783 | MsgExprs.push_back(SuperRep); |
| 2784 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2785 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2786 | |
| 2787 | case ObjCMessageExpr::Class: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2788 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2789 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2790 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2791 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2792 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2793 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2794 | &ClsExprs[0], |
| 2795 | ClsExprs.size(), |
| 2796 | StartLoc, EndLoc); |
| 2797 | MsgExprs.push_back(Cls); |
| 2798 | break; |
| 2799 | } |
| 2800 | |
| 2801 | case ObjCMessageExpr::SuperInstance:{ |
| 2802 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2803 | if (MsgSendStretFlavor) |
| 2804 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2805 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2806 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2807 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2808 | |
| 2809 | InitExprs.push_back( |
| 2810 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2811 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2812 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2813 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2814 | Context->getObjCIdType(), |
| 2815 | VK_RValue, SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2816 | ); // set the 'receiver'. |
| 2817 | |
| 2818 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2819 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2820 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2821 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2822 | &ClsExprs[0], |
| 2823 | ClsExprs.size(), |
| 2824 | StartLoc, EndLoc); |
| 2825 | // (Class)objc_getClass("CurrentClass") |
| 2826 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2827 | Context->getObjCClassType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2828 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2829 | ClsExprs.clear(); |
| 2830 | ClsExprs.push_back(ArgExpr); |
| 2831 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2832 | &ClsExprs[0], ClsExprs.size(), |
| 2833 | StartLoc, EndLoc); |
| 2834 | |
| 2835 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2836 | // To turn off a warning, type-cast to 'id' |
| 2837 | InitExprs.push_back( |
| 2838 | // set 'super class', using class_getSuperclass(). |
| 2839 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2840 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2841 | // struct objc_super |
| 2842 | QualType superType = getSuperStructType(); |
| 2843 | Expr *SuperRep; |
| 2844 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2845 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2846 | SynthSuperConstructorFunctionDecl(); |
| 2847 | // Simulate a constructor call... |
| 2848 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2849 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2850 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2851 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2852 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2853 | // The code for super is a little tricky to prevent collision with |
| 2854 | // the structure definition in the header. The rewriter has it's own |
| 2855 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2856 | // we need the cast below. For example: |
| 2857 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2858 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2859 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2860 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2861 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2862 | SourceLocation()); |
| 2863 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2864 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2865 | CK_BitCast, SuperRep); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2866 | } else { |
| 2867 | // (struct objc_super) { <exprs from above> } |
| 2868 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2869 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2870 | SourceLocation()); |
| 2871 | TypeSourceInfo *superTInfo |
| 2872 | = Context->getTrivialTypeSourceInfo(superType); |
| 2873 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2874 | superType, VK_RValue, ILE, |
| 2875 | false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2876 | } |
| 2877 | MsgExprs.push_back(SuperRep); |
| 2878 | break; |
| 2879 | } |
| 2880 | |
| 2881 | case ObjCMessageExpr::Instance: { |
| 2882 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2883 | // Foo<Proto> *. |
| 2884 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2885 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2886 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2887 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2888 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2889 | ? CK_BlockPointerToObjCPointerCast |
| 2890 | : CK_CPointerToObjCPointerCast; |
| 2891 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2892 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2893 | CK, recExpr); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2894 | MsgExprs.push_back(recExpr); |
| 2895 | break; |
| 2896 | } |
| 2897 | } |
| 2898 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2899 | // 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] | 2900 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2901 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2902 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2903 | &SelExprs[0], SelExprs.size(), |
| 2904 | StartLoc, |
| 2905 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2906 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2908 | // Now push any user supplied arguments. |
| 2909 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2910 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2911 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2912 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2913 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | 8f49033 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2914 | QualType type = ICE->getType(); |
| 2915 | if (needToScanForQualifiers(type)) |
| 2916 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2917 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2918 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 9e7dbd1 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2919 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2920 | CastKind CK; |
| 2921 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2922 | type->isBooleanType()) { |
| 2923 | CK = CK_IntegralToBoolean; |
| 2924 | } else if (type->isObjCObjectPointerType()) { |
| 2925 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2926 | CK = CK_BlockPointerToObjCPointerCast; |
| 2927 | } else if (SubExpr->getType()->isPointerType()) { |
| 2928 | CK = CK_CPointerToObjCPointerCast; |
| 2929 | } else { |
| 2930 | CK = CK_BitCast; |
| 2931 | } |
| 2932 | } else { |
| 2933 | CK = CK_BitCast; |
| 2934 | } |
| 2935 | |
| 2936 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2937 | } |
| 2938 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2939 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2940 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2941 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2942 | userExpr = CE->getSubExpr(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2943 | CastKind CK; |
| 2944 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2945 | CK = CK_IntegralToPointer; |
| 2946 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2947 | CK = CK_BlockPointerToObjCPointerCast; |
| 2948 | } else if (userExpr->getType()->isPointerType()) { |
| 2949 | CK = CK_CPointerToObjCPointerCast; |
| 2950 | } else { |
| 2951 | CK = CK_BitCast; |
| 2952 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2953 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2954 | CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2955 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2957 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2958 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2959 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2960 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2961 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2962 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2963 | // Generate the funky cast. |
| 2964 | CastExpr *cast; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2965 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2966 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2968 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2969 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2970 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2971 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2972 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2973 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2974 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2975 | // Push any user argument types. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2976 | for (const auto *PI : OMD->params()) { |
| 2977 | QualType t = PI->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | ? Context->getObjCIdType() |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2979 | : PI->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2980 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2981 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2982 | ArgTypes.push_back(t); |
| 2983 | } |
Fariborz Jahanian | 9f0bc57 | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 2984 | returnType = Exp->getType(); |
| 2985 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2986 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2987 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2988 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2989 | } |
| 2990 | // 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] | 2991 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2993 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2994 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2995 | VK_LValue, SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2996 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2998 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2999 | // xx.m:13: warning: function called through a non-compatible type |
| 3000 | // 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] | 3001 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3002 | Context->getPointerType(Context->VoidTy), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3003 | CK_BitCast, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3005 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3006 | // If we don't have a method decl, force a variadic cast. |
| 3007 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3008 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3009 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3010 | castType = Context->getPointerType(castType); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3011 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3012 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3013 | |
| 3014 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3015 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3017 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3018 | CallExpr *CE = new (Context) |
| 3019 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3020 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3021 | if (MsgSendStretFlavor) { |
| 3022 | // We have the method which returns a struct/union. Must also generate |
| 3023 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3024 | // expression which dictate which one to envoke depending on size of |
| 3025 | // method's return type. |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 3026 | |
| 3027 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3028 | msgSendType, returnType, |
| 3029 | ArgTypes, MsgExprs, |
| 3030 | Exp->getMethodDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3032 | // Build sizeof(returnType) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3033 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3034 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3035 | Context->getTrivialTypeSourceInfo(returnType), |
| 3036 | Context->getSizeType(), SourceLocation(), |
| 3037 | SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3038 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3039 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3040 | // For X86 it is more complicated and some kind of target specific routine |
| 3041 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3043 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3044 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3045 | llvm::APInt(IntSize, 8), |
| 3046 | Context->IntTy, |
| 3047 | SourceLocation()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3048 | BinaryOperator *lessThanExpr = |
| 3049 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3050 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3051 | false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3052 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3054 | new (Context) ConditionalOperator(lessThanExpr, |
| 3055 | SourceLocation(), CE, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3056 | SourceLocation(), STCE, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3057 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3058 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3059 | CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3060 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3061 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3062 | return ReplacingStmt; |
| 3063 | } |
| 3064 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3065 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3066 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3067 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3069 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3070 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3072 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3073 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3076 | // typedef struct objc_object Protocol; |
| 3077 | QualType RewriteObjC::getProtocolType() { |
| 3078 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3079 | TypeSourceInfo *TInfo |
| 3080 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3081 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3082 | SourceLocation(), SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3083 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3084 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3085 | } |
| 3086 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3087 | } |
| 3088 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3089 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3090 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3091 | /// The forward references (and metadata) are generated in |
| 3092 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3093 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3094 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3095 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3097 | SourceLocation(), ID, getProtocolType(), 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3098 | SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3099 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3100 | VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3101 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3102 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3103 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3104 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3105 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3106 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3107 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3108 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3109 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3110 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3114 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3115 | const char *endBuf) { |
| 3116 | while (startBuf < endBuf) { |
| 3117 | if (*startBuf == '#') { |
| 3118 | // Skip whitespace. |
| 3119 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3120 | ; |
| 3121 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3122 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3123 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3124 | !strncmp(startBuf, "define", strlen("define")) || |
| 3125 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3126 | !strncmp(startBuf, "else", strlen("else")) || |
| 3127 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3128 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3129 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3130 | !strncmp(startBuf, "include", strlen("include")) || |
| 3131 | !strncmp(startBuf, "import", strlen("import")) || |
| 3132 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3133 | return true; |
| 3134 | } |
| 3135 | startBuf++; |
| 3136 | } |
| 3137 | return false; |
| 3138 | } |
| 3139 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3140 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3141 | /// an objective-c class with ivars. |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3142 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3143 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3144 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3145 | assert(CDecl->getName() != "" && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3146 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3147 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3148 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3149 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3150 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3151 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3152 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3153 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3155 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3156 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3158 | // If no ivars and no root or if its root, directly or indirectly, |
| 3159 | // 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] | 3160 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3161 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3162 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3163 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3164 | return; |
| 3165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
| 3167 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3168 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3169 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3170 | Result += CDecl->getNameAsString(); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3171 | if (LangOpts.MicrosoftExt) |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3172 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3173 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3174 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3175 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3177 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3178 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3179 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3180 | // NSURL.h, for example): |
| 3181 | // |
| 3182 | // #ifdef XYZ |
| 3183 | // @interface Foo : NSObject |
| 3184 | // #else |
| 3185 | // @interface FooBar : NSObject |
| 3186 | // #endif |
| 3187 | // { |
| 3188 | // int i; |
| 3189 | // } |
| 3190 | // @end |
| 3191 | // |
| 3192 | // This clause is segregated to avoid breaking the common case. |
| 3193 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3195 | CDecl->getAtStartLoc(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3196 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3197 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3198 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3199 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3200 | // advance to the end of the referenced protocols. |
| 3201 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3202 | endHeader++; |
| 3203 | } |
| 3204 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3205 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3206 | } else { |
| 3207 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3208 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3209 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3210 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3211 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3212 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3213 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3214 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3215 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3217 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3218 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3219 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3220 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3221 | } |
| 3222 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3224 | // Now comment out any visibility specifiers. |
| 3225 | while (cursor < endBuf) { |
| 3226 | if (*cursor == '@') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3227 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3228 | // Skip whitespace. |
| 3229 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3230 | /*scan*/; |
| 3231 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3232 | // FIXME: presence of @public, etc. inside comment results in |
| 3233 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3234 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3235 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3236 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3237 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3238 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3239 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3240 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3241 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3242 | // for type checking. |
| 3243 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3244 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3245 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3246 | cursor = strchr(cursor, '>'); |
| 3247 | cursor++; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3248 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3249 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3250 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3251 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3252 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3253 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3254 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3255 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3256 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3257 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3258 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3259 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3260 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3261 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3262 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3263 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3264 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3265 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3266 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3267 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3268 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3269 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3272 | //===----------------------------------------------------------------------===// |
| 3273 | // Meta Data Emission |
| 3274 | //===----------------------------------------------------------------------===// |
| 3275 | |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3276 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3277 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3278 | /// and emits meta-data. |
| 3279 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3280 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3281 | int ClsDefCount = ClassImplementation.size(); |
| 3282 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3284 | // Rewrite implemented methods |
| 3285 | for (int i = 0; i < ClsDefCount; i++) |
| 3286 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3288 | for (int i = 0; i < CatDefCount; i++) |
| 3289 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3290 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3292 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3293 | const std::string &Name, |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3294 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3295 | assert(BlockByRefDeclNo.count(VD) && |
| 3296 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3297 | if (def) |
| 3298 | ResultStr += "struct "; |
| 3299 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3300 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3301 | } |
| 3302 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3303 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3304 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3305 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3309 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3310 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3311 | std::string Tag) { |
| 3312 | const FunctionType *AFT = CE->getFunctionType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3313 | QualType RT = AFT->getReturnType(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3314 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3315 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3316 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3317 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3318 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3320 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | // 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] | 3322 | // block (to reference imported block decl refs). |
| 3323 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3324 | } else if (BD->param_empty()) { |
| 3325 | S += "(" + StructRef + " *__cself)"; |
| 3326 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3327 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3328 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3329 | S += '('; |
| 3330 | // first add the implicit argument. |
| 3331 | S += StructRef + " *__cself, "; |
| 3332 | std::string ParamStr; |
| 3333 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3334 | E = BD->param_end(); AI != E; ++AI) { |
| 3335 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3336 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3337 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3338 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3339 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3340 | S += ParamStr; |
| 3341 | } |
| 3342 | if (FT->isVariadic()) { |
| 3343 | if (!BD->param_empty()) S += ", "; |
| 3344 | S += "..."; |
| 3345 | } |
| 3346 | S += ')'; |
| 3347 | } |
| 3348 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3350 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3351 | // First, emit a declaration for all "by ref" decls. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3352 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3353 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3354 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3355 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3356 | std::string TypeString; |
| 3357 | RewriteByRefString(TypeString, Name, (*I)); |
| 3358 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3359 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3360 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3362 | // Next, emit a declaration for all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3363 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3364 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3365 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3366 | // Handle nested closure invocation. For example: |
| 3367 | // |
| 3368 | // void (^myImportedClosure)(void); |
| 3369 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3371 | // void (^anotherClosure)(void); |
| 3372 | // anotherClosure = ^(void) { |
| 3373 | // myImportedClosure(); // import and invoke the closure |
| 3374 | // }; |
| 3375 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3376 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3377 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3378 | S += " = ("; |
| 3379 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3380 | S += ")"; |
| 3381 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3382 | } |
| 3383 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3384 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3385 | QualType QT = (*I)->getType(); |
| 3386 | if (HasLocalVariableExternalStorage(*I)) |
| 3387 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3388 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3389 | S += Name + " = __cself->" + |
| 3390 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3391 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3392 | } |
| 3393 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3394 | const char *cstr = RewrittenStr.c_str(); |
| 3395 | while (*cstr++ != '{') ; |
| 3396 | S += cstr; |
| 3397 | S += "\n"; |
| 3398 | return S; |
| 3399 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3400 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3401 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3402 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3403 | std::string Tag) { |
| 3404 | std::string StructRef = "struct " + Tag; |
| 3405 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3407 | S += funcName; |
| 3408 | S += "_block_copy_" + utostr(i); |
| 3409 | S += "(" + StructRef; |
| 3410 | S += "*dst, " + StructRef; |
| 3411 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3413 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3414 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3415 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3416 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3417 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3418 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3419 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3420 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3421 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3422 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3423 | else |
| 3424 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3425 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3426 | S += "}\n"; |
| 3427 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3428 | S += "\nstatic void __"; |
| 3429 | S += funcName; |
| 3430 | S += "_block_dispose_" + utostr(i); |
| 3431 | S += "(" + StructRef; |
| 3432 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3434 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3435 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3436 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3437 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3438 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3439 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3440 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3441 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3442 | else |
| 3443 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3444 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3446 | return S; |
| 3447 | } |
| 3448 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3449 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3450 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3451 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3452 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3454 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3455 | S += " struct " + Desc; |
| 3456 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3458 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3459 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3460 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3462 | if (BlockDeclRefs.size()) { |
| 3463 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3464 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3465 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3466 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3467 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3468 | std::string ArgName = "_" + FieldName; |
| 3469 | // Handle nested closure invocation. For example: |
| 3470 | // |
| 3471 | // void (^myImportedBlock)(void); |
| 3472 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3474 | // void (^anotherBlock)(void); |
| 3475 | // anotherBlock = ^(void) { |
| 3476 | // myImportedBlock(); // import and invoke the closure |
| 3477 | // }; |
| 3478 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3479 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3480 | S += "struct __block_impl *"; |
| 3481 | Constructor += ", void *" + ArgName; |
| 3482 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3483 | QualType QT = (*I)->getType(); |
| 3484 | if (HasLocalVariableExternalStorage(*I)) |
| 3485 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3486 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3487 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3488 | Constructor += ", " + ArgName; |
| 3489 | } |
| 3490 | S += FieldName + ";\n"; |
| 3491 | } |
| 3492 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3493 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3494 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3495 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3496 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3497 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3498 | { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3499 | std::string TypeString; |
| 3500 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3501 | TypeString += " *"; |
| 3502 | FieldName = TypeString + FieldName; |
| 3503 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3504 | Constructor += ", " + ArgName; |
| 3505 | } |
| 3506 | S += FieldName + "; // by ref\n"; |
| 3507 | } |
| 3508 | // Finish writing the constructor. |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3509 | Constructor += ", int flags=0)"; |
| 3510 | // Initialize all "by copy" arguments. |
| 3511 | bool firsTime = true; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3512 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3513 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3514 | std::string Name = (*I)->getNameAsString(); |
| 3515 | if (firsTime) { |
| 3516 | Constructor += " : "; |
| 3517 | firsTime = false; |
| 3518 | } |
| 3519 | else |
| 3520 | Constructor += ", "; |
| 3521 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3522 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3523 | else |
| 3524 | Constructor += Name + "(_" + Name + ")"; |
| 3525 | } |
| 3526 | // Initialize all "by ref" arguments. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3527 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3528 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3529 | std::string Name = (*I)->getNameAsString(); |
| 3530 | if (firsTime) { |
| 3531 | Constructor += " : "; |
| 3532 | firsTime = false; |
| 3533 | } |
| 3534 | else |
| 3535 | Constructor += ", "; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3536 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | Constructor += " {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3540 | if (GlobalVarDecl) |
| 3541 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3542 | else |
| 3543 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3544 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3545 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3546 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3547 | } else { |
| 3548 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3549 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3550 | if (GlobalVarDecl) |
| 3551 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3552 | else |
| 3553 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3554 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3555 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3556 | } |
| 3557 | Constructor += " "; |
| 3558 | Constructor += "}\n"; |
| 3559 | S += Constructor; |
| 3560 | S += "};\n"; |
| 3561 | return S; |
| 3562 | } |
| 3563 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3564 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3565 | std::string ImplTag, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3566 | StringRef FunName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3567 | unsigned hasCopy) { |
| 3568 | std::string S = "\nstatic struct " + DescTag; |
| 3569 | |
| 3570 | S += " {\n unsigned long reserved;\n"; |
| 3571 | S += " unsigned long Block_size;\n"; |
| 3572 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3573 | S += " void (*copy)(struct "; |
| 3574 | S += ImplTag; S += "*, struct "; |
| 3575 | S += ImplTag; S += "*);\n"; |
| 3576 | |
| 3577 | S += " void (*dispose)(struct "; |
| 3578 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3579 | } |
| 3580 | S += "} "; |
| 3581 | |
| 3582 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3583 | S += ImplTag + ")"; |
| 3584 | if (hasCopy) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3585 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3586 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3587 | } |
| 3588 | S += "};\n"; |
| 3589 | return S; |
| 3590 | } |
| 3591 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3592 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3593 | StringRef FunName) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3594 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3595 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3596 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3597 | bool RewriteSC = (GlobalVarDecl && |
| 3598 | !Blocks.empty() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3599 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3600 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3601 | if (RewriteSC) { |
| 3602 | std::string SC(" void __"); |
| 3603 | SC += GlobalVarDecl->getNameAsString(); |
| 3604 | SC += "() {}"; |
| 3605 | InsertText(FunLocStart, SC); |
| 3606 | } |
| 3607 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3608 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3609 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3610 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3611 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3612 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3613 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3614 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3615 | ValueDecl *VD = Exp->getDecl(); |
| 3616 | BlockDeclRefs.push_back(Exp); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3617 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3618 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3619 | BlockByCopyDecls.push_back(VD); |
| 3620 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3621 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3622 | BlockByRefDeclsPtrSet.insert(VD); |
| 3623 | BlockByRefDecls.push_back(VD); |
| 3624 | } |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3625 | // imported objects in the inner blocks not used in the outer |
| 3626 | // blocks must be copied/disposed in the outer block as well. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3627 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3628 | VD->getType()->isObjCObjectPointerType() || |
| 3629 | VD->getType()->isBlockPointerType()) |
| 3630 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3631 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3632 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3633 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3634 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3636 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3637 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3638 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3639 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3640 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3642 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3643 | |
| 3644 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3645 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3646 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3647 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3648 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3649 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3650 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3652 | BlockDeclRefs.clear(); |
| 3653 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3654 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3655 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3656 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3657 | ImportedBlockDecls.clear(); |
| 3658 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3659 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3660 | // Must insert any 'const/volatile/static here. Since it has been |
| 3661 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3662 | std::string SC; |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3663 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3664 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3665 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3666 | SC += "const "; |
| 3667 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3668 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3669 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3670 | SC += "restrict "; |
| 3671 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3674 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3675 | InnerDeclRefsCount.clear(); |
| 3676 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3677 | RewrittenBlockExprs.clear(); |
| 3678 | } |
| 3679 | |
| 3680 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3681 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3682 | StringRef FuncName = FD->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3684 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3685 | } |
| 3686 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3687 | static void BuildUniqueMethodName(std::string &Name, |
| 3688 | ObjCMethodDecl *MD) { |
| 3689 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3690 | Name = IFace->getName(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3691 | Name += "__" + MD->getSelector().getAsString(); |
| 3692 | // Convert colons to underscores. |
| 3693 | std::string::size_type loc = 0; |
| 3694 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3695 | Name.replace(loc, 1, "_"); |
| 3696 | } |
| 3697 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3698 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3699 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3700 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3701 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3702 | std::string FuncName; |
| 3703 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3704 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3708 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3709 | if (*CI) { |
| 3710 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3711 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3712 | else |
| 3713 | GetBlockDeclRefExprs(*CI); |
| 3714 | } |
| 3715 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3716 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3717 | if (DRE->refersToEnclosingLocal()) { |
| 3718 | // FIXME: Handle enums. |
| 3719 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3720 | BlockDeclRefs.push_back(DRE); |
| 3721 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3722 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3723 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3724 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3725 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3726 | return; |
| 3727 | } |
| 3728 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3729 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 3730 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3731 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3732 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3733 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3734 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3735 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3736 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3737 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3738 | InnerContexts); |
| 3739 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3740 | else |
| 3741 | GetInnerBlockDeclRefExprs(*CI, |
| 3742 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3743 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3744 | |
| 3745 | } |
| 3746 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3747 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3748 | if (DRE->refersToEnclosingLocal()) { |
| 3749 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3750 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3751 | InnerBlockDeclRefs.push_back(DRE); |
| 3752 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3753 | if (Var->isFunctionOrMethodVarDecl()) |
| 3754 | ImportedLocalExternalDecls.insert(Var); |
| 3755 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3756 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3757 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3758 | return; |
| 3759 | } |
| 3760 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3761 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3762 | /// 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] | 3763 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3764 | /// all block pointers to function pointers. |
| 3765 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3766 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3767 | // FTP will be null for closures that don't take arguments. |
| 3768 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3769 | SmallVector<QualType, 8> ArgTypes; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3770 | QualType Res = FT->getReturnType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3771 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3772 | |
| 3773 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3774 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3775 | E = FTP->param_type_end(); |
| 3776 | I && (I != E); ++I) { |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3777 | QualType t = *I; |
| 3778 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3779 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3780 | HasBlockType = true; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3781 | ArgTypes.push_back(t); |
| 3782 | } |
| 3783 | } |
| 3784 | QualType FuncType; |
| 3785 | // FIXME. Does this work if block takes no argument but has a return type |
| 3786 | // which is of block type? |
| 3787 | if (HasBlockType) |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3788 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3789 | else FuncType = QualType(FT, 0); |
| 3790 | return FuncType; |
| 3791 | } |
| 3792 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3793 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3794 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3795 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3797 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3798 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3799 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3800 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3801 | } |
| 3802 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3803 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3804 | } |
| 3805 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3806 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3807 | else if (const ConditionalOperator *CEXPR = |
| 3808 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3809 | Expr *LHSExp = CEXPR->getLHS(); |
| 3810 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3811 | Expr *RHSExp = CEXPR->getRHS(); |
| 3812 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3813 | Expr *CONDExp = CEXPR->getCond(); |
| 3814 | ConditionalOperator *CondExpr = |
| 3815 | new (Context) ConditionalOperator(CONDExp, |
| 3816 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3817 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3818 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3819 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3820 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3821 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3822 | } else if (const PseudoObjectExpr *POE |
| 3823 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3824 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3825 | } else { |
| 3826 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3827 | } |
| 3828 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3829 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3830 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3831 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3832 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3834 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3835 | SourceLocation(), SourceLocation(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3836 | &Context->Idents.get("__block_impl")); |
| 3837 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3838 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3839 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3840 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3841 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3842 | // Push the block argument type. |
| 3843 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3844 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3845 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3846 | E = FTP->param_type_end(); |
| 3847 | I && (I != E); ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3848 | QualType t = *I; |
| 3849 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3850 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3851 | convertToUnqualifiedObjCType(t); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3852 | ArgTypes.push_back(t); |
| 3853 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3854 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3855 | // Now do the pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3856 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3858 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3860 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3861 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3862 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3863 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3864 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3865 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3866 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3867 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3868 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3869 | SourceLocation(), |
| 3870 | &Context->Idents.get("FuncPtr"), |
| 3871 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3872 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3873 | ICIS_NoInit); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3874 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3875 | FD->getType(), VK_LValue, |
| 3876 | OK_Ordinary); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3878 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3879 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3880 | CK_BitCast, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3881 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3883 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3884 | // Add the implicit argument. |
| 3885 | BlkExprs.push_back(BlkCast); |
| 3886 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3888 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3889 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3890 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3891 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3892 | Exp->getType(), VK_RValue, |
| 3893 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3894 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3897 | // We need to return the rewritten expression to handle cases where the |
| 3898 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3899 | // For example: |
| 3900 | // |
| 3901 | // int main() { |
| 3902 | // __block Foo *f; |
| 3903 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3905 | // void (^myblock)() = ^() { |
| 3906 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3907 | // i = 77; |
| 3908 | // }; |
| 3909 | //} |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3910 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3911 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3912 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3913 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3914 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3915 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3916 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3917 | SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3918 | &Context->Idents.get("__forwarding"), |
| 3919 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3920 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3921 | ICIS_NoInit); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3922 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3923 | FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3924 | FD->getType(), VK_LValue, |
| 3925 | OK_Ordinary); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3926 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3927 | StringRef Name = VD->getName(); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3928 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3929 | &Context->Idents.get(Name), |
| 3930 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3931 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3932 | ICIS_NoInit); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3933 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3934 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3935 | |
| 3936 | |
| 3937 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3938 | // Need parens to enforce precedence. |
Fariborz Jahanian | 5bba75f | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3939 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3940 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3941 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3942 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3943 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3946 | // Rewrites the imported local variable V with external storage |
| 3947 | // (static, extern, etc.) as *V |
| 3948 | // |
| 3949 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3950 | ValueDecl *VD = DRE->getDecl(); |
| 3951 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3952 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3953 | return DRE; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3954 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3955 | VK_LValue, OK_Ordinary, |
| 3956 | DRE->getLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3957 | // Need parens to enforce precedence. |
| 3958 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3959 | Exp); |
| 3960 | ReplaceStmt(DRE, PE); |
| 3961 | return PE; |
| 3962 | } |
| 3963 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3964 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3965 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3966 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3967 | |
| 3968 | // Need to avoid trying to rewrite synthesized casts. |
| 3969 | if (LocStart.isInvalid()) |
| 3970 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3971 | // Need to avoid trying to rewrite casts contained in macros. |
| 3972 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3973 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3975 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3976 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3977 | QualType QT = CE->getType(); |
| 3978 | const Type* TypePtr = QT->getAs<Type>(); |
| 3979 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3980 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3981 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 3982 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 3983 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3984 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3985 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3986 | return; |
| 3987 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3988 | // advance the location to startArgList. |
| 3989 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3990 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3991 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3992 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3993 | case '^': |
| 3994 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3995 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3996 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3997 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | return; |
| 4001 | } |
| 4002 | |
| 4003 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4004 | SourceLocation DeclLoc = FD->getLocation(); |
| 4005 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4007 | // We have 1 or more arguments that have closure pointers. |
| 4008 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4009 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4011 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4013 | parenCount++; |
| 4014 | // advance the location to startArgList. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4015 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4016 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4018 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4020 | while (*argPtr++ && parenCount) { |
| 4021 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4022 | case '^': |
| 4023 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4024 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4025 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4026 | break; |
| 4027 | case '(': |
| 4028 | parenCount++; |
| 4029 | break; |
| 4030 | case ')': |
| 4031 | parenCount--; |
| 4032 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | return; |
| 4036 | } |
| 4037 | |
| 4038 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4039 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4040 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4041 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4042 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4043 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4044 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4045 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4046 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | } |
| 4048 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4049 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4050 | E = FTP->param_type_end(); |
| 4051 | I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4052 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4053 | return true; |
| 4054 | } |
| 4055 | return false; |
| 4056 | } |
| 4057 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4058 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4059 | const FunctionProtoType *FTP; |
| 4060 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4061 | if (PT) { |
| 4062 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4063 | } else { |
| 4064 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4065 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4066 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4067 | } |
| 4068 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4069 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4070 | E = FTP->param_type_end(); |
| 4071 | I != E; ++I) { |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4072 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4073 | return true; |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4074 | if ((*I)->isObjCObjectPointerType() && |
| 4075 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4076 | return true; |
| 4077 | } |
| 4078 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4079 | } |
| 4080 | return false; |
| 4081 | } |
| 4082 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4083 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4084 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4085 | const char *argPtr = strchr(Name, '('); |
| 4086 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4088 | LParen = argPtr; // output the start. |
| 4089 | argPtr++; // skip past the left paren. |
| 4090 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4092 | while (*argPtr && parenCount) { |
| 4093 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4094 | case '(': parenCount++; break; |
| 4095 | case ')': parenCount--; break; |
| 4096 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4097 | } |
| 4098 | if (parenCount) argPtr++; |
| 4099 | } |
| 4100 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4101 | RParen = argPtr; // output the end |
| 4102 | } |
| 4103 | |
| 4104 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4105 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4106 | RewriteBlockPointerFunctionArgs(FD); |
| 4107 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4109 | // Handle Variables and Typedefs. |
| 4110 | SourceLocation DeclLoc = ND->getLocation(); |
| 4111 | QualType DeclT; |
| 4112 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4113 | DeclT = VD->getType(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4114 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | DeclT = TDD->getUnderlyingType(); |
| 4116 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4117 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4119 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4121 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4122 | const char *endBuf = startBuf; |
| 4123 | // scan backward (from the decl location) for the end of the previous decl. |
| 4124 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4125 | startBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4126 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4127 | std::string buf; |
| 4128 | unsigned OrigLength=0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4129 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4130 | // may take block argument types (which will be handled below). |
| 4131 | if (*startBuf == '^') { |
| 4132 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4133 | buf = '*'; |
| 4134 | startBuf++; |
| 4135 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4136 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4137 | while (*startBuf != ')') { |
| 4138 | buf += *startBuf; |
| 4139 | startBuf++; |
| 4140 | OrigLength++; |
| 4141 | } |
| 4142 | buf += ')'; |
| 4143 | OrigLength++; |
| 4144 | |
| 4145 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4146 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4147 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4148 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4149 | DeclLoc = ND->getLocation(); |
| 4150 | startBuf = SM->getCharacterData(DeclLoc); |
| 4151 | const char *argListBegin, *argListEnd; |
| 4152 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4153 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4154 | if (*argListBegin == '^') |
| 4155 | buf += '*'; |
| 4156 | else if (*argListBegin == '<') { |
| 4157 | buf += "/*"; |
| 4158 | buf += *argListBegin++; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4159 | OrigLength++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4160 | while (*argListBegin != '>') { |
| 4161 | buf += *argListBegin++; |
| 4162 | OrigLength++; |
| 4163 | } |
| 4164 | buf += *argListBegin; |
| 4165 | buf += "*/"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4166 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4167 | else |
| 4168 | buf += *argListBegin; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4169 | argListBegin++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4170 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4171 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4172 | buf += ')'; |
| 4173 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4174 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4175 | ReplaceText(Start, OrigLength, buf); |
| 4176 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4177 | return; |
| 4178 | } |
| 4179 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4180 | |
| 4181 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4182 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4183 | /// struct Block_byref_id_object *src) { |
| 4184 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4185 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4186 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4187 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4188 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4189 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4190 | /// } |
| 4191 | /// And: |
| 4192 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4193 | /// _Block_object_dispose(_src->object, |
| 4194 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4195 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4196 | /// _Block_object_dispose(_src->object, |
| 4197 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4198 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4199 | /// } |
| 4200 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4201 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4202 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4203 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4204 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4205 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4206 | CopyDestroyCache.insert(flag); |
| 4207 | S = "static void __Block_byref_id_object_copy_"; |
| 4208 | S += utostr(flag); |
| 4209 | S += "(void *dst, void *src) {\n"; |
| 4210 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4211 | // offset into the object pointer is computed as: |
| 4212 | // void * + void* + int + int + void* + void * |
| 4213 | unsigned IntSize = |
| 4214 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4215 | unsigned VoidPtrSize = |
| 4216 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4217 | |
Ken Dyck | d9c83e6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4218 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4219 | S += " _Block_object_assign((char*)dst + "; |
| 4220 | S += utostr(offset); |
| 4221 | S += ", *(void * *) ((char*)src + "; |
| 4222 | S += utostr(offset); |
| 4223 | S += "), "; |
| 4224 | S += utostr(flag); |
| 4225 | S += ");\n}\n"; |
| 4226 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4227 | S += "static void __Block_byref_id_object_dispose_"; |
| 4228 | S += utostr(flag); |
| 4229 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4230 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4231 | S += utostr(offset); |
| 4232 | S += "), "; |
| 4233 | S += utostr(flag); |
| 4234 | S += ");\n}\n"; |
| 4235 | return S; |
| 4236 | } |
| 4237 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4238 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4239 | /// the declaration into: |
| 4240 | /// struct __Block_byref_ND { |
| 4241 | /// void *__isa; // NULL for everything except __weak pointers |
| 4242 | /// struct __Block_byref_ND *__forwarding; |
| 4243 | /// int32_t __flags; |
| 4244 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4245 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4246 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4247 | /// typex ND; |
| 4248 | /// }; |
| 4249 | /// |
| 4250 | /// It then replaces declaration of ND variable with: |
| 4251 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4252 | /// __size=sizeof(struct __Block_byref_ND), |
| 4253 | /// ND=initializer-if-any}; |
| 4254 | /// |
| 4255 | /// |
| 4256 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4257 | // Insert declaration for the function in which block literal is |
| 4258 | // used. |
| 4259 | if (CurFunctionDeclToDeclareForBlock) |
| 4260 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4261 | int flag = 0; |
| 4262 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4263 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4264 | if (DeclLoc.isInvalid()) |
| 4265 | // If type location is missing, it is because of missing type (a warning). |
| 4266 | // Use variable's location which is good for this case. |
| 4267 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4268 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4269 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4270 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4271 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4272 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4273 | std::string ByrefType; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4274 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4275 | ByrefType += " {\n"; |
| 4276 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4277 | RewriteByRefString(ByrefType, Name, ND); |
| 4278 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4279 | ByrefType += " int __flags;\n"; |
| 4280 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4281 | // Add void *__Block_byref_id_object_copy; |
| 4282 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4283 | QualType Ty = ND->getType(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4284 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4285 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4286 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4287 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4288 | } |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4289 | |
| 4290 | QualType T = Ty; |
| 4291 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4292 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4293 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4294 | ByrefType += " " + Name + ";\n"; |
| 4295 | ByrefType += "};\n"; |
| 4296 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4297 | SourceLocation FunLocStart; |
| 4298 | if (CurFunctionDef) |
| 4299 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4300 | else { |
| 4301 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4302 | FunLocStart = CurMethodDef->getLocStart(); |
| 4303 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4304 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4305 | if (Ty.isObjCGCWeak()) { |
| 4306 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4307 | isa = 1; |
| 4308 | } |
| 4309 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4310 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4311 | flag = BLOCK_BYREF_CALLER; |
| 4312 | QualType Ty = ND->getType(); |
| 4313 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4314 | if (Ty->isBlockPointerType()) |
| 4315 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4316 | else |
| 4317 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4318 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4319 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4320 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4321 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4322 | |
| 4323 | // struct __Block_byref_ND ND = |
| 4324 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4325 | // initializer-if-any}; |
| 4326 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4327 | unsigned flags = 0; |
| 4328 | if (HasCopyAndDispose) |
| 4329 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4330 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4331 | ByrefType.clear(); |
| 4332 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4333 | std::string ForwardingCastType("("); |
| 4334 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4335 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4336 | ByrefType += " " + Name + " = {(void*)"; |
| 4337 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4338 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4339 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4340 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4341 | ByrefType += "sizeof("; |
| 4342 | RewriteByRefString(ByrefType, Name, ND); |
| 4343 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4344 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4345 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4346 | ByrefType += utostr(flag); |
| 4347 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4348 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4349 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4350 | ByrefType += "};\n"; |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4351 | unsigned nameSize = Name.size(); |
| 4352 | // for block or function pointer declaration. Name is aleady |
| 4353 | // part of the declaration. |
| 4354 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4355 | nameSize = 1; |
| 4356 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4357 | } |
| 4358 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4359 | SourceLocation startLoc; |
| 4360 | Expr *E = ND->getInit(); |
| 4361 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4362 | startLoc = ECE->getLParenLoc(); |
| 4363 | else |
| 4364 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4365 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4366 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4367 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4368 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4369 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4370 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4371 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4372 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4373 | ByrefType += "sizeof("; |
| 4374 | RewriteByRefString(ByrefType, Name, ND); |
| 4375 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4376 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4377 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4378 | ByrefType += utostr(flag); |
| 4379 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4380 | ByrefType += utostr(flag); |
| 4381 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4382 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4383 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4384 | |
| 4385 | // Complete the newly synthesized compound expression by inserting a right |
| 4386 | // curly brace before the end of the declaration. |
| 4387 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4388 | // also assumes there is only one declarator. For example, the following |
| 4389 | // isn't currently supported by this routine (in general): |
| 4390 | // |
| 4391 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4392 | // |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4393 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4394 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4395 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4396 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4397 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4398 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4399 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4400 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4401 | return; |
| 4402 | } |
| 4403 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4404 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4405 | // Add initializers for any closure decl refs. |
| 4406 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4407 | if (BlockDeclRefs.size()) { |
| 4408 | // Unique all "by copy" declarations. |
| 4409 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4410 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4411 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4412 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4413 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4414 | } |
| 4415 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4416 | // Unique all "by ref" declarations. |
| 4417 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4418 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4419 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4420 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4421 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4422 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | } |
| 4424 | // Find any imported blocks...they will need special attention. |
| 4425 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4426 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4427 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4428 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4429 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4433 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4434 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4435 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4436 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4437 | SourceLocation(), ID, FType, 0, SC_Extern, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4438 | false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4441 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4442 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4443 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4444 | Blocks.push_back(Exp); |
| 4445 | |
| 4446 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4447 | |
| 4448 | // Add inner imported variables now used in current block. |
| 4449 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4450 | if (!InnerBlockDeclRefs.empty()) { |
| 4451 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4452 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4453 | ValueDecl *VD = Exp->getDecl(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4454 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4455 | // We need to save the copied-in variables in nested |
| 4456 | // blocks because it is needed at the end for some of the API generations. |
| 4457 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4458 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4459 | BlockDeclRefs.push_back(Exp); |
| 4460 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4461 | BlockByCopyDecls.push_back(VD); |
| 4462 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4463 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4464 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4465 | BlockDeclRefs.push_back(Exp); |
| 4466 | BlockByRefDeclsPtrSet.insert(VD); |
| 4467 | BlockByRefDecls.push_back(VD); |
| 4468 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4469 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4470 | // Find any imported blocks...they will need special attention. |
| 4471 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4472 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4473 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4474 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4475 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4476 | } |
| 4477 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4478 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4479 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4481 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4482 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4483 | else if (CurMethodDef) |
| 4484 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4485 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4486 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4488 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4490 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4491 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4493 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4494 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4495 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4496 | |
| 4497 | FunctionDecl *FD; |
| 4498 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 4500 | // Simulate a constructor call... |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4501 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4502 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4503 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4505 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4507 | // Initialize the block function. |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4508 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4509 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4510 | VK_LValue, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4511 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4512 | CK_BitCast, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | InitExprs.push_back(castExpr); |
| 4514 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4515 | // Initialize the block descriptor. |
| 4516 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4518 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4519 | SourceLocation(), SourceLocation(), |
| 4520 | &Context->Idents.get(DescData.c_str()), |
| 4521 | Context->VoidPtrTy, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4522 | SC_Static); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4523 | UnaryOperator *DescRefExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4524 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4525 | Context->VoidPtrTy, |
| 4526 | VK_LValue, |
| 4527 | SourceLocation()), |
| 4528 | UO_AddrOf, |
| 4529 | Context->getPointerType(Context->VoidPtrTy), |
| 4530 | VK_RValue, OK_Ordinary, |
| 4531 | SourceLocation()); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4532 | InitExprs.push_back(DescRefExpr); |
| 4533 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4534 | // Add initializers for any closure decl refs. |
| 4535 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4536 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4537 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4538 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4539 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4540 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4541 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4542 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4543 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4544 | SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4545 | if (HasLocalVariableExternalStorage(*I)) { |
| 4546 | QualType QT = (*I)->getType(); |
| 4547 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4548 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4549 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4550 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4551 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4552 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4553 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4554 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4555 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4556 | CK_BitCast, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | } else { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4558 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4559 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4560 | SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4561 | if (HasLocalVariableExternalStorage(*I)) { |
| 4562 | QualType QT = (*I)->getType(); |
| 4563 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4564 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4565 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4568 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4569 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4570 | } |
| 4571 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4572 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4573 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4574 | ValueDecl *ND = (*I); |
| 4575 | std::string Name(ND->getNameAsString()); |
| 4576 | std::string RecName; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4577 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4578 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4579 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4580 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4581 | SourceLocation(), SourceLocation(), |
| 4582 | II); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4583 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4584 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4585 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4586 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4587 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4588 | SourceLocation()); |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4589 | bool isNestedCapturedVar = false; |
| 4590 | if (block) |
| 4591 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4592 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4593 | const VarDecl *variable = ci->getVariable(); |
| 4594 | if (variable == ND && ci->isNested()) { |
| 4595 | assert (ci->isByRef() && |
| 4596 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4597 | isNestedCapturedVar = true; |
| 4598 | break; |
| 4599 | } |
| 4600 | } |
| 4601 | // captured nested byref variable has its address passed. Do not take |
| 4602 | // its address again. |
| 4603 | if (!isNestedCapturedVar) |
| 4604 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4605 | Context->getPointerType(Exp->getType()), |
| 4606 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4607 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4608 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4609 | } |
| 4610 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4611 | if (ImportedBlockDecls.size()) { |
| 4612 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4613 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4614 | unsigned IntSize = |
| 4615 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4616 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4617 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4618 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4619 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4620 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4621 | FType, VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4622 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | Context->getPointerType(NewRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4624 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4625 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4626 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4627 | BlockDeclRefs.clear(); |
| 4628 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4629 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4630 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4631 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4632 | ImportedBlockDecls.clear(); |
| 4633 | return NewRep; |
| 4634 | } |
| 4635 | |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4636 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4637 | if (const ObjCForCollectionStmt * CS = |
| 4638 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4639 | return CS->getElement() == DS; |
| 4640 | return false; |
| 4641 | } |
| 4642 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4643 | //===----------------------------------------------------------------------===// |
| 4644 | // Function Body / Expression rewriting |
| 4645 | //===----------------------------------------------------------------------===// |
| 4646 | |
| 4647 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4649 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4650 | Stmts.push_back(S); |
| 4651 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4652 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4653 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4654 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4656 | // Pseudo-object operations and ivar references need special |
| 4657 | // treatment because we're going to recursively rewrite them. |
| 4658 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4659 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4660 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4661 | } else { |
| 4662 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4663 | } |
| 4664 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4665 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4666 | } |
| 4667 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4668 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4670 | // Perform a bottom up rewrite of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4671 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4672 | if (*CI) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4673 | Stmt *childStmt = (*CI); |
| 4674 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4675 | if (newStmt) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4676 | *CI = newStmt; |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4677 | } |
Nick Lewycky | 508ef2c | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4680 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4681 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4682 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4683 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4684 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4685 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4686 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4687 | // Rewrite the block body in place. |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4688 | Stmt *SaveCurrentBody = CurrentBody; |
| 4689 | CurrentBody = BE->getBody(); |
| 4690 | PropParentMap = 0; |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4691 | // block literal on rhs of a property-dot-sytax assignment |
| 4692 | // must be replaced by its synthesize ast so getRewrittenText |
| 4693 | // works as expected. In this case, what actually ends up on RHS |
| 4694 | // is the blockTranscribed which is the helper function for the |
| 4695 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4696 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4697 | DisableReplaceStmt = false; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4698 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4699 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4700 | CurrentBody = SaveCurrentBody; |
| 4701 | PropParentMap = 0; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4702 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4703 | // 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] | 4704 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4705 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4707 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4708 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4709 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4710 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4711 | return blockTranscribed; |
| 4712 | } |
| 4713 | // Handle specific things. |
| 4714 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4715 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4717 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4718 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4720 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4721 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4722 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4723 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4724 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | // Before we rewrite it, put the original message expression in a comment. |
| 4726 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4727 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4728 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4729 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4730 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4732 | std::string messString; |
| 4733 | messString += "// "; |
| 4734 | messString.append(startBuf, endBuf-startBuf+1); |
| 4735 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
| 4737 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4738 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4739 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4740 | // Tried this, but it didn't work either... |
| 4741 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4742 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | return RewriteMessageExpr(MessExpr); |
| 4744 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4746 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4747 | return RewriteObjCTryStmt(StmtTry); |
| 4748 | |
| 4749 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4750 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4751 | |
| 4752 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4753 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4755 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4756 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
| 4758 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4759 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4760 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4761 | OrigStmtRange.getEnd()); |
| 4762 | if (BreakStmt *StmtBreakStmt = |
| 4763 | dyn_cast<BreakStmt>(S)) |
| 4764 | return RewriteBreakStmt(StmtBreakStmt); |
| 4765 | if (ContinueStmt *StmtContinueStmt = |
| 4766 | dyn_cast<ContinueStmt>(S)) |
| 4767 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | |
| 4769 | // 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] | 4770 | // and cast exprs. |
| 4771 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4772 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4773 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4774 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4775 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4776 | // the context of an ObjCForCollectionStmt. For example: |
| 4777 | // NSArray *someArray; |
| 4778 | // for (id <FooProtocol> index in someArray) ; |
| 4779 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4780 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4781 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4782 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4784 | // Blocks rewrite rules. |
| 4785 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4786 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4787 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4788 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4789 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4790 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4791 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4792 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4793 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4794 | if (VD->hasAttr<BlocksAttr>()) { |
| 4795 | static unsigned uniqueByrefDeclCount = 0; |
| 4796 | assert(!BlockByRefDeclNo.count(ND) && |
| 4797 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4798 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4799 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4800 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4801 | else |
| 4802 | RewriteTypeOfDecl(VD); |
| 4803 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4804 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4805 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4806 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4807 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4809 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4810 | } |
| 4811 | } |
| 4812 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4814 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4815 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | |
| 4817 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4818 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4819 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4821 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4822 | && "Statement stack mismatch"); |
| 4823 | Stmts.pop_back(); |
| 4824 | } |
| 4825 | // Handle blocks rewriting. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4826 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4827 | ValueDecl *VD = DRE->getDecl(); |
| 4828 | if (VD->hasAttr<BlocksAttr>()) |
| 4829 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4830 | if (HasLocalVariableExternalStorage(VD)) |
| 4831 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4834 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4835 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4836 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4837 | ReplaceStmt(S, BlockCall); |
| 4838 | return BlockCall; |
| 4839 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4840 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4841 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4842 | RewriteCastExpr(CE); |
| 4843 | } |
| 4844 | #if 0 |
| 4845 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4846 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4847 | ICE->getSubExpr(), |
| 4848 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4849 | // Get the new text. |
| 4850 | std::string SStr; |
| 4851 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4852 | Replacement->printPretty(Buf); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4853 | const std::string &Str = Buf.str(); |
| 4854 | |
| 4855 | printf("CAST = %s\n", &Str[0]); |
| 4856 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4857 | delete S; |
| 4858 | return Replacement; |
| 4859 | } |
| 4860 | #endif |
| 4861 | // Return this stmt unmodified. |
| 4862 | return S; |
| 4863 | } |
| 4864 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4865 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4866 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4867 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4868 | FieldDecl *FD = *i; |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4869 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4870 | RewriteBlockPointerDecl(FD); |
| 4871 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4872 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4873 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4874 | } |
| 4875 | } |
| 4876 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4877 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4878 | /// main file of the input. |
| 4879 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4880 | switch (D->getKind()) { |
| 4881 | case Decl::Function: { |
| 4882 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4883 | if (FD->isOverloadedOperator()) |
| 4884 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4885 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4886 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4887 | // prototype. This enables us to rewrite function declarations and |
| 4888 | // definitions using the same code. |
| 4889 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4890 | |
Argyrios Kyrtzidis | 75627ad | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4891 | if (!FD->isThisDeclarationADefinition()) |
| 4892 | break; |
| 4893 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4894 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4895 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4896 | CurFunctionDef = FD; |
| 4897 | CurFunctionDeclToDeclareForBlock = FD; |
| 4898 | CurrentBody = Body; |
| 4899 | Body = |
| 4900 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4901 | FD->setBody(Body); |
| 4902 | CurrentBody = 0; |
| 4903 | if (PropParentMap) { |
| 4904 | delete PropParentMap; |
| 4905 | PropParentMap = 0; |
| 4906 | } |
| 4907 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4908 | // and any copy/dispose helper functions. |
| 4909 | InsertBlockLiteralsWithinFunction(FD); |
| 4910 | CurFunctionDef = 0; |
| 4911 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4912 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4913 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4914 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4915 | case Decl::ObjCMethod: { |
| 4916 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4917 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4918 | CurMethodDef = MD; |
| 4919 | CurrentBody = Body; |
| 4920 | Body = |
| 4921 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4922 | MD->setBody(Body); |
| 4923 | CurrentBody = 0; |
| 4924 | if (PropParentMap) { |
| 4925 | delete PropParentMap; |
| 4926 | PropParentMap = 0; |
| 4927 | } |
| 4928 | InsertBlockLiteralsWithinMethod(MD); |
| 4929 | CurMethodDef = 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; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4932 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4933 | case Decl::ObjCImplementation: { |
| 4934 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4935 | ClassImplementation.push_back(CI); |
| 4936 | break; |
| 4937 | } |
| 4938 | case Decl::ObjCCategoryImpl: { |
| 4939 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4940 | CategoryImplementation.push_back(CI); |
| 4941 | break; |
| 4942 | } |
| 4943 | case Decl::Var: { |
| 4944 | VarDecl *VD = cast<VarDecl>(D); |
| 4945 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4946 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4947 | RewriteBlockPointerDecl(VD); |
| 4948 | else if (VD->getType()->isFunctionPointerType()) { |
| 4949 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4950 | if (VD->getInit()) { |
| 4951 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4952 | RewriteCastExpr(CE); |
| 4953 | } |
| 4954 | } |
| 4955 | } else if (VD->getType()->isRecordType()) { |
| 4956 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4957 | if (RD->isCompleteDefinition()) |
| 4958 | RewriteRecordBody(RD); |
| 4959 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4960 | if (VD->getInit()) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4961 | GlobalVarDecl = VD; |
| 4962 | CurrentBody = VD->getInit(); |
| 4963 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4964 | CurrentBody = 0; |
| 4965 | if (PropParentMap) { |
| 4966 | delete PropParentMap; |
| 4967 | PropParentMap = 0; |
| 4968 | } |
| 4969 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4970 | GlobalVarDecl = 0; |
| 4971 | |
| 4972 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4973 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4974 | RewriteCastExpr(CE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4977 | break; |
| 4978 | } |
| 4979 | case Decl::TypeAlias: |
| 4980 | case Decl::Typedef: { |
| 4981 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4982 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 4983 | RewriteBlockPointerDecl(TD); |
| 4984 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4985 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4986 | } |
| 4987 | break; |
| 4988 | } |
| 4989 | case Decl::CXXRecord: |
| 4990 | case Decl::Record: { |
| 4991 | RecordDecl *RD = cast<RecordDecl>(D); |
| 4992 | if (RD->isCompleteDefinition()) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4993 | RewriteRecordBody(RD); |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4994 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4995 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4996 | default: |
| 4997 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4998 | } |
| 4999 | // Nothing yet. |
| 5000 | } |
| 5001 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5002 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5003 | if (Diags.hasErrorOccurred()) |
| 5004 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5005 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5006 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5007 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5008 | // Here's a great place to add any extra declarations that may be needed. |
| 5009 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5010 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5011 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5012 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5013 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5014 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5015 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5016 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5017 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5018 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5019 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5021 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5022 | //printf("Changed:\n"); |
| 5023 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5024 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5025 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5026 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5027 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5028 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5029 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5030 | // Rewrite Objective-c meta data* |
| 5031 | std::string ResultStr; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5032 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5033 | // Emit metadata. |
| 5034 | *OutFile << ResultStr; |
| 5035 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5036 | OutFile->flush(); |
| 5037 | } |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5038 | |
| 5039 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5040 | InitializeCommon(context); |
| 5041 | |
| 5042 | // declaring objc_selector outside the parameter list removes a silly |
| 5043 | // scope related warning... |
| 5044 | if (IsHeader) |
| 5045 | Preamble = "#pragma once\n"; |
| 5046 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5047 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5048 | Preamble += "struct objc_object *superClass; "; |
| 5049 | if (LangOpts.MicrosoftExt) { |
| 5050 | // Add a constructor for creating temporary objects. |
| 5051 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5052 | ": "; |
| 5053 | Preamble += "object(o), superClass(s) {} "; |
| 5054 | } |
| 5055 | Preamble += "};\n"; |
| 5056 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5057 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5058 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5059 | Preamble += "#endif\n"; |
| 5060 | if (LangOpts.MicrosoftExt) { |
| 5061 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5062 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5063 | } else |
| 5064 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5065 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5066 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5067 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5068 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5069 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5070 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5071 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5072 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5073 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5074 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5075 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5076 | Preamble += "(const char *);\n"; |
| 5077 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5078 | Preamble += "(struct objc_class *);\n"; |
| 5079 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5080 | Preamble += "(const char *);\n"; |
| 5081 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5082 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5083 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5084 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5085 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5086 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5087 | // @synchronized hooks. |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5088 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5089 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5090 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5091 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5092 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5093 | Preamble += "unsigned long state;\n\t"; |
| 5094 | Preamble += "void **itemsPtr;\n\t"; |
| 5095 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5096 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5097 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5098 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5099 | Preamble += "#endif\n"; |
| 5100 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5101 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5102 | Preamble += " int *isa;\n"; |
| 5103 | Preamble += " int flags;\n"; |
| 5104 | Preamble += " char *str;\n"; |
| 5105 | Preamble += " long length;\n"; |
| 5106 | Preamble += "};\n"; |
| 5107 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5108 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5109 | Preamble += "#else\n"; |
| 5110 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5111 | Preamble += "#endif\n"; |
| 5112 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5113 | Preamble += "#endif\n"; |
| 5114 | // Blocks preamble. |
| 5115 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5116 | Preamble += "#define BLOCK_IMPL\n"; |
| 5117 | Preamble += "struct __block_impl {\n"; |
| 5118 | Preamble += " void *isa;\n"; |
| 5119 | Preamble += " int Flags;\n"; |
| 5120 | Preamble += " int Reserved;\n"; |
| 5121 | Preamble += " void *FuncPtr;\n"; |
| 5122 | Preamble += "};\n"; |
| 5123 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5124 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5125 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5126 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5127 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5128 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5129 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5130 | Preamble += "#else\n"; |
| 5131 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5132 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5133 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5134 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5135 | Preamble += "#endif\n"; |
| 5136 | Preamble += "#endif\n"; |
| 5137 | if (LangOpts.MicrosoftExt) { |
| 5138 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5139 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5140 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5141 | Preamble += "#define __attribute__(X)\n"; |
| 5142 | Preamble += "#endif\n"; |
| 5143 | Preamble += "#define __weak\n"; |
| 5144 | } |
| 5145 | else { |
| 5146 | Preamble += "#define __block\n"; |
| 5147 | Preamble += "#define __weak\n"; |
| 5148 | } |
| 5149 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5150 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5151 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5152 | } |
| 5153 | |
| 5154 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5155 | /// ivar offset. |
| 5156 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5157 | std::string &Result) { |
| 5158 | if (ivar->isBitField()) { |
| 5159 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5160 | // place all bitfields at offset 0. |
| 5161 | Result += "0"; |
| 5162 | } else { |
| 5163 | Result += "__OFFSETOFIVAR__(struct "; |
| 5164 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5165 | if (LangOpts.MicrosoftExt) |
| 5166 | Result += "_IMPL"; |
| 5167 | Result += ", "; |
| 5168 | Result += ivar->getNameAsString(); |
| 5169 | Result += ")"; |
| 5170 | } |
| 5171 | } |
| 5172 | |
| 5173 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5174 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5175 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5176 | StringRef ClassName, std::string &Result) { |
| 5177 | static bool objc_protocol_methods = false; |
| 5178 | |
| 5179 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5180 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5181 | /* struct protocol_methods { |
| 5182 | SEL _cmd; |
| 5183 | char *method_types; |
| 5184 | } |
| 5185 | */ |
| 5186 | Result += "\nstruct _protocol_methods {\n"; |
| 5187 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5188 | Result += "\tchar *method_types;\n"; |
| 5189 | Result += "};\n"; |
| 5190 | |
| 5191 | objc_protocol_methods = true; |
| 5192 | } |
| 5193 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5194 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5195 | return; |
| 5196 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5197 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5198 | PDecl = Def; |
| 5199 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5200 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5201 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5202 | PDecl->instmeth_end()); |
| 5203 | /* struct _objc_protocol_method_list { |
| 5204 | int protocol_method_count; |
| 5205 | struct protocol_methods protocols[]; |
| 5206 | } |
| 5207 | */ |
| 5208 | Result += "\nstatic struct {\n"; |
| 5209 | Result += "\tint protocol_method_count;\n"; |
| 5210 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5211 | Result += utostr(NumMethods); |
| 5212 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5213 | Result += PDecl->getNameAsString(); |
| 5214 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5215 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5216 | |
| 5217 | // Output instance methods declared in this protocol. |
| 5218 | for (ObjCProtocolDecl::instmeth_iterator |
| 5219 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5220 | I != E; ++I) { |
| 5221 | if (I == PDecl->instmeth_begin()) |
| 5222 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5223 | else |
| 5224 | Result += "\t ,{(struct objc_selector *)\""; |
| 5225 | Result += (*I)->getSelector().getAsString(); |
| 5226 | std::string MethodTypeString; |
| 5227 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5228 | Result += "\", \""; |
| 5229 | Result += MethodTypeString; |
| 5230 | Result += "\"}\n"; |
| 5231 | } |
| 5232 | Result += "\t }\n};\n"; |
| 5233 | } |
| 5234 | |
| 5235 | // Output class methods declared in this protocol. |
| 5236 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5237 | PDecl->classmeth_end()); |
| 5238 | if (NumMethods > 0) { |
| 5239 | /* struct _objc_protocol_method_list { |
| 5240 | int protocol_method_count; |
| 5241 | struct protocol_methods protocols[]; |
| 5242 | } |
| 5243 | */ |
| 5244 | Result += "\nstatic struct {\n"; |
| 5245 | Result += "\tint protocol_method_count;\n"; |
| 5246 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5247 | Result += utostr(NumMethods); |
| 5248 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5249 | Result += PDecl->getNameAsString(); |
| 5250 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5251 | "{\n\t"; |
| 5252 | Result += utostr(NumMethods); |
| 5253 | Result += "\n"; |
| 5254 | |
| 5255 | // Output instance methods declared in this protocol. |
| 5256 | for (ObjCProtocolDecl::classmeth_iterator |
| 5257 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5258 | I != E; ++I) { |
| 5259 | if (I == PDecl->classmeth_begin()) |
| 5260 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5261 | else |
| 5262 | Result += "\t ,{(struct objc_selector *)\""; |
| 5263 | Result += (*I)->getSelector().getAsString(); |
| 5264 | std::string MethodTypeString; |
| 5265 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5266 | Result += "\", \""; |
| 5267 | Result += MethodTypeString; |
| 5268 | Result += "\"}\n"; |
| 5269 | } |
| 5270 | Result += "\t }\n};\n"; |
| 5271 | } |
| 5272 | |
| 5273 | // Output: |
| 5274 | /* struct _objc_protocol { |
| 5275 | // Objective-C 1.0 extensions |
| 5276 | struct _objc_protocol_extension *isa; |
| 5277 | char *protocol_name; |
| 5278 | struct _objc_protocol **protocol_list; |
| 5279 | struct _objc_protocol_method_list *instance_methods; |
| 5280 | struct _objc_protocol_method_list *class_methods; |
| 5281 | }; |
| 5282 | */ |
| 5283 | static bool objc_protocol = false; |
| 5284 | if (!objc_protocol) { |
| 5285 | Result += "\nstruct _objc_protocol {\n"; |
| 5286 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5287 | Result += "\tchar *protocol_name;\n"; |
| 5288 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5289 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5290 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5291 | Result += "};\n"; |
| 5292 | |
| 5293 | objc_protocol = true; |
| 5294 | } |
| 5295 | |
| 5296 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5297 | Result += PDecl->getNameAsString(); |
| 5298 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5299 | "{\n\t0, \""; |
| 5300 | Result += PDecl->getNameAsString(); |
| 5301 | Result += "\", 0, "; |
| 5302 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5303 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5304 | Result += PDecl->getNameAsString(); |
| 5305 | Result += ", "; |
| 5306 | } |
| 5307 | else |
| 5308 | Result += "0, "; |
| 5309 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5310 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5311 | Result += PDecl->getNameAsString(); |
| 5312 | Result += "\n"; |
| 5313 | } |
| 5314 | else |
| 5315 | Result += "0\n"; |
| 5316 | Result += "};\n"; |
| 5317 | |
| 5318 | // Mark this protocol as having been generated. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5319 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5320 | llvm_unreachable("protocol already synthesized"); |
| 5321 | |
| 5322 | } |
| 5323 | |
| 5324 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5325 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5326 | StringRef prefix, StringRef ClassName, |
| 5327 | std::string &Result) { |
| 5328 | if (Protocols.empty()) return; |
| 5329 | |
| 5330 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5331 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5332 | |
| 5333 | // Output the top lovel protocol meta-data for the class. |
| 5334 | /* struct _objc_protocol_list { |
| 5335 | struct _objc_protocol_list *next; |
| 5336 | int protocol_count; |
| 5337 | struct _objc_protocol *class_protocols[]; |
| 5338 | } |
| 5339 | */ |
| 5340 | Result += "\nstatic struct {\n"; |
| 5341 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5342 | Result += "\tint protocol_count;\n"; |
| 5343 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5344 | Result += utostr(Protocols.size()); |
| 5345 | Result += "];\n} _OBJC_"; |
| 5346 | Result += prefix; |
| 5347 | Result += "_PROTOCOLS_"; |
| 5348 | Result += ClassName; |
| 5349 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5350 | "{\n\t0, "; |
| 5351 | Result += utostr(Protocols.size()); |
| 5352 | Result += "\n"; |
| 5353 | |
| 5354 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5355 | Result += Protocols[0]->getNameAsString(); |
| 5356 | Result += " \n"; |
| 5357 | |
| 5358 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5359 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5360 | Result += Protocols[i]->getNameAsString(); |
| 5361 | Result += "\n"; |
| 5362 | } |
| 5363 | Result += "\t }\n};\n"; |
| 5364 | } |
| 5365 | |
| 5366 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5367 | std::string &Result) { |
| 5368 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5369 | |
| 5370 | // Explicitly declared @interface's are already synthesized. |
| 5371 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5372 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5373 | // produce correct synthesis as yet. |
| 5374 | RewriteObjCInternalStruct(CDecl, Result); |
| 5375 | } |
| 5376 | |
| 5377 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5378 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5379 | ? IDecl->ivar_size() |
| 5380 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5381 | if (NumIvars > 0) { |
| 5382 | static bool objc_ivar = false; |
| 5383 | if (!objc_ivar) { |
| 5384 | /* struct _objc_ivar { |
| 5385 | char *ivar_name; |
| 5386 | char *ivar_type; |
| 5387 | int ivar_offset; |
| 5388 | }; |
| 5389 | */ |
| 5390 | Result += "\nstruct _objc_ivar {\n"; |
| 5391 | Result += "\tchar *ivar_name;\n"; |
| 5392 | Result += "\tchar *ivar_type;\n"; |
| 5393 | Result += "\tint ivar_offset;\n"; |
| 5394 | Result += "};\n"; |
| 5395 | |
| 5396 | objc_ivar = true; |
| 5397 | } |
| 5398 | |
| 5399 | /* struct { |
| 5400 | int ivar_count; |
| 5401 | struct _objc_ivar ivar_list[nIvars]; |
| 5402 | }; |
| 5403 | */ |
| 5404 | Result += "\nstatic struct {\n"; |
| 5405 | Result += "\tint ivar_count;\n"; |
| 5406 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5407 | Result += utostr(NumIvars); |
| 5408 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5409 | Result += IDecl->getNameAsString(); |
| 5410 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5411 | "{\n\t"; |
| 5412 | Result += utostr(NumIvars); |
| 5413 | Result += "\n"; |
| 5414 | |
| 5415 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5416 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5417 | if (!IDecl->ivar_empty()) { |
| 5418 | for (ObjCInterfaceDecl::ivar_iterator |
| 5419 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5420 | IV != IVEnd; ++IV) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5421 | IVars.push_back(*IV); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5422 | IVI = IDecl->ivar_begin(); |
| 5423 | IVE = IDecl->ivar_end(); |
| 5424 | } else { |
| 5425 | IVI = CDecl->ivar_begin(); |
| 5426 | IVE = CDecl->ivar_end(); |
| 5427 | } |
| 5428 | Result += "\t,{{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5429 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5430 | Result += "\", \""; |
| 5431 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5432 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5433 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5434 | Result += StrEncoding; |
| 5435 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5436 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5437 | Result += "}\n"; |
| 5438 | for (++IVI; IVI != IVE; ++IVI) { |
| 5439 | Result += "\t ,{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5440 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5441 | Result += "\", \""; |
| 5442 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5443 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5444 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5445 | Result += StrEncoding; |
| 5446 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5447 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5448 | Result += "}\n"; |
| 5449 | } |
| 5450 | |
| 5451 | Result += "\t }\n};\n"; |
| 5452 | } |
| 5453 | |
| 5454 | // Build _objc_method_list for class's instance methods if needed |
| 5455 | SmallVector<ObjCMethodDecl *, 32> |
| 5456 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5457 | |
| 5458 | // If any of our property implementations have associated getters or |
| 5459 | // setters, produce metadata for them as well. |
| 5460 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5461 | PropEnd = IDecl->propimpl_end(); |
| 5462 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5463 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5464 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5465 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5466 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5467 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5468 | if (!PD) |
| 5469 | continue; |
| 5470 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5471 | if (!Getter->isDefined()) |
| 5472 | InstanceMethods.push_back(Getter); |
| 5473 | if (PD->isReadOnly()) |
| 5474 | continue; |
| 5475 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5476 | if (!Setter->isDefined()) |
| 5477 | InstanceMethods.push_back(Setter); |
| 5478 | } |
| 5479 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5480 | true, "", IDecl->getName(), Result); |
| 5481 | |
| 5482 | // Build _objc_method_list for class's class methods if needed |
| 5483 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5484 | false, "", IDecl->getName(), Result); |
| 5485 | |
| 5486 | // Protocols referenced in class declaration? |
| 5487 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5488 | "CLASS", CDecl->getName(), Result); |
| 5489 | |
| 5490 | // Declaration of class/meta-class metadata |
| 5491 | /* struct _objc_class { |
| 5492 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5493 | const char *super_class_name; |
| 5494 | char *name; |
| 5495 | long version; |
| 5496 | long info; |
| 5497 | long instance_size; |
| 5498 | struct _objc_ivar_list *ivars; |
| 5499 | struct _objc_method_list *methods; |
| 5500 | struct objc_cache *cache; |
| 5501 | struct objc_protocol_list *protocols; |
| 5502 | const char *ivar_layout; |
| 5503 | struct _objc_class_ext *ext; |
| 5504 | }; |
| 5505 | */ |
| 5506 | static bool objc_class = false; |
| 5507 | if (!objc_class) { |
| 5508 | Result += "\nstruct _objc_class {\n"; |
| 5509 | Result += "\tstruct _objc_class *isa;\n"; |
| 5510 | Result += "\tconst char *super_class_name;\n"; |
| 5511 | Result += "\tchar *name;\n"; |
| 5512 | Result += "\tlong version;\n"; |
| 5513 | Result += "\tlong info;\n"; |
| 5514 | Result += "\tlong instance_size;\n"; |
| 5515 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5516 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5517 | Result += "\tstruct objc_cache *cache;\n"; |
| 5518 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5519 | Result += "\tconst char *ivar_layout;\n"; |
| 5520 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5521 | Result += "};\n"; |
| 5522 | objc_class = true; |
| 5523 | } |
| 5524 | |
| 5525 | // Meta-class metadata generation. |
| 5526 | ObjCInterfaceDecl *RootClass = 0; |
| 5527 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5528 | while (SuperClass) { |
| 5529 | RootClass = SuperClass; |
| 5530 | SuperClass = SuperClass->getSuperClass(); |
| 5531 | } |
| 5532 | SuperClass = CDecl->getSuperClass(); |
| 5533 | |
| 5534 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5535 | Result += CDecl->getNameAsString(); |
| 5536 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5537 | "{\n\t(struct _objc_class *)\""; |
| 5538 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5539 | Result += "\""; |
| 5540 | |
| 5541 | if (SuperClass) { |
| 5542 | Result += ", \""; |
| 5543 | Result += SuperClass->getNameAsString(); |
| 5544 | Result += "\", \""; |
| 5545 | Result += CDecl->getNameAsString(); |
| 5546 | Result += "\""; |
| 5547 | } |
| 5548 | else { |
| 5549 | Result += ", 0, \""; |
| 5550 | Result += CDecl->getNameAsString(); |
| 5551 | Result += "\""; |
| 5552 | } |
| 5553 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5554 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5555 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5556 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5557 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5558 | Result += IDecl->getNameAsString(); |
| 5559 | Result += "\n"; |
| 5560 | } |
| 5561 | else |
| 5562 | Result += ", 0\n"; |
| 5563 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5564 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5565 | Result += CDecl->getNameAsString(); |
| 5566 | Result += ",0,0\n"; |
| 5567 | } |
| 5568 | else |
| 5569 | Result += "\t,0,0,0,0\n"; |
| 5570 | Result += "};\n"; |
| 5571 | |
| 5572 | // class metadata generation. |
| 5573 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5574 | Result += CDecl->getNameAsString(); |
| 5575 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5576 | "{\n\t&_OBJC_METACLASS_"; |
| 5577 | Result += CDecl->getNameAsString(); |
| 5578 | if (SuperClass) { |
| 5579 | Result += ", \""; |
| 5580 | Result += SuperClass->getNameAsString(); |
| 5581 | Result += "\", \""; |
| 5582 | Result += CDecl->getNameAsString(); |
| 5583 | Result += "\""; |
| 5584 | } |
| 5585 | else { |
| 5586 | Result += ", 0, \""; |
| 5587 | Result += CDecl->getNameAsString(); |
| 5588 | Result += "\""; |
| 5589 | } |
| 5590 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5591 | Result += ", 0,1"; |
| 5592 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5593 | Result += ",0"; |
| 5594 | else { |
| 5595 | // class has size. Must synthesize its size. |
| 5596 | Result += ",sizeof(struct "; |
| 5597 | Result += CDecl->getNameAsString(); |
| 5598 | if (LangOpts.MicrosoftExt) |
| 5599 | Result += "_IMPL"; |
| 5600 | Result += ")"; |
| 5601 | } |
| 5602 | if (NumIvars > 0) { |
| 5603 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5604 | Result += CDecl->getNameAsString(); |
| 5605 | Result += "\n\t"; |
| 5606 | } |
| 5607 | else |
| 5608 | Result += ",0"; |
| 5609 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5610 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5611 | Result += CDecl->getNameAsString(); |
| 5612 | Result += ", 0\n\t"; |
| 5613 | } |
| 5614 | else |
| 5615 | Result += ",0,0"; |
| 5616 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5617 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5618 | Result += CDecl->getNameAsString(); |
| 5619 | Result += ", 0,0\n"; |
| 5620 | } |
| 5621 | else |
| 5622 | Result += ",0,0,0\n"; |
| 5623 | Result += "};\n"; |
| 5624 | } |
| 5625 | |
| 5626 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5627 | int ClsDefCount = ClassImplementation.size(); |
| 5628 | int CatDefCount = CategoryImplementation.size(); |
| 5629 | |
| 5630 | // For each implemented class, write out all its meta data. |
| 5631 | for (int i = 0; i < ClsDefCount; i++) |
| 5632 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5633 | |
| 5634 | // For each implemented category, write out all its meta data. |
| 5635 | for (int i = 0; i < CatDefCount; i++) |
| 5636 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5637 | |
| 5638 | // Write objc_symtab metadata |
| 5639 | /* |
| 5640 | struct _objc_symtab |
| 5641 | { |
| 5642 | long sel_ref_cnt; |
| 5643 | SEL *refs; |
| 5644 | short cls_def_cnt; |
| 5645 | short cat_def_cnt; |
| 5646 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5647 | }; |
| 5648 | */ |
| 5649 | |
| 5650 | Result += "\nstruct _objc_symtab {\n"; |
| 5651 | Result += "\tlong sel_ref_cnt;\n"; |
| 5652 | Result += "\tSEL *refs;\n"; |
| 5653 | Result += "\tshort cls_def_cnt;\n"; |
| 5654 | Result += "\tshort cat_def_cnt;\n"; |
| 5655 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5656 | Result += "};\n\n"; |
| 5657 | |
| 5658 | Result += "static struct _objc_symtab " |
| 5659 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5660 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5661 | + ", " + utostr(CatDefCount) + "\n"; |
| 5662 | for (int i = 0; i < ClsDefCount; i++) { |
| 5663 | Result += "\t,&_OBJC_CLASS_"; |
| 5664 | Result += ClassImplementation[i]->getNameAsString(); |
| 5665 | Result += "\n"; |
| 5666 | } |
| 5667 | |
| 5668 | for (int i = 0; i < CatDefCount; i++) { |
| 5669 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5670 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5671 | Result += "_"; |
| 5672 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5673 | Result += "\n"; |
| 5674 | } |
| 5675 | |
| 5676 | Result += "};\n\n"; |
| 5677 | |
| 5678 | // Write objc_module metadata |
| 5679 | |
| 5680 | /* |
| 5681 | struct _objc_module { |
| 5682 | long version; |
| 5683 | long size; |
| 5684 | const char *name; |
| 5685 | struct _objc_symtab *symtab; |
| 5686 | } |
| 5687 | */ |
| 5688 | |
| 5689 | Result += "\nstruct _objc_module {\n"; |
| 5690 | Result += "\tlong version;\n"; |
| 5691 | Result += "\tlong size;\n"; |
| 5692 | Result += "\tconst char *name;\n"; |
| 5693 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5694 | Result += "};\n\n"; |
| 5695 | Result += "static struct _objc_module " |
| 5696 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5697 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5698 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5699 | Result += "};\n\n"; |
| 5700 | |
| 5701 | if (LangOpts.MicrosoftExt) { |
| 5702 | if (ProtocolExprDecls.size()) { |
| 5703 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5704 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5705 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5706 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5707 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5708 | Result += (*I)->getNameAsString(); |
| 5709 | Result += " = &_OBJC_PROTOCOL_"; |
| 5710 | Result += (*I)->getNameAsString(); |
| 5711 | Result += ";\n"; |
| 5712 | } |
| 5713 | Result += "#pragma data_seg(pop)\n\n"; |
| 5714 | } |
| 5715 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5716 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5717 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5718 | Result += "&_OBJC_MODULES;\n"; |
| 5719 | Result += "#pragma data_seg(pop)\n\n"; |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5724 | /// implementation. |
| 5725 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5726 | std::string &Result) { |
| 5727 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5728 | // Find category declaration for this implementation. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5729 | ObjCCategoryDecl *CDecl |
| 5730 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5731 | |
| 5732 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5733 | FullCategoryName += '_'; |
| 5734 | FullCategoryName += IDecl->getNameAsString(); |
| 5735 | |
| 5736 | // Build _objc_method_list for class's instance methods if needed |
| 5737 | SmallVector<ObjCMethodDecl *, 32> |
| 5738 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5739 | |
| 5740 | // If any of our property implementations have associated getters or |
| 5741 | // setters, produce metadata for them as well. |
| 5742 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5743 | PropEnd = IDecl->propimpl_end(); |
| 5744 | Prop != PropEnd; ++Prop) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5745 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5746 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5747 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5748 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5749 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5750 | if (!PD) |
| 5751 | continue; |
| 5752 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5753 | InstanceMethods.push_back(Getter); |
| 5754 | if (PD->isReadOnly()) |
| 5755 | continue; |
| 5756 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5757 | InstanceMethods.push_back(Setter); |
| 5758 | } |
| 5759 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5760 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5761 | Result); |
| 5762 | |
| 5763 | // Build _objc_method_list for class's class methods if needed |
| 5764 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5765 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5766 | Result); |
| 5767 | |
| 5768 | // Protocols referenced in class declaration? |
| 5769 | // Null CDecl is case of a category implementation with no category interface |
| 5770 | if (CDecl) |
| 5771 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5772 | FullCategoryName, Result); |
| 5773 | /* struct _objc_category { |
| 5774 | char *category_name; |
| 5775 | char *class_name; |
| 5776 | struct _objc_method_list *instance_methods; |
| 5777 | struct _objc_method_list *class_methods; |
| 5778 | struct _objc_protocol_list *protocols; |
| 5779 | // Objective-C 1.0 extensions |
| 5780 | uint32_t size; // sizeof (struct _objc_category) |
| 5781 | struct _objc_property_list *instance_properties; // category's own |
| 5782 | // @property decl. |
| 5783 | }; |
| 5784 | */ |
| 5785 | |
| 5786 | static bool objc_category = false; |
| 5787 | if (!objc_category) { |
| 5788 | Result += "\nstruct _objc_category {\n"; |
| 5789 | Result += "\tchar *category_name;\n"; |
| 5790 | Result += "\tchar *class_name;\n"; |
| 5791 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5792 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5793 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5794 | Result += "\tunsigned int size;\n"; |
| 5795 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5796 | Result += "};\n"; |
| 5797 | objc_category = true; |
| 5798 | } |
| 5799 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5800 | Result += FullCategoryName; |
| 5801 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5802 | Result += IDecl->getNameAsString(); |
| 5803 | Result += "\"\n\t, \""; |
| 5804 | Result += ClassDecl->getNameAsString(); |
| 5805 | Result += "\"\n"; |
| 5806 | |
| 5807 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5808 | Result += "\t, (struct _objc_method_list *)" |
| 5809 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5810 | Result += FullCategoryName; |
| 5811 | Result += "\n"; |
| 5812 | } |
| 5813 | else |
| 5814 | Result += "\t, 0\n"; |
| 5815 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5816 | Result += "\t, (struct _objc_method_list *)" |
| 5817 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5818 | Result += FullCategoryName; |
| 5819 | Result += "\n"; |
| 5820 | } |
| 5821 | else |
| 5822 | Result += "\t, 0\n"; |
| 5823 | |
| 5824 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5825 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5826 | Result += FullCategoryName; |
| 5827 | Result += "\n"; |
| 5828 | } |
| 5829 | else |
| 5830 | Result += "\t, 0\n"; |
| 5831 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5832 | } |
| 5833 | |
| 5834 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5835 | /// class methods. |
| 5836 | template<typename MethodIterator> |
| 5837 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5838 | MethodIterator MethodEnd, |
| 5839 | bool IsInstanceMethod, |
| 5840 | StringRef prefix, |
| 5841 | StringRef ClassName, |
| 5842 | std::string &Result) { |
| 5843 | if (MethodBegin == MethodEnd) return; |
| 5844 | |
| 5845 | if (!objc_impl_method) { |
| 5846 | /* struct _objc_method { |
| 5847 | SEL _cmd; |
| 5848 | char *method_types; |
| 5849 | void *_imp; |
| 5850 | } |
| 5851 | */ |
| 5852 | Result += "\nstruct _objc_method {\n"; |
| 5853 | Result += "\tSEL _cmd;\n"; |
| 5854 | Result += "\tchar *method_types;\n"; |
| 5855 | Result += "\tvoid *_imp;\n"; |
| 5856 | Result += "};\n"; |
| 5857 | |
| 5858 | objc_impl_method = true; |
| 5859 | } |
| 5860 | |
| 5861 | // Build _objc_method_list for class's methods if needed |
| 5862 | |
| 5863 | /* struct { |
| 5864 | struct _objc_method_list *next_method; |
| 5865 | int method_count; |
| 5866 | struct _objc_method method_list[]; |
| 5867 | } |
| 5868 | */ |
| 5869 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5870 | Result += "\nstatic struct {\n"; |
| 5871 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5872 | Result += "\tint method_count;\n"; |
| 5873 | Result += "\tstruct _objc_method method_list["; |
| 5874 | Result += utostr(NumMethods); |
| 5875 | Result += "];\n} _OBJC_"; |
| 5876 | Result += prefix; |
| 5877 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5878 | Result += "_METHODS_"; |
| 5879 | Result += ClassName; |
| 5880 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5881 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5882 | Result += "_meth\")))= "; |
| 5883 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5884 | |
| 5885 | Result += "\t,{{(SEL)\""; |
| 5886 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5887 | std::string MethodTypeString; |
| 5888 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5889 | Result += "\", \""; |
| 5890 | Result += MethodTypeString; |
| 5891 | Result += "\", (void *)"; |
| 5892 | Result += MethodInternalNames[*MethodBegin]; |
| 5893 | Result += "}\n"; |
| 5894 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5895 | Result += "\t ,{(SEL)\""; |
| 5896 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5897 | std::string MethodTypeString; |
| 5898 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5899 | Result += "\", \""; |
| 5900 | Result += MethodTypeString; |
| 5901 | Result += "\", (void *)"; |
| 5902 | Result += MethodInternalNames[*MethodBegin]; |
| 5903 | Result += "}\n"; |
| 5904 | } |
| 5905 | Result += "\t }\n};\n"; |
| 5906 | } |
| 5907 | |
| 5908 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5909 | SourceRange OldRange = IV->getSourceRange(); |
| 5910 | Expr *BaseExpr = IV->getBase(); |
| 5911 | |
| 5912 | // Rewrite the base, but without actually doing replaces. |
| 5913 | { |
| 5914 | DisableReplaceStmtScope S(*this); |
| 5915 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5916 | IV->setBase(BaseExpr); |
| 5917 | } |
| 5918 | |
| 5919 | ObjCIvarDecl *D = IV->getDecl(); |
| 5920 | |
| 5921 | Expr *Replacement = IV; |
| 5922 | if (CurMethodDef) { |
| 5923 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5924 | const ObjCInterfaceType *iFaceDecl = |
| 5925 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5926 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5927 | // lookup which class implements the instance variable. |
| 5928 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5929 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5930 | clsDeclared); |
| 5931 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5932 | |
| 5933 | // Synthesize an explicit cast to gain access to the ivar. |
| 5934 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5935 | RecName += "_IMPL"; |
| 5936 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5937 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5938 | SourceLocation(), SourceLocation(), |
| 5939 | II); |
| 5940 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5941 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5942 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5943 | CK_BitCast, |
| 5944 | IV->getBase()); |
| 5945 | // Don't forget the parens to enforce the proper binding. |
| 5946 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5947 | OldRange.getEnd(), |
| 5948 | castExpr); |
| 5949 | if (IV->isFreeIvar() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5950 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5951 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5952 | IV->getLocation(), |
| 5953 | D->getType(), |
| 5954 | VK_LValue, OK_Ordinary); |
| 5955 | Replacement = ME; |
| 5956 | } else { |
| 5957 | IV->setBase(PE); |
| 5958 | } |
| 5959 | } |
| 5960 | } else { // we are outside a method. |
| 5961 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5962 | |
| 5963 | // Explicit ivar refs need to have a cast inserted. |
| 5964 | // FIXME: consider sharing some of this code with the code above. |
| 5965 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5966 | const ObjCInterfaceType *iFaceDecl = |
| 5967 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5968 | // lookup which class implements the instance variable. |
| 5969 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5970 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5971 | clsDeclared); |
| 5972 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5973 | |
| 5974 | // Synthesize an explicit cast to gain access to the ivar. |
| 5975 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5976 | RecName += "_IMPL"; |
| 5977 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5978 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5979 | SourceLocation(), SourceLocation(), |
| 5980 | II); |
| 5981 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5982 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5983 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5984 | CK_BitCast, |
| 5985 | IV->getBase()); |
| 5986 | // Don't forget the parens to enforce the proper binding. |
| 5987 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 5988 | IV->getBase()->getLocEnd(), castExpr); |
| 5989 | // Cannot delete IV->getBase(), since PE points to it. |
| 5990 | // Replace the old base with the cast. This is important when doing |
| 5991 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 5992 | IV->setBase(PE); |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 5997 | return Replacement; |
| 5998 | } |